Skip to content

Commit

Permalink
test(handler-fetch): add some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Jan 15, 2024
1 parent 6d5cb29 commit 0267ff7
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 2 deletions.
127 changes: 127 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/handler-fetch/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param {string} termType Oxigraph termType value
* @returns {string} SPARQL termType value
*/
const convertTermType = (termType) => {
export const convertTermType = (termType) => {
switch (termType) {
case 'Literal':
return 'literal'
Expand All @@ -29,7 +29,7 @@ const convertTermType = (termType) => {
* type: 'ASK' | 'SELECT' | 'CONSTRUCT';
* }>} SPARQL response.
*/
const handleOxigraphResult = async (results, isConstructQuery = false) => {
export const handleOxigraphResult = async (results, isConstructQuery = false) => {
let sparqlResponse = {}

// Handle ASK queries
Expand Down
111 changes: 111 additions & 0 deletions packages/handler-fetch/test/handler-fetch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { strictEqual, deepEqual } from 'node:assert'
import { describe, it } from 'mocha'
import { convertTermType, handleOxigraphResult } from '../lib/query.js'

describe('trifid-handler-fetch', () => {
describe('check that required functions are defined', () => {
it('fetch', () => {
strictEqual(typeof fetch, 'function')
})
})

describe('query', () => {
it('should convert to the expected TermType', () => {
strictEqual(convertTermType('Literal'), 'literal')
strictEqual(convertTermType('BlankNode'), 'bnode')
strictEqual(convertTermType('NamedNode'), 'uri')
strictEqual(convertTermType(''), 'literal')
})

it('should handle ASK queries', async () => {
const results = true
const { raw, response, contentType, type } = await handleOxigraphResult(results)
strictEqual(raw.boolean, results)
strictEqual(response, JSON.stringify(raw, null, 2))
strictEqual(contentType, 'application/sparql-results+json')
strictEqual(type, 'ASK')
})

it('should handle empty results', async () => {
const results = []
const expectedResult = {
head: {
vars: [],
},
results: {
bindings: [],
},
}
const { raw, response, contentType, type } = await handleOxigraphResult(results)
deepEqual(raw, expectedResult)
strictEqual(response, JSON.stringify(expectedResult, null, 2))
strictEqual(contentType, 'application/sparql-results+json')
strictEqual(type, 'SELECT')
})

it('should handle CONSTRUCT queries', async () => {
const results = []
const { raw, response, contentType, type } = await handleOxigraphResult(results, true)
strictEqual(raw, '')
strictEqual(response, '')
strictEqual(contentType, 'application/n-triples')
strictEqual(type, 'CONSTRUCT')
})

it('should handle SELECT queries', async () => {
const results = []
const resultsMap = new Map()
resultsMap.set('s', { termType: 'NamedNode', value: 'http://example.org/subject' })
resultsMap.set('p', { termType: 'NamedNode', value: 'http://example.org/predicate' })
resultsMap.set('o', { termType: 'Literal', value: 'object' })
results.push(resultsMap)

const expectedResult = {
head: {
vars: ['s', 'p', 'o'],
},
results: {
bindings: [
{
s: {
type: 'uri',
value: 'http://example.org/subject',
},
p: {
type: 'uri',
value: 'http://example.org/predicate',
},
o: {
type: 'literal',
value: 'object',
},
},
],
},
}

const { raw, response, contentType, type } = await handleOxigraphResult(results)
deepEqual(raw, expectedResult)
deepEqual(JSON.parse(response), expectedResult)
strictEqual(contentType, 'application/sparql-results+json')
strictEqual(type, 'SELECT')
})

it('should handle CONSTRUCT queries', async () => {
const results = [
{ toString: () => '<http://example.org/subject1> <http://example.org/predicate1> "object1"' },
{ toString: () => '<http://example.org/subject2> <http://example.org/predicate2> "object2"' },
{ toString: () => '<http://example.org/subject3> <http://example.org/predicate3> "object3"' },
]

const expectedRawResult = results.map((result) => result.toString())
const expectedResult = `${expectedRawResult.join(' . \n')} .`

const { raw, response, contentType, type } = await handleOxigraphResult(results, true)
deepEqual(raw, expectedRawResult)
deepEqual(response, expectedResult)
strictEqual(contentType, 'application/n-triples')
strictEqual(type, 'CONSTRUCT')
})
})
})

0 comments on commit 0267ff7

Please sign in to comment.