-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate_test.ts
94 lines (89 loc) · 2.81 KB
/
migrate_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { testingAsserts as ta } from "./deps-test.ts";
import { jsonMutator as jm, path } from "./deps.ts";
import { NihLhcForm } from "./lform.ts";
import * as mod from "./mod.ts";
function testFilePath(relTestFileName: string): string {
return path.join(
path.relative(
Deno.cwd(),
path.dirname(path.fromFileUrl(import.meta.url)),
),
relTestFileName,
);
}
Deno.test(`mutate LHC Form values (for data migrations)`, () => {
const qcExactSuppliers = new mod.FormItemJsonPatchMutationsSupplierTextMap<
NihLhcForm
>();
const qcRegExSuppliers = new mod.FormItemJsonPatchMutationsSupplierRegExMap<
NihLhcForm
>();
qcRegExSuppliers.set(/^002-01-01|002-01-02$/, (ctx) => {
ctx.itemJPMS.removeValues(
`codingInstructions`,
`copyrightNotice`,
`dataType`,
`units`,
);
});
qcExactSuppliers.set("Q002-02-11", (ctx) => {
ctx.itemJPMS.removeValues(`dataType`, `hideUnits`);
});
qcExactSuppliers.set("002-02-00::002-02-01", (ctx) => {
ctx.itemJPMS.removeValues(`codingInstructions`);
});
let commonEncountered = 0;
let noMatchEncountered = 0;
let reportMatchEncountered = 0;
let itemsWithAnswersEncountered = 0;
let answersEncountered = 0;
const lfmp = mod.lhcFormFlexibleMutationsSupplier(
(formCtx) => {
formCtx.formJPMS.removeValues(...[
"/code",
"/PATH_DELIMITER",
"/template",
"/type",
]);
},
mod.lhcFormQuestionCodeMutationsSuppliers({
questionCodesHierarchySearchKey: (...questionCodes: string[]): string => {
return questionCodes.join("::");
},
exactMutators: qcExactSuppliers,
regExMutators: qcRegExSuppliers,
everyMutator: (ctx) => {
commonEncountered++;
if (ctx.forEachAnswer) {
itemsWithAnswersEncountered++;
ctx.forEachAnswer(() => {
answersEncountered++;
});
}
return undefined;
},
noMatchMutator: () => {
noMatchEncountered++;
return undefined;
},
reportMatch: (qc, supplier, regExp) => {
reportMatchEncountered++;
},
}),
);
const mlfr = mod.migrateLhcFormFile(
testFilePath("test5-institution-profile.lhc-form.json"),
lfmp,
);
ta.assertEquals(commonEncountered, 31);
ta.assertEquals(noMatchEncountered, 27);
ta.assertEquals(reportMatchEncountered, 4);
ta.assertEquals(itemsWithAnswersEncountered, 4);
ta.assertEquals(answersEncountered, 21);
ta.assert(jm.isJsonPatchMutationResult(mlfr.mutationResult));
ta.assert(mlfr.mutationResult.mutated);
// we created 14 patch operations but only 7 are valid so those are kept;
// the others probaby weren't found in the source so they were filtered
ta.assertEquals(mlfr.suggestedPatchOps.length, 15);
ta.assertEquals(mlfr.mutationResult.patchOps.length, 7);
});