Source

django-selectable / selectable / tests / functests.py

Full commit
  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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
"""
Larger functional tests for fields and widgets.
"""
from __future__ import unicode_literals

from django import forms

from selectable.forms import AutoCompleteSelectField, AutoCompleteSelectMultipleField
from selectable.forms import AutoCompleteSelectWidget, AutoComboboxSelectWidget
from selectable.tests import ManyThing, OtherThing, ThingLookup
from selectable.tests.base import BaseSelectableTestCase, parsed_inputs


__all__ = (
    'FuncAutoCompleteSelectTestCase',
    'FuncSelectModelChoiceTestCase',
    'FuncComboboxModelChoiceTestCase',
    'FuncManytoManyMultipleSelectTestCase',
    'FuncFormTestCase',
)


class OtherThingForm(forms.ModelForm):

    thing = AutoCompleteSelectField(lookup_class=ThingLookup)

    class Meta(object):
        model = OtherThing


class FuncAutoCompleteSelectTestCase(BaseSelectableTestCase):

    def setUp(self):
        self.test_thing = self.create_thing()

    def test_valid_form(self):
        "Valid form using an AutoCompleteSelectField."
        data = {
            'name': self.get_random_string(),
            'thing_0': self.test_thing.name, # Text input
            'thing_1': self.test_thing.pk, # Hidden input
        }
        form = OtherThingForm(data=data)
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_invalid_form_missing_selected_pk(self):
        "Invalid form using an AutoCompleteSelectField."
        data = {
            'name': self.get_random_string(),
            'thing_0': self.test_thing.name, # Text input
            'thing_1': '', # Hidden input
        }
        form = OtherThingForm(data=data)
        self.assertFalse(form.is_valid(), 'Form should not be valid')
        self.assertFalse('name' in form.errors)
        self.assertTrue('thing' in form.errors)

    def test_invalid_form_missing_name(self):
        "Invalid form using an AutoCompleteSelectField."
        data = {
            'name': '',
            'thing_0': self.test_thing.name, # Text input
            'thing_1': self.test_thing.pk, # Hidden input
        }
        form = OtherThingForm(data=data)
        self.assertFalse(form.is_valid(), 'Form should not be valid')
        self.assertTrue('name' in form.errors)
        self.assertFalse('thing' in form.errors)

    def test_invalid_but_still_selected(self):
        "Invalid form should keep selected item."
        data = {
            'name': '',
            'thing_0': self.test_thing.name, # Text input
            'thing_1': self.test_thing.pk, # Hidden input
        }
        form = OtherThingForm(data=data)
        self.assertFalse(form.is_valid(), 'Form should not be valid')
        rendered_form = form.as_p()
        inputs = parsed_inputs(rendered_form)
        # Selected text should be populated
        thing_0 = inputs['thing_0'][0]
        self.assertEqual(thing_0.attributes['value'].value, self.test_thing.name)
        # Selected pk should be populated
        thing_1 = inputs['thing_1'][0]
        self.assertEqual(int(thing_1.attributes['value'].value), self.test_thing.pk)

    def test_populate_from_model(self):
        "Populate from existing model."
        other_thing = OtherThing.objects.create(thing=self.test_thing, name='a')
        form = OtherThingForm(instance=other_thing)
        rendered_form = form.as_p()
        inputs = parsed_inputs(rendered_form)
        # Selected text should be populated
        thing_0 = inputs['thing_0'][0]
        self.assertEqual(thing_0.attributes['value'].value, self.test_thing.name)
        # Selected pk should be populated
        thing_1 = inputs['thing_1'][0]
        self.assertEqual(int(thing_1.attributes['value'].value), self.test_thing.pk)


class SelectWidgetForm(forms.ModelForm):

    class Meta(object):
        model = OtherThing
        widgets = {
            'thing': AutoCompleteSelectWidget(lookup_class=ThingLookup)
        }


class FuncSelectModelChoiceTestCase(BaseSelectableTestCase):
    """
    Functional tests for AutoCompleteSelectWidget compatibility
    with a ModelChoiceField.
    """

    def setUp(self):
        self.test_thing = self.create_thing()

    def test_valid_form(self):
        "Valid form using an AutoCompleteSelectWidget."
        data = {
            'name': self.get_random_string(),
            'thing_0': self.test_thing.name, # Text input
            'thing_1': self.test_thing.pk, # Hidden input
        }
        form = SelectWidgetForm(data=data)
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_missing_pk(self):
        "Invalid form (missing required pk) using an AutoCompleteSelectWidget."
        data = {
            'name': self.get_random_string(),
            'thing_0': self.test_thing.name, # Text input
            'thing_1': '', # Hidden input missing
        }
        form = SelectWidgetForm(data=data)
        self.assertFalse(form.is_valid())
        self.assertTrue('thing' in form.errors)

    def test_invalid_pk(self):
        "Invalid form (invalid pk value) using an AutoCompleteSelectWidget."
        data = {
            'name': self.get_random_string(),
            'thing_0': self.test_thing.name, # Text input
            'thing_1': 'XXX', # Hidden input doesn't match a PK
        }
        form = SelectWidgetForm(data=data)
        self.assertFalse(form.is_valid())
        self.assertTrue('thing' in form.errors)


