-
Notifications
You must be signed in to change notification settings - Fork 35
Convert test_loaders_pydantic.py to pytest format #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ialarmedalien
merged 5 commits into
linkml:main
from
vladistan:convert-test-loaders-pydantic-to-pytest
Sep 25, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b80a5d8
Convert test_loaders_pydantic.py to pytest format
vladistan a52862c
Inline and simplify loader test
vladistan 883e590
Whitespace fix
vladistan af3f997
Merge branch 'main' into convert-test-loaders-pydantic-to-pytest
vladistan 00a0e8f
Merge branch 'main' into convert-test-loaders-pydantic-to-pytest
ialarmedalien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
tests/test_loaders_dumpers/input/book_series_lotr_json.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| id: S001 | ||
| name: Lord of the Rings | ||
| genres: | ||
| - fantasy | ||
| creator: | ||
| name: JRR Tolkien | ||
| from_country: England | ||
| books: | ||
| - id: S001.1 | ||
| name: Fellowship of the Ring | ||
| summary: Hobbits | ||
| price: 5.99 | ||
| - id: S001.2 | ||
| name: The Two Towers | ||
| summary: More hobbits | ||
| price: 5.99 | ||
| - id: S001.3 | ||
| name: Return of the King | ||
| summary: Yet more hobbits | ||
| price: 6.99 |
20 changes: 20 additions & 0 deletions
20
tests/test_loaders_dumpers/input/book_series_lotr_yaml.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| id: S001 | ||
| name: Lord of the Rings | ||
| genres: | ||
| - fantasy | ||
| creator: | ||
| name: JRR Tolkien | ||
| from_country: England | ||
| books: | ||
| - id: S001.1 | ||
| name: Fellowship of the Ring | ||
| summary: Hobbits | ||
| price: 5.99 | ||
| - id: S001.2 | ||
| name: The Two Towers | ||
| summary: More hobbits | ||
| price: 5.99 | ||
| - id: S001.3 | ||
| name: Return of the King | ||
| summary: Yet more hobbits | ||
| price: 6.99 | ||
41 changes: 41 additions & 0 deletions
41
tests/test_loaders_dumpers/input/kitchen_sink_normalized_inst_01_json.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| persons: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like this is also identical to the yaml version -- just add one, no need for duplicates. |
||
| - id: P:001 | ||
| name: fred bloggs | ||
| age_in_years: 33 | ||
| - id: P:002 | ||
| name: joe schmoe | ||
| has_employment_history: | ||
| - started_at_time: 2019-01-01 | ||
| is_current: true | ||
| employed_at: ROR:1 | ||
| has_familial_relationships: | ||
| - related_to: P:001 | ||
| type: SIBLING_OF | ||
| has_medical_history: | ||
| - started_at_time: 2019-01-01 | ||
| in_location: GEO:1234 | ||
| diagnosis: | ||
| id: CODE:D0001 | ||
| name: headache | ||
| in_code_system: DiseaseCodes | ||
| procedure: | ||
| id: CODE:P0001 | ||
| name: trepanation | ||
| in_code_system: ProcedureCodes | ||
| addresses: | ||
| - street: 1 foo street | ||
| city: foo city | ||
| companies: | ||
| - id: ROR:1 | ||
| name: foo | ||
| activities: | ||
| - id: A:1 | ||
| started_at_time: 2019-01-01 | ||
| was_associated_with: Agent:987 | ||
| code_systems: | ||
| DiseaseCodes: | ||
| id: DiseaseCodes | ||
| name: disease codes | ||
| ProcedureCodes: | ||
| id: ProcedureCodes | ||
| name: disease codes | ||
41 changes: 41 additions & 0 deletions
41
tests/test_loaders_dumpers/input/kitchen_sink_normalized_inst_01_yaml.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| persons: | ||
| - id: P:001 | ||
| name: fred bloggs | ||
| age_in_years: 33 | ||
| - id: P:002 | ||
| name: joe schmoe | ||
| has_employment_history: | ||
| - started_at_time: 2019-01-01 | ||
| is_current: true | ||
| employed_at: ROR:1 | ||
| has_familial_relationships: | ||
| - related_to: P:001 | ||
| type: SIBLING_OF | ||
| has_medical_history: | ||
| - started_at_time: 2019-01-01 | ||
| in_location: GEO:1234 | ||
| diagnosis: | ||
| id: CODE:D0001 | ||
| name: headache | ||
| in_code_system: DiseaseCodes | ||
| procedure: | ||
| id: CODE:P0001 | ||
| name: trepanation | ||
| in_code_system: ProcedureCodes | ||
| addresses: | ||
| - street: 1 foo street | ||
| city: foo city | ||
| companies: | ||
| - id: ROR:1 | ||
| name: foo | ||
| activities: | ||
| - id: A:1 | ||
| started_at_time: 2019-01-01 | ||
| was_associated_with: Agent:987 | ||
| code_systems: | ||
| DiseaseCodes: | ||
| id: DiseaseCodes | ||
| name: disease codes | ||
| ProcedureCodes: | ||
| id: ProcedureCodes | ||
| name: disease codes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,39 @@ | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| from hbreader import FileInfo | ||
| from pydantic import BaseModel | ||
|
|
||
| from linkml_runtime.dumpers import yaml_dumper | ||
| from linkml_runtime.loaders import json_loader, yaml_loader | ||
| from tests.test_loaders_dumpers.environment import env | ||
| from tests.test_loaders_dumpers.loaderdumpertestcase import LoaderDumperTestCase | ||
| from tests.test_loaders_dumpers.models.books_normalized_pydantic import BookSeries | ||
| from tests.test_loaders_dumpers.models.kitchen_sink_pydantic import Dataset | ||
|
|
||
|
|
||
| class PydanticLoadersUnitTest(LoaderDumperTestCase): | ||
| env = env | ||
|
|
||
| def test_yaml_loader_single(self): | ||
| """Load obo_sample.yaml, emit obo_sample_yaml.yaml and compare to obo_sample_output.yaml""" | ||
| self.loader_test("book_series_lotr.yaml", BookSeries, yaml_loader) | ||
|
|
||
| def test_json_loader(self): | ||
| """Load obo_sample.json, emit obo_sample_json.yaml and check the results""" | ||
| self.loader_test("book_series_lotr.json", BookSeries, json_loader) | ||
|
|
||
| def test_yaml_loader_kitchen_sink(self): | ||
| self.loader_test("kitchen_sink_normalized_inst_01.yaml", Dataset, yaml_loader) | ||
|
|
||
| def test_json_loader_kitchen_sink(self): | ||
| self.loader_test("kitchen_sink_normalized_inst_01.json", Dataset, json_loader) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| @pytest.mark.parametrize( | ||
| "filename,model,loader", | ||
| [ | ||
| ("book_series_lotr.yaml", BookSeries, yaml_loader), | ||
| ("book_series_lotr.json", BookSeries, json_loader), | ||
| ("kitchen_sink_normalized_inst_01.yaml", Dataset, yaml_loader), | ||
| ("kitchen_sink_normalized_inst_01.json", Dataset, json_loader), | ||
| ], | ||
| ) | ||
| def test_loader_basemodel(filename, model, loader): | ||
| name = Path(filename).stem | ||
| type = Path(filename).suffix.lstrip(".") | ||
| expected_yaml_file = env.input_path(f"{name}_{type}.yaml") | ||
|
|
||
| metadata = FileInfo() | ||
|
|
||
| python_obj: BaseModel = loader.load(filename, model, metadata=metadata, base_dir=env.indir) | ||
|
|
||
| # Load expected output | ||
| with open(expected_yaml_file) as expf: | ||
| expected = expf.read() | ||
|
|
||
| got = yaml_dumper.dumps(python_obj) | ||
| expected_trimmed = expected.replace("\r\n", "\n").strip() | ||
| got_trimmed = got.replace("\r\n", "\n").strip() | ||
| assert expected_trimmed == got_trimmed |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is identical to the other file -- I would just have a single file,
book_series_lotr.yaml.