From 14b7391b5bbbb5312f30012536c73a16815c6dbf Mon Sep 17 00:00:00 2001 From: kjefferson Date: Sun, 20 Oct 2024 17:46:13 -0400 Subject: [PATCH] edit to conditional, tests from feedback --- src/export/CodeSystemExporter.ts | 15 ++++-------- src/export/InstanceExporter.ts | 15 ++++-------- src/export/StructureDefinitionExporter.ts | 15 ++++-------- src/export/ValueSetExporter.ts | 15 ++++-------- test/export/CodeSystemExporter.test.ts | 4 ++-- test/export/InstanceExporter.test.ts | 24 ++++++++++--------- .../StructureDefinitionExporter.test.ts | 4 ++-- test/export/ValueSetExporter.test.ts | 4 ++-- 8 files changed, 39 insertions(+), 57 deletions(-) diff --git a/src/export/CodeSystemExporter.ts b/src/export/CodeSystemExporter.ts index 656aaf67..f15ae552 100644 --- a/src/export/CodeSystemExporter.ts +++ b/src/export/CodeSystemExporter.ts @@ -273,16 +273,11 @@ export class CodeSystemExporter { } exportCodeSystem(fshDefinition: FshCodeSystem): CodeSystem { - if ( - this.pkg.codeSystems.some(cs => cs.name === fshDefinition.name) || - this.pkg.instances.some(instance => fshDefinition.name === instance._instanceMeta.name) || - this.pkg.profiles.some(prof => fshDefinition.name === prof.name) || - this.pkg.extensions.some(extn => fshDefinition.name === extn.name) || - this.pkg.logicals.some(logical => fshDefinition.name === logical.name) || - this.pkg.resources.some(resource => fshDefinition.name === resource.name) || - this.pkg.valueSets.some(valueSet => fshDefinition.name === valueSet.name) - ) { - logger.error(`Multiple FSH entities created with name ${fshDefinition.name}.`); + const duplicatesList = Object.values(Object.fromEntries(this.pkg.fshMap)).find(entry => entry.fshName == fshDefinition.name); + if (duplicatesList) { + logger.error(`Cannot export CodeSystem ${fshDefinition.name}: a ${duplicatesList.fshType} with this name already exists.`, + fshDefinition.sourceInfo + ); return; } const codeSystem = new CodeSystem(); diff --git a/src/export/InstanceExporter.ts b/src/export/InstanceExporter.ts index 25e51041..4215278c 100644 --- a/src/export/InstanceExporter.ts +++ b/src/export/InstanceExporter.ts @@ -746,16 +746,11 @@ export class InstanceExporter implements Fishable { } exportInstance(fshDefinition: Instance): InstanceDefinition { - if ( - this.pkg.instances.some(i => i._instanceMeta.name === fshDefinition.name) || - this.pkg.codeSystems.some(cs => cs.name === fshDefinition.name) || - this.pkg.profiles.some(prof => fshDefinition.name === prof.name) || - this.pkg.extensions.some(extn => fshDefinition.name === extn.name) || - this.pkg.logicals.some(logical => fshDefinition.name === logical.name) || - this.pkg.resources.some(resource => fshDefinition.name === resource.name) || - this.pkg.valueSets.some(valueSet => fshDefinition.name === valueSet.name) - ) { - logger.error(`Multiple FSH entities created with name ${fshDefinition.name}.`); + const duplicatesList = Object.values(Object.fromEntries(this.pkg.fshMap)).find(entry => entry.fshName == fshDefinition.name); + if (duplicatesList) { + logger.error(`Cannot export Instance ${fshDefinition.name}: a ${duplicatesList.fshType} with this name already exists.`, + fshDefinition.sourceInfo + ); return; } diff --git a/src/export/StructureDefinitionExporter.ts b/src/export/StructureDefinitionExporter.ts index a043d1df..aea942f7 100644 --- a/src/export/StructureDefinitionExporter.ts +++ b/src/export/StructureDefinitionExporter.ts @@ -1337,16 +1337,11 @@ export class StructureDefinitionExporter implements Fishable { * @throws {InvalidLogicalParentError} when Logical does not have valid parent */ exportStructDef(fshDefinition: Profile | Extension | Logical | Resource): StructureDefinition { - if ( - this.pkg.profiles.some(sd => sd.name === fshDefinition.name) || - this.pkg.extensions.some(sd => sd.name === fshDefinition.name) || - this.pkg.logicals.some(sd => sd.name === fshDefinition.name) || - this.pkg.resources.some(sd => sd.name === fshDefinition.name) || - this.pkg.instances.some(i => i._instanceMeta.name === fshDefinition.name) || - this.pkg.valueSets.some(valueSet => fshDefinition.name === valueSet.name) || - this.pkg.codeSystems.some(cs => cs.name === fshDefinition.name) - ) { - logger.error(`Multiple FSH entities created with name ${fshDefinition.name}.`); + const duplicatesList = Object.values(Object.fromEntries(this.pkg.fshMap)).find(entry => entry.fshName == fshDefinition.name); + if (duplicatesList) { + logger.error(`Cannot export StructureDefinition ${fshDefinition.name}: a ${duplicatesList.fshType} with this name already exists.`, + fshDefinition.sourceInfo + ); return; } diff --git a/src/export/ValueSetExporter.ts b/src/export/ValueSetExporter.ts index 6c007b9e..94d1d892 100644 --- a/src/export/ValueSetExporter.ts +++ b/src/export/ValueSetExporter.ts @@ -425,16 +425,11 @@ export class ValueSetExporter { } exportValueSet(fshDefinition: FshValueSet): ValueSet { - if ( - this.pkg.valueSets.some(vs => vs.name === fshDefinition.name) || - this.pkg.profiles.some(sd => sd.name === fshDefinition.name) || - this.pkg.extensions.some(sd => sd.name === fshDefinition.name) || - this.pkg.logicals.some(sd => sd.name === fshDefinition.name) || - this.pkg.resources.some(sd => sd.name === fshDefinition.name) || - this.pkg.instances.some(i => i._instanceMeta.name === fshDefinition.name) || - this.pkg.codeSystems.some(cs => cs.name === fshDefinition.name) - ) { - logger.error(`Multiple FSH entities created with name ${fshDefinition.name}.`); + const duplicatesList = Object.values(Object.fromEntries(this.pkg.fshMap)).find(entry => entry.fshName == fshDefinition.name); + if (duplicatesList) { + logger.error(`Cannot export ValueSet ${fshDefinition.name}: a ${duplicatesList.fshType} with this name already exists.`, + fshDefinition.sourceInfo + ); return; } const vs = new ValueSet(); diff --git a/test/export/CodeSystemExporter.test.ts b/test/export/CodeSystemExporter.test.ts index d96e4a99..6aecb757 100644 --- a/test/export/CodeSystemExporter.test.ts +++ b/test/export/CodeSystemExporter.test.ts @@ -525,7 +525,7 @@ describe('CodeSystemExporter', () => { exporter.exportCodeSystem(secondCodeSystem); expect(loggerSpy.getAllMessages('error')).toHaveLength(1); expect(loggerSpy.getLastMessage()).toMatch( - /Multiple FSH entities created with name FirstCodeSystem/s + /Cannot export CodeSystem FirstCodeSystem: a CodeSystem with this name already exists/s ); }); @@ -544,7 +544,7 @@ describe('CodeSystemExporter', () => { expect(loggerSpy.getAllMessages('error')).toHaveLength(1); expect(loggerSpy.getLastMessage()).toMatch( - /Multiple FSH entities created with name FirstCodeSystem/s + /Cannot export Instance FirstCodeSystem: a CodeSystem with this name already exists/s ); }); diff --git a/test/export/InstanceExporter.test.ts b/test/export/InstanceExporter.test.ts index 02648e18..8a0b94fc 100644 --- a/test/export/InstanceExporter.test.ts +++ b/test/export/InstanceExporter.test.ts @@ -1166,7 +1166,7 @@ describe('InstanceExporter', () => { exporter.exportInstance(secondExamplePractitioner); expect(loggerSpy.getAllMessages('error')).toHaveLength(1); expect(loggerSpy.getLastMessage()).toMatch( - /Multiple FSH entities created with name MySameExampleName/s + /Cannot export Instance MySameExampleName: a Instance with this name already exists/s ); }); @@ -1181,7 +1181,7 @@ describe('InstanceExporter', () => { csExporter.exportCodeSystem(codeSystem); expect(loggerSpy.getAllMessages('error')).toHaveLength(1); expect(loggerSpy.getLastMessage()).toMatch( - /Multiple FSH entities created with name MySameExampleName/s + /Cannot export CodeSystem MySameExampleName: a Instance with this name already exists/s ); }); @@ -2236,7 +2236,6 @@ describe('InstanceExporter', () => { seacowGiven.value = 'Seacow'; seacowName.rules.push(seacowGiven); doc.instances.set(seacowName.name, seacowName); - exportInstance(seacowName); // Instance: ThisIsSeacow // InstanceOf: SeacowPatient @@ -2247,8 +2246,14 @@ describe('InstanceExporter', () => { thisIsName.value = 'SeacowName'; thisIsName.isInstance = true; thisIsSeacow.rules.push(thisIsName); - const exported = exportInstance(thisIsSeacow); - // expect(loggerSpy.getAllMessages('error')).toHaveLength(0); + doc.instances.set(thisIsSeacow.name, thisIsSeacow); + + csExporter.export(); + vsExporter.export(); + sdExporter.export(); + exporter.exportInstance(seacowName); + const exported = exporter.exportInstance(thisIsSeacow); + expect(loggerSpy.getAllMessages('error')).toHaveLength(0); expect(exported.contact[0].name.given).toEqual(['Manatee', 'Seacow']); }); @@ -6905,9 +6910,9 @@ describe('InstanceExporter', () => { // * instantiatesCanonical = Canonical(TestVS) carePlanInstance.rules.push(assignedRefRule); - const exported = exportInstance(carePlanInstance); + const exported = exporter.exportInstance(carePlanInstance); expect(exported.instantiatesCanonical).toEqual(undefined); // instantiatesCanonical is not set with invalid type - expect(loggerSpy.getMessageAtIndex(7, 'error')).toMatch( + expect(loggerSpy.getMessageAtIndex(1, 'error')).toMatch( /The type "Canonical\(ValueSet\)" does not match any of the allowed types\D*/s ); }); @@ -10342,10 +10347,7 @@ describe('InstanceExporter', () => { afterEach(() => { // None of the test expect warnings or errors. All should be clean. expect(loggerSpy.getAllLogs('warn')).toHaveLength(0); - expect(loggerSpy.getAllLogs('error')).toHaveLength(1); - expect(loggerSpy.getMessageAtIndex(0, 'error')).toMatch( - /Multiple FSH entities created with name/ - ); + expect(loggerSpy.getAllLogs('error')).toHaveLength(1); // TODO }); // Setting resourceType diff --git a/test/export/StructureDefinitionExporter.test.ts b/test/export/StructureDefinitionExporter.test.ts index 888ddbe9..63396bf1 100644 --- a/test/export/StructureDefinitionExporter.test.ts +++ b/test/export/StructureDefinitionExporter.test.ts @@ -1026,7 +1026,7 @@ describe('StructureDefinitionExporter R4', () => { exporter.exportStructDef(secondProfile); expect(loggerSpy.getAllMessages('error')).toHaveLength(1); expect(loggerSpy.getLastMessage('error')).toMatch( - /Multiple FSH entities created with name ExampleProfile/s + /Cannot export StructureDefinition ExampleProfile: a Profile with this name already exists/s ); }); @@ -1046,7 +1046,7 @@ describe('StructureDefinitionExporter R4', () => { expect(loggerSpy.getAllMessages('error')).toHaveLength(1); expect(loggerSpy.getLastMessage('error')).toMatch( - /Multiple FSH entities created with name SameExampleName/s + /Cannot export Instance SameExampleName: a Profile with this name already exists/s ); }); diff --git a/test/export/ValueSetExporter.test.ts b/test/export/ValueSetExporter.test.ts index 153fd9af..cf403fc1 100644 --- a/test/export/ValueSetExporter.test.ts +++ b/test/export/ValueSetExporter.test.ts @@ -341,7 +341,7 @@ describe('ValueSetExporter', () => { exporter.exportValueSet(dinner); expect(loggerSpy.getAllMessages('error')).toHaveLength(1); expect(loggerSpy.getLastMessage('error')).toMatch( - /Multiple FSH entities created with name BreakfastVS/s + /Cannot export ValueSet BreakfastVS: a ValueSet with this name already exists/s ); }); @@ -358,7 +358,7 @@ describe('ValueSetExporter', () => { expect(loggerSpy.getAllMessages('error')).toHaveLength(1); expect(loggerSpy.getLastMessage('error')).toMatch( - /Multiple FSH entities created with name BreakfastVS/s + /Cannot export Instance BreakfastVS: a ValueSet with this name already exists/s ); });