|
1 |
| -import unittest |
| 1 | +import os |
| 2 | +from typing import Union |
2 | 3 |
|
| 4 | +import pytest |
| 5 | +from hbreader import FileInfo |
| 6 | +from pydantic import BaseModel |
| 7 | + |
| 8 | +import tests.environment as test_base |
| 9 | +from linkml_runtime.dumpers import yaml_dumper |
3 | 10 | from linkml_runtime.loaders import json_loader, yaml_loader
|
| 11 | +from linkml_runtime.loaders.loader_root import Loader |
| 12 | +from linkml_runtime.utils.yamlutils import YAMLRoot |
4 | 13 | from tests.test_loaders_dumpers.environment import env
|
5 |
| -from tests.test_loaders_dumpers.loaderdumpertestcase import LoaderDumperTestCase |
6 | 14 | from tests.test_loaders_dumpers.models.books_normalized_pydantic import BookSeries
|
7 | 15 | from tests.test_loaders_dumpers.models.kitchen_sink_pydantic import Dataset
|
8 | 16 |
|
9 | 17 |
|
10 |
| -class PydanticLoadersUnitTest(LoaderDumperTestCase): |
11 |
| - env = env |
12 |
| - |
13 |
| - def test_yaml_loader_single(self): |
14 |
| - """Load obo_sample.yaml, emit obo_sample_yaml.yaml and compare to obo_sample_output.yaml""" |
15 |
| - self.loader_test("book_series_lotr.yaml", BookSeries, yaml_loader) |
16 |
| - |
17 |
| - def test_json_loader(self): |
18 |
| - """Load obo_sample.json, emit obo_sample_json.yaml and check the results""" |
19 |
| - self.loader_test("book_series_lotr.json", BookSeries, json_loader) |
20 |
| - |
21 |
| - def test_yaml_loader_kitchen_sink(self): |
22 |
| - self.loader_test("kitchen_sink_normalized_inst_01.yaml", Dataset, yaml_loader) |
23 |
| - |
24 |
| - def test_json_loader_kitchen_sink(self): |
25 |
| - self.loader_test("kitchen_sink_normalized_inst_01.json", Dataset, json_loader) |
26 |
| - |
27 |
| - |
28 |
| -if __name__ == "__main__": |
29 |
| - unittest.main() |
| 18 | +def loader_test(filename: str, model: Union[type[YAMLRoot], type[BaseModel]], loader: Loader) -> None: |
| 19 | + """ |
| 20 | + Test the various permutations of the supplied loader using the input file 'filename' -- both load and loads |
| 21 | +
|
| 22 | + :param filename: un-pathed file name to load |
| 23 | + :param model: model to load the file name into |
| 24 | + :param loader: package that contains 'load' and 'loads' operations |
| 25 | + """ |
| 26 | + metadata = FileInfo() |
| 27 | + name, typ = filename.rsplit(".", 1) |
| 28 | + expected_yaml = env.expected_path("load", name + "_" + typ + ".yaml") |
| 29 | + if issubclass(model, YAMLRoot): |
| 30 | + python_obj: YAMLRoot = loader.load(filename, model, metadata=metadata, base_dir=env.indir) |
| 31 | + elif issubclass(model, BaseModel): |
| 32 | + python_obj: BaseModel = loader.load(filename, model, metadata=metadata, base_dir=env.indir) |
| 33 | + else: |
| 34 | + raise TypeError(f"Unknown target class: {model}") |
| 35 | + env.eval_single_file(expected_yaml, yaml_dumper.dumps(python_obj)) |
| 36 | + |
| 37 | + # Make sure metadata gets filled out properly |
| 38 | + rel_path = os.path.abspath(os.path.join(test_base.env.cwd, "..")) |
| 39 | + assert os.path.normpath("tests/test_loaders_dumpers/input") == os.path.normpath( |
| 40 | + os.path.relpath(metadata.base_path, rel_path) |
| 41 | + ) |
| 42 | + assert os.path.normpath(f"tests/test_loaders_dumpers/input/{filename}") == os.path.normpath( |
| 43 | + os.path.relpath(metadata.source_file, rel_path) |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + "filename,model,loader", |
| 49 | + [ |
| 50 | + ("book_series_lotr.yaml", BookSeries, yaml_loader), |
| 51 | + ("book_series_lotr.json", BookSeries, json_loader), |
| 52 | + ("kitchen_sink_normalized_inst_01.yaml", Dataset, yaml_loader), |
| 53 | + ("kitchen_sink_normalized_inst_01.json", Dataset, json_loader), |
| 54 | + ], |
| 55 | +) |
| 56 | +def test_loader(filename, model, loader): |
| 57 | + loader_test(filename, model, loader) |
0 commit comments