-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtests.py
152 lines (120 loc) · 5.15 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# (c) Svarga project under terms of the new BSD license
from procrustes import procrustes
from procrustes import forms
from attest import Tests, Assert
p = Tests()
f = Tests()
I = procrustes.Integer(max=90, required=False)
S = procrustes.String()
@p.test
def simple():
"""Simple validators."""
Assert(I(10).validate()) == 10
Assert(I().validate()) == None
@p.test
def simple_tuple():
"""Simple tuple ops"""
PT = procrustes.Tuple(I, S, I)
pt = PT([10, 'sdfsdf', 30])
Assert(pt.data) == (10, 'sdfsdf', 30)
@p.test
def simple_list():
PL = procrustes.List(I)
pl = PL(xrange(7))
v1 = pl.validated_data[0]
v1.validate()
Assert(pl.data) == range(7)
@p.test
def simple_dict():
PD = procrustes.Dict({'a': I, 'b': S})
pd = PD({'a': None, 'b': 'Lorem Ipsum'})
Assert(pd.data) == {'a': None, 'b': 'Lorem Ipsum'}
pd = PD({'b': 'Lorem Ipsum'})
Assert(pd.data) == {'a': None, 'b': 'Lorem Ipsum'}
@p.test
def tuple_dict():
PT = procrustes.Tuple(I, S, I)
PD = procrustes.Dict({'a': I, 'b': S, 'c': PT})
pd = PD({'b': 'kuku', 'c': (None, 'Lorem', 91)})
Assert(pd.data) == {'a': None, 'c': (None, 'Lorem', None), 'b': 'kuku'}
pd = PD({'b': 'kuku', 'c': (None, 'Lorem', 90)})
Assert(pd.data) == {'a': None, 'c': (None, 'Lorem', 90), 'b': 'kuku'}
@p.test
def flat_deepen():
PT = procrustes.Tuple(I, S, I)
PD = procrustes.Dict({'a': I, 'b': S, 'c': PT})
pd = PD({'b': 'kuku', 'c': (None, 'Lorem', 78)})
flat = dict(pd.flatten())
Assert(flat) == {'a': None, 'c__2': 78, 'c__1': 'Lorem',
'c__0': None, 'b': 'kuku'}
flat = {'a': None, 'c__2': 78, 'c__1': 'Lorem', 'b': 'kuku'}
deep = PD.deepen(flat)
Assert(deep) == {'a': None, 'c': (None, 'Lorem', 78), 'b': 'kuku'}
pd = PD(deep)
Assert(pd.data) == {'a': None, 'c': (None, 'Lorem', 78), 'b': 'kuku'}
@p.test
def empty_flatten():
I = procrustes.Integer(max=90, required=False)
S = procrustes.String()
PL = procrustes.List(I)
PD = procrustes.Dict({'a': I, 'b': S})
PT = procrustes.Tuple(I, S, I, PL, PD)
Assert(dict(PT().flatten())) == {'4__b': None, '4__a': None, '1': None,
'0': None, '2': None, '3__0': None}
@p.test
def declarative():
class Simple(procrustes.Declarative):
name = procrustes.String(max_length=5)
simple = Simple({'name': 'test'})
Assert(simple.data) == {'name': 'test'}
fail = Simple({'name': 'qweasd'})
Assert(fail.data) == {'name': None}
Assert(fail.errors) == ['Must be shorter than 5']
@p.test
def forms_simple():
str = forms.String()('kukuku')
Assert(str.data) == 'kukuku'
FT = forms.Tuple(forms.String(), forms.String())
ft = FT()
widgets = [widget.render() for widget in ft.widgets()]
Assert(widgets) == [u'<input id="form__0" name="form__0" parent="" value="">',
u'<input id="form__1" name="form__1" parent="" value="">']
ft = FT(('kuku', 'kuku'))
widgets = [widget.render() for widget in ft.widgets()]
Assert(widgets) == [u'<input id="form__0" name="form__0" parent="" value="kuku">',
u'<input id="form__1" name="form__1" parent="" value="kuku">']
FL = forms.List(forms.String())
fl = FL(['kuku', 'dsfasfd', 'xcvxczvx'])
widgets = [widget.render() for widget in fl.widgets() if widget.render()]
Assert(widgets) == [u'<input id="form__0" name="form__0" parent="" value="kuku">',
u'<input id="form__1" name="form__1" parent="" value="dsfasfd">',
u'<input id="form__2" name="form__2" parent="" value="xcvxczvx">']
@p.test
def forms_dict_field():
FD = forms.Dict({'a': forms.String(), 'b': forms.String()})
fd = FD()
widgets = [widget.render() for widget in fd.widgets()]
Assert(widgets) == ['<input id="form__a" name="form__a" parent="" value="">',
'<input id="form__b" name="form__b" parent="" value="">']
fd = FD({'a': 'kuku', 'b': 'may-may'})
widgets = [widget.render() for widget in fd.widgets()]
Assert(widgets) == ['<input id="form__a" name="form__a" parent="" value="kuku">',
'<input id="form__b" name="form__b" parent="" value="may-may">']
@p.test
def forms_flat():
FL = forms.List(forms.String())
FD = forms.Dict({'a': forms.String(), 'b': forms.String(), 'c': FL})
flat = {'form__a': 'kuku', 'form__b': 'may-may', 'form__c__0': 'wer', 'form__c__1': 'kuku'}
unflat = FD.unflat(flat)
Assert(unflat) == {'a': 'kuku', 'b': 'may-may', 'c': ['kuku', 'wer']}
fd = FD(unflat)
widgets = [widget.render() for widget in fd.widgets() if widget.render()]
Assert(widgets) == ['<input id="form__a" name="form__a" parent="" value="kuku">',
'<input id="form__c__0" name="form__c__0" parent="" value="kuku">',
'<input id="form__c__1" name="form__c__1" parent="" value="wer">',
'<input id="form__b" name="form__b" parent="" value="may-may">']
form = FD(flat)
Assert(form.is_valid()) == True
Assert(form.data) == {'a': 'kuku', 'b': 'may-may', 'c': ['kuku', 'wer']}
if __name__ == '__main__':
p.run()