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