From 6e1b54fecd4af9417bc64165a3d21247c2f0b502 Mon Sep 17 00:00:00 2001 From: Joe Flack Date: Sun, 5 May 2024 00:27:00 -0400 Subject: [PATCH] FHIR Feature - Add: _test_to_fhir_json() - Update: test_write_sssom_fhir(): More assertions --- src/sssom/writers.py | 2 ++ tests/test_conversion.py | 10 ++++++++++ tests/test_writers.py | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/sssom/writers.py b/src/sssom/writers.py index 6486b49d..0eb93430 100644 --- a/src/sssom/writers.py +++ b/src/sssom/writers.py @@ -318,8 +318,10 @@ def to_fhir_json(msdf: MappingSetDataFrame) -> Dict: json_obj = { "resourceType": "ConceptMap", "url": mapping_set_id, + # Assumes mapping_set_id is a URI w/ artefact name at end. System becomes URI stem, value becomes artefact name "identifier": [ { + "system": "/".join(mapping_set_id.split("/")[:-1]) + "/", "value": mapping_set_id, } diff --git a/tests/test_conversion.py b/tests/test_conversion.py index faeae644..c4feccd1 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -15,6 +15,7 @@ from sssom.sssom_document import MappingSetDocument from sssom.util import MappingSetDataFrame, to_mapping_set_dataframe from sssom.writers import ( + to_fhir_json, to_json, to_ontoportal_json, to_owl_graph, @@ -62,6 +63,8 @@ def test_conversion(self): self._test_to_json(mdoc, test) logging.info("Testing ontoportal JSON export") self._test_to_ontoportal_json(mdoc, test) + logging.info("Testing fhir_json JSON export") + self._test_to_fhir_json(mdoc, test) def _test_to_owl_graph(self, mdoc, test): msdf = to_mapping_set_dataframe(mdoc) @@ -85,6 +88,13 @@ def _test_to_json(self, mdoc, test: SSSOMTestCase): with open(test.get_out_file("json"), "w") as file: write_json(msdf, file, serialisation="json") + def _test_to_fhir_json(self, mdoc, test: SSSOMTestCase): + msdf = to_mapping_set_dataframe(mdoc) + d = to_fhir_json(msdf) + self.assertEqual( + len(d["group"][0]["element"]), test.ct_data_frame_rows, "wrong number of mappings." + ) + def _test_to_ontoportal_json(self, mdoc, test: SSSOMTestCase): msdf = to_mapping_set_dataframe(mdoc) jsonob = to_ontoportal_json(msdf) diff --git a/tests/test_writers.py b/tests/test_writers.py index 4d94110d..86f2465b 100644 --- a/tests/test_writers.py +++ b/tests/test_writers.py @@ -3,6 +3,7 @@ import json import os import unittest +from typing import Any, Dict import pandas as pd from curies import Converter @@ -128,18 +129,30 @@ def test_update_sssom_context_with_prefixmap(self): def test_write_sssom_fhir(self): """Test writing as FHIR ConceptMap JSON.""" + # Vars path = os.path.join(test_out_dir, "test_write_sssom_fhir.json") + msdf: MappingSetDataFrame = self.msdf + metadata: Dict[str, Any] = msdf.metadata + mapping_set_id: str = metadata["mapping_set_id"] + # Write with open(path, "w") as file: write_json(self.msdf, file, "fhir_json") + # Read # todo: @Joe: after implementing reader/importer, change this to `msdf = parse_sssom_fhir_json()` with open(path, "r") as file: d = json.load(file) - # todo: @Joe: What else is worth checking? + # Test self.assertEqual( len(d["group"][0]["element"]), self.mapping_count, f"{path} has the wrong number of mappings.", ) + self.assertEqual(d["resourceType"], "ConceptMap") + self.assertEqual(d["url"], mapping_set_id) + self.assertIn(d["identifier"][0]["system"], mapping_set_id) + self.assertEqual(d["identifier"][0]["value"], mapping_set_id) + self.assertEqual(d["identifier"][0]["value"], d["url"]) + print() # TODO: temp def test_write_sssom_owl(self):