mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Only accept NamedNodes as predicates for metadata
* refactor: move toCachedNamedNode (private) * chore: only NamedNodes predicates in removes * feat: enforce NamedNode predicates in most cases * feat: getAll only accepts NamedNodes * feat: toCachedNamedNode only accepts string arg * tests: use NamedNodes for getAll calls * test: remove unnecessary string check for coverage * tests: fix NamedNodes in new tests after rebase * feat: metadatawriters store NamedNodes * refactor: toCachedNamedNode as utility function * fix: double write of linkRelMap * test: use the CONTENT_TYPE constant
This commit is contained in:
@@ -3,7 +3,7 @@ import type { BlankNode } from 'n3';
|
||||
import { DataFactory } from 'n3';
|
||||
import type { NamedNode, Quad } from 'rdf-js';
|
||||
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
||||
import { CONTENT_TYPE, SOLID_META, RDFS } from '../../../../src/util/Vocabularies';
|
||||
import { CONTENT_TYPE_TERM, SOLID_META, RDFS } from '../../../../src/util/Vocabularies';
|
||||
const { defaultGraph, literal, namedNode, quad } = DataFactory;
|
||||
|
||||
// Helper functions to filter quads
|
||||
@@ -82,14 +82,14 @@ describe('A RepresentationMetadata', (): void => {
|
||||
|
||||
it('takes overrides for specific predicates.', async(): Promise<void> => {
|
||||
metadata = new RepresentationMetadata({ predVal: 'objVal' });
|
||||
expect(metadata.get('predVal')).toEqualRdfTerm(literal('objVal'));
|
||||
expect(metadata.get(namedNode('predVal'))).toEqualRdfTerm(literal('objVal'));
|
||||
|
||||
metadata = new RepresentationMetadata({ predVal: literal('objVal') });
|
||||
expect(metadata.get('predVal')).toEqualRdfTerm(literal('objVal'));
|
||||
expect(metadata.get(namedNode('predVal'))).toEqualRdfTerm(literal('objVal'));
|
||||
|
||||
metadata = new RepresentationMetadata({ predVal: [ 'objVal1', literal('objVal2') ], predVal2: 'objVal3' });
|
||||
expect(metadata.getAll('predVal')).toEqualRdfTermArray([ literal('objVal1'), literal('objVal2') ]);
|
||||
expect(metadata.get('predVal2')).toEqualRdfTerm(literal('objVal3'));
|
||||
expect(metadata.getAll(namedNode('predVal'))).toEqualRdfTermArray([ literal('objVal1'), literal('objVal2') ]);
|
||||
expect(metadata.get(namedNode('predVal2'))).toEqualRdfTerm(literal('objVal3'));
|
||||
});
|
||||
|
||||
it('can combine overrides with an identifier.', async(): Promise<void> => {
|
||||
@@ -153,7 +153,7 @@ describe('A RepresentationMetadata', (): void => {
|
||||
// `setMetadata` should have the same result as the following
|
||||
const expectedMetadata = new RepresentationMetadata(identifier).addQuads(inputQuads);
|
||||
expectedMetadata.identifier = namedNode('otherId');
|
||||
expectedMetadata.add('test:pred', 'objVal');
|
||||
expectedMetadata.add(namedNode('test:pred'), 'objVal');
|
||||
|
||||
expect(metadata.identifier).toEqual(other.identifier);
|
||||
expect(metadata.quads()).toBeRdfIsomorphic(expectedMetadata.quads());
|
||||
@@ -161,13 +161,13 @@ describe('A RepresentationMetadata', (): void => {
|
||||
|
||||
it('can add a quad.', async(): Promise<void> => {
|
||||
const newQuad = quad(namedNode('random'), namedNode('new'), literal('triple'));
|
||||
metadata.addQuad('random', 'new', 'triple');
|
||||
metadata.addQuad('random', namedNode('new'), 'triple');
|
||||
expect(metadata.quads()).toBeRdfIsomorphic([ ...inputQuads, newQuad ]);
|
||||
});
|
||||
|
||||
it('can add a quad with a graph.', async(): Promise<void> => {
|
||||
const newQuad = quad(namedNode('random'), namedNode('new'), literal('triple'), namedNode('graph'));
|
||||
metadata.addQuad('random', 'new', 'triple', 'graph');
|
||||
metadata.addQuad('random', namedNode('new'), 'triple', 'graph');
|
||||
expect(metadata.quads()).toBeRdfIsomorphic([ ...inputQuads, newQuad ]);
|
||||
});
|
||||
|
||||
@@ -186,7 +186,7 @@ describe('A RepresentationMetadata', (): void => {
|
||||
});
|
||||
|
||||
it('removes all matching triples if graph is undefined.', async(): Promise<void> => {
|
||||
metadata.removeQuad(identifier, 'has', 'data');
|
||||
metadata.removeQuad(identifier, namedNode('has'), 'data');
|
||||
expect(metadata.quads()).toHaveLength(inputQuads.length - 2);
|
||||
expect(metadata.quads()).toBeRdfIsomorphic(removeQuads(inputQuads, identifier.value, 'has', 'data'));
|
||||
});
|
||||
@@ -277,7 +277,6 @@ describe('A RepresentationMetadata', (): void => {
|
||||
|
||||
it('errors if there are multiple values when getting a value.', async(): Promise<void> => {
|
||||
expect((): any => metadata.get(namedNode('has'))).toThrow(Error);
|
||||
expect((): any => metadata.get('has')).toThrow(Error);
|
||||
});
|
||||
|
||||
it('can set the value of a predicate.', async(): Promise<void> => {
|
||||
@@ -293,15 +292,15 @@ describe('A RepresentationMetadata', (): void => {
|
||||
it('has a shorthand for content-type.', async(): Promise<void> => {
|
||||
expect(metadata.contentType).toBeUndefined();
|
||||
metadata.contentType = 'a/b';
|
||||
expect(metadata.get(CONTENT_TYPE)).toEqualRdfTerm(literal('a/b'));
|
||||
expect(metadata.get(CONTENT_TYPE_TERM)).toEqualRdfTerm(literal('a/b'));
|
||||
expect(metadata.contentType).toBe('a/b');
|
||||
metadata.contentType = undefined;
|
||||
expect(metadata.contentType).toBeUndefined();
|
||||
});
|
||||
|
||||
it('errors if a shorthand has multiple values.', async(): Promise<void> => {
|
||||
metadata.add(CONTENT_TYPE, 'a/b');
|
||||
metadata.add(CONTENT_TYPE, 'c/d');
|
||||
metadata.add(CONTENT_TYPE_TERM, 'a/b');
|
||||
metadata.add(CONTENT_TYPE_TERM, 'c/d');
|
||||
expect((): any => metadata.contentType).toThrow();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user