Skip to content

Commit c1c94ae

Browse files
committed
Convert test_loaders_pydantic.py to pytest format
- Extract loader_test helper function from base class - Convert 4 test methods to standalone functions - Test both YAML and JSON loaders with different models
1 parent 4a1dbc1 commit c1c94ae

File tree

1 file changed

+50
-22
lines changed

1 file changed

+50
-22
lines changed
Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,57 @@
1-
import unittest
1+
import os
2+
from typing import Union
23

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
310
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
413
from tests.test_loaders_dumpers.environment import env
5-
from tests.test_loaders_dumpers.loaderdumpertestcase import LoaderDumperTestCase
614
from tests.test_loaders_dumpers.models.books_normalized_pydantic import BookSeries
715
from tests.test_loaders_dumpers.models.kitchen_sink_pydantic import Dataset
816

917

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

Comments
 (0)