Skip to content

Commit

Permalink
minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Oct 4, 2024
1 parent 7878c43 commit cef069c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
7 changes: 2 additions & 5 deletions lib/data/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// 2 When walking the schema to do some later processing of data, this ordering
// allows us to iterate the fields in order and still understand the tree structure.

const { always, last, map } = require('ramda');
const { always, equals, last, map } = require('ramda');
const ptr = last; // just rename to make it more relevant to our context.
const hparser = require('htmlparser2');
const parse = require('csv-parse/lib/sync');
Expand Down Expand Up @@ -590,10 +590,7 @@ const _addBranchIdAndTrunkVersion = (xml) => new Promise((pass, fail) => {
const parser = new hparser.Parser({
onopentag: (fullname) => {
stack.push(stripNamespacesFromPath(fullname));
if ((stack.length) === 7 &&
(stack[0] === 'html') && (stack[1] === 'head') && (stack[2] === 'model') &&
(stack[3] === 'instance') && (stack[4] === 'data') && (stack[5] === 'meta') &&
(stack[6] === 'entity')) {
if (equals(stack, ['html', 'head', 'model', 'instance', 'data', 'meta', 'entity'])) {
const idx = _findSplicePoint(xml, parser.endIndex);
parser.reset(); // stop parsing.
return pass(`${xml.slice(0, idx)} trunkVersion="" branchId=""${xml.slice(idx)}`);
Expand Down
24 changes: 16 additions & 8 deletions test/unit/data/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2088,8 +2088,12 @@ describe('form schema', () => {
});

describe('updateEntityForm', () => {
it('should change version 2023->2024, add trunkVersion, and add branchId', () =>
updateEntityForm(testData.forms.updateEntity, '2023.1.0', '2024.1.0', '_upgrade').then((result) => result.should.equal(`<?xml version="1.0"?>
it('should change version 2023->2024, add trunkVersion, and add branchId', (async () => {
const result = await updateEntityForm(testData.forms.updateEntity, '2023.1.0', '2024.1.0', '_upgrade');
// entities-version has been updated
// version has suffix
// trunkVersion and branchId are present
result.should.equal(`<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:entities="http://www.opendatakit.org/xforms">
<h:head>
<model entities:entities-version="2024.1.0">
Expand All @@ -2109,13 +2113,17 @@ describe('form schema', () => {
<bind nodeset="/data/age" type="int" entities:saveto="age"/>
</model>
</h:head>
</h:html>`)));

it('should not alter a version 2022.1.0 form when the old version to replace is 2023.1.0', () =>
updateEntityForm(testData.forms.simpleEntity, '2023.1.0', '2024.1.0', '_upgrade').then((result) => result.should.equal(testData.forms.simpleEntity)));
</h:html>`);
}));

it('should not alter a version 2024.1.0 form when the old version to replace is 2023.1.0', () =>
updateEntityForm(testData.forms.offlineEntity, '2023.1.0', '2024.1.0', '_upgrade').then((result) => result.should.equal(testData.forms.offlineEntity)));
it('should not alter a version 2022.1.0 form when the old version to replace is 2023.1.0', (async () => {
const result = await updateEntityForm(testData.forms.simpleEntity, '2023.1.0', '2024.1.0', '_upgrade');
result.should.equal(testData.forms.simpleEntity);
}));
it('should not alter a version 2024.1.0 form when the old version to replace is 2023.1.0', (async () => {
const result = await updateEntityForm(testData.forms.offlineEntity, '2023.1.0', '2024.1.0', '_upgrade');
result.should.equal(testData.forms.offlineEntity);
}));
});
});

0 comments on commit cef069c

Please sign in to comment.