refactor: use range query for canteen module

This commit is contained in:
Wieland Schöbl
2021-06-16 07:38:05 +00:00
parent 7b402d61c3
commit 93877c9fc7
7 changed files with 832 additions and 439 deletions

View File

@@ -14,8 +14,10 @@
*/
import {TestBed} from '@angular/core/testing';
import {Client} from '@openstapps/api/lib/client';
import {SCDish, SCMessage, SCSaveableThing, SCSearchQuery,
SCSearchResponse, SCSearchValueFilter, SCThing, SCThingOriginType, SCThings, SCThingType} from '@openstapps/core';
import {
SCDish, SCMessage, SCMultiSearchRequest, SCSaveableThing, SCSearchQuery,
SCSearchResponse, SCSearchValueFilter, SCThing, SCThingOriginType, SCThings, SCThingType,
} from '@openstapps/core';
import {sampleThingsMap} from '../../_helpers/data/sample-things';
import {StorageProvider} from '../storage/storage.provider';
import {DataModule} from './data.module';
@@ -96,6 +98,61 @@ describe('DataProvider', () => {
expect(response).toEqual(sampleResponse);
});
it('should provide backend data items using multi search query', async () => {
spyOn(Client.prototype as any, 'multiSearch').and.callFake(() => ({
then: (callback: any) => {
return callback({
a: sampleResponse,
});
},
}));
const response = await dataProvider.multiSearch({a: sampleQuery});
expect(response).toEqual({a: sampleResponse});
});
it('should partition search requests correctly', async () => {
const request = {
a: 'a',
b: 'b',
c: 'c',
d: 'd',
e: 'e',
} as SCMultiSearchRequest; // and response...
const requestCheck = Object.assign({}, request);
const responseShould = {
a: 'A',
b: 'B',
c: 'C',
d: 'D',
e: 'E',
};
dataProvider.backendQueriesLimit = 2;
spyOn(Client.prototype as any, 'multiSearch').and.callFake((req: SCMultiSearchRequest) => ({
then: (callback: any) => {
let i = 0;
for (const key in req) {
if (req.hasOwnProperty(key)) {
i++;
// @ts-ignore
expect(requestCheck[key]).not.toBeNull();
expect(requestCheck[key]).toEqual(req[key]);
// @ts-ignore
requestCheck[key] = null;
// @ts-ignore
req[key] = req[key].toUpperCase();
}
}
expect(i).toBeLessThanOrEqual(dataProvider.backendQueriesLimit);
return callback(req);
},
}));
const response = await dataProvider.multiSearch(request);
expect(response).toEqual(responseShould);
});
it('should put an data item into the local database (storage)', async () => {
let providedThing: SCSaveableThing<SCThing>;
spyOn(storageProvider, 'put' as any).and.callFake((_id: any, thing: any) => {