class ComboboxSelectWidgetForm(forms.ModelForm):

    class Meta(object):
        model = OtherThing
        widgets = {
            'thing': AutoComboboxSelectWidget(lookup_class=ThingLookup)
        }


class FuncComboboxModelChoiceTestCase(BaseSelectableTestCase):
    """
    Functional tests for AutoComboboxSelectWidget compatibility
    with a ModelChoiceField.
    """

    def setUp(self):
        self.test_thing = self.create_thing()

    def test_valid_form(self):
        "Valid form using an AutoComboboxSelectWidget."
        data = {
            'name': self.get_random_string(),
            'thing_0': self.test_thing.name, # Text input
            'thing_1': self.test_thing.pk, # Hidden input
        }
        form = ComboboxSelectWidgetForm(data=data)
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_missing_pk(self):
        "Invalid form (missing required pk) using an AutoComboboxSelectWidget."
        data = {
            'name': self.get_random_string(),
            'thing_0': self.test_thing.name, # Text input
            'thing_1': '', # Hidden input missing
        }
        form = SelectWidgetForm(data=data)
        self.assertFalse(form.is_valid())
        self.assertTrue('thing' in form.errors)

    def test_invalid_pk(self):
        "Invalid form (invalid pk value) using an AutoComboboxSelectWidget."
        data = {
            'name': self.get_random_string(),
            'thing_0': self.test_thing.name, # Text input
            'thing_1': 'XXX', # Hidden input doesn't match a PK
        }
        form = SelectWidgetForm(data=data)
        self.assertFalse(form.is_valid())
        self.assertTrue('thing' in form.errors)


class ManyThingForm(forms.ModelForm):

    things = AutoCompleteSelectMultipleField(lookup_class=ThingLookup)

    class Meta(object):
        model = ManyThing


class FuncManytoManyMultipleSelectTestCase(BaseSelectableTestCase):
    """
    Functional tests for AutoCompleteSelectMultipleField compatibility
    with a ManyToManyField.
    """

    def setUp(self):
        self.test_thing = self.create_thing()

    def test_valid_form(self):
        "Valid form using an AutoCompleteSelectMultipleField."
        data = {
            'name': self.get_random_string(),
            'things_0': '', # Text input
            'things_1': [self.test_thing.pk, ], # Hidden inputs
        }
        form = ManyThingForm(data=data)
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_valid_save(self):
        "Saving data from a valid form."
        data = {
            'name': self.get_random_string(),
            'things_0': '', # Text input
            'things_1': [self.test_thing.pk, ], # Hidden inputs
        }
        form = ManyThingForm(data=data)
        manything = form.save()
        self.assertEqual(manything.name, data['name'])
        things = manything.things.all()
        self.assertEqual(things.count(), 1)
        self.assertTrue(self.test_thing in things)

    def test_not_required(self):
        "Valid form where many to many is not required."
        data = {
            'name': self.get_random_string(),
            'things_0': '', # Text input
            'things_1': [], # Hidden inputs
        }
        form = ManyThingForm(data=data)
        form.fields['things'].required = False
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_not_required_save(self):
        "Saving data when many to many is not required."
        data = {
            'name': self.get_random_string(),
            'things_0': '', # Text input
            'things_1': [], # Hidden inputs
        }
        form = ManyThingForm(data=data)
        form.fields['things'].required = False
        manything = form.save()
        self.assertEqual(manything.name, data['name'])
        things = manything.things.all()
        self.assertEqual(things.count(), 0)

    def test_has_changed(self):
        "Populate intial data from a model."
        manything = ManyThing.objects.create(name='Foo')
        thing_1 = self.create_thing()
        manything.things.add(thing_1)
        data = {
            'name': manything.name,
            'things_0': '', # Text input
            'things_1': [thing_1.pk], # Hidden inputs
        }
        form = ManyThingForm(data=data, instance=manything)
        self.assertFalse(form.has_changed(), str(form.changed_data))


class SimpleForm(forms.Form):
    "Non-model form usage."
    thing = AutoCompleteSelectField(lookup_class=ThingLookup)
    new_thing = AutoCompleteSelectField(lookup_class=ThingLookup, allow_new=True)
    things = AutoCompleteSelectMultipleField(lookup_class=ThingLookup)


