diff --git a/validobj/tests/test_errors.py b/tests/test_errors.py similarity index 100% rename from validobj/tests/test_errors.py rename to tests/test_errors.py diff --git a/validobj/tests/test_validation.py b/tests/test_validation.py similarity index 89% rename from validobj/tests/test_validation.py rename to tests/test_validation.py index 3ef4fcf..e2d193b 100644 --- a/validobj/tests/test_validation.py +++ b/tests/test_validation.py @@ -94,10 +94,10 @@ def test_good_inp(): assert parse_input(good_inp, Db) == expected_res -def test_bad_inp(): - for k in bad_inp: - with pytest.raises(ValidationError): - parse_input(*k) +@pytest.mark.parametrize(("value", "spec"), bad_inp) +def test_bad_inp(value, spec): + with pytest.raises(ValidationError): + parse_input(value, spec) def test_not_supported(): @@ -149,6 +149,17 @@ def test_none(): with pytest.raises(ValidationError): parse_input("Some value", None) + @pytest.mark.skipif(not HAVE_LITERAL, reason="Literal not found") def test_literal(): assert parse_input(5, Literal[5, Literal[1, 3]]) == 5 + + +def test_enum_by_value() -> None: + """Enums are built also from value.""" + assert parse_input("small", MemOptions) == MemOptions.SMALL + + +def test_enum_by_int_value() -> None: + """Enums are built also from value.""" + assert parse_input(1, Attributes) == Attributes.READ diff --git a/validobj/validation.py b/validobj/validation.py index d9dbb65..a45f385 100644 --- a/validobj/validation.py +++ b/validobj/validation.py @@ -176,12 +176,11 @@ def _parse_dataclass(value, spec): def _parse_single_enum(value, spec): - if not isinstance(value, str): - raise WrongTypeError( - f"Expecting value to be a string, not {_typename(type(value))!r}" - ) - if not value in spec.__members__: - raise NotAnEnumItemError(value, spec) + if value not in spec.__members__: + try: + return spec(value) + except (ValueError, TypeError): + raise NotAnEnumItemError(value, spec) return spec.__members__[value]