Skip to content

Commit 9a79fb2

Browse files
committed
fixing list vaidation
1 parent f4f18cc commit 9a79fb2

3 files changed

Lines changed: 70 additions & 5 deletions

File tree

appkernel/model.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,8 @@ def __init__(self, python_type,
403403
self.generator = generator
404404

405405
def __getattr__(self, attribute):
406-
if self.python_type == list and issubclass(self.sub_type, Model):
406+
if self.python_type == list and (
407+
hasattr(self, 'sub_type') and self.sub_type and issubclass(self.sub_type, Model)):
407408
if hasattr(self.sub_type, attribute):
408409
nested_parameter = getattr(self.sub_type, attribute)
409410
nested_parameter.backreference.array_parameter_name = self.backreference.parameter_name
@@ -987,6 +988,12 @@ def finalise_and_validate(self):
987988
obj = obj_items.get(param_name)
988989
if obj:
989990
obj.finalise_and_validate()
991+
if issubclass(param_object.python_type, (list, set)) and param_name in obj_items:
992+
list_of_objects = obj_items.get(param_name)
993+
if list_of_objects:
994+
for item in list_of_objects:
995+
if isinstance(item, Model):
996+
item.finalise_and_validate()
990997

991998
@staticmethod
992999
def __check_validity(validator: Validator, param_name, obj_items):

tests/test_validators.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import datetime
2+
from datetime import timedelta
23

34
import pytest
4-
from datetime import timedelta
5+
56
from appkernel import ValidationException
67
from tests.utils import ExampleClass, Project, Task, Payment, PaymentMethod
78

@@ -22,6 +23,52 @@ def test_regexp_validation():
2223
test_model_correct_format.finalise_and_validate()
2324

2425

26+
def test_email_validator():
27+
example = ExampleClass(just_numbers='1234')
28+
example.finalise_and_validate()
29+
30+
with pytest.raises(ValidationException):
31+
example.email = 'some_mail'
32+
example.finalise_and_validate()
33+
34+
example.email = 'acme@coppa.com'
35+
example.finalise_and_validate()
36+
37+
38+
def test_min_max():
39+
example = ExampleClass(just_numbers='1234')
40+
example.finalise_and_validate()
41+
42+
with pytest.raises(ValidationException):
43+
example.distance = 1
44+
example.finalise_and_validate()
45+
46+
with pytest.raises(ValidationException):
47+
example.distance = 16
48+
example.finalise_and_validate()
49+
50+
example.distance = 10
51+
example.finalise_and_validate()
52+
53+
54+
def test_unique():
55+
example = ExampleClass(just_numbers='1234')
56+
example.finalise_and_validate()
57+
with pytest.raises(ValidationException):
58+
example.numbers = [1, 2, 1]
59+
example.finalise_and_validate()
60+
61+
with pytest.raises(ValidationException):
62+
example.numbers = ['a', 'b', 'a']
63+
example.finalise_and_validate()
64+
65+
example.numbers = ['a', 'b', 'c']
66+
example.finalise_and_validate()
67+
68+
example.numbers = [1, 2, 3]
69+
example.finalise_and_validate()
70+
71+
2572
def test_not_empty_validation():
2673
project = Project().update(name='')
2774
with pytest.raises(ValidationException):
@@ -39,9 +86,13 @@ def test_past_validation():
3986
project.tasks[0].update(closed_date=(datetime.datetime.now() - timedelta(days=1)))
4087
print(('\n\n> one day in the past \n{}'.format(project)))
4188
project.finalise_and_validate()
89+
4290
with pytest.raises(ValidationException):
4391
project.tasks[0].update(closed_date=(datetime.datetime.now() + timedelta(days=1)))
44-
print(('\n\n> one day in the in the future \n{}'.format(project)))
92+
project.finalise_and_validate()
93+
94+
with pytest.raises(ValidationException):
95+
project.tasks[0].update(closed_date='past')
4596
project.finalise_and_validate()
4697

4798

@@ -56,10 +107,14 @@ def test_future_validation():
56107
print(('\n\n> one day in the in the future \n{}'.format(test_model)))
57108
test_model.finalise_and_validate()
58109

110+
with pytest.raises(ValidationException):
111+
test_model.future_field = 'future'
112+
test_model.finalise_and_validate()
113+
59114

60115
def test_validate_method():
61116
payment = Payment(method=PaymentMethod.MASTER, customer_id='123456', customer_secret='123')
62117
with pytest.raises(ValidationException):
63118
payment.finalise_and_validate()
64-
payment.customer_id='1234567890123456'
119+
payment.customer_id = '1234567890123456'
65120
payment.finalise_and_validate()

tests/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from money import Money
77
from passlib.hash import pbkdf2_sha256
88

9-
from appkernel import AuditableRepository, MongoRepository, AppKernelException, ValidationException
9+
from appkernel import AuditableRepository, MongoRepository, AppKernelException, ValidationException, Email, Unique
1010
from appkernel import IdentityMixin, Role, CurrentSubject, Anonymous, TextIndex, Index
1111
from appkernel import Max, Min
1212
from appkernel import Model, Property, UniqueIndex
@@ -247,6 +247,9 @@ def create_and_save_portfolio_with_owner():
247247
class ExampleClass(Model):
248248
just_numbers = Property(str, required=True, validators=[Regexp('^[0-9]+$')])
249249
future_field = Property(datetime, validators=[Future])
250+
email = Property(str, validators=Email)
251+
distance = Property(int, validators=[Min(10), Max(15)])
252+
numbers = Property(list, validators=Unique)
250253

251254

252255
class Priority(Enum):

0 commit comments

Comments
 (0)