Skip to content

Commit e9ccaef

Browse files
author
Sylvain MARIE
committed
Updated tests to cover the latest features
1 parent 5ed1dab commit e9ccaef

1 file changed

Lines changed: 54 additions & 14 deletions

File tree

vtypes/tests/test_vtypes.py

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from six import with_metaclass
88
from valid8 import ValidationError
99

10-
from vtypes.core import VType, VTypeMeta
11-
from vtypes import vtype
10+
from vtypes.core import VTypeMeta
11+
from vtypes import vtype, is_vtype, VType
1212

1313

1414
@pytest.mark.parametrize('val_to_test,valid_type, valid_value',
@@ -18,7 +18,7 @@
1818
])
1919
@pytest.mark.parametrize("validator_style", ['callable', 'tuple', 'dict', 'list'],
2020
ids="validator_style={}".format)
21-
@pytest.mark.parametrize("vtype_style", ['function', 'class_unofficial'],
21+
@pytest.mark.parametrize("vtype_style", ['function', 'class'],
2222
ids="vtype_style={}".format)
2323
def test_vtype_basic(validator_style, vtype_style, val_to_test, valid_type, valid_value):
2424

@@ -33,8 +33,7 @@ def test_vtype_basic(validator_style, vtype_style, val_to_test, valid_type, vali
3333
else:
3434
raise ValueError(validator_style)
3535

36-
if vtype_style == 'class_unofficial':
37-
# this style will be abandoned
36+
if vtype_style == 'class':
3837
class PositiveInt(VType):
3938
__type__ = int
4039
__validators__ = validators
@@ -63,7 +62,10 @@ class PositiveInt(VType):
6362
assert not issubclass(int, VType)
6463
assert issubclass(PositiveInt, VType)
6564
assert not issubclass(VType, PositiveInt)
65+
66+
# This is the precise reason why we need to put the __type__ in the bases
6667
assert issubclass(PositiveInt, int)
68+
6769
assert not issubclass(int, PositiveInt)
6870

6971
for cls in (PositiveInt, ):
@@ -74,23 +76,61 @@ class PositiveInt(VType):
7476
# make sure the string representation will be correct
7577
assert PositiveInt.__module__ == test_vtype_basic.__module__
7678

79+
# is_vtype
80+
assert not is_vtype(VTypeMeta)
81+
assert is_vtype(VType)
82+
assert is_vtype(PositiveInt)
83+
assert not is_vtype(1)
84+
assert not is_vtype(int)
85+
86+
87+
def test_vtype_direct_vtype_meta():
88+
"""Tests that one MUST inherit from VType when using the class style """
89+
90+
with pytest.raises(TypeError):
91+
# TypeError: It is not possible to create a VType without inheriting from `VType`
92+
class NotInheritingVType(with_metaclass(VTypeMeta, object)):
93+
pass
94+
95+
96+
def test_extra_class_attr():
97+
"""Tests that one cannot define other attributes on `VType` classes"""
98+
with pytest.raises(TypeError):
99+
# TypeError: It is not possible to create a VType without inheriting from `VType`
100+
class NotInheritingVType(with_metaclass(VTypeMeta, object)):
101+
pass
102+
77103

104+
@pytest.mark.parametrize("vtype_style", ['function', 'class', 'class2'],
105+
ids="vtype_style={}".format)
78106
@pytest.mark.parametrize('val_to_test,valid_type, valid_value',
79107
[('1', True, True),
80108
('', True, False),
81109
(1, False, False)])
82-
def test_vtypes_inheritance(val_to_test, valid_type, valid_value):
110+
def test_vtypes_inheritance(vtype_style, val_to_test, valid_type, valid_value):
83111
""" Test with inherited vtypes """
84112

85-
with pytest.raises(TypeError):
86-
class NonEmpty(with_metaclass(VTypeMeta, object)):
87-
__validators__ = {'should be non empty': lambda x: len(x) > 0}
113+
validators = {'should be non empty': lambda x: len(x) > 0}
114+
115+
if vtype_style == 'class':
116+
class NonEmpty(VType):
117+
__validators__ = validators
118+
119+
class NonEmptyStr(NonEmpty, str):
120+
pass
121+
elif vtype_style == 'class2':
122+
class NonEmpty(VType):
123+
__validators__ = validators
88124

89-
class NonEmpty(VType):
90-
__validators__ = {'should be non empty': lambda x: len(x) > 0}
125+
class NonEmptyStr(VType):
126+
"""A VType for non-empty strings - alternate style"""
127+
__type__ = NonEmpty, str
91128

92-
class NonEmptyStr(NonEmpty, str):
93-
pass
129+
elif vtype_style == 'function':
130+
NonEmpty = vtype('NonEmpty', (), validators)
131+
NonEmptyStr = vtype('NonEmptyStr', (NonEmpty, str), ())
132+
else:
133+
raise ValueError(vtype_style)
94134

95135
# values test
96136
if valid_type and valid_value:
@@ -116,4 +156,4 @@ class NonEmptyStr(NonEmpty, str):
116156
for cls in (NonEmpty, NonEmptyStr):
117157
assert '__type__' in cls.__dict__
118158
assert '__validators__' in cls.__dict__
119-
assert '_validator' in cls.__dict__
159+
assert '_validator' in cls.__dict__

0 commit comments

Comments
 (0)