Skip to content

Commit

Permalink
Merge pull request #436 from UUDigitalHumanitieslab/feature/api-pagin…
Browse files Browse the repository at this point in the history
…ation

Feature/api pagination
  • Loading branch information
BeritJanssen authored Apr 14, 2021
2 parents 8f65828 + eec93df commit e2bbfda
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 211 deletions.
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"apiRoot": "/api/",
"authRoot": "/rest-auth/",
"proxyRoot": "/proxy/",
"sparqlRoot": "/sparql/",
"nsRoot": "http://localhost:8000/"
}
1 change: 1 addition & 0 deletions frontend/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"apiRoot": "/api/",
"authRoot": "/rest-auth/",
"proxyRoot": "/proxy/",
"sparqlRoot": "/sparql/",
"nsRoot": "http://readit.example/"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { map } from 'lodash';
import { map, pick, partial } from 'lodash';
import * as _ from 'lodash';

import { event, timeout, startStore, endStore } from '../test-util';
import { oa, item } from '../common-rdf/ns';
Expand Down Expand Up @@ -31,13 +32,13 @@ describe('FlatAnnotationCollection', function() {
it('adds flat annotations when types are known', async function() {
const spy = jasmine.createSpy();
this.flat.on('add complete', spy);
this.items.set(itemData);
this.items.set(map(itemData, partial(pick, _, ['@id', '@type'])));
expect(this.flat.length).toBe(numAnnotations);
await timeout(50);
// We don't expect `'add'` events in this case, because these
// annotations were passed to the collection during initialization.
// Also, we don't expect `'complete'` because the ontology data are
// still missing.
// Also, we don't expect `'complete'` because most of the required data
// are still missing.
expect(spy).not.toHaveBeenCalled();
});

Expand Down
49 changes: 40 additions & 9 deletions frontend/src/common-adapters/flat-item-model-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { mapValues, invert, pick, assign, omit, each, delay } from 'lodash';
import {
mapValues, invert, pick, assign, omit, each, delay,
map, partition, keys, random,
} from 'lodash';
import { Events } from 'backbone';

import { event, timeout, startStore, endStore } from '../test-util';
Expand All @@ -7,6 +10,7 @@ import mockItems from '../mock-data/mock-items';
import { skos, dcterms, oa, readit, item } from '../common-rdf/ns';
import { asNative } from '../common-rdf/conversion';
import Node from '../common-rdf/node';
import Graph from '../common-rdf/graph';
import FlatItem from './flat-item-model';

interface NodeMap {
Expand Down Expand Up @@ -106,13 +110,41 @@ describe('FlatItem', function() {
(index + 1) * 10
));
await timeout(80);
expect(flatAnno.complete).toBe(false);
ontologyClass.set(contentClass);
await completion(flatAnno);
expect(flatAnno.complete).toBe(true);
expect(flatAnno.attributes).toEqual(expectedFlatAttributes);
});

describe('completes even in the face of fragmented resources', function() {
const resourceAttributes = itemAttributes.concat(contentClass);
const coinflip = () => random(1);
const partitionKeys = attr => partition(keys(attr), coinflip);

// This is a fuzz test. Ten repetitions with random sampling.
for (let i = 0; i < 10; ++i) {
it(`fuzz ${i}`, async function() {
// For each item/class, we partition the keys randomly.
const chunks = map(resourceAttributes, partitionKeys);
// Next, we create two partial representations of the resources.
// Together, they contain all properties.
const [firstBatch, secondBatch] = map([0, 1], (order) => {
return map(resourceAttributes, (attr, index) => {
// We include `@id` in both sets so that Backbone is
// able to merge them again.
return pick(attr, chunks[index][order], '@id');
});
});
const graph = new Graph(firstBatch);
const flatAnno = new FlatItem(graph.at(0));
await timeout(10);
graph.set(secondBatch as unknown as Node[]);
await completion(flatAnno);
expect(flatAnno.attributes).toEqual(expectedFlatAttributes);
});
}
});

it('updates the class and item after the fact', async function() {
const items = getFullItems();
const ontologyClass = new Node(contentClass);
Expand All @@ -126,10 +158,9 @@ describe('FlatItem', function() {
});
items.annotation.unset(oa.hasBody, items.item);
expect(flatAnno.has('item')).toBeFalsy();
const itemEvent = event(flatAnno, 'change:label');
items.annotation.set(oa.hasBody, replacementItem);
await Promise.all([
event(flatAnno, 'change:label'), event(flatAnno, 'change:cssClass')
]);
await itemEvent;
expect(flatAnno.get('label')).toBe('The slacker in Bohemia');
expect(flatAnno.get('item')).toBe(replacementItem);
expect(flatAnno.get('cssClass')).toBe(expectedFlatAttributes.cssClass);
Expand All @@ -138,10 +169,9 @@ describe('FlatItem', function() {
const replacementClass = new Node(readerClass);
items.annotation.unset(oa.hasBody, ontologyClass);
expect(flatAnno.has('class')).toBeFalsy();
const classEvent = event(flatAnno, 'change:cssClass');
items.annotation.set(oa.hasBody, replacementClass);
await Promise.all([
event(flatAnno, 'change:label'), event(flatAnno, 'change:cssClass')
]);
await classEvent;
expect(flatAnno.get('label')).toBe('The slacker in Bohemia');
expect(flatAnno.get('item')).toBe(replacementItem);
expect(flatAnno.get('cssClass')).toBe('is-readit-reader');
Expand All @@ -159,8 +189,9 @@ describe('FlatItem', function() {
flatAnno._completionFlags ^= 32;
flatAnno._setCompletionFlag(32);
// unset and reset all flags
const backup = flatAnno._completionFlags;
flatAnno._completionFlags = 0;
flatAnno._setCompletionFlag(63);
flatAnno._setCompletionFlag(backup);
await timeout(50);
expect(spy).not.toHaveBeenCalled();
});
Expand Down
Loading

0 comments on commit e2bbfda

Please sign in to comment.