-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(handler-fetch): add some basic tests
- Loading branch information
1 parent
6d5cb29
commit 0267ff7
Showing
3 changed files
with
240 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |