Skip to content
Merged
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
14 changes: 14 additions & 0 deletions tests/tx/designations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ describe('Designations', () => {
expect(presentation).toContain("'French term'");
expect(presentation).toContain("'German term'");
});

test('presentCount counts what present lists, across regions of the language', () => {
// displayCount stops at the best kind of match: for 'en' that is the plain 'en'
// designation, while present lists the 'en-US' one too
const langList = Languages.fromAcceptLanguage('en', languageDefinitions);
expect(designations.displayCount(langList, null, false)).toBe(1);
expect(designations.present(langList, null, false)).toBe("'English base' (en) or 'US English display' (en-US)");
expect(designations.presentCount(langList, null, false)).toBe(2);
});

test('presentCount counts the fallback list when no language matches', () => {
const langList = Languages.fromAcceptLanguage('zh', languageDefinitions);
expect(designations.presentCount(langList, null, false)).toBe(4);
});
});

describe('Complex language preference scenarios', () => {
Expand Down
9 changes: 9 additions & 0 deletions tests/tx/snomed-dialect-display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,14 @@ describe('SNOMED CT display by English dialect', () => {
}
}
});

test('a wrong display is told the number of choices listed, GB synonym included', async () => {
const list = "'Hematoma' (en), 'Haematoma' (en-GB), 'Hematoma, NOS' (en), 'Haematoma, NOS' (en) or 'Hematoma (morphologic abnormality)' (en)";
for (const lang of ['en', 'en-US', 'en-GB']) {
const r = await validate(US_FIRST, lang, 'Wrong');
expect(r.result).toBe(false);
expect(r.message).toContain(`Valid display is one of 5 choices: ${list} (for the language(s) '${lang}')`);
}
});
});
});
120 changes: 120 additions & 0 deletions tests/tx/validate-display-choice-count.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* The wrong-display message "Valid display is one of N choices: ..." says how many
* choices it lists. The count used to come from Designations.displayCount(), which counts
* only the best kind of language match, while the list comes from present(), which matches
* by language alone: for 'en', a concept with displays in 'en' and 'en-GB' was reported as
* "one of 2 choices" followed by three, and for 'en-GB' the single-choice wording was used
* in front of a list of three.
*/

const { ValidateWorker } = require('../../tx/workers/validate');
const { CodeSystem } = require('../../tx/library/codesystem');
const { FhirCodeSystemProvider } = require('../../tx/cs/cs-cs');
const ValueSet = require('../../tx/library/valueset');
const { Languages } = require('../../library/languages');
const { OperationContext } = require('../../tx/operation-context');
const { TestUtilities } = require('../test-utilities');
const { TxParameters } = require('../../tx/params');

const mockLog = { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() };
const URL = 'http://example.org/devices';

function devicesCS() {
return new CodeSystem({
resourceType: 'CodeSystem', url: URL, version: '1.0.0', name: 'Devices', status: 'active',
language: 'en', caseSensitive: true, content: 'complete',
concept: [
{
code: 'analyzer', display: 'Oxygen analyzer',
designation: [
{ language: 'en', value: 'O2 analyzer' },
{ language: 'en-GB', value: 'Oxygen analyser' }
]
},
{
code: 'monitor', display: 'Monitor',
designation: [{ language: 'en-GB', value: 'Display unit' }]
},
{ code: 'pump', display: 'Pump' }
]
});
}

function provider() {
return {
getCodeSystem: (c, url) => url === URL ? devicesCS().jsonObj : null,
getCodeSystemProvider: (c, url, version, supps) =>
url === URL ? new FhirCodeSystemProvider(c, devicesCS(), supps || []) : null,
getCodeSystemById: () => null,
findValueSet: () => null,
getValueSetById: () => null,
loadSupplements: () => []
};
}

function message(result) {
return (result.parameter.find(p => p.name === 'message') || {}).valueString;
}

describe('wrong-display message counts the choices it lists', () => {
let worker, opContext, txp;

beforeEach(async () => {
jest.clearAllMocks();
const langDefs = await TestUtilities.loadLanguageDefinitions();
const i18n = await TestUtilities.loadTranslations(langDefs);
opContext = new OperationContext(new Languages(), i18n);
worker = new ValidateWorker(opContext, mockLog, provider(), langDefs, i18n);
// validating = true, as $validate-code sets it: without it an implicit "*" is added to
// the working languages, every designation matches fully, and the bug does not show
txp = new TxParameters(opContext.i18n.languageDefinitions, opContext.i18n, true);
});

function language(lang) {
txp.readParams({
resourceType: 'Parameters',
parameter: [{ name: '__Accept-Language', valueCode: lang }]
});
}

async function validateCS(code) {
const coded = { coding: [{ system: URL, code, display: 'Wrong' }] };
const csp = new FhirCodeSystemProvider(opContext, devicesCS(), []);
return message(await worker.doValidationCS(coded, csp, txp, { issuePath: 'Coding', mode: 'coding' }));
}

async function validateVS(code) {
const coded = { coding: [{ system: URL, code, display: 'Wrong' }] };
const valueSet = new ValueSet({
resourceType: 'ValueSet', url: 'http://example.org/ValueSet/devices',
compose: { include: [{ system: URL }] }
});
return message(await worker.doValidationVS(coded, valueSet, txp, 'coded', 'Coding'));
}

for (const [name, validate] of [['CodeSystem', validateCS], ['ValueSet', validateVS]]) {
test(`${name}: two 'en' displays and one 'en-GB' are three choices`, async () => {
language('en');
const m = await validate('analyzer');
expect(m).toContain("one of 3 choices: 'Oxygen analyzer' (en), 'O2 analyzer' (en) or 'Oxygen analyser' (en-GB)");
});

test(`${name}: one 'en' display and one 'en-GB' are two choices`, async () => {
language('en');
const m = await validate('monitor');
expect(m).toContain("one of 2 choices: 'Monitor' (en) or 'Display unit' (en-GB)");
});

test(`${name}: for 'en-GB', three listed choices are not worded as one`, async () => {
language('en-GB');
const m = await validate('analyzer');
expect(m).toContain("one of 3 choices: 'Oxygen analyzer' (en), 'O2 analyzer' (en) or 'Oxygen analyser' (en-GB)");
});

test(`${name}: a single display is not counted`, async () => {
language('en');
const m = await validate('pump');
expect(m).toContain("Valid display is 'Pump' (en) (for the language(s) 'en')");
});
}
});
53 changes: 32 additions & 21 deletions tx/library/designations.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,45 +482,56 @@ class Designations {
}

