Skip to content

Commit

Permalink
unit tests about parsing branch and trunk values from submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Jun 21, 2024
1 parent bfd6a4a commit 207e656
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 17 deletions.
29 changes: 14 additions & 15 deletions lib/data/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,23 @@ const extractBaseVersionFromSubmission = (entity) => {
}
};

// TODO: write unit tests of this
const extractBranchIdFromSubmission = (entity) => {
const { branchId } = entity.system;
if (branchId === '')
return null;

if (branchId) {
const matches = _uuidPattern.exec(branchId);
if (matches == null) throw Problem.user.invalidDataTypeOfParameter({ field: 'branchId', expected: 'valid version 4 UUID' });
return matches[2].toLowerCase();
}
};

const extractTrunkVersionFromSubmission = (entity) => {
const { trunkVersion, branchId } = entity.system;
const { trunkVersion } = entity.system;
if (trunkVersion) {
// branchId must be present with trunk version
const branchId = extractBranchIdFromSubmission(entity);
if (!branchId)
throw Problem.user.missingParameter({ field: 'branchId' });

Expand All @@ -83,19 +95,6 @@ const extractTrunkVersionFromSubmission = (entity) => {
return null;
};

// TODO: write unit tests of this
const extractBranchIdFromSubmission = (entity) => {
const { branchId } = entity.system;
if (branchId === '')
return null;

if (branchId) {
const matches = _uuidPattern.exec(branchId);
if (matches == null) throw Problem.user.invalidDataTypeOfParameter({ field: 'branchId', expected: 'valid version 4 UUID' });
return matches[2].toLowerCase();
}
};

// This works similarly to processing submissions for export, but also note:
// 1. this is expecting the entityFields to be filled in with propertyName attributes
// 2. the "meta/entity" structural field should be included to get necessary
Expand Down
2 changes: 1 addition & 1 deletion lib/model/frames/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class Entity extends Frame.define(
const uuid = normalizeUuid(entityData.system.id);
const label = extractLabelFromSubmission(entityData, options);
const baseVersion = extractBaseVersionFromSubmission(entityData);
const trunkVersion = extractTrunkVersionFromSubmission(entityData);
const branchId = extractBranchIdFromSubmission(entityData);
const trunkVersion = extractTrunkVersionFromSubmission(entityData);
const dataReceived = { ...data, ...(label && { label }) };
return new Entity.Partial({ uuid }, {
def: new Entity.Def({
Expand Down
65 changes: 64 additions & 1 deletion test/unit/data/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ const appRoot = require('app-root-path');
const assert = require('assert');
const { ConflictType } = require('../../../lib/data/entity');
const { Entity } = require('../../../lib/model/frames');
const { normalizeUuid, extractLabelFromSubmission, extractBaseVersionFromSubmission, parseSubmissionXml, extractEntity, extractBulkSource, extractSelectedProperties, selectFields, diffEntityData, getDiffProp, getWithConflictDetails } = require(appRoot + '/lib/data/entity');
const { normalizeUuid,
extractLabelFromSubmission,
extractBaseVersionFromSubmission,
extractBranchIdFromSubmission,
extractTrunkVersionFromSubmission,
parseSubmissionXml,
extractEntity,
extractBulkSource,
extractSelectedProperties,
selectFields,
diffEntityData,
getDiffProp,
getWithConflictDetails } = require(appRoot + '/lib/data/entity');
const { fieldsFor } = require(appRoot + '/test/util/schema');
const testData = require(appRoot + '/test/data/xml');

Expand Down Expand Up @@ -160,6 +172,57 @@ describe('extracting and validating entities', () => {
});
});
});

describe('extractBranchIdFromSubmission', () => {
it('should extract branchId as uuid', () => {
const entity = { system: { branchId: 'dcd8906c-e795-45f8-8670-48e97ba79437' } };
extractBranchIdFromSubmission(entity).should.equal('dcd8906c-e795-45f8-8670-48e97ba79437');
});

it('should complain if branchId is provided but is not a v4 uuid', () => {
const entity = { system: { branchId: 'not-a-uuid' } };
assert.throws(() => { extractBranchIdFromSubmission(entity); }, (err) => {
err.problemCode.should.equal(400.11);
err.message.should.equal('Invalid input data type: expected (branchId) to be (valid version 4 UUID)');
return true;
});
});

it('should return null for branch id if empty string', () => {
const entity = { system: { branchId: '' } };
should.not.exist(extractBranchIdFromSubmission(entity));
});

it('should return null for branch id not provided', () => {
const entity = { system: { } };
should.not.exist(extractBranchIdFromSubmission(entity));
});
});

describe('extractTrunkVersionFromSubmission', () => {
it('should extract trunkVersion', () => {
const entity = { system: { trunkVersion: '4', branchId: 'dcd8906c-e795-45f8-8670-48e97ba79437' } };
extractTrunkVersionFromSubmission(entity).should.equal(4);
});

it('should complain if trunkVersion is provided with invalid branchId', () => {
const entity = { system: { trunkVersion: '1', branchId: 'not-a-uuid' } };
assert.throws(() => { extractTrunkVersionFromSubmission(entity); }, (err) => {
err.problemCode.should.equal(400.11);
err.message.should.equal('Invalid input data type: expected (branchId) to be (valid version 4 UUID)');
return true;
});
});

it('should complain if trunkVersion is provided without branchId', () => {
const entity = { system: { trunkVersion: '1' } };
assert.throws(() => { extractTrunkVersionFromSubmission(entity); }, (err) => {
err.problemCode.should.equal(400.2);
err.message.should.equal('Required parameter branchId missing.');
return true;
});
});
});
});

describe('extract entity from submission: parseSubmissionXml', () => {
Expand Down

0 comments on commit 207e656

Please sign in to comment.