|
1 | 1 | import os |
2 | | -import unittest |
3 | 2 | from typing import cast |
4 | 3 |
|
| 4 | +import pytest |
5 | 5 | from rdflib import SKOS, Literal, Namespace |
6 | 6 |
|
7 | 7 | from linkml_runtime.dumpers import json_dumper, rdf_dumper, yaml_dumper |
|
16 | 16 | LD_11_SSL_SVR, |
17 | 17 | LD_11_SVR, |
18 | 18 | ) |
19 | | -from tests.test_loaders_dumpers.loaderdumpertestcase import LoaderDumperTestCase |
| 19 | +from tests.test_loaders_dumpers.environment import env |
20 | 20 | from tests.test_loaders_dumpers.models.termci_schema import ConceptReference, ConceptSystem, Package |
21 | 21 |
|
22 | 22 | OBO = Namespace("http://purl.obolibrary.org/obo/") |
23 | 23 | NCIT = Namespace("http://purl.obolibrary.org/obo/NCI_") |
24 | 24 |
|
25 | 25 |
|
26 | | -class DumpersTestCase(LoaderDumperTestCase): |
27 | | - pass |
28 | | - |
29 | | - @classmethod |
30 | | - def setUpClass(cls) -> None: |
31 | | - """Generate a small sample TermCI instance for testing purposes""" |
32 | | - LoaderDumperTestCase.setUpClass() |
33 | | - e1 = ConceptReference( |
34 | | - OBO.NCI_C147796, |
35 | | - code="C147796", |
36 | | - defined_in=OBO, |
37 | | - designation="TSCYC - Being Frightened of Men", |
38 | | - definition="Trauma Symptom Checklist for Young Children (TSCYC) Please indicate how often" |
39 | | - " the child has done, felt, or experienced each of the following things in " |
40 | | - "the last month: Being frightened of men.", |
41 | | - narrower_than=OBO.NCI_C147557, |
42 | | - reference=OBO.NCI_C147796, |
43 | | - ) |
44 | | - e2 = ConceptReference( |
45 | | - OBO.NCI_C147557, |
46 | | - code="C147557", |
47 | | - defined_in=OBO, |
48 | | - designation="TSCYC Questionnaire Question", |
49 | | - definition="A question associated with the TSCYC questionnaire.", |
50 | | - narrower_than=OBO.NCI_C91102, |
51 | | - ) |
52 | | - c1 = ConceptSystem(OBO, "OBO", contents=[e1, e2]) |
53 | | - cls.test_package = Package([c1]) |
54 | | - |
55 | | - def test_yaml_dumper(self): |
56 | | - """Test the yaml emitter""" |
57 | | - # TODO: Once this is entered into the BiolinkML test package, compare this to input/obo_test.yaml |
58 | | - self.dump_test("obo_sample.yaml", lambda out_fname: yaml_dumper.dump(self.test_package, out_fname)) |
59 | | - self.dumps_test("obo_sample.yaml", lambda: yaml_dumper.dumps(self.test_package)) |
60 | | - |
61 | | - def test_json_dumper(self): |
62 | | - """Test the json emitter""" |
63 | | - # TODO: Same as test_yaml_dumper |
64 | | - self.dump_test("obo_sample.json", lambda out_fname: json_dumper.dump(self.test_package, out_fname)) |
65 | | - |
66 | | - obo_json_obj = cast(Package, as_json_object(self.test_package)) |
67 | | - self.assertEqual(OBO, obo_json_obj.system[0].namespace) |
68 | | - self.assertEqual("C147796", obo_json_obj.system[0].contents[0].code) |
69 | | - |
70 | | - self.dumps_test("obo_sample.json", lambda: json_dumper.dumps(self.test_package)) |
71 | | - self.dump_test( |
72 | | - "obo_sample_context.json", |
73 | | - lambda out_fname: json_dumper.dump( |
74 | | - self.test_package, out_fname, GITHUB_LD10_CONTEXT + "termci_schema.context.jsonld" |
75 | | - ), |
76 | | - ) |
77 | | - self.dumps_test( |
78 | | - "obo_sample_context.json", |
79 | | - lambda: json_dumper.dumps(self.test_package, GITHUB_LD11_CONTEXT + "termci_schema_inlined.context.jsonld"), |
80 | | - ) |
81 | | - |
82 | | - @unittest.skipIf(True, "This needs an enhanced (https://github.com/hsolbrig/pyld) version of pyld") |
83 | | - def test_rdf_dumper(self): |
84 | | - """Test the rdf dumper""" |
85 | | - contexts = os.path.join(LD_11_DIR, "termci_schema_inlined.context.jsonld") |
86 | | - self.dump_test( |
87 | | - "obo_sample.ttl", |
88 | | - lambda out_file: rdf_dumper.dump(self.test_package, out_file, contexts), |
89 | | - comparator=ClickTestCase.rdf_comparator, |
| 26 | +@pytest.fixture(scope="module") |
| 27 | +def test_package(): |
| 28 | + """Generate a small sample TermCI instance for testing purposes""" |
| 29 | + e1 = ConceptReference( |
| 30 | + OBO.NCI_C147796, |
| 31 | + code="C147796", |
| 32 | + defined_in=OBO, |
| 33 | + designation="TSCYC - Being Frightened of Men", |
| 34 | + definition="Trauma Symptom Checklist for Young Children (TSCYC) Please indicate how often" |
| 35 | + " the child has done, felt, or experienced each of the following things in " |
| 36 | + "the last month: Being frightened of men.", |
| 37 | + narrower_than=OBO.NCI_C147557, |
| 38 | + reference=OBO.NCI_C147796, |
| 39 | + ) |
| 40 | + e2 = ConceptReference( |
| 41 | + OBO.NCI_C147557, |
| 42 | + code="C147557", |
| 43 | + defined_in=OBO, |
| 44 | + designation="TSCYC Questionnaire Question", |
| 45 | + definition="A question associated with the TSCYC questionnaire.", |
| 46 | + narrower_than=OBO.NCI_C91102, |
| 47 | + ) |
| 48 | + c1 = ConceptSystem(OBO, "OBO", contents=[e1, e2]) |
| 49 | + return Package([c1]) |
| 50 | + |
| 51 | + |
| 52 | +def dump_test(filename: str, dumper, comparator=None) -> bool: |
| 53 | + """ |
| 54 | + Invoke the dumper passing it the output file name and then compare the result to an expected output |
| 55 | + """ |
| 56 | + actual_file = env.actual_path(filename) |
| 57 | + expected_file = env.expected_path("dump", filename) |
| 58 | + |
| 59 | + dumper(actual_file) |
| 60 | + |
| 61 | + with open(actual_file) as actual_f: |
| 62 | + actual = actual_f.read() |
| 63 | + return env.eval_single_file(expected_file, actual, comparator=comparator) |
| 64 | + |
| 65 | + |
| 66 | +def dumps_test(filename: str, dumper, comparator=None) -> bool: |
| 67 | + """ |
| 68 | + Invoke the string dumper and evaluate the results |
| 69 | + """ |
| 70 | + actual = dumper() |
| 71 | + expected_file = env.expected_path("dumps", filename) |
| 72 | + return env.eval_single_file(expected_file, actual, comparator=comparator) |
| 73 | + |
| 74 | + |
| 75 | +def test_yaml_dumper(test_package): |
| 76 | + """Test the yaml emitter""" |
| 77 | + # TODO: Once this is entered into the BiolinkML test package, compare this to input/obo_test.yaml |
| 78 | + dump_test("obo_sample.yaml", lambda out_fname: yaml_dumper.dump(test_package, out_fname)) |
| 79 | + dumps_test("obo_sample.yaml", lambda: yaml_dumper.dumps(test_package)) |
| 80 | + |
| 81 | + |
| 82 | +def test_json_dumper(test_package): |
| 83 | + """Test the json emitter""" |
| 84 | + # TODO: Same as test_yaml_dumper |
| 85 | + dump_test("obo_sample.json", lambda out_fname: json_dumper.dump(test_package, out_fname)) |
| 86 | + |
| 87 | + obo_json_obj = cast(Package, as_json_object(test_package)) |
| 88 | + assert obo_json_obj.system[0].namespace == OBO |
| 89 | + assert obo_json_obj.system[0].contents[0].code == "C147796" |
| 90 | + |
| 91 | + dumps_test("obo_sample.json", lambda: json_dumper.dumps(test_package)) |
| 92 | + dump_test( |
| 93 | + "obo_sample_context.json", |
| 94 | + lambda out_fname: json_dumper.dump( |
| 95 | + test_package, out_fname, GITHUB_LD10_CONTEXT + "termci_schema.context.jsonld" |
| 96 | + ), |
| 97 | + ) |
| 98 | + dumps_test( |
| 99 | + "obo_sample_context.json", |
| 100 | + lambda: json_dumper.dumps(test_package, GITHUB_LD11_CONTEXT + "termci_schema_inlined.context.jsonld"), |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.skip(reason="This needs an enhanced (https://github.com/hsolbrig/pyld) version of pyld") |
| 105 | +def test_rdf_dumper(test_package): |
| 106 | + """Test the rdf dumper""" |
| 107 | + contexts = os.path.join(LD_11_DIR, "termci_schema_inlined.context.jsonld") |
| 108 | + dump_test( |
| 109 | + "obo_sample.ttl", |
| 110 | + lambda out_file: rdf_dumper.dump(test_package, out_file, contexts), |
| 111 | + comparator=ClickTestCase.rdf_comparator, |
| 112 | + ) |
| 113 | + |
| 114 | + g = rdf_dumper.as_rdf_graph(test_package, contexts) |
| 115 | + assert OBO[""] in g.subjects() |
| 116 | + assert NCIT.C147796 in g.subjects() |
| 117 | + assert Literal("C147796") in g.objects(NCIT.C147796, SKOS.notation) |
| 118 | + |
| 119 | + dumps_test( |
| 120 | + "obo_sample.ttl", |
| 121 | + lambda: rdf_dumper.dumps(test_package, contexts), |
| 122 | + comparator=ClickTestCase.rdf_comparator, |
| 123 | + ) |
| 124 | + |
| 125 | + # Build a vanilla jsonld image for subsequent testing |
| 126 | + fname = "obo_sample.jsonld" |
| 127 | + dump_test( |
| 128 | + fname, |
| 129 | + lambda out_file: rdf_dumper.dump(test_package, out_file, contexts, fmt="json-ld"), |
| 130 | + comparator=lambda e, a: ClickTestCase.rdf_comparator(e, a, fmt="json-ld"), |
| 131 | + ) |
| 132 | + with open(env.expected_path("dump", fname)) as f: |
| 133 | + txt = f.read() |
| 134 | + with open(env.input_path("obo_sample.jsonld"), "w") as f: |
| 135 | + f.write(txt) |
| 136 | + |
| 137 | + |
| 138 | +@pytest.mark.skip(reason="Waiting until PyLD learns to handle relative context URI's") |
| 139 | +def test_nested_contexts(test_package): |
| 140 | + """Test JSON-LD with fully nested contexts""" |
| 141 | + from tests.test_loaders_dumpers.loaderdumpertestcase import LoaderDumperTestCase |
| 142 | + |
| 143 | + context_servers = [] |
| 144 | + for possible_server in [LD_11_SVR, LD_11_SSL_SVR]: |
| 145 | + svr = LoaderDumperTestCase.check_context_servers([possible_server]) |
| 146 | + if svr: |
| 147 | + context_servers.append(svr) |
| 148 | + |
| 149 | + if not context_servers: |
| 150 | + pytest.skip( |
| 151 | + f"*****> Nested contexts test skipped - no servers found on sockets {HTTP_TEST_PORT} or {HTTPS_TEST_PORT}" |
90 | 152 | ) |
91 | 153 |
|
92 | | - g = rdf_dumper.as_rdf_graph(self.test_package, contexts) |
93 | | - self.assertIn(OBO[""], g.subjects()) |
94 | | - self.assertIn(NCIT.C147796, g.subjects()) |
95 | | - self.assertIn(Literal("C147796"), g.objects(NCIT.C147796, SKOS.notation)) |
96 | | - |
97 | | - self.dumps_test( |
98 | | - "obo_sample.ttl", |
99 | | - lambda: rdf_dumper.dumps(self.test_package, contexts), |
100 | | - comparator=ClickTestCase.rdf_comparator, |
101 | | - ) |
102 | | - |
103 | | - # Build a vanilla jsonld image for subsequent testing |
104 | | - fname = "obo_sample.jsonld" |
105 | | - self.dump_test( |
106 | | - fname, |
107 | | - lambda out_file: rdf_dumper.dump(self.test_package, out_file, contexts, fmt="json-ld"), |
108 | | - comparator=lambda e, a: ClickTestCase.rdf_comparator(e, a, fmt="json-ld"), |
109 | | - ) |
110 | | - with open(self.env.expected_path("dump", fname)) as f: |
111 | | - txt = f.read() |
112 | | - with open(self.env.input_path("obo_sample.jsonld"), "w") as f: |
113 | | - f.write(txt) |
114 | | - |
115 | | - @unittest.skip("Waiting until PyLD learns to handle relative context URI's") |
116 | | - def test_nested_contexts(self): |
117 | | - """Test JSON-LD with fully nested contexts""" |
118 | | - |
119 | | - context_servers = [] |
120 | | - for possible_server in [LD_11_SVR, LD_11_SSL_SVR]: |
121 | | - svr = self.check_context_servers([possible_server]) |
122 | | - if svr: |
123 | | - context_servers.append(svr) |
124 | | - |
125 | | - if not context_servers: |
126 | | - raise unittest.SkipTest( |
127 | | - f"*****> Nested contexts test skipped - no servers found on sockets " |
128 | | - f"{HTTP_TEST_PORT} or {HTTPS_TEST_PORT}" |
129 | | - ) |
130 | | - |
131 | | - for context_base in context_servers: |
132 | | - nested_context = context_base + "Package.context.jsonld" |
133 | | - self.dump_test( |
134 | | - "obo_sample_nested.ttl", lambda out_file: rdf_dumper.dump(self.test_package, out_file, nested_context) |
135 | | - ) |
136 | | - self.dumps_test("obo_sample_nested.ttl", lambda: rdf_dumper.dumps(self.test_package, nested_context)) |
137 | | - |
138 | | - |
139 | | -if __name__ == "__main__": |
140 | | - unittest.main() |
| 154 | + for context_base in context_servers: |
| 155 | + nested_context = context_base + "Package.context.jsonld" |
| 156 | + dump_test("obo_sample_nested.ttl", lambda out_file: rdf_dumper.dump(test_package, out_file, nested_context)) |
| 157 | + dumps_test("obo_sample_nested.ttl", lambda: rdf_dumper.dumps(test_package, nested_context)) |
0 commit comments