Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring entity tests and adding new entity update tests #1021

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/model/query/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { equals, extender, unjoiner, page, markDeleted } = require('../../util/db
const { map, mergeRight } = require('ramda');
const { blankStringToNull, construct } = require('../../util/util');
const { QueryOptions } = require('../../util/db');
const { getOrNotFound } = require('../../util/promise');
const { odataFilter } = require('../../data/odata-filter');
const { odataToColumnMap, parseSubmissionXml } = require('../../data/entity');
const { isTrue } = require('../../util/http');
Expand Down Expand Up @@ -156,11 +157,14 @@ const _createEntity = (dataset, entityData, submissionId, submissionDef, submiss
};

const _updateEntity = (dataset, entityData, submissionId, submissionDef, submissionDefId, event) => async ({ Audits, Entities }) => {
if (!(event.action === 'submission.create')) // only update on submission.create
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this condition be in _processSubmissionEvent function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I moved these lines into _createEntity

if ((dataset.approvalRequired && event.details.reviewState !== 'approved') ||
      (!dataset.approvalRequired && event.action === 'submission.update'))

it felt reasonable to move this event action check into _updateEntity

return null;

// Get client version of entity
const clientEntity = await Entity.fromParseEntityData(entityData); // validation happens here

// Get version of entity on the server
const serverEntity = await Entities.getById(dataset.id, clientEntity.uuid).then(o => o.get());
const serverEntity = await Entities.getById(dataset.id, clientEntity.uuid).then(getOrNotFound);

// merge data
const mergedData = mergeRight(serverEntity.aux.currentVersion.data, clientEntity.def.data);
Expand Down
112 changes: 103 additions & 9 deletions test/integration/api/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,19 +340,11 @@ describe('datasets and entities', () => {
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.patch('/v1/projects/1/forms/simpleEntity/submissions/one')
.send({ reviewState: 'approved' })
.expect(200);

await asAlice.post('/v1/projects/1/forms/simpleEntity/submissions')
.send(testData.instances.simpleEntity.two)
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.patch('/v1/projects/1/forms/simpleEntity/submissions/two')
.send({ reviewState: 'approved' })
.expect(200);

await exhaust(container);

const result = await asAlice.get('/v1/projects/1/datasets/people/entities.csv')
Expand Down Expand Up @@ -403,6 +395,63 @@ describe('datasets and entities', () => {

}));

it('should stream csv of dataset with entities from multiple forms', testService(async (service, container) => {
const asAlice = await service.login('alice');

await asAlice.post('/v1/projects/1/forms')
.send(testData.forms.multiPropertyEntity)
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.multiPropertyEntity
.replace('multiPropertyEntity', 'multiPropertyEntity2')
.replace('b_q1', 'f_q1')
.replace('d_q2', 'e_q2'))
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.post('/v1/projects/1/forms/multiPropertyEntity/draft/publish').expect(200);

await asAlice.post('/v1/projects/1/forms/multiPropertyEntity/submissions')
.send(testData.instances.multiPropertyEntity.one)
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.post('/v1/projects/1/forms/multiPropertyEntity/submissions')
.send(testData.instances.multiPropertyEntity.two)
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.post('/v1/projects/1/forms/multiPropertyEntity2/submissions')
.send(testData.instances.multiPropertyEntity.one
.replace('multiPropertyEntity', 'multiPropertyEntity2')
.replace('uuid:12345678-1234-4123-8234-123456789aaa', 'uuid:12345678-1234-4123-8234-123456789ccc')
.replace('b_q1', 'f_q1')
.replace('d_q2', 'e_q2'))
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.patch('/v1/projects/1/forms/multiPropertyEntity/submissions/one')
.send({ reviewState: 'approved' });
await asAlice.patch('/v1/projects/1/forms/multiPropertyEntity/submissions/two')
.send({ reviewState: 'approved' });
await asAlice.patch('/v1/projects/1/forms/multiPropertyEntity2/submissions/one')
.send({ reviewState: 'approved' });

await exhaust(container);

const { text } = await asAlice.get('/v1/projects/1/datasets/foo/entities.csv');

const withOutTs = text.replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/g, '');
withOutTs.should.be.eql(
'__id,label,f_q1,e_q2,a_q3,c_q4,b_q1,d_q2,__createdAt,__creatorId,__creatorName,__updates,__updatedAt,__version\n' +
'12345678-1234-4123-8234-123456789ccc,one,w,x,y,z,,,,5,Alice,0,,1\n'+
'12345678-1234-4123-8234-123456789bbb,two,,,c,d,a,b,,5,Alice,0,,1\n'+
'12345678-1234-4123-8234-123456789aaa,one,,,y,z,w,x,,5,Alice,0,,1\n'
);
}));

it('should not return deleted entities', testService(async (service) => {
const asAlice = await service.login('alice');

Expand Down Expand Up @@ -437,7 +486,7 @@ describe('datasets and entities', () => {

}));

it('should return updated value correctly', testService(async (service) => {
it('should return updated value correctly (entity updated via API)', testService(async (service) => {
const asAlice = await service.login('alice');

await asAlice.post('/v1/projects/1/forms?publish=true')
Expand Down Expand Up @@ -476,6 +525,51 @@ describe('datasets and entities', () => {

}));

it('should return updated value correctly (entity updated via submission)', testService(async (service, container) => {
const asAlice = await service.login('alice');

await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.simpleEntity)
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.post('/v1/projects/1/datasets/people/entities')
.send({
uuid: '12345678-1234-4123-8234-123456789abc',
label: 'Johnny Doe',
data: { first_name: 'Johnny', age: '22' }
})
.expect(200);

// create form and submission to update entity
await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.updateEntity)
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.post('/v1/projects/1/forms/updateEntity/submissions')
.send(testData.instances.updateEntity.one)
.set('Content-Type', 'application/xml')
.expect(200);

await exhaust(container);

const result = await asAlice.get('/v1/projects/1/datasets/people/entities.csv')
.expect(200)
.then(r => r.text);

const isoRegex = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/g;

result.match(isoRegex).should.have.length(2);

const withOutTs = result.replace(isoRegex, '');
withOutTs.should.be.eql(
'__id,label,first_name,age,__createdAt,__creatorId,__creatorName,__updates,__updatedAt,__version\n' +
'12345678-1234-4123-8234-123456789abc,Alicia (85),Alicia,85,,5,Alice,1,,2\n'
);

}));

it('should return 304 content not changed if ETag matches', testService(async (service, container) => {
const asAlice = await service.login('alice');

Expand Down
Loading