Skip to content

Commit 12c0ae3

Browse files
committed
♻️ REFACTOR: yaml format
make a mapping and use title as keys
1 parent 1336faa commit 12c0ae3

5 files changed

Lines changed: 21 additions & 19 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ expected output content
2525

2626
`yaml` format:
2727
```yaml
28-
- title: name1
29-
description: description
28+
name1:
29+
description: optional description
3030
input: |-
3131
input content
3232
expected: |-
3333
expected output content
34-
- title: name2
35-
description: description
34+
name2:
35+
description: optional description
3636
input: |-
3737
input content
3838
expected: |-

pytest_param_files/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Create pytest parametrize decorators from external files."""
22
from .main import ParamTestData
33

4-
__version__ = "0.4.0"
4+
__version__ = "0.5.0"
55
__all__ = ("ParamTestData",)

pytest_param_files/main.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,23 +265,25 @@ def read(self) -> Iterator[ParamTestData]:
265265
"""
266266
text = self.path.read_text(encoding=self.encoding)
267267
node = yaml.compose(text, yaml.SafeLoader)
268-
if not isinstance(node, yaml.SequenceNode):
268+
if not isinstance(node, yaml.MappingNode):
269269
raise TypeError(f"Expected sequence, got {type(node)}")
270-
data = yaml.safe_load(text)
270+
data: dict = yaml.safe_load(text)
271271
assert len(node.value) == len(data), "YAML node count mismatch"
272-
item_node: yaml.Node
273-
for index, (item_node, item) in enumerate(zip(node.value, data)):
274-
line = item_node.start_mark.line + 1
272+
title_node: yaml.Node
273+
for index, ((title_node, _), (title, item)) in enumerate(
274+
zip(node.value, data.items())
275+
):
276+
line = title_node.start_mark.line + 1
275277
if not isinstance(item, dict):
276278
raise TypeError(
277-
f"Expected mapping at line {line}, got {type(item_node)}"
279+
f"Expected mapping value at line {line}, got {type(item)}"
278280
)
279-
for key in ("title", "content", "expected"):
281+
for key in ("content", "expected"):
280282
if key not in item:
281283
raise KeyError(f"Missing '{key}' key for item at line {line}")
282284
yield ParamTestData(
283285
line,
284-
item["title"],
286+
title,
285287
item.get("description"),
286288
item["content"],
287289
item["expected"],
@@ -326,7 +328,7 @@ def regen_file(self, data: ParamTestData, actual: Any, **kwargs: Any) -> None:
326328
# TODO ideally here we would maintain comments and formatting
327329
# perhaps using ruamel.yaml, although that is a pain
328330
new = yaml.safe_load(self.path.read_text(encoding=self.encoding))
329-
new[data.index]["expected"] = actual
331+
new[data.title]["expected"] = actual
330332
text = yaml.dump(
331333
new, Dumper=CustomDumper, default_flow_style=False, sort_keys=False
332334
)

tests/fixtures/basic.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
- title: name1
1+
name1:
22
description: description
33
content: Something
44
expected: Other
5-
- title: name2
5+
name2:
66
description: description
77
content: Something
88
expected: Other

tests/test_basic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_basic_yaml(file_params):
3131
assert file_params.title.startswith("name")
3232
assert file_params.description == "description"
3333
assert file_params.content.rstrip() == "Something"
34-
# assert file_params.expected.rstrip() == "Other"
34+
assert file_params.expected.rstrip() == "Other"
3535
file_params.assert_expected("Other", rstrip=True)
36-
# with pytest.raises(AssertionError, match="basic.yaml"):
37-
# file_params.assert_expected("Otherx", rstrip=True)
36+
with pytest.raises(AssertionError, match="basic.yaml"):
37+
file_params.assert_expected("Otherx", rstrip=True)

0 commit comments

Comments
 (0)