mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Support strings in addQuad.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import 'jest-rdf';
|
||||
import { DataFactory } from 'n3';
|
||||
import { literal, namedNode, quad } from '@rdfjs/data-model';
|
||||
import type { Quad } from 'rdf-js';
|
||||
import { parseQuads, pushQuad, serializeQuads } from '../../../src/util/QuadUtil';
|
||||
import { guardedStreamFrom, readableToString } from '../../../src/util/StreamUtil';
|
||||
@@ -8,19 +8,27 @@ describe('QuadUtil', (): void => {
|
||||
describe('#pushQuad', (): void => {
|
||||
it('creates a quad and adds it to the given array.', async(): Promise<void> => {
|
||||
const quads: Quad[] = [];
|
||||
pushQuad(quads, DataFactory.namedNode('sub'), DataFactory.namedNode('pred'), DataFactory.literal('obj'));
|
||||
pushQuad(quads, namedNode('sub'), namedNode('pred'), literal('obj'));
|
||||
expect(quads).toEqualRdfQuadArray([
|
||||
DataFactory.quad(DataFactory.namedNode('sub'), DataFactory.namedNode('pred'), DataFactory.literal('obj')),
|
||||
quad(namedNode('sub'), namedNode('pred'), literal('obj')),
|
||||
]);
|
||||
});
|
||||
|
||||
it('creates a quad from strings and adds it to the given array.', async(): Promise<void> => {
|
||||
const quads: Quad[] = [];
|
||||
pushQuad(quads, 'sub', 'pred', 'obj');
|
||||
expect(quads).toEqualRdfQuadArray([
|
||||
quad(namedNode('sub'), namedNode('pred'), namedNode('obj')),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#serializeQuads', (): void => {
|
||||
it('converts quads to the requested format.', async(): Promise<void> => {
|
||||
const quads = [ DataFactory.quad(
|
||||
DataFactory.namedNode('pre:sub'),
|
||||
DataFactory.namedNode('pre:pred'),
|
||||
DataFactory.literal('obj'),
|
||||
const quads = [ quad(
|
||||
namedNode('pre:sub'),
|
||||
namedNode('pre:pred'),
|
||||
literal('obj'),
|
||||
) ];
|
||||
const stream = serializeQuads(quads, 'application/n-triples');
|
||||
await expect(readableToString(stream)).resolves.toMatch('<pre:sub> <pre:pred> "obj" .');
|
||||
@@ -30,10 +38,10 @@ describe('QuadUtil', (): void => {
|
||||
describe('#parseQuads', (): void => {
|
||||
it('parses quads from the requested format.', async(): Promise<void> => {
|
||||
const stream = guardedStreamFrom([ '<pre:sub> <pre:pred> "obj".' ]);
|
||||
await expect(parseQuads(stream, 'application/n-triples')).resolves.toEqualRdfQuadArray([ DataFactory.quad(
|
||||
DataFactory.namedNode('pre:sub'),
|
||||
DataFactory.namedNode('pre:pred'),
|
||||
DataFactory.literal('obj'),
|
||||
await expect(parseQuads(stream, 'application/n-triples')).resolves.toEqualRdfQuadArray([ quad(
|
||||
namedNode('pre:sub'),
|
||||
namedNode('pre:pred'),
|
||||
literal('obj'),
|
||||
) ]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import 'jest-rdf';
|
||||
import { literal, namedNode } from '@rdfjs/data-model';
|
||||
import { CONTENT_TYPE, XSD } from '../../../src/util/UriConstants';
|
||||
import { toCachedNamedNode, toObjectTerm, toLiteral, isTerm } from '../../../src/util/UriUtil';
|
||||
import {
|
||||
toCachedNamedNode,
|
||||
toSubjectTerm,
|
||||
toPredicateTerm,
|
||||
toObjectTerm,
|
||||
toLiteral,
|
||||
isTerm,
|
||||
} from '../../../src/util/UriUtil';
|
||||
|
||||
describe('An UriUtil', (): void => {
|
||||
describe('isTerm function', (): void => {
|
||||
@@ -14,7 +21,7 @@ describe('An UriUtil', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNamedNode function', (): void => {
|
||||
describe('toCachedNamedNode function', (): void => {
|
||||
it('returns the input if it was a named node.', async(): Promise<void> => {
|
||||
const term = namedNode('name');
|
||||
expect(toCachedNamedNode(term)).toBe(term);
|
||||
@@ -35,7 +42,29 @@ describe('An UriUtil', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('getObjectTerm function', (): void => {
|
||||
describe('toSubjectTerm function', (): void => {
|
||||
it('returns the input if it was a term.', async(): Promise<void> => {
|
||||
const nn = namedNode('name');
|
||||
expect(toSubjectTerm(nn)).toBe(nn);
|
||||
});
|
||||
|
||||
it('returns a named node when a string is used.', async(): Promise<void> => {
|
||||
expect(toSubjectTerm('nn')).toEqualRdfTerm(namedNode('nn'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('toPredicateTerm function', (): void => {
|
||||
it('returns the input if it was a term.', async(): Promise<void> => {
|
||||
const nn = namedNode('name');
|
||||
expect(toPredicateTerm(nn)).toBe(nn);
|
||||
});
|
||||
|
||||
it('returns a named node when a string is used.', async(): Promise<void> => {
|
||||
expect(toPredicateTerm('nn')).toEqualRdfTerm(namedNode('nn'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('toObjectTerm function', (): void => {
|
||||
it('returns the input if it was a term.', async(): Promise<void> => {
|
||||
const nn = namedNode('name');
|
||||
const lit = literal('lit');
|
||||
@@ -43,12 +72,16 @@ describe('An UriUtil', (): void => {
|
||||
expect(toObjectTerm(lit)).toBe(lit);
|
||||
});
|
||||
|
||||
it('returns a literal when a string is used.', async(): Promise<void> => {
|
||||
expect(toObjectTerm('lit')).toEqualRdfTerm(literal('lit'));
|
||||
it('returns a named node when a string is used.', async(): Promise<void> => {
|
||||
expect(toObjectTerm('nn')).toEqualRdfTerm(namedNode('nn'));
|
||||
});
|
||||
|
||||
it('returns a literal when a string is used with preferLiteral.', async(): Promise<void> => {
|
||||
expect(toObjectTerm('lit', true)).toEqualRdfTerm(literal('lit'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTypedLiteral function', (): void => {
|
||||
describe('toLiteral function', (): void => {
|
||||
it('converts the input to a valid literal with the given type.', async(): Promise<void> => {
|
||||
const expected = literal('5', namedNode(XSD.integer));
|
||||
expect(toLiteral(5, XSD.integer)).toEqualRdfTerm(expected);
|
||||
|
||||
Reference in New Issue
Block a user