Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Jun 21, 2024
1 parent 207e656 commit 0f7eee9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 26 deletions.
6 changes: 3 additions & 3 deletions lib/model/query/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const _updateEntity = (dataset, entityData, submissionId, submissionDef, submiss
const clientEntity = await Entity.fromParseEntityData(entityData, { update: true }); // validation happens here

// Get version of entity on the server
// TODO: comments about what is happening here, too
// If the entity doesn't exist, check branchId - maybe this is an update for an entity created offline
let serverEntity = await Entities.getById(dataset.id, clientEntity.uuid, QueryOptions.forUpdate);
if (!serverEntity.isDefined()) {
if (clientEntity.def.branchId == null) {
Expand Down Expand Up @@ -313,9 +313,9 @@ const _updateEntity = (dataset, entityData, submissionId, submissionDef, submiss
data: mergedData,
label: mergedLabel,
dataReceived: clientEntity.def.dataReceived,
trunkVersion: clientEntity.def.trunkVersion,
branchId: clientEntity.def.branchId,
branchBaseVersion: clientEntity.def.baseVersion,
trunkVersion: clientEntity.def.trunkVersion,
branchBaseVersion: (clientEntity.def.branchId != null) ? clientEntity.def.baseVersion : null,
conflictingProperties
})
});
Expand Down
95 changes: 72 additions & 23 deletions test/integration/api/offline-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,7 @@ const testOfflineEntities = (test) => testService(async (service, container) =>

describe('Offline Entities', () => {
describe('parsing branchId and trunkVersion from submission xml', () => {
it('should ignore trunkVersion and branchId if empty string', 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.one
.replace('trunkVersion="1"', `trunkVersion=""`)
)
.set('Content-Type', 'application/xml')
.expect(200);

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' });
}));

it('should parse and save run info from sub creating an entity', testOfflineEntities(async (service, container) => {
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);
Expand All @@ -77,7 +56,7 @@ describe('Offline Entities', () => {
entity.aux.currentVersion.data.should.eql({ age: '20', status: 'new', first_name: 'Megan' });
}));

it('should parse and save run info from sub updating an entity', testOfflineEntities(async (service, container) => {
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);
Expand Down Expand Up @@ -106,6 +85,76 @@ describe('Offline Entities', () => {
entity.aux.currentVersion.branchBaseVersion.should.equal(1);
entity.aux.currentVersion.data.should.eql({ age: '22', status: 'arrived', first_name: 'Johnny' });
}));

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
await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions')
.send(testData.instances.offlineEntity.one
.replace('trunkVersion="1"', `trunkVersion=""`)
)
.set('Content-Type', 'application/xml')
.expect(200);

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' });
}));

it('should do something 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 = ""
await asAlice.post('/v1/projects/1/forms/offlineEntity/submissions')
.send(testData.instances.offlineEntity.one)
.set('Content-Type', 'application/xml')
.expect(200);

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' });

await asAlice.get('/v1/projects/1/forms/offlineEntity/submissions/one/audits')
.expect(200)
.then(({ body }) => {
body[0].details.errorMessage.should.eql('Required parameter branchId missing.');
});
}));

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
.replace('trunkVersion="1"', `trunkVersion=""`)
)
.set('Content-Type', 'application/xml')
.expect(200);

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' });
}));
});

describe('offline branches submitted in order', () => {
Expand Down

0 comments on commit 0f7eee9

Please sign in to comment.