|
1 | 1 | import os
|
2 |
| -import unittest |
3 | 2 | from pathlib import Path
|
4 | 3 | from typing import Optional, TextIO, Union
|
5 | 4 |
|
| 5 | +import pytest |
6 | 6 | from hbreader import FileInfo
|
7 | 7 |
|
8 | 8 | from linkml_runtime.loaders import RDFLoader, json_loader, rdf_loader, yaml_loader
|
9 | 9 | from linkml_runtime.utils.yamlutils import YAMLRoot
|
10 | 10 | from tests.test_loaders_dumpers import LD_11_DIR, LD_11_SSL_SVR, LD_11_SVR
|
11 | 11 | from tests.test_loaders_dumpers.environment import env
|
12 |
| -from tests.test_loaders_dumpers.loaderdumpertestcase import LoaderDumperTestCase |
13 | 12 | from tests.test_loaders_dumpers.models.termci_schema import Package
|
14 |
| - |
15 |
| - |
16 |
| -class LoadersUnitTest(LoaderDumperTestCase): |
17 |
| - env = env |
18 |
| - |
19 |
| - @classmethod |
20 |
| - def setUpClass(cls) -> None: |
21 |
| - cls.context_server = cls.check_context_servers([LD_11_SVR, LD_11_SSL_SVR]) |
22 |
| - if not cls.context_server: |
23 |
| - cls.context_server = LD_11_DIR |
24 |
| - |
25 |
| - def test_yaml_loader(self): |
26 |
| - """Load obo_sample.yaml, emit obo_sample_yaml.yaml and compare to obo_sample_output.yaml""" |
27 |
| - self.loader_test("obo_sample.yaml", Package, yaml_loader) |
28 |
| - |
29 |
| - def test_json_loader_path(self): |
30 |
| - """Load obo_sample.json, emit obo_sample_json.yaml and check the results""" |
31 |
| - REPO_ROOT = Path(__file__).parent.parent.parent |
32 |
| - path = REPO_ROOT / "tests" / "test_loaders_dumpers" / "input" / "obo_sample.json" |
33 |
| - data = json_loader.load(Path(path), Package, base_dir=self.env.indir) |
34 |
| - assert isinstance(data, Package) |
35 |
| - assert "system" in data |
36 |
| - |
37 |
| - def test_json_loader(self): |
38 |
| - """Load obo_sample.json, emit obo_sample_json.yaml and check the results""" |
39 |
| - self.loader_test("obo_sample.json", Package, json_loader) |
40 |
| - |
41 |
| - def test_json_load_to_dict(self): |
42 |
| - data = json_loader.load_as_dict("obo_sample.json", base_dir=self.env.indir) |
43 |
| - assert isinstance(data, dict) |
44 |
| - assert "system" in data |
45 |
| - |
46 |
| - def test_yaml_load_to_dict(self): |
47 |
| - data = yaml_loader.load_as_dict("obo_sample.yaml", base_dir=self.env.indir) |
48 |
| - assert isinstance(data, dict) |
49 |
| - assert "system" in data |
50 |
| - |
51 |
| - @unittest.skipIf(True, "This test will not work until https://github.com/digitalbazaar/pyld/issues/149 is fixed") |
52 |
| - def test_rdf_loader(self): |
53 |
| - """Load obo_sample.ttl, emit obo_sample_ttl.yaml and check the results |
54 |
| - Load obo_sample.jsonld, emit obo_sample_jsonld.yaml and check the results |
55 |
| - """ |
56 |
| - if self.context_server == LD_11_DIR: |
57 |
| - raise unittest.SkipTest("*****> Loading skipped until JSON-LD processor can handle non-http files") |
58 |
| - |
59 |
| - contexts = os.path.join(self.context_server, "termci_schema_inlined.context.jsonld") |
60 |
| - fmt = "turtle" |
61 |
| - |
62 |
| - class RDFLoaderWrapper(RDFLoader): |
63 |
| - def load( |
64 |
| - self, |
65 |
| - source: Union[str, dict, TextIO], |
66 |
| - target_class: type[YAMLRoot], |
67 |
| - *, |
68 |
| - base_dir: Optional[str] = None, |
69 |
| - metadata: Optional[FileInfo] = None, |
70 |
| - **_, |
71 |
| - ) -> YAMLRoot: |
72 |
| - return rdf_loader.load( |
73 |
| - source, |
74 |
| - target_class, |
75 |
| - base_dir=LoadersUnitTest.env.indir, |
76 |
| - fmt=fmt, |
77 |
| - metadata=metadata, |
78 |
| - contexts=contexts, |
79 |
| - ) |
80 |
| - |
81 |
| - def loads( |
82 |
| - self, source: str, target_class: type[YAMLRoot], *, metadata: Optional[FileInfo] = None, **_ |
83 |
| - ) -> YAMLRoot: |
84 |
| - return rdf_loader.loads(source, target_class, contexts=contexts, fmt=fmt, metadata=metadata) |
85 |
| - |
86 |
| - self.loader_test("obo_sample.ttl", Package, RDFLoaderWrapper()) |
87 |
| - fmt = "json-ld" |
88 |
| - self.loader_test("obo_sample.jsonld", Package, RDFLoaderWrapper()) |
89 |
| - |
90 |
| - |
91 |
| -if __name__ == "__main__": |
92 |
| - unittest.main() |
| 13 | +from tests.test_loaders_dumpers.test_loaders_pydantic import loader_test |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope="module") |
| 17 | +def context_server(): |
| 18 | + """Set up context server for testing.""" |
| 19 | + # Check context servers - this mimics the original setUpClass logic |
| 20 | + from tests.test_loaders_dumpers.loaderdumpertestcase import LoaderDumperTestCase |
| 21 | + |
| 22 | + context_server = LoaderDumperTestCase.check_context_servers([LD_11_SVR, LD_11_SSL_SVR]) |
| 23 | + if not context_server: |
| 24 | + context_server = LD_11_DIR |
| 25 | + return context_server |
| 26 | + |
| 27 | + |
| 28 | +def test_yaml_loader(): |
| 29 | + """Load obo_sample.yaml, emit obo_sample_yaml.yaml and compare to obo_sample_output.yaml""" |
| 30 | + loader_test("obo_sample.yaml", Package, yaml_loader) |
| 31 | + |
| 32 | + |
| 33 | +def test_json_loader_path(): |
| 34 | + """Load obo_sample.json using Path object and check the results""" |
| 35 | + REPO_ROOT = Path(__file__).parent.parent.parent |
| 36 | + path = REPO_ROOT / "tests" / "test_loaders_dumpers" / "input" / "obo_sample.json" |
| 37 | + data = json_loader.load(Path(path), Package, base_dir=env.indir) |
| 38 | + assert isinstance(data, Package) |
| 39 | + assert "system" in data |
| 40 | + |
| 41 | + |
| 42 | +def test_json_loader(): |
| 43 | + """Load obo_sample.json, emit obo_sample_json.yaml and check the results""" |
| 44 | + loader_test("obo_sample.json", Package, json_loader) |
| 45 | + |
| 46 | + |
| 47 | +def test_json_load_to_dict(): |
| 48 | + """Test loading JSON file as dictionary""" |
| 49 | + data = json_loader.load_as_dict("obo_sample.json", base_dir=env.indir) |
| 50 | + assert isinstance(data, dict) |
| 51 | + assert "system" in data |
| 52 | + |
| 53 | + |
| 54 | +def test_yaml_load_to_dict(): |
| 55 | + """Test loading YAML file as dictionary""" |
| 56 | + data = yaml_loader.load_as_dict("obo_sample.yaml", base_dir=env.indir) |
| 57 | + assert isinstance(data, dict) |
| 58 | + assert "system" in data |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.skip(reason="This test will not work until https://github.com/digitalbazaar/pyld/issues/149 is fixed") |
| 62 | +def test_rdf_loader(context_server): |
| 63 | + """Load obo_sample.ttl and obo_sample.jsonld, emit yaml and check the results""" |
| 64 | + if context_server == LD_11_DIR: |
| 65 | + pytest.skip("*****> Loading skipped until JSON-LD processor can handle non-http files") |
| 66 | + |
| 67 | + contexts = os.path.join(context_server, "termci_schema_inlined.context.jsonld") |
| 68 | + fmt = "turtle" |
| 69 | + |
| 70 | + class RDFLoaderWrapper(RDFLoader): |
| 71 | + def load( |
| 72 | + self, |
| 73 | + source: Union[str, dict, TextIO], |
| 74 | + target_class: type[YAMLRoot], |
| 75 | + *, |
| 76 | + base_dir: Optional[str] = None, |
| 77 | + metadata: Optional[FileInfo] = None, |
| 78 | + **_, |
| 79 | + ) -> YAMLRoot: |
| 80 | + return rdf_loader.load( |
| 81 | + source, |
| 82 | + target_class, |
| 83 | + base_dir=env.indir, |
| 84 | + fmt=fmt, |
| 85 | + metadata=metadata, |
| 86 | + contexts=contexts, |
| 87 | + ) |
| 88 | + |
| 89 | + def loads( |
| 90 | + self, source: str, target_class: type[YAMLRoot], *, metadata: Optional[FileInfo] = None, **_ |
| 91 | + ) -> YAMLRoot: |
| 92 | + return rdf_loader.loads(source, target_class, contexts=contexts, fmt=fmt, metadata=metadata) |
| 93 | + |
| 94 | + loader_test("obo_sample.ttl", Package, RDFLoaderWrapper()) |
| 95 | + fmt = "json-ld" |
| 96 | + loader_test("obo_sample.jsonld", Package, RDFLoaderWrapper()) |
0 commit comments