mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
Add generic QuadToRdfConverter
This commit is contained in:
committed by
Joachim Van Herwegen
parent
4273c8bc04
commit
2c3ae4eded
80
test/unit/storage/conversion/QuadToRdfConverter.test.ts
Normal file
80
test/unit/storage/conversion/QuadToRdfConverter.test.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { namedNode, triple } from '@rdfjs/data-model';
|
||||
import stringifyStream from 'stream-to-string';
|
||||
import streamifyArray from 'streamify-array';
|
||||
import { Representation } from '../../../../src/ldp/representation/Representation';
|
||||
import { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
|
||||
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
|
||||
import { QuadToRdfConverter } from '../../../../src/storage/conversion/QuadToRdfConverter';
|
||||
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes';
|
||||
|
||||
describe('A QuadToRdfConverter', (): void => {
|
||||
const converter = new QuadToRdfConverter();
|
||||
const identifier: ResourceIdentifier = { path: 'path' };
|
||||
|
||||
it('can handle quad to turtle conversions.', async(): Promise<void> => {
|
||||
const representation = { metadata: { contentType: CONTENT_TYPE_QUADS }} as Representation;
|
||||
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
|
||||
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('can handle quad to JSON-LD conversions.', async(): Promise<void> => {
|
||||
const representation = { metadata: { contentType: CONTENT_TYPE_QUADS }} as Representation;
|
||||
const preferences: RepresentationPreferences = { type: [{ value: 'application/ld+json', weight: 1 }]};
|
||||
await expect(converter.canHandle({ identifier, representation, preferences })).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('converts quads to turtle.', async(): Promise<void> => {
|
||||
const representation = {
|
||||
data: streamifyArray([ triple(
|
||||
namedNode('http://test.com/s'),
|
||||
namedNode('http://test.com/p'),
|
||||
namedNode('http://test.com/o'),
|
||||
) ]),
|
||||
metadata: { contentType: CONTENT_TYPE_QUADS },
|
||||
} as Representation;
|
||||
const preferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
|
||||
const result = await converter.handle({ identifier, representation, preferences });
|
||||
expect(result).toMatchObject({
|
||||
dataType: DATA_TYPE_BINARY,
|
||||
metadata: {
|
||||
contentType: 'text/turtle',
|
||||
},
|
||||
});
|
||||
await expect(stringifyStream(result.data)).resolves.toEqual(
|
||||
`<http://test.com/s> <http://test.com/p> <http://test.com/o>.
|
||||
`,
|
||||
);
|
||||
});
|
||||
|
||||
it('converts quads to JSON-LD.', async(): Promise<void> => {
|
||||
const representation = {
|
||||
data: streamifyArray([ triple(
|
||||
namedNode('http://test.com/s'),
|
||||
namedNode('http://test.com/p'),
|
||||
namedNode('http://test.com/o'),
|
||||
) ]),
|
||||
metadata: { contentType: CONTENT_TYPE_QUADS },
|
||||
} as Representation;
|
||||
const preferences: RepresentationPreferences = { type: [{ value: 'application/ld+json', weight: 1 }]};
|
||||
const result = await converter.handle({ identifier, representation, preferences });
|
||||
expect(result).toMatchObject({
|
||||
dataType: DATA_TYPE_BINARY,
|
||||
metadata: {
|
||||
contentType: 'application/ld+json',
|
||||
},
|
||||
});
|
||||
await expect(stringifyStream(result.data)).resolves.toEqual(
|
||||
`[
|
||||
{
|
||||
"@id": "http://test.com/s",
|
||||
"http://test.com/p": [
|
||||
{
|
||||
"@id": "http://test.com/o"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
`,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user