Skip to content

Commit 66b3861

Browse files
Merge pull request #446 from vladistan/convert-test-loaders-pydantic-to-pytest
Convert test_loaders_pydantic.py to pytest format
2 parents 2948d08 + 00a0e8f commit 66b3861

File tree

6 files changed

+155
-23
lines changed

6 files changed

+155
-23
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ are correct and expected given the context of the PR.
7272
2. Install all the dependencies from your project, which are typically specified in a `uv.lock` file.
7373

7474
```
75-
uv sync --all-extras --dev
75+
uv sync --all-extras --dev
7676
```
7777

7878
3. Run any python scripts or CLI commands by prefixing with `uv run`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
id: S001
2+
name: Lord of the Rings
3+
genres:
4+
- fantasy
5+
creator:
6+
name: JRR Tolkien
7+
from_country: England
8+
books:
9+
- id: S001.1
10+
name: Fellowship of the Ring
11+
summary: Hobbits
12+
price: 5.99
13+
- id: S001.2
14+
name: The Two Towers
15+
summary: More hobbits
16+
price: 5.99
17+
- id: S001.3
18+
name: Return of the King
19+
summary: Yet more hobbits
20+
price: 6.99
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
id: S001
2+
name: Lord of the Rings
3+
genres:
4+
- fantasy
5+
creator:
6+
name: JRR Tolkien
7+
from_country: England
8+
books:
9+
- id: S001.1
10+
name: Fellowship of the Ring
11+
summary: Hobbits
12+
price: 5.99
13+
- id: S001.2
14+
name: The Two Towers
15+
summary: More hobbits
16+
price: 5.99
17+
- id: S001.3
18+
name: Return of the King
19+
summary: Yet more hobbits
20+
price: 6.99
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
persons:
2+
- id: P:001
3+
name: fred bloggs
4+
age_in_years: 33
5+
- id: P:002
6+
name: joe schmoe
7+
has_employment_history:
8+
- started_at_time: 2019-01-01
9+
is_current: true
10+
employed_at: ROR:1
11+
has_familial_relationships:
12+
- related_to: P:001
13+
type: SIBLING_OF
14+
has_medical_history:
15+
- started_at_time: 2019-01-01
16+
in_location: GEO:1234
17+
diagnosis:
18+
id: CODE:D0001
19+
name: headache
20+
in_code_system: DiseaseCodes
21+
procedure:
22+
id: CODE:P0001
23+
name: trepanation
24+
in_code_system: ProcedureCodes
25+
addresses:
26+
- street: 1 foo street
27+
city: foo city
28+
companies:
29+
- id: ROR:1
30+
name: foo
31+
activities:
32+
- id: A:1
33+
started_at_time: 2019-01-01
34+
was_associated_with: Agent:987
35+
code_systems:
36+
DiseaseCodes:
37+
id: DiseaseCodes
38+
name: disease codes
39+
ProcedureCodes:
40+
id: ProcedureCodes
41+
name: disease codes
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
persons:
2+
- id: P:001
3+
name: fred bloggs
4+
age_in_years: 33
5+
- id: P:002
6+
name: joe schmoe
7+
has_employment_history:
8+
- started_at_time: 2019-01-01
9+
is_current: true
10+
employed_at: ROR:1
11+
has_familial_relationships:
12+
- related_to: P:001
13+
type: SIBLING_OF
14+
has_medical_history:
15+
- started_at_time: 2019-01-01
16+
in_location: GEO:1234
17+
diagnosis:
18+
id: CODE:D0001
19+
name: headache
20+
in_code_system: DiseaseCodes
21+
procedure:
22+
id: CODE:P0001
23+
name: trepanation
24+
in_code_system: ProcedureCodes
25+
addresses:
26+
- street: 1 foo street
27+
city: foo city
28+
companies:
29+
- id: ROR:1
30+
name: foo
31+
activities:
32+
- id: A:1
33+
started_at_time: 2019-01-01
34+
was_associated_with: Agent:987
35+
code_systems:
36+
DiseaseCodes:
37+
id: DiseaseCodes
38+
name: disease codes
39+
ProcedureCodes:
40+
id: ProcedureCodes
41+
name: disease codes
Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,39 @@
1-
import unittest
1+
from pathlib import Path
22

3+
import pytest
4+
from hbreader import FileInfo
5+
from pydantic import BaseModel
6+
7+
from linkml_runtime.dumpers import yaml_dumper
38
from linkml_runtime.loaders import json_loader, yaml_loader
49
from tests.test_loaders_dumpers.environment import env
5-
from tests.test_loaders_dumpers.loaderdumpertestcase import LoaderDumperTestCase
610
from tests.test_loaders_dumpers.models.books_normalized_pydantic import BookSeries
711
from tests.test_loaders_dumpers.models.kitchen_sink_pydantic import Dataset
812

913

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()
14+
@pytest.mark.parametrize(
15+
"filename,model,loader",
16+
[
17+
("book_series_lotr.yaml", BookSeries, yaml_loader),
18+
("book_series_lotr.json", BookSeries, json_loader),
19+
("kitchen_sink_normalized_inst_01.yaml", Dataset, yaml_loader),
20+
("kitchen_sink_normalized_inst_01.json", Dataset, json_loader),
21+
],
22+
)
23+
def test_loader_basemodel(filename, model, loader):
24+
name = Path(filename).stem
25+
type = Path(filename).suffix.lstrip(".")
26+
expected_yaml_file = env.input_path(f"{name}_{type}.yaml")
27+
28+
metadata = FileInfo()
29+
30+
python_obj: BaseModel = loader.load(filename, model, metadata=metadata, base_dir=env.indir)
31+
32+
# Load expected output
33+
with open(expected_yaml_file) as expf:
34+
expected = expf.read()
35+
36+
got = yaml_dumper.dumps(python_obj)
37+
expected_trimmed = expected.replace("\r\n", "\n").strip()
38+
got_trimmed = got.replace("\r\n", "\n").strip()
39+
assert expected_trimmed == got_trimmed

0 commit comments

Comments
 (0)