Skip to content

Commit

Permalink
FHIR Feature
Browse files Browse the repository at this point in the history
- Add: _test_to_fhir_json()
- Update: test_write_sssom_fhir(): More assertions
  • Loading branch information
joeflack4 committed May 5, 2024
1 parent 157ae22 commit 6e1b54f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/sssom/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
10 changes: 10 additions & 0 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion tests/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os
import unittest
from typing import Any, Dict

import pandas as pd
from curies import Converter
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 6e1b54f

Please sign in to comment.