def json_to_ttl(self: FHIRInstanceTestCase, dirpath: str, fname: str) -> bool: json_file = os.path.join(dirpath, fname) turtle_fname = fname.rsplit('.', 1)[0] + '.ttl' source = FHIRResource(self.fhir_ontology, json_file, FHIRInstanceTestCase.base_dir) test_ttl_fname = os.path.join(dirpath, turtle_fname) if os.path.exists(test_ttl_fname): target = Graph() target.load(test_ttl_fname, format="turtle") diffs_nc = rdf_compare(target, source.graph, ignore_owl_version=True, ignore_type_arcs=True) diffs = rdf_compare(target, source.graph, ignore_owl_version=True, ignore_type_arcs=True, compare_filter=fhir_decimal_issue_filter) if diffs_nc and not diffs: print("---> {} has decimal precision issues".format(fname)) if diffs: with open(os.path.join(self.output_directory, turtle_fname), 'w') as f: f.write(diffs) f.write('\n\n' + '=' * 40 + '\n') f.write(str(source)) return False return True else: print("===> {} does not exist!".format(test_ttl_fname)) return True
def test_patient_example(self): from fhirtordf.fhirtordf import main test_directory = os.path.join( os.path.split(os.path.abspath(__file__))[0], '..', 'data') infname = os.path.join(test_directory, "patient-example.json") testfname = os.path.join(test_directory, "patient-example.ttl") if save_output: outfname = testfname else: outfname = os.path.join(test_directory, "patient-example-out.ttl") args = "-i {} -o {} -s".format(infname, outfname) self.assertTrue(main(args.split())) self.assertFalse(save_output, "Test file: {} generated".format(outfname)) out_graph = Graph() out_graph.load(outfname, format="turtle") test_graph = Graph() test_graph.load(testfname, format="turtle") comp_result = rdf_compare(test_graph, out_graph, ignore_owl_version=True, ignore_type_arcs=True) if len(comp_result): print(comp_result) self.assertTrue(len(comp_result) == 0)
def test_fhir_files(self): from fhirtordf.fhirtordf import fhirtordf fnames = [ 'activitydefinition-example', 'activitydefinition-medicationorder-example', 'capabilitystatement-base', 'careplan-example-f001-heart', 'claim-example', 'patient-example' ] for fname in fnames: in_url = "{}{}.json".format(test_fhir_server, fname) test_url = "{}{}.ttl".format(test_fhir_server, fname) # fmv_url = "{}/fhir.ttl".format(target_fhir_build) fmv_url = os.path.join( os.path.split(os.path.abspath(__file__))[0], '..', 'data', 'fhir_metadata_vocabulary', 'fhir.ttl') test_directory = os.path.join( os.path.split(os.path.abspath(__file__))[0], 'data') outfname = os.path.join(test_directory, "{}.ttl".format(fname)) args = "-i {} -o {} -mv {} -s".format(in_url, outfname, fmv_url) self.assertTrue(fhirtordf(args.split())) out_graph = Graph() out_graph.load(outfname, format="turtle") test_graph = Graph() test_graph.load(test_url, format="turtle") comp_result = rdf_compare(test_graph, out_graph, ignore_owl_version=True, ignore_type_arcs=True) if len(comp_result): print(comp_result) self.assertTrue(len(comp_result) == 0)
def test_reference(self): test_json = loads(data) from fhirtordf.loaders.fhirresourceloader import FHIRResource test_rdf = FHIRResource(FHIRGraph(), None, "http://hl7.org/fhir", test_json) g = test_rdf.graph expected_graph = Graph() diffs = rdf_compare(expected_graph, test_rdf.graph, ignore_owl_version=True, ignore_type_arcs=True) self.assertEqual("", diffs)
def do_test(self, fname): from fhirtordf.loaders.fhirresourceloader import FHIRResource json_file = fname + ".json" turtle_file = fname + ".ttl" source = FHIRResource(self.fhir_ontology, os.path.join(self.base_dir, json_file), "http://hl7.org/fhir/") turtle_fname = os.path.join(self.base_dir, turtle_file) target = PrettyGraph() target.load(turtle_fname, format="turtle") self.maxDiff = None self.assertEqual( '', rdf_compare(source.graph, target, ignore_owl_version=False))
def bester_tester(self, args: str, test_fname: str): from fhirtordf.fhirtordf import fhirtordf output = self._push_stdout() fhirtordf(args.split(), default_exit=False) g2 = Graph() g2.parse(data=output.getvalue(), format="turtle") self._pop_stdout() if save_sample_output: with open(test_fname, 'w') as f: f.write(output.getvalue()) g1 = Graph() g1.parse(test_fname, format="turtle") comp_result = rdf_compare(g1, g2) if len(comp_result): print(comp_result) self.assertTrue(len(comp_result) == 0) self.assertFalse(save_sample_output, "save_sample_output is True -- test not applied")
def test_data_entry(self): save_output = False from fhirtordf.loaders.fhirresourceloader import FHIRResource with open( os.path.join(self.base_dir, 'synthea_data', 'Adams301_Keyshawn30_74.json')) as f: collection = load(f) source = FHIRResource(self.fhir_ontology, None, "http://standardhealthrecord.org/fhir/", data=collection.entry[0].resource) turtle_fname = os.path.join(self.base_dir, 'synthea_data', 'Adams301_Keyshawn30_74_entry0.ttl') if save_output: with open(turtle_fname, 'w') as output: output.write(str(source)) target = PrettyGraph() target.load(turtle_fname, format="turtle") # Note: This will fail if we use the pure turtle serializer (vs our changes in this package) self.maxDiff = None self.assertEqual( '', rdf_compare(source.graph, target, ignore_owl_version=True)) self.assertFalse(save_output, "Update output file always fails")