Skip to content

Commit a7c8aed

Browse files
committed
Tests: Add check for model card existence and test that the FileNotFoundError is not raised.
1 parent e081a0e commit a7c8aed

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

ingredient_parser/_common.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,21 @@ def show_model_card(lang: str = "en") -> None:
9595
----------
9696
lang : str, optional
9797
Selected language to open model card for.
98+
99+
Raises
100+
------
101+
FileNotFoundError
102+
Raised if model card not found at expected path.
103+
ValueError
104+
Raised if unsupported language provided in lang argument.
98105
"""
99106
if lang not in SUPPORTED_LANGUAGES:
100107
raise ValueError(f'Unsupported language "{lang}"')
101108

102109
with as_file(files(__package__) / lang / f"data/ModelCard.{lang}.md") as p:
110+
if not p.exists():
111+
raise FileNotFoundError(f"Could not find Model Card at {p}")
112+
103113
if platform.system() == "Darwin": # macOS
104114
subprocess.call(("open", p))
105115
elif platform.system() == "Windows": # Windows

tests/test_common.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
from unittest.mock import patch
2+
13
import pytest
24

35
from ingredient_parser._common import (
46
consume,
57
group_consecutive_idx,
68
is_float,
79
is_range,
10+
show_model_card,
811
)
912

1013

@@ -87,7 +90,7 @@ def test_x(self):
8790
assert not is_range("1x")
8891

8992

90-
class TestPostProcessor_group_consecutive_indices:
93+
class Test_group_consecutive_indices:
9194
def test_single_group(self):
9295
"""
9396
Return single group
@@ -103,3 +106,18 @@ def test_multiple_groups(self):
103106
input_indices = [0, 1, 2, 4, 5, 6, 8, 9]
104107
groups = group_consecutive_idx(input_indices)
105108
assert [list(g) for g in groups] == [[0, 1, 2], [4, 5, 6], [8, 9]]
109+
110+
111+
class Test_show_model_card:
112+
@patch("os.startfile", create=True)
113+
@patch("subprocess.call")
114+
def test_model_card_found(self, mock_startfile, mock_subprocess_call):
115+
"""Test model card found at path derived from selected language.
116+
117+
The calls to os.startfile and subprocess.call are mocked to prevent the model
118+
card from actually opening.
119+
"""
120+
try:
121+
show_model_card("en")
122+
except FileNotFoundError:
123+
pytest.fail("Model card not found.")

0 commit comments

Comments
 (0)