Skip to content

Commit

Permalink
Fix baseVersion parsing when create=true
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Oct 4, 2024
1 parent a0e1f27 commit 688fa02
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
7 changes: 5 additions & 2 deletions lib/data/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const { PartialPipe } = require('../util/stream');
const Problem = require('../util/problem');
const { submissionXmlToFieldStream } = require('./submission');
const { nextUrlFor, getServiceRoot, jsonDataFooter, extractPaging } = require('../util/odata');
const { sanitizeOdataIdentifier, blankStringToNull } = require('../util/util');
const { sanitizeOdataIdentifier, blankStringToNull, isBlank } = require('../util/util');

const odataToColumnMap = new Map([
['__system/createdAt', 'entities.createdAt'],
Expand Down Expand Up @@ -61,12 +61,15 @@ const extractBaseVersionFromSubmission = (entity, options = { update: true }) =>
if (update) {
if (!baseVersion)
throw Problem.user.missingParameter({ field: 'baseVersion' });

}
if (!isBlank(baseVersion)) {
// Check type and parseInt in both create and update case
if (!/^\d+$/.test(baseVersion))
throw Problem.user.invalidDataTypeOfParameter({ field: 'baseVersion', expected: 'integer' });

return parseInt(entity.system.baseVersion, 10);
}
return null;
};

const extractBranchIdFromSubmission = (entity) => {
Expand Down
20 changes: 10 additions & 10 deletions test/unit/data/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,9 @@ describe('extracting and validating entities', () => {
extractBaseVersionFromSubmission(entity, { update: true }).should.equal(99);
});

it('not return base version if create is true because it is not relevant', () => {
it('should extract base version even if create is true and update is not', () => {
const entity = { system: { baseVersion: '99' } };
should.not.exist(extractBaseVersionFromSubmission(entity, { create: true }));
});

it('not complain if baseVersion is missing when update is false and create is true', () => {
const entity = { system: { update: '1', create: '1' } };
// the create/update values do not get pulled from the entity system data here
// but rather from the branch in the code that decides whether a create
// or update is currently being attempted.
should.not.exist(extractBaseVersionFromSubmission(entity, { create: true }));
extractBaseVersionFromSubmission(entity, { create: true }).should.equal(99);
});

it('should complain if baseVersion is missing when update is true', () => {
Expand All @@ -157,6 +149,14 @@ describe('extracting and validating entities', () => {
});
});

it('not complain if baseVersion is missing when update is false and create is true', () => {
const entity = { system: { } };
// the create/update values do not get pulled from the entity system data here
// but rather from the branch in the code that decides whether a create
// or update is currently being attempted.
should.not.exist(extractBaseVersionFromSubmission(entity, { create: true }));
});

it('should complain if baseVersion not an integer', () => {
const entity = { system: { update: '1', baseVersion: 'ten' } };
assert.throws(() => { extractBaseVersionFromSubmission(entity); }, (err) => {
Expand Down
53 changes: 53 additions & 0 deletions test/unit/model/frames/entity.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const appRoot = require('app-root-path');
const { Entity } = require(appRoot + '/lib/model/frames');
const assert = require('assert');


describe('entity', () => {
describe('fromParseEntityData', () => {
Expand All @@ -23,6 +25,57 @@ describe('entity', () => {
}));
partial.aux.should.have.property('dataset', 'people');
});

describe('baseVersion', () => {
it('should parse successfully for empty baseVersion, create: true', () => {
const partial = Entity.fromParseEntityData({
system: {
label: 'label',
id: 'uuid:12345678-1234-4123-8234-abcd56789abc',
create: '1',
baseVersion: '',
dataset: 'people'
},
data: { field: 'value' }
},
{ create: true });
partial.aux.def.should.not.have.property('baseVersion');
});

it('should return baseVersion even when create: true', () => {
const partial = Entity.fromParseEntityData({
system: {
label: 'label',
id: 'uuid:12345678-1234-4123-8234-abcd56789abc',
create: '1',
baseVersion: '73',
dataset: 'people'
},
data: { field: 'value' }
},
{ create: true });
partial.aux.def.baseVersion.should.equal(73);
});

it('should complain about missing baseVersion when update: true', () => {
const entity = {
system: {
label: 'label',
id: 'uuid:12345678-1234-4123-8234-abcd56789abc',
create: '1',
baseVersion: '',
dataset: 'people'
},
data: { field: 'value' }
};

assert.throws(() => { Entity.fromParseEntityData(entity, { update: true }); }, (err) => {
err.problemCode.should.equal(400.2);
err.message.should.equal('Required parameter baseVersion missing.');
return true;
});
});
});
});
});

0 comments on commit 688fa02

Please sign in to comment.