/**
* Present all matching designations as a formatted string
* The designations present() lists: those in the requested languages (matched by
* language alone), or every designation when none of them is
*/
present(langList, defLang, displayOnly) {
validateOptionalParameter(langList, 'langList', Languages, true); // Allow null
validateOptionalParameter(defLang, 'defLang', Language);
validateParameter(displayOnly, 'displayOnly', Boolean);

_presented(langList, displayOnly) {
const results = [];
let count = 0;

// Collect matching designations
for (const cd of this.designations) {
if ((!displayOnly || this.isDisplay(cd)) &&
this._langsMatch(langList, cd.language, LangMatchType.LANG, null) &&
cd.value) {
count++;
if (cd.language) {
results.push(`'${cd.display}' (${cd.language.code})`);
} else {
results.push(`'${cd.display}'`);
}
results.push(cd);
}
}

// If no language-specific matches, get all
if (count === 0) {
if (results.length === 0) {
for (const cd of this.designations) {
if ((!displayOnly || this.isDisplay(cd)) && cd.value) {
count++;
if (cd.language) {
results.push(`'${cd.display}' (${cd.language.code})`);
} else {
results.push(`'${cd.display}'`);
}
results.push(cd);
}
}
}

return this._joinWithOr(results);
return results;
}

/**
* Present all matching designations as a formatted string
*/
present(langList, defLang, displayOnly) {
validateOptionalParameter(langList, 'langList', Languages, true); // Allow null
validateOptionalParameter(defLang, 'defLang', Language);
validateParameter(displayOnly, 'displayOnly', Boolean);

return this._joinWithOr(this._presented(langList, displayOnly).map(cd =>
cd.language ? `'${cd.display}' (${cd.language.code})` : `'${cd.display}'`));
}

/**
* How many designations present() lists for the same arguments. This can be more than
* displayCount(), which counts only the best kind of language match: for 'en' it counts
* the 'en' designations and not an 'en-GB' one, where present() lists both.
*/
presentCount(langList, defLang, displayOnly) {
validateOptionalParameter(langList, 'langList', Languages, true); // Allow null
validateOptionalParameter(defLang, 'defLang', Language);
validateParameter(displayOnly, 'displayOnly', Boolean);

return this._presented(langList, displayOnly).length;
}

/**
Expand Down
18 changes: 8 additions & 10 deletions tx/workers/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1355,12 +1355,11 @@ class ValueSetChecker {
baseMsg = 'NO_VALID_DISPLAY_AT_ALL';
m = this.worker.i18n.translate('NO_VALID_DISPLAY_AT_ALL', this.params.HTTPLanguages,
[c.display, prov.system(), c.code, this.params.langSummary()]);
} else if (dc === 1) {
m = this.worker.i18n.translate(baseMsg + '_one', this.params.HTTPLanguages,
['', prov.system(), c.code, list.present(this.params.workingLanguages(), defLang.value, true), c.display, this.params.langSummary()]);
} else {
m = this.worker.i18n.translate(baseMsg + '_other', this.params.HTTPLanguages,
[dc.toString(), prov.system(), c.code, list.present(this.params.workingLanguages(), defLang.value, true), c.display, this.params.langSummary()]);
// the count in the message is the number of choices the message lists
const shown = list.presentCount(this.params.workingLanguages(), defLang.value, true);
m = this.worker.i18n.translate(baseMsg + (shown === 1 ? '_one' : '_other'), this.params.HTTPLanguages,
[shown === 1 ? '' : shown.toString(), prov.system(), c.code, list.present(this.params.workingLanguages(), defLang.value, true), c.display, this.params.langSummary()]);
}
msg(m);
op.addIssue(new Issue(severity, 'invalid', addToPath(path, 'display'), baseMsg, m, 'invalid-display'));
Expand Down Expand Up @@ -1672,12 +1671,11 @@ class ValueSetChecker {
}
}
}
} else if (dc === 1) {
m = this.worker.i18n.translate(baseMsg + '_one', this.params.HTTPLanguages,
['', c.system, c.code, list.present(this.params.workingLanguages(), defLang.value, dc > 0), c.display, this.params.langSummary()]);
} else {
m = this.worker.i18n.translate(baseMsg + '_other', this.params.HTTPLanguages,
[dc.toString(), c.system, c.code, list.present(this.params.workingLanguages(), defLang.value, dc > 0), c.display, this.params.langSummary()]);
// the count in the message is the number of choices the message lists
const shown = list.presentCount(this.params.workingLanguages(), defLang.value, true);
m = this.worker.i18n.translate(baseMsg + (shown === 1 ? '_one' : '_other'), this.params.HTTPLanguages,
[shown === 1 ? '' : shown.toString(), c.system, c.code, list.present(this.params.workingLanguages(), defLang.value, true), c.display, this.params.langSummary()]);
}
msg(m);
op.addIssue(new Issue(severity, 'invalid', addToPath(path, 'display'), mid, m, 'invalid-display'));
Expand Down
Loading