Skip to content

Commit

Permalink
tests: api
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed May 1, 2021
1 parent cf24040 commit caf93e1
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 15 deletions.
16 changes: 15 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,19 @@
"rules": {
"no-unused-expressions": "off"
}
}]
}, {
"files": ["*.test.ts"],
"rules": {
"import/no-unresolved": "off"
}
}],
"settings": {
"import/resolver": {
"typescript": {
"project": [
"packages/*/tsconfig.json"
]
}
}
}
}
1 change: 1 addition & 0 deletions mocha-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require('@babel/register')({
configFile: './babel.config.json',
extensions: ['.js', '.jsx', '.ts', '.tsx'],
})
require('tsconfig-paths/register')

require('chai-snapshot-matcher')

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
]
},
"devDependencies": {
"eslint-import-resolver-typescript": "^2.4.0"
"eslint-import-resolver-typescript": "^2.4.0",
"tsconfig-paths": "^3.9.0"
}
}
17 changes: 5 additions & 12 deletions packages/knossos/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { NamedNode, Term } from 'rdf-js'
import { NamedNode, Stream, Term } from 'rdf-js'
import type { Api } from 'hydra-box/Api'
import StreamClient from 'sparql-http-client/StreamClient'
import $rdf from 'rdf-ext'
import { Debugger } from 'debug'
import ApiBase from 'hydra-box/Api'
import { ApiFactory } from '@hydrofoil/labyrinth'
import { CONSTRUCT } from '@tpluscode/sparql-builder'
import { hydra } from '@tpluscode/rdf-ns-builders'
import { Handler } from '@hydrofoil/knossos-events'
import clownface, { GraphPointer } from 'clownface'
import DatasetExt from 'rdf-ext/lib/Dataset'
import express from 'express'
import httpStatus from 'http-status'
import { ResourceStore } from './store'
import * as query from './query'

interface ApiFromStore {
store: ResourceStore
log?: Debugger
loadClasses?(): Promise<Stream>
}

function assertTerm(term: Term | undefined): asserts term is NamedNode {
Expand All @@ -39,7 +39,7 @@ export const DELETE: express.RequestHandler = (req, res) => {
res.send(httpStatus.NO_CONTENT)
}

const createApi: (arg: ApiFromStore) => ApiFactory = ({ store, log }) => async ({ path = '/api', sparql, codePath }) => {
const createApi: (arg: ApiFromStore) => ApiFactory = ({ store, log, loadClasses = query.loadClasses }) => async ({ path = '/api', sparql, codePath }) => {
const client = new StreamClient(sparql)

return new (class extends ApiBase implements Api {
Expand All @@ -65,14 +65,7 @@ const createApi: (arg: ApiFromStore) => ApiFactory = ({ store, log }) => async (
api = await store.load(this.term)
}

const resources = await CONSTRUCT`?s ?p ?o . ?c ?cp ?co. ${this.term} ${hydra.supportedClass} ?c`
.WHERE`
?c a ${hydra.Class} ;
${hydra.apiDocumentation} ${this.term} ;
?cp ?co .
?c (<>|!<>)+ ?s .
?s ?p ?o .
`.execute(client.query)
const resources = await loadClasses(this.term, client)
await api.dataset.import(resources)

this.dataset = api.dataset
Expand Down
15 changes: 15 additions & 0 deletions packages/knossos/lib/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NamedNode, Stream } from 'rdf-js'
import { StreamClient } from 'sparql-http-client/StreamClient'
import { CONSTRUCT } from '@tpluscode/sparql-builder'
import { hydra } from '@tpluscode/rdf-ns-builders'

export function loadClasses(api: NamedNode, client: StreamClient): Promise<Stream> {
return CONSTRUCT`?s ?p ?o . ?c ?cp ?co. ${api} ${hydra.supportedClass} ?c`
.WHERE`
?c a ${hydra.Class} ;
${hydra.apiDocumentation} ${api} ;
?cp ?co .
?c (<>|!<>)+ ?s .
?s ?p ?o .
`.execute(client.query)
}
82 changes: 82 additions & 0 deletions packages/knossos/test/lib/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import factory from 'knossos/lib/api'
import sinon from 'sinon'
import { ResourceStore } from 'knossos/lib/store'
import { expect } from 'chai'
import $rdf from 'rdf-ext'
import { Api } from 'hydra-box/Api'
import { namedNode } from '@labyrinth/testing/nodeFactory'

describe('@hydrofoil/knossos/lib/api', () => {
const term = $rdf.namedNode('/api/docs')
const codePath = './lib/hydra'
const path = '/api/path'
const sparql: any = {}
let store: sinon.SinonStubbedInstance<ResourceStore>
let loadClasses: sinon.SinonStub

describe('API', () => {
let api: Api

beforeEach(async () => {
loadClasses = sinon.stub().resolves($rdf.dataset().toStream())
store = {
exists: sinon.stub(),
load: sinon.stub(),
delete: sinon.stub(),
save: sinon.stub(),
}

api = await factory({
log: sinon.spy() as any,
store,
loadClasses,
})({
codePath,
path,
sparql,
})
api.term = term
})

afterEach(() => {
sinon.restore()
})

describe('.init', () => {
it('loads resource from store if it exists', async () => {
// given
store.exists.resolves(true)
store.load.resolves(namedNode(term))

// when
await api.init()

// then
expect(store.load).to.have.been.calledWith(sinon.match(term))
})

it('does not load if API resource does not exist', async () => {
// given
store.exists.resolves(false)

// when
await api.init()

// then
expect(store.load).not.to.have.been.called
})

it('sets initialized', async () => {
// given
store.exists.resolves(true)
store.load.resolves(namedNode(term))

// when
await api.init()

// then
expect(api.initialized).to.eq(true)
})
})
})
})
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
"typeRoots": [
"node_modules/@types",
"types"
]
],
"baseUrl": ".",
"paths": {
"knossos/*": ["packages/knossos/*"]
}
},
"include": [
"apps",
Expand Down

0 comments on commit caf93e1

Please sign in to comment.