Skip to content

Commit

Permalink
Added deltaset tests (#294)
Browse files Browse the repository at this point in the history
* Added deltaset tests

* added new tests for limit and skip and some unit tests

* skipped tests that use websql to not run on other than html5 and removed a .only

* removed another .only

* did some refactoring and fixed tests failing for Nativescript and Phonegap

* Fix status code set with nock for failing unit test

* Added deltaset tests

* added new tests for limit and skip and some unit tests

* skipped tests that use websql to not run on other than html5 and removed a .only

* removed another .only

* did some refactoring and fixed tests failing for Nativescript and Phonegap

* Fix status code set with nock for failing unit test

* added a test for autopagination with skip and limit

* fixing an expectation string and wrong expectation for deltaset test

* Added deltaset tests

* added new tests for limit and skip and some unit tests

* skipped tests that use websql to not run on other than html5 and removed a .only

* removed another .only

* did some refactoring and fixed tests failing for Nativescript and Phonegap

* Fix status code set with nock for failing unit test

* removed .only again
  • Loading branch information
Martin-Hristov authored and thomasconner committed May 18, 2018
1 parent 40318c7 commit 5152da3
Show file tree
Hide file tree
Showing 5 changed files with 1,297 additions and 3 deletions.
128 changes: 127 additions & 1 deletion src/core/datastore/cachestore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SyncOperation } from './sync';
import { init } from '../kinvey';
import { Query } from '../query';
import { Aggregation } from '../aggregation';
import { KinveyError, NotFoundError, ServerError } from '../errors';
import { KinveyError, NotFoundError, ServerError, BadRequestError } from '../errors';
import { randomString } from '../utils';
import { NetworkRack } from '../request';
import { NodeHttpMiddleware } from '../../node/http';
Expand Down Expand Up @@ -378,6 +378,132 @@ describe('CacheStore', () => {
})
.catch(done);
});

it('should send regular GET request with outdated lastRequest', function (done){
const entity1 = { _id: randomString() };
const entity2 = { _id: randomString() };
const store = new CacheStore(collection, null, { useDeltaSet: true});
const onNextSpy = expect.createSpy();
const lastRequestDate = new Date();
lastRequestDate.setDate(new Date().getDate()-31);

nock(store.client.apiHostname)
.get(`/appdata/${store.client.appKey}/${collection}`)
.reply(200, [entity1, entity2], {
'X-Kinvey-Request-Start': lastRequestDate.toISOString()
});

store.pull()
.then(()=>{
nock(store.client.apiHostname)
.get(`/appdata/${store.client.appKey}/${collection}/_deltaset`)
.query({ since: lastRequestDate.toISOString() })
.reply(400, {debug: "The 'since' timestamp must be within the past 1 days.",
description: "The value specified for one of the request parameters is out of range",
error: "ParameterValueOutOfRange"});

nock(store.client.apiHostname)
.get(`/appdata/${store.client.appKey}/${collection}`)
.reply(200, [entity1, entity2], {
'X-Kinvey-Request-Start': lastRequestDate.toISOString()
});
store.find()
.subscribe(onNextSpy, done, ()=>{
try{
expect(onNextSpy.calls.length).toEqual(2);
expect(onNextSpy.calls[0].arguments).toEqual([[entity1, entity2]]);
expect(onNextSpy.calls[1].arguments).toEqual([[entity1, entity2]]);
done();
} catch (error) {
done(error);
}
})
})
.catch(done);
});

it('should send regular GET request when configuration is missing on the backend', function (done){
const entity1 = { _id: randomString() };
const entity2 = { _id: randomString() };
const store = new CacheStore(collection, null, { useDeltaSet: true});
const onNextSpy = expect.createSpy();
const lastRequestDate = new Date();
const firstNock = nock(store.client.apiHostname)
.get(`/appdata/${store.client.appKey}/${collection}`)
.reply(200, [entity1, entity2], {
'X-Kinvey-Request-Start': lastRequestDate.toISOString()
});

store.pull()
.then(()=>{
firstNock.done();
const secondNock = nock(store.client.apiHostname)
.get(`/appdata/${store.client.appKey}/${collection}/_deltaset`)
.query({ since: lastRequestDate.toISOString() })
.reply(403, {
"error": "MissingConfiguration",
"description": "This feature is not properly configured for this app backend. Please configure it through the console first, or contact support for more information.",
"debug": "This collection has not been configured for Delta Set access."
});

const thirdNock = nock(store.client.apiHostname)
.get(`/appdata/${store.client.appKey}/${collection}`)
.reply(200, [entity1, entity2], {
'X-Kinvey-Request-Start': lastRequestDate.toISOString()
});
store.find()
.subscribe(onNextSpy, done, ()=>{
try{
secondNock.done();
thirdNock.done();
expect(onNextSpy.calls.length).toEqual(2);
expect(onNextSpy.calls[0].arguments).toEqual([[entity1, entity2]]);
expect(onNextSpy.calls[1].arguments).toEqual([[entity1, entity2]]);
done();
} catch (error) {
done(error);
}
})
})
.catch(done);
});

it('should return error if more than 10000 items are changed', function (done){
const entity1 = { _id: randomString() };
const entity2 = { _id: randomString() };
const store = new CacheStore(collection, null, { useDeltaSet: true});
const onNextSpy = expect.createSpy();
const lastRequestDate = new Date();

nock(store.client.apiHostname)
.get(`/appdata/${store.client.appKey}/${collection}`)
.reply(200, [entity1, entity2], {
'X-Kinvey-Request-Start': lastRequestDate.toISOString()
});

store.pull()
.then(()=>{
nock(store.client.apiHostname)
.get(`/appdata/${store.client.appKey}/${collection}/_deltaset`)
.query({ since: lastRequestDate.toISOString() })
.reply(400, {
error: "BadRequest",
description: "Unable to understand request",
debug: "ResultSetSizeExceeded"
});
store.find()
.subscribe(null, (error)=>{
try{
expect(error).toBeA(BadRequestError);
expect(error.debug).toEqual('ResultSetSizeExceeded')
done();
} catch (e) {
done(e);
}
})
})
.catch(done);
});
});

it('should remove entities that no longer exist on the backend from the cache', (done) => {
Expand Down
1 change: 1 addition & 0 deletions test/integration/configs/tests-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

const testsConfig = {
collectionName: 'Books',
deltaCollectionName: 'BooksDelta',
fbEmail: process.env.FACEBOOK_EMAIL,
fbPassword: process.env.FACEBOOK_PASSWORD,
authServiceId: 'decad9197f0f4680a46d902327c5c131'
Expand Down
Loading

0 comments on commit 5152da3

Please sign in to comment.