Skip to content

Commit

Permalink
Don't log an error if parent doesn't have mappings (#1455)
Browse files Browse the repository at this point in the history
  • Loading branch information
jafeltra authored Apr 17, 2024
1 parent 1ed4c56 commit 99bf99b
Show file tree
Hide file tree
Showing 3 changed files with 2,026 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/export/MappingExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class MappingExporter {
Type.Extension,
Type.Logical
);
const matchingParentMapping = parent?.mapping.find(
const matchingParentMapping = parent?.mapping?.find(
(m: StructureDefinitionMapping) => m.identity === fshDefinition.id
);
if (matchingParentMapping != null) {
Expand Down
42 changes: 40 additions & 2 deletions test/export/MappingExporter.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
import path from 'path';
import { cloneDeep } from 'lodash';
import { loadFromPath } from 'fhir-package-loader';
import { MappingExporter, Package } from '../../src/export';
import { MappingExporter, StructureDefinitionExporter, Package } from '../../src/export';
import { FSHDocument, FSHTank } from '../../src/import';
import { TestFisher } from '../testhelpers';
import { loggerSpy } from '../testhelpers';
import { FHIRDefinitions } from '../../src/fhirdefs';
import { StructureDefinition } from '../../src/fhirtypes';
import { Mapping, RuleSet } from '../../src/fshtypes';
import { Mapping, Profile, RuleSet } from '../../src/fshtypes';
import { MappingRule, InsertRule, AssignmentRule } from '../../src/fshtypes/rules';
import { minimalConfig } from '../utils/minimalConfig';
import { readFileSync } from 'fs';

describe('MappingExporter', () => {
let defs: FHIRDefinitions;
let doc: FSHDocument;
let exporter: MappingExporter;
let sdExporter: StructureDefinitionExporter;
let observation: StructureDefinition;
let practitioner: StructureDefinition;
let noMappingsProfile: StructureDefinition;
let logical: StructureDefinition;
let resource: StructureDefinition;

beforeAll(() => {
defs = new FHIRDefinitions();
loadFromPath(path.join(__dirname, '..', 'testhelpers', 'testdefs'), 'r4-definitions', defs);
const extraProfile = JSON.parse(
readFileSync(
path.join(
__dirname,
'..',
'testhelpers',
'testdefs',
'StructureDefinition-NoMappingsProfile.json'
),
'utf-8'
).trim()
);
defs.add(extraProfile);
});

beforeEach(() => {
Expand All @@ -31,13 +47,17 @@ describe('MappingExporter', () => {
const input = new FSHTank([doc], minimalConfig);
const pkg = new Package(input.config);
const fisher = new TestFisher(input, defs, pkg);
sdExporter = new StructureDefinitionExporter(input, pkg, fisher);
exporter = new MappingExporter(input, pkg, fisher);
observation = fisher.fishForStructureDefinition('Observation');
observation.id = 'MyObservation';
pkg.profiles.push(observation);
practitioner = fisher.fishForStructureDefinition('Practitioner');
practitioner.id = 'MyPractitioner';
pkg.profiles.push(practitioner);
noMappingsProfile = fisher.fishForStructureDefinition('NoMappingsProfile');
noMappingsProfile.id = 'NoMappingsProfile';
pkg.profiles.push(noMappingsProfile);
logical = fisher.fishForStructureDefinition('eLTSSServiceModel');
logical.id = 'MyLogical';
pkg.logicals.push(logical);
Expand Down Expand Up @@ -97,6 +117,24 @@ describe('MappingExporter', () => {
expect(exported.identity).toBe('MyMapping');
});

it('should export a mapping whose source is based on a structure definition without any existing mappings', () => {
/**
* Mapping: MyMapping
* Source: ChildProfile
*/
const childProfile = new Profile('ChildProfile');
childProfile.parent = 'NoMappingsProfile';
doc.profiles.set(childProfile.name, childProfile);
const mapping = new Mapping('MyMapping');
mapping.source = 'ChildProfile'; // Note - ChildProfile is based off ProfileWithNoMappings
doc.mappings.set(mapping.name, mapping);
const childSd = sdExporter.export().profiles[3];
exporter.export();
expect(childSd.mapping).toHaveLength(1);
expect(childSd.mapping[0].identity).toBe('MyMapping');
expect(loggerSpy.getAllMessages('error')).toHaveLength(0);
});

it('should export a mapping with optional metadata', () => {
/**
* Mapping: MyMapping
Expand Down
Loading

0 comments on commit 99bf99b

Please sign in to comment.