Skip to content

Commit a52862c

Browse files
committed
Inline and simplify loader test
Explicitly add input files for test cases do not read test inputs from output directory
1 parent b80a5d8 commit a52862c

File tree

5 files changed

+140
-36
lines changed

5 files changed

+140
-36
lines changed
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: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
1-
import os
2-
from typing import Union
1+
from pathlib import Path
32

43
import pytest
54
from hbreader import FileInfo
65
from pydantic import BaseModel
76

8-
import tests.environment as test_base
97
from linkml_runtime.dumpers import yaml_dumper
108
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
139
from tests.test_loaders_dumpers.environment import env
1410
from tests.test_loaders_dumpers.models.books_normalized_pydantic import BookSeries
1511
from tests.test_loaders_dumpers.models.kitchen_sink_pydantic import Dataset
1612

1713

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-
4714
@pytest.mark.parametrize(
4815
"filename,model,loader",
4916
[
@@ -53,5 +20,20 @@ def loader_test(filename: str, model: Union[type[YAMLRoot], type[BaseModel]], lo
5320
("kitchen_sink_normalized_inst_01.json", Dataset, json_loader),
5421
],
5522
)
56-
def test_loader(filename, model, loader):
57-
loader_test(filename, model, loader)
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)