Skip to content
Closed
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
52 changes: 13 additions & 39 deletions tests/tx/sqlite-v2-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ describe('sqlite-v2 terminology file builder', () => {
expect(order).toEqual(['R', 'Y', 'root', 'B', 'D', 'F', 'C', 'E', 'G', 'S1', 'Z', 'H', 'Q', 'X']);
});

test('the catalog\'s computed fields: rows, subject and target kinds, by_target for the hierarchy', () => {
const row = (code) => db.prepare('SELECT shape, subject_kind, target_kind, n_rows, by_target, filterable FROM property WHERE code = ?').get(code);
expect(row('isa')).toEqual({ shape: 'link', subject_kind: 'concept', target_kind: 'concept', n_rows: 12, by_target: 1, filterable: 1 });
expect(row('colour')).toEqual({ shape: 'literal', subject_kind: 'concept', target_kind: null, n_rows: 4, by_target: 0, filterable: 1 });
expect(row('S1')).toEqual({ shape: 'record', subject_kind: 'concept', target_kind: null, n_rows: 3, by_target: 0, filterable: 1 });
test('the catalog\'s computed fields: subject kind, by_target for the hierarchy', () => {
const row = (code) => db.prepare('SELECT shape, subject_kind, by_target, filterable FROM property WHERE code = ?').get(code);
expect(row('isa')).toEqual({ shape: 'link', subject_kind: 'concept', by_target: 1, filterable: 1 });
expect(row('colour')).toEqual({ shape: 'literal', subject_kind: 'concept', by_target: 0, filterable: 1 });
expect(row('S1')).toEqual({ shape: 'record', subject_kind: 'concept', by_target: 0, filterable: 1 });
});

test('descendant sets equal the closure recomputed with a recursive CTE', () => {
Expand Down Expand Up @@ -197,12 +197,14 @@ describe('sqlite-v2 terminology file builder', () => {
expect(db.prepare("SELECT rowid FROM ix_text_item WHERE ix_text_item MATCH 'eff'").pluck().all()).toEqual([15]);
});

test('meta: the format, the languages, provenance; PRAGMA user_version 1; a unique code index', () => {
test('meta: the format, requires, integrity, the languages, provenance; PRAGMA user_version 1; a unique code index', () => {
const m = Object.fromEntries(db.prepare('SELECT key, value FROM meta').raw().all());
expect(m.format).toBe(FORMAT);
expect(FORMAT).toBe('tx-terminology/1');
expect(JSON.parse(m.languages)).toEqual(['en']);
expect(m['provenance.importer']).toBe('test');
expect(JSON.parse(m.requires)).toEqual(['identifier-text']);
expect(Object.keys(JSON.parse(m.integrity))).toEqual(['items', 'schema', 'catalog']);
expect(db.pragma('user_version', { simple: true })).toBe(1);
expect(db.prepare("SELECT sql FROM sqlite_master WHERE name = 'concept_code'").pluck().get()).toMatch(/^CREATE UNIQUE INDEX/);
});
Expand Down Expand Up @@ -352,44 +354,16 @@ describe('sqlite-v2 identifiers accepted as codes; definitions; the format check
expect(() => openStore(older)).toThrow(/format tx-terminology\/0\.3; this server reads tx-terminology\/1: re-import/);
});

test('behaviour settings: defaults, the ones a file sets, and an unknown key or value refused at open', () => {
test('behaviour: the grammar only; a builder refuses any other behaviour key (meta check)', () => {
const S = openStore(file, { unregistered: true });
try {
expect(S.behaviour).toMatchObject({ grammar: null, expansionOrder: 'ids', lookupName: 'name', distinctDesignationTexts: false });
expect(S.grammar).toBeNull();
expect(S.expansionOrder).toBe('ids'); // a generic system without a stored order
} finally { S.close(); }
const bad = (key, value, msg) => {
const f = path.join(dir, 'behaviour.db');
fs.copyFileSync(file, f);
const w = new Database(f);
w.prepare('INSERT INTO meta VALUES (?, ?)').run(key, value);
w.close();
expect(() => openStore(f)).toThrow(msg);
};
bad('behaviour.colour', '"red"', /unknown behaviour setting behaviour.colour/);
bad('behaviour.designationUses', '"sometimes"', /behaviour.designationUses = "sometimes" is not a value this server knows/);
bad('behaviour.grammar', 'snomed', /is not JSON/);
bad('behaviour.preferredForLanguage', '"yes"', /behaviour.preferredForLanguage = "yes" is not a value/);
bad('behaviour.aliasLiterals', '{"code-type": 11}', /behaviour.aliasLiterals = .* is not a value/);
bad('behaviour.filters', '[{"code": "kind", "operator": ["like"]}]', /behaviour.filters = .* is not a value/);
bad('behaviour.filters', '[{"code": "kind", "operator": ["="], "values": "product"}]', /behaviour.filters = .* is not a value/);
const good = path.join(dir, 'behaviour-ok.db');
fs.copyFileSync(file, good);
const w = new Database(good);
w.prepare('INSERT INTO meta VALUES (?, ?)').run('behaviour.preferredForLanguage', 'true');
w.prepare('INSERT INTO meta VALUES (?, ?)').run('behaviour.aliasLiterals', '{"code-type": "11-digit", "synonym": "{code}"}');
w.prepare('INSERT INTO meta VALUES (?, ?)').run('behaviour.filters',
'[{"code": "inactive", "operator": ["="], "values": ["true", "false"]}, {"code": "category", "operator": ["=", "in"]}]');
w.close();
const G = openStore(good, { unregistered: true });
try {
expect(G.behaviour.preferredForLanguage).toBe(true);
expect(G.behaviour.aliasLiterals).toEqual({ 'code-type': '11-digit', synonym: '{code}' });
expect(G.behaviour.filters.map((f) => f.code)).toEqual(['inactive', 'category']);
} finally { G.close(); }
const b = new TerminologyBuilder(path.join(dir, 'never.db'), { overwrite: true, importer: 'test' });
b.codeSystem({ uri: 'http://example.org/x', version: '1' });
b.concept('a');
b.setBehaviour('lookupName', 'nickname');
expect(() => b.finish()).toThrow(/behaviour.lookupName = "nickname" is not a value/);
b.setBehaviour('lookupName', 'title');
expect(() => b.finish()).toThrow(/unknown meta key behaviour.lookupName \(a retired key/);
});
});
6 changes: 4 additions & 2 deletions tests/tx/sqlite-v2-filter-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ beforeAll(async () => {
await importSnomedV2FromCache({ source: cache, dest: path.join(dir, 'sct.db'), overwrite: true, log: () => {} });
fs.copyFileSync(path.join(dir, 'sct.db'), path.join(dir, 'sct-bare.db'));
const bare = new Database(path.join(dir, 'sct-bare.db'));
// without its word index (ix_word, and the rules it was made by, meta ix.ix_word.format)
bare.exec("DELETE FROM ix_word; DELETE FROM meta WHERE key = 'ix.ix_word.format'");
// without its word index (ix_word, and its facts, meta ix.ix_word.*); finished again, as a tool
// that rewrites a finished file does (its integrity facts)
bare.exec("DELETE FROM ix_word; DELETE FROM meta WHERE key LIKE 'ix.ix\\_word.%' ESCAPE '\\'");
require('../../tx/importers/sqlite-v2/builder').finishFile(bare, path.join(dir, 'sct-bare.db'));
bare.close();
stored = await loadSqliteV2Factory(op.i18n, path.join(dir, 'sct.db'));
memory = await loadSqliteV2Factory(op.i18n, path.join(dir, 'sct-bare.db'));
Expand Down
4 changes: 2 additions & 2 deletions tests/tx/sqlite-v2-format-cases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ describe('4. RxNorm atom-level relationships (importer option atomRelationships)
const db = rxAtoms.store.db;
const rows = db.prepare(`SELECT p.code, i.value, c.code FROM identifier i JOIN property p ON p.id = i.property_id JOIN concept c ON c.id = i.subject_id
ORDER BY 1, 2, 3`).raw().all();
// 2002 -> 5001 (concept 500, no RxNorm atom) is left out; values are stored as integers
expect(rows).toEqual([['AUI:RO:has_active_ingredient', 2002, '300'], ['AUI:RO:has_ingredient', 2002, '100'], ['AUI:SY:', 1001, '100'], ['AUI:SY:', 3001, '300']]);
// 2002 -> 5001 (concept 500, no RxNorm atom) is left out; values are stored as text (identifier-text)
expect(rows).toEqual([['AUI:RO:has_active_ingredient', '2002', '300'], ['AUI:RO:has_ingredient', '2002', '100'], ['AUI:SY:', '1001', '100'], ['AUI:SY:', '3001', '300']]);
const members = db.prepare(`SELECT u.code, group_concat(m.code, ',' ORDER BY m.code) FROM property_member x JOIN property u ON u.id = x.parent_id JOIN property m ON m.id = x.child_id
WHERE u.code LIKE 'AUI:%' GROUP BY u.code ORDER BY u.code`).raw().all();
expect(members).toEqual([['AUI:RO', 'AUI:RO:has_active_ingredient,AUI:RO:has_ingredient'], ['AUI:SY', 'AUI:SY:'],
Expand Down
Loading
Loading