fix: Have SimpleResourceStore return text/turtle by default

This commit is contained in:
Joachim Van Herwegen 2020-07-22 15:23:15 +02:00
parent b93a77c11b
commit 4001050588
2 changed files with 28 additions and 5 deletions

View File

@ -122,17 +122,22 @@ export class SimpleResourceStore implements ResourceStore {
* Converts an array of Quads to a Representation. * Converts an array of Quads to a Representation.
* If preferences.type contains 'text/turtle' the result will be a stream of turtle strings, * If preferences.type contains 'text/turtle' the result will be a stream of turtle strings,
* otherwise a stream of Quads. * otherwise a stream of Quads.
*
* Note that in general this should be done by resource store specifically made for converting to turtle,
* this is just here to make this simple resource store work.
*
* @param data - Quads to transform. * @param data - Quads to transform.
* @param preferences - Requested preferences. * @param preferences - Requested preferences.
* *
* @returns The resulting Representation. * @returns The resulting Representation.
*/ */
private generateRepresentation(data: Quad[], preferences: RepresentationPreferences): Representation { private generateRepresentation(data: Quad[], preferences: RepresentationPreferences): Representation {
if (preferences.type && preferences.type.some((preference): boolean => preference.value.includes('text/turtle'))) { // Always return turtle unless explicitly asked for quads
return this.generateBinaryRepresentation(data); if (preferences.type?.some((preference): boolean => preference.value.includes('internal/quads'))) {
}
return this.generateQuadRepresentation(data); return this.generateQuadRepresentation(data);
} }
return this.generateBinaryRepresentation(data);
}
/** /**
* Creates a {@link BinaryRepresentation} of the incoming Quads. * Creates a {@link BinaryRepresentation} of the incoming Quads.

View File

@ -47,7 +47,7 @@ describe('A SimpleResourceStore', (): void => {
it('can write and read data.', async(): Promise<void> => { it('can write and read data.', async(): Promise<void> => {
const identifier = await store.addResource({ path: base }, representation); const identifier = await store.addResource({ path: base }, representation);
expect(identifier.path.startsWith(base)).toBeTruthy(); expect(identifier.path.startsWith(base)).toBeTruthy();
const result = await store.getRepresentation(identifier, {}); const result = await store.getRepresentation(identifier, { type: [{ value: 'internal/quads', weight: 1 }]});
expect(result).toEqual({ expect(result).toEqual({
dataType: 'quad', dataType: 'quad',
data: expect.any(Readable), data: expect.any(Readable),
@ -84,9 +84,27 @@ describe('A SimpleResourceStore', (): void => {
); );
}); });
it('returns turtle data if no preference was set.', async(): Promise<void> => {
const identifier = await store.addResource({ path: base }, representation);
expect(identifier.path.startsWith(base)).toBeTruthy();
const result = await store.getRepresentation(identifier, { });
expect(result).toEqual({
dataType: 'binary',
data: expect.any(Readable),
metadata: {
profiles: [],
raw: [],
contentType: 'text/turtle',
},
});
await expect(arrayifyStream(result.data)).resolves.toContain(
`<${quad.subject.value}> <${quad.predicate.value}> <${quad.object.value}>`,
);
});
it('can set data.', async(): Promise<void> => { it('can set data.', async(): Promise<void> => {
await store.setRepresentation({ path: base }, representation); await store.setRepresentation({ path: base }, representation);
const result = await store.getRepresentation({ path: base }, {}); const result = await store.getRepresentation({ path: base }, { type: [{ value: 'internal/quads', weight: 1 }]});
expect(result).toEqual({ expect(result).toEqual({
dataType: 'quad', dataType: 'quad',
data: expect.any(Readable), data: expect.any(Readable),