-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_with_peppercorn.py
174 lines (155 loc) · 6.62 KB
/
test_with_peppercorn.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from unittest import TestCase
from lxml import etree
import schemaish
from validatish import validator
import transformer
from transformer import get_validated
from transformer import PeppercornForm
class TestCase(TestCase):
def test_sequence_peppercorn(self):
"""Check form validation and rendering of a simple sequence of
values
"""
# create a very simple schema
schema = schemaish.Structure()
urls = schemaish.Sequence(
attr=schemaish.String(
validator=validator.URL()))
schema.add('urls', urls)
transformer.schema_registry = {'myschema': schema}
# validate some invalid data
example = {'schema': 'myschema',
'data': {
'urls': [3, "http://www.com"]}}
validated_data = get_validated(example)
self.assertEqual({'data': {'urls': [3, 'http://www.com']},
'errors': {'urls.0': 'must be a url'},
'schema': 'myschema'},
validated_data)
# parse a form template to render the data against
form = open("form_template.html").read()
form = PeppercornForm(form, data=validated_data)
form.validate()
# turn it to an etree for testing returned values
rendered = etree.fromstring(form.transform())
expected = rendered.xpath('//input[@name="urls"]')
self.assertEqual(expected[0].attrib['value'], "3")
self.assertEqual(expected[1].attrib['value'],
"http://www.com")
error = rendered.xpath('//div[@class="error-message"]')
self.assertEqual(error[0].text, "must be a url")
self.assertEqual(error[1].text, None)
def test_structure_peppercorn(self):
"""Check form validation and rendering of a sequence of
dictionaries
"""
# a schema which contains a list of dictionaries
schema = schemaish.Structure()
# the dictionary is a "person"...
person = schemaish.Structure()
person.add('name', schemaish.String())
person.add('age', schemaish.Integer())
# ...add this to a list of "people"...
people = schemaish.Sequence(attr=person)
# ...and add it to the schema
schema.add('people', people)
transformer.schema_registry = {'myschema': schema}
# try validating some valid data
example = {'schema': 'myschema',
'data': {
'people': [{'name':'zephania',
'age':24},
{'name':'methusula',
'age':133}]
}
}
validated_data = get_validated(example)
form = open("form_structure_template.html").read()
form = PeppercornForm(form, data=validated_data)
form.validate()
# the template we supplied should have been used to make the
# repeating, list-type fieldsets:
rendered = etree.fromstring(form.transform())
expected = rendered.xpath('//input[@name="name"]')[0]
self.assertEqual(expected.attrib['value'],
'zephania')
expected = rendered.xpath('//input[@name="age"]')[1]
self.assertEqual(expected.attrib['value'],
'133')
def test_structure_peppercorn_validation(self):
"""Check form validation and rendering (including errors) of a
sequence of dictionaries
"""
# as above, but with a validation error within the nested dict
schema = schemaish.Structure()
person = schemaish.Structure()
person.add('name', schemaish.String())
person.add('age', schemaish.Integer(validator=validator.Required()))
people = schemaish.Sequence(attr=person)
schema.add('people', people)
transformer.schema_registry = {'myschema': schema}
example = {'schema': 'myschema',
'data': {
'people': [{'name':'zephenia',
'age':24},
{'name':'methusula',
'age':None}]
}
}
validated_data = get_validated(example)
form = open("form_structure_template.html").read()
form = PeppercornForm(form, data=validated_data)
form.validate()
rendered = etree.fromstring(form.transform())
expected = rendered.xpath('//*[@class="error-message"]')[3]
self.assertEqual(expected.text, 'is required')
def xtest_deeper_peppercorn_validation(self):
# disabled as lists-within-lists have a bug (see below)
schema = schemaish.Structure()
person = schemaish.Structure()
person.add('name', schemaish.String())
person.add('numbers',
schemaish.Sequence(
attr=schemaish.Integer(
validated=validator.Integer()
)
)
)
people = schemaish.Sequence(attr=person)
schema.add('people', people)
transformer.schema_registry = {'myschema': schema}
example = {'schema': 'myschema',
'data': {
'people': [{'name':'bob',
'numbers': [1, 2, 3]},
{'name':'sue',
'numbers': [9, 7, "frob"]}]
}
}
validated_data = get_validated(example)
form = open("form_deep_structure_template.html").read()
form = PeppercornForm(form, data=validated_data)
form.validate()
# XXX the following fails because nested lists end up copying
# templates when they shouldn't -- see 'parse_list' method of
# transformer
rendered = etree.fromstring(form.transform())
expected = rendered.xpath('//*[@class="error-message"]')[3]
self.assertEqual(expected.text, 'is required')
def test_different_schema_types(self):
"""Ensure we can refer to schemas by name or by instance
"""
schema = schemaish.Structure()
transformer.schema_registry = {'myschema': schema}
example1 = {'schema': 'myschema',
'data': {}}
example2 = {'schema': schema,
'data': {}}
# both of these should work
get_validated(example1)
get_validated(example2)
def runTest(self):
self.test_something()
if __name__ == "__main__":
test = TestCase()
test.runTest()