refactor(test): Refactor Python tests

This commit is contained in:
Dmitry Dygalo 2022-04-25 18:57:05 +02:00 committed by Dmitry Dygalo
parent 0ea012d422
commit 37955910cf
1 changed files with 26 additions and 31 deletions

View File

@ -170,28 +170,28 @@ On instance["foo"]:
) )
SCHEMA = {"properties": {"foo": {"type": "integer"}, "bar": {"type": "string"}}}
@pytest.mark.parametrize( @pytest.mark.parametrize(
"func", "func",
( (
JSONSchema({"properties": {"foo": {"type": "integer"}, "bar": {"type": "string"}}}).iter_errors, JSONSchema(SCHEMA).iter_errors,
partial(iter_errors, {"properties": {"foo": {"type": "integer"}, "bar": {"type": "string"}}}), partial(iter_errors, SCHEMA),
), ),
) )
def test_iter_err_message(func): def test_iter_err_message(func):
instance = {"foo": None, "bar": None} errors = func({"foo": None, "bar": None})
errs = func(instance)
err_a = next(errs) first = next(errors)
assert err_a.message == 'null is not of type "string"' assert first.message == 'null is not of type "string"'
err_b = next(errs) second = next(errors)
assert err_b.message == 'null is not of type "integer"' assert second.message == 'null is not of type "integer"'
try: with suppress(StopIteration):
next(errs) next(errors)
pytest.fail("Validation error should happen") pytest.fail("Validation error should happen")
except StopIteration:
pass
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -202,14 +202,11 @@ def test_iter_err_message(func):
), ),
) )
def test_iter_err_empty(func): def test_iter_err_empty(func):
schema = {"properties": {"foo": {"type": "integer"}}}
instance = {"foo": 1} instance = {"foo": 1}
errs = func(instance) errs = func(instance)
try: with suppress(StopIteration):
next(errs) next(errs)
pytest.fail("Validation error should happen") pytest.fail("Validation error should happen")
except StopIteration:
pass
class StrEnum(Enum): class StrEnum(Enum):
@ -222,18 +219,16 @@ class IntEnum(Enum):
foo = 2 foo = 2
def test_enum_str(): @pytest.mark.parametrize(
schema = {"properties": {"foo": {"type": "string"}}} "type_, value, expected",
instance = {"foo": StrEnum.bar} (
assert is_valid(schema, instance) == True ("number", IntEnum.bar, True),
("number", StrEnum.bar, False),
instance["foo"] = IntEnum.bar ("string", IntEnum.bar, False),
assert is_valid(schema, instance) == False ("string", StrEnum.bar, True),
),
)
def test_enum_int(): def test_enums(type_, value, expected):
schema = {"properties": {"foo": {"type": "number"}}} schema = {"properties": {"foo": {"type": type_}}}
instance = {"foo": IntEnum.bar} instance = {"foo": value}
assert is_valid(schema, instance) == True assert is_valid(schema, instance) is expected
instance["foo"] = StrEnum.bar
assert is_valid(schema, instance) == False