forked from fusionbox/django-betterforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
351 lines (297 loc) · 11.1 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from collections import OrderedDict
from django.test import TestCase
from django.test.client import RequestFactory
from django.views.generic import CreateView
from django.utils.encoding import force_text
from .models import User, Profile, Badge, Book
from .forms import (
UserProfileMultiForm, BadgeMultiForm, ErrorMultiForm, MixedForm,
NeedsFileField, ManyToManyMultiForm, RaisesErrorBookMultiForm,
CleanedBookMultiForm, BookMultiForm, RaisesErrorCustomCleanMultiform,
ModifiesDataCustomCleanMultiform,
)
try:
from django.urls import reverse
except ImportError: # Django 1.9 and earlier
from django.core.urlresolvers import reverse
class MultiFormTest(TestCase):
def test_initial_data(self):
form = UserProfileMultiForm(
initial={
'user': {
'name': 'foo',
},
'profile': {
'display_name': 'bar',
}
}
)
self.assertEqual(form['user']['name'].value(), 'foo')
self.assertEqual(form['profile']['display_name'].value(), 'bar')
def test_iter(self):
form = UserProfileMultiForm()
# get the field off of the BoundField
fields = [field.field for field in form]
self.assertEqual(fields, [
form['user'].fields['name'],
form['profile'].fields['name'],
form['profile'].fields['display_name'],
])
def test_as_table(self):
form = UserProfileMultiForm()
user_table = form['user'].as_table()
profile_table = form['profile'].as_table()
self.assertEqual(form.as_table(), user_table + profile_table)
def test_fields(self):
form = UserProfileMultiForm()
self.assertEqual(form.fields, [
'user-name', 'profile-name', 'profile-display_name'
])
def test_errors(self):
form = ErrorMultiForm()
self.assertEqual(form.errors, {})
def test_errors_crossform(self):
form = ErrorMultiForm()
form.add_crossform_error("Error")
self.assertEqual(form.errors, {'__all__': ['Error']})
def test_to_str_is_as_table(self):
form = UserProfileMultiForm()
self.assertEqual(force_text(form), form.as_table())
def test_as_ul(self):
form = UserProfileMultiForm()
user_ul = form['user'].as_ul()
profile_ul = form['profile'].as_ul()
self.assertEqual(form.as_ul(), user_ul + profile_ul)
def test_as_p(self):
form = UserProfileMultiForm()
user_p = form['user'].as_p()
profile_p = form['profile'].as_p()
self.assertEqual(form.as_p(), user_p + profile_p)
def test_is_not_valid(self):
form = UserProfileMultiForm({
'user-name': 'foo',
})
self.assertFalse(form.is_valid())
self.assertTrue(form['user'].is_valid())
self.assertFalse(form['profile'].is_valid())
form = UserProfileMultiForm({
'user-name': 'foo',
'profile-name': 'foo',
})
self.assertTrue(form.is_valid())
def test_non_field_errors(self):
# we have to pass in a value for data to force real
# validation.
form = ErrorMultiForm(data={})
self.assertFalse(form.is_valid())
self.assertEqual(form.non_field_errors().as_text(),
'* It broke\n* It broke')
def test_is_multipart(self):
form1 = ErrorMultiForm()
self.assertFalse(form1.is_multipart())
form2 = NeedsFileField()
self.assertTrue(form2.is_multipart())
def test_media(self):
form = NeedsFileField()
self.assertIn('test.js', form.media._js)
def test_is_bound(self):
form = ErrorMultiForm()
self.assertFalse(form.is_bound)
form = ErrorMultiForm(data={})
self.assertTrue(form.is_bound)
def test_hidden_fields(self):
form = NeedsFileField()
hidden_fields = [field.field for field in form.hidden_fields()]
self.assertEqual(hidden_fields, [
form['file'].fields['hidden'],
form['errors'].fields['hidden'],
])
def test_visible_fields(self):
form = NeedsFileField()
visible_fields = [field.field for field in form.visible_fields()]
self.assertEqual(visible_fields, [
form['file'].fields['date'],
form['file'].fields['image'],
form['errors'].fields['name'],
])
def test_prefix(self):
form = ErrorMultiForm(prefix='foo')
self.assertEqual(form['errors'].prefix, 'errors__foo')
def test_cleaned_data(self):
form = UserProfileMultiForm({
'user-name': 'foo',
'profile-name': 'foo',
})
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data, OrderedDict([
('user', {
'name': 'foo',
}),
('profile', {
'name': 'foo',
'display_name': '',
}),
]))
def test_handles_none_initial_value(self):
# Used to throw an AttributeError
UserProfileMultiForm(initial=None)
def test_works_with_wizard_view(self):
url = reverse('test_wizard')
self.client.get(url)
response = self.client.post(url, {
'test_wizard_view-current_step': '0',
'profile__0-name': 'John Doe',
})
view = response.context['view']
self.assertEqual(view.storage.current_step, '1')
response = self.client.post(url, {
'test_wizard_view-current_step': '1',
'1-confirm': True,
})
form_list = response.context['form_list']
# In Django>=1.7 on Python 3, form_list is a ValuesView, which doesn't
# support indexing, you are probably recommending to use form_dict
# instead of form_list on Django>=1.7 anyway though.
form_list = list(form_list)
self.assertEqual(form_list[0]['profile'].cleaned_data['name'],
'John Doe')
def test_custom_clean_errors(self):
form = RaisesErrorCustomCleanMultiform({
'user-name': 'foo',
'profile-name': 'foo',
})
self.assertFalse(form.is_valid())
self.assertEqual(form.cleaned_data, OrderedDict([
('user', {
'name': u'foo'
}),
('profile', {
'name': u'foo',
'display_name': u'',
})
]))
self.assertEqual(form.non_field_errors().as_text(), '* It broke')
def test_custom_clean_data_change(self):
form = ModifiesDataCustomCleanMultiform({
'user-name': 'foo',
'profile-name': 'foo',
'profile-display_name': 'uncleaned name',
})
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data, OrderedDict([
('user', {
'name': u'foo'
}),
('profile', {
'name': u'foo',
'display_name': u'cleaned name',
})
]))
class MultiModelFormTest(TestCase):
def test_save(self):
form = BadgeMultiForm({
'badge1-name': 'foo',
'badge1-color': 'blue',
'badge2-name': 'bar',
'badge2-color': 'purple',
})
objects = form.save()
self.assertEqual(objects['badge1'], Badge.objects.get(name='foo'))
self.assertEqual(objects['badge2'], Badge.objects.get(name='bar'))
def test_save_m2m(self):
book1 = Book.objects.create(name='Foo')
Book.objects.create(name='Bar')
form = ManyToManyMultiForm({
'badge-name': 'badge name',
'badge-color': 'badge color',
'author-name': 'author name',
'author-books': [
book1.pk,
],
})
self.assertTrue(form.is_valid())
objects = form.save(commit=False)
objects['badge'].save()
objects['author'].save()
self.assertEqual(objects['author'].books.count(), 0)
form.save_m2m()
self.assertEqual(objects['author'].books.get(), book1)
def test_instance(self):
user = User(name='foo')
profile = Profile(display_name='bar')
# Django checks model equality by checking that the pks are the same.
# A User with no pk is the same as any other User with no pk.
user.pk = 1
profile.pk = 1
form = UserProfileMultiForm(
instance={
'user': user,
'profile': profile,
}
)
self.assertEqual(form['user'].instance, user)
self.assertEqual(form['profile'].instance, profile)
def test_model_and_non_model_forms(self):
# This tests that it is possible to instantiate a non-model form using
# the MultiModelForm class too, previously it would explode because it
# was being passed an instance parameter.
MixedForm()
def test_works_with_create_view_get(self):
viewfn = CreateView.as_view(
form_class=UserProfileMultiForm,
template_name='noop.html',
)
factory = RequestFactory()
request = factory.get('/')
# This would fail as CreateView passes instance=None
viewfn(request)
def test_works_with_create_view_post(self):
viewfn = CreateView.as_view(
form_class=BadgeMultiForm,
# required after success
success_url='/',
template_name='noop.html',
)
factory = RequestFactory()
request = factory.post('/', data={
'badge1-name': 'foo',
'badge1-color': 'blue',
'badge2-name': 'bar',
'badge2-color': 'purple',
})
resp = viewfn(request)
self.assertEqual(resp.status_code, 302)
self.assertEqual(Badge.objects.count(), 2)
def test_is_valid_with_formset(self):
form = BookMultiForm({
'book-name': 'Test',
'images-0-name': 'One',
'images-1-name': 'Two',
'images-TOTAL_FORMS': '3',
'images-INITIAL_FORMS': '0',
'images-MAX_NUM_FORMS': '1000',
})
assert form.is_valid()
def test_override_clean(self):
form = CleanedBookMultiForm({
'book-name': 'Test',
'images-0-name': 'One',
'images-1-name': 'Two',
'images-TOTAL_FORMS': '3',
'images-INITIAL_FORMS': '0',
'images-MAX_NUM_FORMS': '1000',
})
assert form.is_valid()
assert form['book'].cleaned_data['name'] == 'Overridden'
assert form['images'].forms[0].cleaned_data['name'] == 'Two'
assert form['images'].forms[1].cleaned_data['name'] == 'Three'
def test_non_field_errors_with_formset(self):
form = RaisesErrorBookMultiForm({
'book-name': '',
'images-0-name': '',
'images-TOTAL_FORMS': '3',
'images-INITIAL_FORMS': '0',
'images-MAX_NUM_FORMS': '1000',
})
# assertDoesntRaise AttributeError
self.assertEqual(form.non_field_errors().as_text(), '* It broke')