refactor: Indicate caching on method name.

This commit is contained in:
Ruben Verborgh
2021-01-02 17:50:17 +01:00
parent da5515d50d
commit a572825909
12 changed files with 74 additions and 74 deletions

View File

@@ -2,14 +2,14 @@ import { createResponse } from 'node-mocks-http';
import { LinkRelMetadataWriter } from '../../../../../src/ldp/http/metadata/LinkRelMetadataWriter';
import { RepresentationMetadata } from '../../../../../src/ldp/representation/RepresentationMetadata';
import { LDP, RDF } from '../../../../../src/util/UriConstants';
import { toNamedNode } from '../../../../../src/util/UriUtil';
import { toCachedNamedNode } from '../../../../../src/util/UriUtil';
describe('A LinkRelMetadataWriter', (): void => {
const writer = new LinkRelMetadataWriter({ [RDF.type]: 'type', dummy: 'dummy' });
it('adds the correct link headers.', async(): Promise<void> => {
const response = createResponse();
const metadata = new RepresentationMetadata({ [RDF.type]: toNamedNode(LDP.Resource), unused: 'text' });
const metadata = new RepresentationMetadata({ [RDF.type]: toCachedNamedNode(LDP.Resource), unused: 'text' });
await expect(writer.handle({ response, metadata })).resolves.toBeUndefined();
expect(response.getHeaders()).toEqual({ link: `<${LDP.Resource}>; rel="type"` });
});

View File

@@ -19,7 +19,7 @@ import { SingleRootIdentifierStrategy } from '../../../src/util/identifiers/Sing
import * as quadUtil from '../../../src/util/QuadUtil';
import { guardedStreamFrom } from '../../../src/util/StreamUtil';
import { CONTENT_TYPE, HTTP, LDP, PIM, RDF } from '../../../src/util/UriConstants';
import { toNamedNode } from '../../../src/util/UriUtil';
import { toCachedNamedNode } from '../../../src/util/UriUtil';
import quad = DataFactory.quad;
import namedNode = DataFactory.namedNode;
@@ -160,7 +160,7 @@ describe('A DataAccessorBasedStore', (): void => {
it('errors when trying to create a container with non-RDF data.', async(): Promise<void> => {
const resourceID = { path: root };
representation.metadata.add(RDF.type, toNamedNode(LDP.Container));
representation.metadata.add(RDF.type, toCachedNamedNode(LDP.Container));
await expect(store.addResource(resourceID, representation)).rejects.toThrow(BadRequestHttpError);
});
@@ -169,7 +169,7 @@ describe('A DataAccessorBasedStore', (): void => {
const mock = jest.spyOn(quadUtil, 'parseQuads').mockImplementationOnce(async(): Promise<any> => {
throw 'apple';
});
representation.metadata.add(RDF.type, toNamedNode(LDP.Container));
representation.metadata.add(RDF.type, toCachedNamedNode(LDP.Container));
await expect(store.addResource(resourceID, representation)).rejects.toBe('apple');
mock.mockRestore();
});
@@ -186,7 +186,7 @@ describe('A DataAccessorBasedStore', (): void => {
it('can write containers.', async(): Promise<void> => {
const resourceID = { path: root };
representation.metadata.add(RDF.type, toNamedNode(LDP.Container));
representation.metadata.add(RDF.type, toCachedNamedNode(LDP.Container));
representation.metadata.contentType = 'text/turtle';
representation.data = guardedStreamFrom([ `<${`${root}resource/`}> a <coolContainer>.` ]);
const result = await store.addResource(resourceID, representation);
@@ -269,14 +269,14 @@ describe('A DataAccessorBasedStore', (): void => {
representation.metadata.identifier = DataFactory.namedNode(resourceID.path);
const newRepresentation = { ...representation };
newRepresentation.metadata = new RepresentationMetadata(representation.metadata);
newRepresentation.metadata.add(RDF.type, toNamedNode(LDP.Container));
newRepresentation.metadata.add(RDF.type, toCachedNamedNode(LDP.Container));
await expect(store.setRepresentation(resourceID, newRepresentation))
.rejects.toThrow(new ConflictHttpError('Input resource type does not match existing resource type.'));
});
it('will error if the ending slash does not match its resource type.', async(): Promise<void> => {
const resourceID = { path: `${root}resource` };
representation.metadata.add(RDF.type, toNamedNode(LDP.Container));
representation.metadata.add(RDF.type, toCachedNamedNode(LDP.Container));
await expect(store.setRepresentation(resourceID, representation)).rejects.toThrow(
new BadRequestHttpError('Containers should have a `/` at the end of their path, resources should not.'),
);
@@ -294,7 +294,7 @@ describe('A DataAccessorBasedStore', (): void => {
it('errors when trying to create a container with non-RDF data.', async(): Promise<void> => {
const resourceID = { path: `${root}container/` };
representation.metadata.add(RDF.type, toNamedNode(LDP.Container));
representation.metadata.add(RDF.type, toCachedNamedNode(LDP.Container));
await expect(store.setRepresentation(resourceID, representation)).rejects.toThrow(BadRequestHttpError);
});
@@ -332,7 +332,7 @@ describe('A DataAccessorBasedStore', (): void => {
it('errors when trying to create a container with containment triples.', async(): Promise<void> => {
const resourceID = { path: `${root}container/` };
representation.metadata.add(RDF.type, toNamedNode(LDP.Container));
representation.metadata.add(RDF.type, toCachedNamedNode(LDP.Container));
representation.metadata.contentType = 'text/turtle';
representation.metadata.identifier = DataFactory.namedNode(`${root}resource/`);
representation.data = guardedStreamFrom(
@@ -390,7 +390,7 @@ describe('A DataAccessorBasedStore', (): void => {
});
it('will error when deleting a root storage container.', async(): Promise<void> => {
representation.metadata.add(RDF.type, toNamedNode(PIM.Storage));
representation.metadata.add(RDF.type, toCachedNamedNode(PIM.Storage));
accessor.data[`${root}container`] = representation;
await expect(store.deleteResource({ path: `${root}container` }))
.rejects.toThrow(new MethodNotAllowedHttpError('Cannot delete a root storage container.'));

View File

@@ -13,7 +13,7 @@ import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/Unsup
import type { Guarded } from '../../../../src/util/GuardedStream';
import { guardedStreamFrom, readableToString } from '../../../../src/util/StreamUtil';
import { CONTENT_TYPE, DCTERMS, LDP, POSIX, RDF, XSD } from '../../../../src/util/UriConstants';
import { toNamedNode, toTypedLiteral } from '../../../../src/util/UriUtil';
import { toCachedNamedNode, toLiteral } from '../../../../src/util/UriUtil';
import { mockFs } from '../../../util/Util';
jest.mock('fs');
@@ -98,9 +98,9 @@ describe('A FileDataAccessor', (): void => {
expect(metadata.identifier.value).toBe(`${base}resource.ttl`);
expect(metadata.contentType).toBe('text/turtle');
expect(metadata.get(RDF.type)?.value).toBe(LDP.Resource);
expect(metadata.get(POSIX.size)).toEqualRdfTerm(toTypedLiteral('data'.length, XSD.integer));
expect(metadata.get(DCTERMS.modified)).toEqualRdfTerm(toTypedLiteral(now.toISOString(), XSD.dateTime));
expect(metadata.get(POSIX.mtime)).toEqualRdfTerm(toTypedLiteral(Math.floor(now.getTime() / 1000), XSD.integer));
expect(metadata.get(POSIX.size)).toEqualRdfTerm(toLiteral('data'.length, XSD.integer));
expect(metadata.get(DCTERMS.modified)).toEqualRdfTerm(toLiteral(now.toISOString(), XSD.dateTime));
expect(metadata.get(POSIX.mtime)).toEqualRdfTerm(toLiteral(Math.floor(now.getTime() / 1000), XSD.integer));
});
it('generates the metadata for a container and its non-meta children.', async(): Promise<void> => {
@@ -108,22 +108,22 @@ describe('A FileDataAccessor', (): void => {
metadata = await accessor.getMetadata({ path: `${base}container/` });
expect(metadata.identifier.value).toBe(`${base}container/`);
expect(metadata.getAll(RDF.type)).toEqualRdfTermArray(
[ toNamedNode(LDP.Container), toNamedNode(LDP.BasicContainer), toNamedNode(LDP.Resource) ],
[ toCachedNamedNode(LDP.Container), toCachedNamedNode(LDP.BasicContainer), toCachedNamedNode(LDP.Resource) ],
);
expect(metadata.get(POSIX.size)).toEqualRdfTerm(toTypedLiteral(0, XSD.integer));
expect(metadata.get(DCTERMS.modified)).toEqualRdfTerm(toTypedLiteral(now.toISOString(), XSD.dateTime));
expect(metadata.get(POSIX.mtime)).toEqualRdfTerm(toTypedLiteral(Math.floor(now.getTime() / 1000), XSD.integer));
expect(metadata.get(POSIX.size)).toEqualRdfTerm(toLiteral(0, XSD.integer));
expect(metadata.get(DCTERMS.modified)).toEqualRdfTerm(toLiteral(now.toISOString(), XSD.dateTime));
expect(metadata.get(POSIX.mtime)).toEqualRdfTerm(toLiteral(Math.floor(now.getTime() / 1000), XSD.integer));
expect(metadata.getAll(LDP.contains)).toEqualRdfTermArray(
[ toNamedNode(`${base}container/resource`), toNamedNode(`${base}container/container2/`) ],
[ toCachedNamedNode(`${base}container/resource`), toCachedNamedNode(`${base}container/container2/`) ],
);
const childQuads = metadata.quads().filter((quad): boolean =>
quad.subject.value === `${base}container/resource`);
const childMetadata = new RepresentationMetadata({ path: `${base}container/resource` }).addQuads(childQuads);
expect(childMetadata.get(RDF.type)?.value).toBe(LDP.Resource);
expect(childMetadata.get(POSIX.size)).toEqualRdfTerm(toTypedLiteral('data'.length, XSD.integer));
expect(childMetadata.get(DCTERMS.modified)).toEqualRdfTerm(toTypedLiteral(now.toISOString(), XSD.dateTime));
expect(childMetadata.get(POSIX.mtime)).toEqualRdfTerm(toTypedLiteral(Math.floor(now.getTime() / 1000),
expect(childMetadata.get(POSIX.size)).toEqualRdfTerm(toLiteral('data'.length, XSD.integer));
expect(childMetadata.get(DCTERMS.modified)).toEqualRdfTerm(toLiteral(now.toISOString(), XSD.dateTime));
expect(childMetadata.get(POSIX.mtime)).toEqualRdfTerm(toLiteral(Math.floor(now.getTime() / 1000),
XSD.integer));
});
@@ -168,7 +168,7 @@ describe('A FileDataAccessor', (): void => {
});
it('does not write metadata that is stored by the file system.', async(): Promise<void> => {
metadata.add(RDF.type, toNamedNode(LDP.Resource));
metadata.add(RDF.type, toCachedNamedNode(LDP.Resource));
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined();
expect(cache.data.resource).toBe('data');
expect(cache.data['resource.meta']).toBeUndefined();
@@ -289,7 +289,7 @@ describe('A FileDataAccessor', (): void => {
it('does not write metadata that is stored by the file system.', async(): Promise<void> => {
metadata = new RepresentationMetadata(
{ path: `${base}container/` },
{ [RDF.type]: [ toNamedNode(LDP.BasicContainer), toNamedNode(LDP.Resource) ]},
{ [RDF.type]: [ toCachedNamedNode(LDP.BasicContainer), toCachedNamedNode(LDP.Resource) ]},
);
await expect(accessor.writeContainer({ path: `${base}container/` }, metadata)).resolves.toBeUndefined();
expect(cache.data.container).toEqual({});

View File

@@ -7,7 +7,7 @@ import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError
import type { Guarded } from '../../../../src/util/GuardedStream';
import { guardedStreamFrom, readableToString } from '../../../../src/util/StreamUtil';
import { CONTENT_TYPE, LDP, RDF } from '../../../../src/util/UriConstants';
import { toNamedNode } from '../../../../src/util/UriUtil';
import { toCachedNamedNode } from '../../../../src/util/UriUtil';
describe('An InMemoryDataAccessor', (): void => {
const base = 'http://test.com/';
@@ -80,13 +80,13 @@ describe('An InMemoryDataAccessor', (): void => {
await expect(accessor.writeContainer({ path: `${base}container/container2` }, metadata)).resolves.toBeUndefined();
metadata = await accessor.getMetadata({ path: `${base}container/` });
expect(metadata.getAll(LDP.contains)).toEqualRdfTermArray(
[ toNamedNode(`${base}container/resource`), toNamedNode(`${base}container/container2/`) ],
[ toCachedNamedNode(`${base}container/resource`), toCachedNamedNode(`${base}container/container2/`) ],
);
});
it('adds stored metadata when requesting document metadata.', async(): Promise<void> => {
const identifier = { path: `${base}resource` };
const inputMetadata = new RepresentationMetadata(identifier, { [RDF.type]: toNamedNode(LDP.Resource) });
const inputMetadata = new RepresentationMetadata(identifier, { [RDF.type]: toCachedNamedNode(LDP.Resource) });
await expect(accessor.writeDocument(identifier, data, inputMetadata)).resolves.toBeUndefined();
metadata = await accessor.getMetadata(identifier);
expect(metadata.identifier.value).toBe(`${base}resource`);
@@ -97,7 +97,7 @@ describe('An InMemoryDataAccessor', (): void => {
it('adds stored metadata when requesting container metadata.', async(): Promise<void> => {
const identifier = { path: `${base}container/` };
const inputMetadata = new RepresentationMetadata(identifier, { [RDF.type]: toNamedNode(LDP.Container) });
const inputMetadata = new RepresentationMetadata(identifier, { [RDF.type]: toCachedNamedNode(LDP.Container) });
await expect(accessor.writeContainer(identifier, inputMetadata)).resolves.toBeUndefined();
metadata = await accessor.getMetadata(identifier);
@@ -109,7 +109,7 @@ describe('An InMemoryDataAccessor', (): void => {
it('can overwrite the metadata of an existing container without overwriting children.', async(): Promise<void> => {
const identifier = { path: `${base}container/` };
const inputMetadata = new RepresentationMetadata(identifier, { [RDF.type]: toNamedNode(LDP.Container) });
const inputMetadata = new RepresentationMetadata(identifier, { [RDF.type]: toCachedNamedNode(LDP.Container) });
await expect(accessor.writeContainer(identifier, inputMetadata)).resolves.toBeUndefined();
const resourceMetadata = new RepresentationMetadata();
await expect(accessor.writeDocument(
@@ -117,7 +117,7 @@ describe('An InMemoryDataAccessor', (): void => {
)).resolves.toBeUndefined();
const newMetadata = new RepresentationMetadata(inputMetadata);
newMetadata.add(RDF.type, toNamedNode(LDP.BasicContainer));
newMetadata.add(RDF.type, toCachedNamedNode(LDP.BasicContainer));
await expect(accessor.writeContainer(identifier, newMetadata)).resolves.toBeUndefined();
metadata = await accessor.getMetadata(identifier);
@@ -135,7 +135,7 @@ describe('An InMemoryDataAccessor', (): void => {
it('can write to the root container without overriding its children.', async(): Promise<void> => {
const identifier = { path: `${base}` };
const inputMetadata = new RepresentationMetadata(identifier, { [RDF.type]: toNamedNode(LDP.Container) });
const inputMetadata = new RepresentationMetadata(identifier, { [RDF.type]: toCachedNamedNode(LDP.Container) });
await expect(accessor.writeContainer(identifier, inputMetadata)).resolves.toBeUndefined();
const resourceMetadata = new RepresentationMetadata();
await expect(accessor.writeDocument(

View File

@@ -15,7 +15,7 @@ import type { Guarded } from '../../../../src/util/GuardedStream';
import { SingleRootIdentifierStrategy } from '../../../../src/util/identifiers/SingleRootIdentifierStrategy';
import { guardedStreamFrom } from '../../../../src/util/StreamUtil';
import { CONTENT_TYPE, LDP, RDF } from '../../../../src/util/UriConstants';
import { toNamedNode } from '../../../../src/util/UriUtil';
import { toCachedNamedNode } from '../../../../src/util/UriUtil';
const { literal, namedNode, quad } = DataFactory;
@@ -94,7 +94,7 @@ describe('A SparqlDataAccessor', (): void => {
metadata = await accessor.getMetadata({ path: 'http://identifier' });
expect(metadata.quads()).toBeRdfIsomorphic([
quad(namedNode('this'), namedNode('a'), namedNode('triple')),
quad(namedNode('http://identifier'), toNamedNode(CONTENT_TYPE), literal(INTERNAL_QUADS)),
quad(namedNode('http://identifier'), toCachedNamedNode(CONTENT_TYPE), literal(INTERNAL_QUADS)),
]);
expect(fetchTriples).toHaveBeenCalledTimes(1);
@@ -135,7 +135,7 @@ describe('A SparqlDataAccessor', (): void => {
it('overwrites the metadata when writing a container and updates parent.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/container/' },
{ [RDF.type]: [ toNamedNode(LDP.Resource), toNamedNode(LDP.Container) ]});
{ [RDF.type]: [ toCachedNamedNode(LDP.Resource), toCachedNamedNode(LDP.Container) ]});
await expect(accessor.writeContainer({ path: 'http://test.com/container/' }, metadata)).resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
@@ -154,7 +154,7 @@ describe('A SparqlDataAccessor', (): void => {
it('does not write containment triples when writing to a root container.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/' },
{ [RDF.type]: [ toNamedNode(LDP.Resource), toNamedNode(LDP.Container) ]});
{ [RDF.type]: [ toCachedNamedNode(LDP.Resource), toCachedNamedNode(LDP.Container) ]});
await expect(accessor.writeContainer({ path: 'http://test.com/' }, metadata)).resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
@@ -172,7 +172,7 @@ describe('A SparqlDataAccessor', (): void => {
it('overwrites the data and metadata when writing a resource and updates parent.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/container/resource' },
{ [RDF.type]: [ toNamedNode(LDP.Resource) ]});
{ [RDF.type]: [ toCachedNamedNode(LDP.Resource) ]});
await expect(accessor.writeDocument({ path: 'http://test.com/container/resource' }, data, metadata))
.resolves.toBeUndefined();
@@ -191,7 +191,7 @@ describe('A SparqlDataAccessor', (): void => {
it('overwrites the data and metadata when writing an empty resource.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/container/resource' },
{ [RDF.type]: [ toNamedNode(LDP.Resource) ]});
{ [RDF.type]: [ toCachedNamedNode(LDP.Resource) ]});
const empty = guardedStreamFrom([]);
await expect(accessor.writeDocument({ path: 'http://test.com/container/resource' }, empty, metadata))
.resolves.toBeUndefined();
@@ -210,7 +210,7 @@ describe('A SparqlDataAccessor', (): void => {
it('removes all references when deleting a resource.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/container/' },
{ [RDF.type]: [ toNamedNode(LDP.Resource), toNamedNode(LDP.Container) ]});
{ [RDF.type]: [ toCachedNamedNode(LDP.Resource), toCachedNamedNode(LDP.Container) ]});
await expect(accessor.deleteResource({ path: 'http://test.com/container/' })).resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);
@@ -224,7 +224,7 @@ describe('A SparqlDataAccessor', (): void => {
it('does not try to remove containment triples when deleting a root container.', async(): Promise<void> => {
metadata = new RepresentationMetadata({ path: 'http://test.com/' },
{ [RDF.type]: [ toNamedNode(LDP.Resource), toNamedNode(LDP.Container) ]});
{ [RDF.type]: [ toCachedNamedNode(LDP.Resource), toCachedNamedNode(LDP.Container) ]});
await expect(accessor.deleteResource({ path: 'http://test.com/' })).resolves.toBeUndefined();
expect(fetchUpdate).toHaveBeenCalledTimes(1);

View File

@@ -1,7 +1,7 @@
import 'jest-rdf';
import { literal, namedNode } from '@rdfjs/data-model';
import { CONTENT_TYPE, XSD } from '../../../src/util/UriConstants';
import { toNamedNode, toObjectTerm, toTypedLiteral, isTerm } from '../../../src/util/UriUtil';
import { toCachedNamedNode, toObjectTerm, toLiteral, isTerm } from '../../../src/util/UriUtil';
describe('An UriUtil', (): void => {
describe('isTerm function', (): void => {
@@ -17,21 +17,21 @@ describe('An UriUtil', (): void => {
describe('getNamedNode function', (): void => {
it('returns the input if it was a named node.', async(): Promise<void> => {
const term = namedNode('name');
expect(toNamedNode(term)).toBe(term);
expect(toCachedNamedNode(term)).toBe(term);
});
it('returns a named node when a string is used.', async(): Promise<void> => {
expect(toNamedNode('name')).toEqualRdfTerm(namedNode('name'));
expect(toCachedNamedNode('name')).toEqualRdfTerm(namedNode('name'));
});
it('caches generated named nodes.', async(): Promise<void> => {
const result = toNamedNode('name');
const result = toCachedNamedNode('name');
expect(result).toEqualRdfTerm(namedNode('name'));
expect(toNamedNode('name')).toBe(result);
expect(toCachedNamedNode('name')).toBe(result);
});
it('supports URI shorthands.', async(): Promise<void> => {
expect(toNamedNode('contentType')).toEqualRdfTerm(namedNode(CONTENT_TYPE));
expect(toCachedNamedNode('contentType')).toEqualRdfTerm(namedNode(CONTENT_TYPE));
});
});
@@ -51,7 +51,7 @@ describe('An UriUtil', (): void => {
describe('getTypedLiteral 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(toTypedLiteral(5, XSD.integer)).toEqualRdfTerm(expected);
expect(toLiteral(5, XSD.integer)).toEqualRdfTerm(expected);
});
});
});