-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start enumerating tests for constraints factory.
Also, add support for unbound forward references.
- Loading branch information
1 parent
b4394ef
commit 88d218d
Showing
10 changed files
with
281 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,3 +111,4 @@ venv.bak/ | |
/pip-wheel-metadata/ | ||
/benchmark/.cases.json | ||
*.DS_Store | ||
coverage.db |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
from __future__ import annotations | ||
|
||
import decimal | ||
import enum | ||
import inspect | ||
import typing | ||
|
||
import pytest | ||
|
||
from typical.core import constants | ||
from typical.core.constraints import factory | ||
from typical.core.constraints.core import types, validators | ||
|
||
|
||
class MyEnum(enum.Enum): | ||
... | ||
|
||
|
||
class MyClass: | ||
... | ||
|
||
|
||
@pytest.mark.suite( | ||
anytype=dict( | ||
given_type=typing.Any, | ||
given_context=dict(), | ||
expected_constraints_cls=types.UndeclaredTypeConstraints, | ||
expected_validator_cls=validators.NoOpInstanceValidator, | ||
), | ||
constants_empty=dict( | ||
given_type=constants.empty, | ||
given_context=dict(), | ||
expected_constraints_cls=types.UndeclaredTypeConstraints, | ||
expected_validator_cls=validators.NoOpInstanceValidator, | ||
), | ||
param_empty=dict( | ||
given_type=inspect.Parameter.empty, | ||
given_context=dict(), | ||
expected_constraints_cls=types.UndeclaredTypeConstraints, | ||
expected_validator_cls=validators.NoOpInstanceValidator, | ||
), | ||
ellipsis=dict( | ||
given_type=Ellipsis, | ||
given_context=dict(), | ||
expected_constraints_cls=types.UndeclaredTypeConstraints, | ||
expected_validator_cls=validators.NoOpInstanceValidator, | ||
), | ||
enum=dict( | ||
given_type=MyEnum, | ||
given_context=dict(), | ||
expected_constraints_cls=types.EnumerationConstraints, | ||
expected_validator_cls=validators.OneOfValidator, | ||
), | ||
literal=dict( | ||
given_type=typing.Literal[1, 2], | ||
given_context=dict(), | ||
expected_constraints_cls=types.EnumerationConstraints, | ||
expected_validator_cls=validators.OneOfValidator, | ||
), | ||
string=dict( | ||
given_type=str, | ||
given_context=dict(), | ||
expected_constraints_cls=types.TextConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
bytestring=dict( | ||
given_type=bytes, | ||
given_context=dict(), | ||
expected_constraints_cls=types.TextConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
boolean=dict( | ||
given_type=bool, | ||
given_context=dict(), | ||
expected_constraints_cls=types.TypeConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
integer=dict( | ||
given_type=int, | ||
given_context=dict(), | ||
expected_constraints_cls=types.NumberConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
float=dict( | ||
given_type=float, | ||
given_context=dict(), | ||
expected_constraints_cls=types.NumberConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
decimal=dict( | ||
given_type=decimal.Decimal, | ||
given_context=dict(), | ||
expected_constraints_cls=types.DecimalConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
structured=dict( | ||
given_type=MyClass, | ||
given_context=dict(), | ||
expected_constraints_cls=types.StructuredObjectConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
dict=dict( | ||
given_type=dict, | ||
given_context=dict(), | ||
expected_constraints_cls=types.MappingConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
mapping=dict( | ||
given_type=typing.Mapping, | ||
given_context=dict(), | ||
expected_constraints_cls=types.MappingConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
collection=dict( | ||
given_type=typing.Collection, | ||
given_context=dict(), | ||
expected_constraints_cls=types.ArrayConstraints, | ||
expected_validator_cls=validators.IsInstanceValidator, | ||
), | ||
optional=dict( | ||
given_type=typing.Optional[str], | ||
given_context=dict(), | ||
expected_constraints_cls=types.TextConstraints, | ||
expected_validator_cls=validators.NullableIsInstanceValidator, | ||
), | ||
) | ||
def test_build( | ||
given_type, given_context, expected_constraints_cls, expected_validator_cls | ||
): | ||
# When | ||
built_cv = factory.build(t=given_type, **given_context) | ||
# Then | ||
assert isinstance(built_cv.constraints, expected_constraints_cls) | ||
assert isinstance(built_cv.validator, expected_validator_cls) | ||
|
||
|
||
def test_build_forwardref(): | ||
# Given | ||
given_type = "dict | None" | ||
expected_constraints_cls = types.MappingConstraints | ||
expected_validator_cls = validators.NullableIsInstanceValidator | ||
# When | ||
built_dcv = factory.build(t=given_type) | ||
built_cv = built_dcv.cv | ||
# Then | ||
assert isinstance(built_cv.constraints, expected_constraints_cls) | ||
assert isinstance(built_cv.validator, expected_validator_cls) |
Oops, something went wrong.