To thoroughly test coverage, test case data is separated from test code.
Currently these are mixed.
Original
def test_keyed_choice_map_allow_zero_or_one():
schema = KeyedChoiceMap(
choices=[
("eq", Str()),
("in", Seq(Str())),
],
minimum_keys=0,
maximum_keys=1,
)
doc = schema(YAMLChunk(CommentedMap({})))
assert doc.data == {}
doc2 = load("in:\n - a\n - b", schema)
assert doc2.data == {"in": ["a", "b"]}
With pytest.mark.parametrize
from typing import TYPE_CHECKING
import pytest
from strictyaml.yamllocation import YAMLChunk
from strictyaml.ruamel.comments import CommentedMap
from strictyamlx import load, KeyedChoiceMap, Seq, Str
if TYPE_CHECKING:
from typing import Any
testdata_keyed_choice_map_allow_zero_or_one = (
(
0,
1,
YAMLChunk(CommentedMap({})),
{},
"in:\n - a\n - b",
{"in": ["a", "b"]},
),
)
ids_keyed_choice_map_allow_zero_or_one = (
"no minimum",
)
@pytest.mark.parametrize(
(
"minimum_keys, maximum_keys, chunk_empty, "
"chunk_empty_expected, str_yaml, d_expected"
),
testdata_keyed_choice_map_allow_zero_or_one,
ids=ids_keyed_choice_map_allow_zero_or_one,
)
def test_keyed_choice_map_allow_zero_or_one(
minimum_keys: int,
maximum_keys: int,
chunk_empty: "YAMLChunk",
chunk_empty_expected: "dict[str, Any]",
str_yaml: str,
d_expected: "dict[str, Any]",
) -> None:
"""pytest prints this, so it is non optional"""
schema = KeyedChoiceMap(
choices=[
("eq", Str()),
("in", Seq(Str())),
],
minimum_keys=minimum_keys,
maximum_keys=maximum_keys,
)
doc_0 = schema(chunk_empty)
d_actual_0 = doc_0.data
assert d_actual_0 == chunk_empty_expected
doc_1 = load(str_yaml, schema)
d_actual_1 = doc_1.data
assert d_actual_1 == d_expected
The test is now much much cleaner, although it could be even more flexible:
- catch exceptions even when no exception is expected
- Separate out the schema
Did neither so comparing the original and suggested are easier. Learning curves are no fun, so kept it simple.
Also coders start counting from zero, not one.
To aid in memorize spelling of @pytest.mark.parametrize, param-et-rize
Have no idea what that word means.
To thoroughly test coverage, test case data is separated from test code.
Currently these are mixed.
Original
With pytest.mark.parametrize
The test is now much much cleaner, although it could be even more flexible:
Did neither so comparing the original and suggested are easier. Learning curves are no fun, so kept it simple.
Also coders start counting from zero, not one.
To aid in memorize spelling of
@pytest.mark.parametrize, param-et-rizeHave no idea what that word means.