class FuncFormTestCase(BaseSelectableTestCase):
    """
    Functional tests for using AutoCompleteSelectField
    and AutoCompleteSelectMultipleField outside the context
    of a ModelForm.
    """

    def setUp(self):
        self.test_thing = self.create_thing()

    def test_blank_new_item(self):
        "Regression test for #91. new_thing is required but both are blank."
        data = {
            'thing_0': self.test_thing.name,
            'thing_1': self.test_thing.pk,
            'new_thing_0': '',
            'new_thing_1': '',
            'things_0': '',
            'things_1': [self.test_thing.pk, ]
        }
        form = SimpleForm(data=data)
        self.assertFalse(form.is_valid())
        self.assertTrue('new_thing' in form.errors)

    def test_has_changed_with_empty_permitted(self):
        """
        Regression test for #92. has_changed fails when there is no initial and
        allow_new=False.
        """
        data = {
            'thing_0': '',
            'thing_1': self.test_thing.pk,
            'new_thing_0': self.test_thing.name,
            'new_thing_1': self.test_thing.pk,
            'things_0': '',
            'things_1': [self.test_thing.pk, ]
        }
        form = SimpleForm(data=data, empty_permitted=True)
        self.assertTrue(form.has_changed())
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_not_changed(self):
        """
        Regression test for #92. has_changed fails when there is no initial and
        allow_new=False.
        """
        data = {
            'thing_0': self.test_thing.name,
            'thing_1': self.test_thing.pk,
            'new_thing_0': self.test_thing.name,
            'new_thing_1': self.test_thing.pk,
            'things_0': '',
            'things_1': [self.test_thing.pk, ]
        }
        initial = {
            'thing': self.test_thing.pk,
            'new_thing': self.test_thing.pk,
            'things': [self.test_thing.pk, ]
        }
        form = SimpleForm(data=data, initial=initial)
        self.assertFalse(form.has_changed())
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_not_changed_with_empty_permitted(self):
        """
        Regression test for #92. has_changed fails when there is no initial and
        allow_new=False.
        """
        data = {
            'thing_0': '',
            'thing_1': '',
            'new_thing_0': '',
            'new_thing_1': '',
            'things_0': '',
            'things_1': '',
        }
        initial = {
            'thing': '',
            'new_thing': '',
            'things': '',
        }
        form = SimpleForm(data=data, initial=initial, empty_permitted=True)
        self.assertFalse(form.has_changed(), str(form.changed_data))
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_no_initial_with_empty_permitted(self):
        """
        If empty data is submitted and allowed with no initial then
        the form should not be seen as changed.
        """
        data = {
            'thing_0': '',
            'thing_1': '',
            'new_thing_0': '',
            'new_thing_1': '',
            'things_0': '',
            'things_1': '',
        }
        form = SimpleForm(data=data, empty_permitted=True)
        self.assertFalse(form.has_changed(), str(form.changed_data))
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_no_data_with_empty_permitted(self):
        """
        If no data is submitted and allowed with no initial then
        the form should not be seen as changed.
        """
        form = SimpleForm(data={}, empty_permitted=True)
        self.assertFalse(form.has_changed(), str(form.changed_data))
        self.assertTrue(form.is_valid(), str(form.errors))

    def test_select_multiple_changed(self):
        """
        Detect changes for a multiple select input with and without
        initial data.
        """
        data = {
            'thing_0': '',
            'thing_1': '',
            'new_thing_0': '',
            'new_thing_1': '',
            'things_0': '',
            'things_1': [self.test_thing.pk, ]
        }
        form = SimpleForm(data=data)
        self.assertTrue(form.has_changed())
        self.assertTrue('things' in form.changed_data)

        initial = {
            'thing': '',
            'new_thing': '',
            'things': [self.test_thing.pk, ],
        }
        form = SimpleForm(data=data, initial=initial)
        self.assertFalse(form.has_changed(), str(form.changed_data))

        initial = {
            'thing': '',
            'new_thing': '',
            'things': [],
        }
        form = SimpleForm(data=data, initial=initial)
        self.assertTrue(form.has_changed())
        self.assertTrue('things' in form.changed_data)

    def test_single_select_changed(self):
        """
        Detect changes for a single select input with and without
        initial data.
        """
        data = {
            'thing_0': '',
            'thing_1': self.test_thing.pk,
            'new_thing_0': '',
            'new_thing_1': '',
            'things_0': '',
            'things_1': ''
        }
        form = SimpleForm(data=data)
        self.assertTrue(form.has_changed())
        self.assertTrue('thing' in form.changed_data)

        initial = {
            'thing': self.test_thing.pk,
            'new_thing': '',
            'things': '',
        }
        form = SimpleForm(data=data, initial=initial)
        self.assertFalse(form.has_changed(), str(form.changed_data))

        initial = {
            'thing': '',
            'new_thing': '',
            'things': '',
        }
        form = SimpleForm(data=data, initial=initial)
        self.assertTrue(form.has_changed())
        self.assertTrue('thing' in form.changed_data)

    def test_new_select_changed(self):
        """
        Detect changes for a single select input which allows new items
        with and without initial data.
        """
        data = {
            'thing_0': '',
            'thing_1': '',
            'new_thing_0': 'Foo',
            'new_thing_1': '',
            'things_0': '',
            'things_1': ''
        }
        form = SimpleForm(data=data)
        self.assertTrue(form.has_changed())
        self.assertTrue('new_thing' in form.changed_data)

        initial = {
            'thing': '',
            'new_thing': ['Foo', None],
            'things': '',
        }
        form = SimpleForm(data=data, initial=initial)
        self.assertFalse(form.has_changed(), str(form.changed_data))

        initial = {
            'thing': '',
            'new_thing': '',
            'things': '',
        }
        form = SimpleForm(data=data, initial=initial)
        self.assertTrue(form.has_changed())
        self.assertTrue('new_thing' in form.changed_data)