diff --git a/src/storage/SimpleResourceStore.ts b/src/storage/SimpleResourceStore.ts index f29228530..a57ad95f7 100644 --- a/src/storage/SimpleResourceStore.ts +++ b/src/storage/SimpleResourceStore.ts @@ -122,16 +122,21 @@ export class SimpleResourceStore implements ResourceStore { * Converts an array of Quads to a Representation. * If preferences.type contains 'text/turtle' the result will be a stream of turtle strings, * 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 preferences - Requested preferences. * * @returns The resulting Representation. */ private generateRepresentation(data: Quad[], preferences: RepresentationPreferences): Representation { - if (preferences.type && preferences.type.some((preference): boolean => preference.value.includes('text/turtle'))) { - return this.generateBinaryRepresentation(data); + // Always return turtle unless explicitly asked for quads + if (preferences.type?.some((preference): boolean => preference.value.includes('internal/quads'))) { + return this.generateQuadRepresentation(data); } - return this.generateQuadRepresentation(data); + return this.generateBinaryRepresentation(data); } /** diff --git a/test/unit/storage/SimpleResourceStore.test.ts b/test/unit/storage/SimpleResourceStore.test.ts index e7ac21852..53f1c5943 100644 --- a/test/unit/storage/SimpleResourceStore.test.ts +++ b/test/unit/storage/SimpleResourceStore.test.ts @@ -47,7 +47,7 @@ describe('A SimpleResourceStore', (): void => { it('can write and read data.', async(): Promise => { const identifier = await store.addResource({ path: base }, representation); 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({ dataType: 'quad', data: expect.any(Readable), @@ -84,9 +84,27 @@ describe('A SimpleResourceStore', (): void => { ); }); + it('returns turtle data if no preference was set.', async(): Promise => { + 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 => { 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({ dataType: 'quad', data: expect.any(Readable),