diff --git a/test/integration/api/offline-entities.js b/test/integration/api/offline-entities.js index e2dcec928..fc3d7d0ab 100644 --- a/test/integration/api/offline-entities.js +++ b/test/integration/api/offline-entities.js @@ -1,7 +1,6 @@ const appRoot = require('app-root-path'); const { testService } = require('../setup'); const testData = require('../../data/xml'); -const { getOrNotFound } = require('../../../lib/util/promise'); const uuid = require('uuid').v4; const should = require('should'); const { sql } = require('slonik'); @@ -36,7 +35,6 @@ describe('Offline Entities', () => { it('should parse and save branch info from sub creating an entity', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') .send(testData.instances.offlineEntity.two @@ -47,19 +45,21 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789ddd').then(getOrNotFound); - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.version.should.equal(1); - // This is the first version of the entity so there should be no base or trunk versions - should.not.exist(entity.aux.currentVersion.trunkVersion); - should.not.exist(entity.aux.currentVersion.baseVersion); - entity.aux.currentVersion.data.should.eql({ age: '20', status: 'new', first_name: 'Megan' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789ddd') + .then(({ body }) => { + body.currentVersion.data.should.eql({ age: '20', status: 'new', first_name: 'Megan' }); + body.currentVersion.version.should.equal(1); + + // This is the first version of the entity so there should be no base or trunk versions + should.not.exist(body.currentVersion.trunkVersion); + should.not.exist(body.currentVersion.baseVersion); + body.currentVersion.branchId.should.equal(branchId); + }); })); it('should parse and save branch info from sub updating an entity', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') .send(testData.instances.offlineEntity.one @@ -70,25 +70,21 @@ describe('Offline Entities', () => { await exhaust(container); - // Check base version through API await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') .expect(200) .then(({ body }) => { body.currentVersion.version.should.equal(2); body.currentVersion.baseVersion.should.equal(1); - }); + body.currentVersion.data.should.eql({ age: '22', status: 'arrived', first_name: 'Johnny' }); - // Check entity details on Frame - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound); - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.baseVersion.should.equal(1); - entity.aux.currentVersion.branchBaseVersion.should.equal(1); - entity.aux.currentVersion.data.should.eql({ age: '22', status: 'arrived', first_name: 'Johnny' }); + body.currentVersion.branchId.should.equal(branchId); + body.currentVersion.branchBaseVersion.should.equal(1); + body.currentVersion.trunkVersion.should.equal(1); + }); })); it('should ignore empty string trunkVersion and branchId values in update scenario', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); // branchId = "" and trunkVersion = "" // apply update as though it were not offline case @@ -101,19 +97,23 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound); - should.not.exist(entity.aux.currentVersion.trunkVersion); - should.not.exist(entity.aux.currentVersion.branchId); - should.not.exist(entity.aux.currentVersion.branchBaseVersion); - entity.aux.currentVersion.version.should.equal(2); - entity.aux.currentVersion.data.should.eql({ age: '22', status: 'arrived', first_name: 'Johnny' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(2); + body.currentVersion.baseVersion.should.equal(1); + body.currentVersion.data.should.eql({ age: '22', status: 'arrived', first_name: 'Johnny' }); + + should.not.exist(body.currentVersion.trunkVersion); + should.not.exist(body.currentVersion.branchBaseVersion); + should.not.exist(body.currentVersion.branchId); + }); })); - it('should do something if trunkVersion is set but branchId is not', testOfflineEntities(async (service, container) => { + it('should log processing error if trunkVersion is set but branchId is not', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); - // branchId = "" + // branchId = "" but trunkVersion = "1" (will cause entity processing error) await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') .send(testData.instances.offlineEntity.one) .set('Content-Type', 'application/xml') @@ -121,12 +121,18 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound); - should.not.exist(entity.aux.currentVersion.trunkVersion); - should.not.exist(entity.aux.currentVersion.branchId); - should.not.exist(entity.aux.currentVersion.branchBaseVersion); - entity.aux.currentVersion.version.should.equal(1); - entity.aux.currentVersion.data.should.eql({ age: '22', first_name: 'Johnny' }); + // hasn't been updated + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(1); + should.not.exist(body.currentVersion.baseVersion); + body.currentVersion.data.should.eql({ age: '22', first_name: 'Johnny' }); + + should.not.exist(body.currentVersion.trunkVersion); + should.not.exist(body.currentVersion.branchBaseVersion); + should.not.exist(body.currentVersion.branchId); + }); await asAlice.get('/v1/projects/1/forms/offlineEntity/submissions/one/audits') .expect(200) @@ -137,7 +143,6 @@ describe('Offline Entities', () => { it('should ignore empty string trunkVersion and branchId values in create scenario', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') .send(testData.instances.offlineEntity.two @@ -148,12 +153,17 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789ddd').then(getOrNotFound); - should.not.exist(entity.aux.currentVersion.trunkVersion); - should.not.exist(entity.aux.currentVersion.branchId); - should.not.exist(entity.aux.currentVersion.branchBaseVersion); - entity.aux.currentVersion.version.should.equal(1); - entity.aux.currentVersion.data.should.eql({ age: '20', status: 'new', first_name: 'Megan' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789ddd') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(1); + should.not.exist(body.currentVersion.baseVersion); + body.currentVersion.data.should.eql({ age: '20', status: 'new', first_name: 'Megan' }); + + should.not.exist(body.currentVersion.trunkVersion); + should.not.exist(body.currentVersion.branchBaseVersion); + should.not.exist(body.currentVersion.branchId); + }); })); }); @@ -161,7 +171,6 @@ describe('Offline Entities', () => { it('should let multiple updates in the same branch get applied in order', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') .send(testData.instances.offlineEntity.one @@ -182,16 +191,22 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound); - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.baseVersion.should.equal(2); - entity.aux.currentVersion.data.should.eql({ age: '22', status: 'departed', first_name: 'Johnny' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(3); + body.currentVersion.baseVersion.should.equal(2); + body.currentVersion.data.should.eql({ age: '22', status: 'departed', first_name: 'Johnny' }); + + body.currentVersion.branchId.should.equal(branchId); + body.currentVersion.branchBaseVersion.should.equal(2); + body.currentVersion.trunkVersion.should.equal(1); + }); })); it('should apply update branch in order after server version has advanced', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); await asAlice.patch('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc?baseVersion=1') .send({ label: 'Johnny Doe (age 22)' }) @@ -217,18 +232,22 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound); - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.baseVersion.should.equal(3); - entity.aux.currentVersion.trunkVersion.should.equal(1); - entity.aux.currentVersion.branchBaseVersion.should.equal(2); - entity.aux.currentVersion.data.should.eql({ age: '22', status: 'departed', first_name: 'Johnny' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(4); + body.currentVersion.baseVersion.should.equal(3); + body.currentVersion.data.should.eql({ age: '22', status: 'departed', first_name: 'Johnny' }); + + body.currentVersion.branchId.should.equal(branchId); + body.currentVersion.branchBaseVersion.should.equal(2); + body.currentVersion.trunkVersion.should.equal(1); + }); })); it('should handle updating a branch in order, but with an interruption', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); // Update entity on the server (interrupt at beginning, too) await asAlice.patch('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc?baseVersion=1') @@ -264,19 +283,22 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound); - entity.aux.currentVersion.version.should.equal(5); - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.baseVersion.should.equal(3); - entity.aux.currentVersion.trunkVersion.should.equal(1); - entity.aux.currentVersion.branchBaseVersion.should.equal(2); - entity.aux.currentVersion.data.should.eql({ age: '22', status: 'departed', first_name: 'Johnny' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(5); + body.currentVersion.baseVersion.should.equal(3); + body.currentVersion.data.should.eql({ age: '22', status: 'departed', first_name: 'Johnny' }); + + body.currentVersion.branchId.should.equal(branchId); + body.currentVersion.branchBaseVersion.should.equal(2); + body.currentVersion.trunkVersion.should.equal(1); + }); })); it('should handle an offline branch that starts with a create', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); // First submission creates the entity, offline version is now 1 await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') @@ -300,11 +322,17 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789ddd').then(getOrNotFound); - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.version.should.equal(2); - entity.aux.currentVersion.baseVersion.should.equal(1); - entity.aux.currentVersion.data.should.eql({ age: '20', status: 'checked in', first_name: 'Megan' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789ddd') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(2); + body.currentVersion.baseVersion.should.equal(1); + body.currentVersion.data.should.eql({ age: '20', status: 'checked in', first_name: 'Megan' }); + + body.currentVersion.branchId.should.equal(branchId); + should.not.exist(body.currentVersion.trunkVersion); + body.currentVersion.branchBaseVersion.should.equal(1); + }); })); }); @@ -341,7 +369,6 @@ describe('Offline Entities', () => { it('should not apply out of order update from a run after starting a run', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); // start run correctly await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') @@ -364,16 +391,22 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound); - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.baseVersion.should.equal(1); - entity.aux.currentVersion.data.should.eql({ age: '22', status: 'arrived', first_name: 'Johnny' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(2); + body.currentVersion.baseVersion.should.equal(1); + body.currentVersion.data.should.eql({ age: '22', status: 'arrived', first_name: 'Johnny' }); + + body.currentVersion.branchId.should.equal(branchId); + body.currentVersion.branchBaseVersion.should.equal(1); + body.currentVersion.trunkVersion.should.equal(1); + }); })); it('should not apply later trunkVersion when run has not even started', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); // much later run index await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') @@ -388,17 +421,23 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound); - should.not.exist(entity.aux.currentVersion.branchId); - //should.not.exist(entity.aux.currentVersion.runIndex); - // something about base version - entity.aux.currentVersion.data.should.eql({ age: '22', first_name: 'Johnny' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(1); + should.not.exist(body.currentVersion.baseVersion); + body.currentVersion.data.should.eql({ age: '22', first_name: 'Johnny' }); + + + should.not.exist(body.currentVersion.trunkVersion); + should.not.exist(body.currentVersion.branchBaseVersion); + should.not.exist(body.currentVersion.branchId); + }); })); it('should apply later run received earlier', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); // start run correctly await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') @@ -436,16 +475,22 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789abc').then(getOrNotFound); - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.baseVersion.should.equal(3); - entity.aux.currentVersion.data.should.eql({ age: '22', status: 'departed', first_name: 'Johnny' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(4); + body.currentVersion.baseVersion.should.equal(3); + body.currentVersion.data.should.eql({ age: '22', status: 'departed', first_name: 'Johnny' }); + + body.currentVersion.branchId.should.equal(branchId); + body.currentVersion.branchBaseVersion.should.equal(3); + body.currentVersion.trunkVersion.should.equal(1); + }); })); it('should handle offline update that comes before a create', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); // Second submission updates the entity but entity hasn't been created yet await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') @@ -474,17 +519,22 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789ddd').then(getOrNotFound); - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.version.should.equal(2); - entity.aux.currentVersion.baseVersion.should.equal(1); - entity.aux.currentVersion.data.should.eql({ age: '20', status: 'checked in', first_name: 'Megan' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789ddd') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(2); + body.currentVersion.baseVersion.should.equal(1); + body.currentVersion.data.should.eql({ age: '20', status: 'checked in', first_name: 'Megan' }); + + body.currentVersion.branchId.should.equal(branchId); + should.not.exist(body.currentVersion.trunkVersion); + body.currentVersion.branchBaseVersion.should.equal(1); + }); })); it('should handle offline create/update that comes in backwards', testOfflineEntities(async (service, container) => { const asAlice = await service.login('alice'); const branchId = uuid(); - const dataset = await container.Datasets.get(1, 'people', true).then(getOrNotFound); // First submission contains the last update await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions') @@ -529,12 +579,17 @@ describe('Offline Entities', () => { await exhaust(container); - const entity = await container.Entities.getById(dataset.id, '12345678-1234-4123-8234-123456789ddd').then(getOrNotFound); - //console.log(entity.aux.currentVersion.data) - entity.aux.currentVersion.branchId.should.equal(branchId); - entity.aux.currentVersion.version.should.equal(3); - entity.aux.currentVersion.baseVersion.should.equal(2); - entity.aux.currentVersion.data.should.eql({ age: '20', status: 'working', first_name: 'Megan' }); + await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789ddd') + .expect(200) + .then(({ body }) => { + body.currentVersion.version.should.equal(3); + body.currentVersion.baseVersion.should.equal(2); + body.currentVersion.data.should.eql({ age: '20', status: 'working', first_name: 'Megan' }); + + body.currentVersion.branchId.should.equal(branchId); + should.not.exist(body.currentVersion.trunkVersion); + body.currentVersion.branchBaseVersion.should.equal(2); + }); })); }); });