forked from theatlantic/django-nested-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
914 lines (686 loc) · 39.9 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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
import os
import tempfile
import time
from unittest import skipIf, SkipTest
import django
from nested_admin.tests.base import (
BaseNestedAdminTestCase, expected_failure_if_suit)
from .models import (
StackedGroup, StackedSection, StackedItem,
TabularGroup, TabularSection, TabularItem,
SortableWithExtraRoot, SortableWithExtraChild)
class InlineAdminTestCaseMixin(object):
@classmethod
def setUpClass(cls):
super(InlineAdminTestCaseMixin, cls).setUpClass()
cls.section_cls, cls.item_cls = cls.nested_models
def test_add_section_to_empty(self):
group = self.root_model.objects.create(slug='test')
self.load_admin(group)
self.add_inline(slug="test")
self.save_form()
sections = group.section_set.all()
self.assertEqual(len(sections), 1)
self.assertEqual(sections[0].slug, 'test')
self.assertEqual(sections[0].position, 0)
def test_add_item_to_empty(self):
group = self.root_model.objects.create(slug='test')
section = self.section_cls.objects.create(slug='test', group=group, position=0)
self.load_admin(group)
item_verbose_name = self.item_cls._meta.verbose_name.title()
with self.clickable_xpath('//a[contains(string(.), "Add another %s")]' % item_verbose_name) as el:
el.click()
with self.clickable_xpath('//input[@name="section_set-0-item_set-0-name"]') as el:
el.send_keys("Test")
self.save_form()
items = section.item_set.all()
self.assertEqual(len(items), 1)
self.assertEqual(items[0].name, "Test")
self.assertEqual(items[0].position, 0)
def test_drag_last_item_between_sections(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.drag_and_drop_item(from_indexes=[1, 2], to_indexes=[0, 1],
screenshot_hack=True)
self.save_form()
item_b_2 = self.item_cls.objects.get(name='B 2')
self.assertEqual(item_b_2.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_2.position, 1, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/B 2[1]',
'group/a[0]/A 1[2]',
'group/a[0]/A 2[3]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 1[1]'])
def test_drag_middle_item_between_sections(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.drag_and_drop_item(from_indexes=[1, 1], to_indexes=[0, 1])
self.save_form()
item_b_1 = self.item_cls.objects.get(name='B 1')
self.assertEqual(item_b_1.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_1.position, 1, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/B 1[1]',
'group/a[0]/A 1[2]',
'group/a[0]/A 2[3]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 2[1]'])
def test_drag_middle_item_between_sections_after_adding_new_item(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.add_inline(indexes=[1], name='B 3')
self.drag_and_drop_item(from_indexes=[1, 1], to_indexes=[0, 1],
screenshot_hack=True)
self.save_form()
item_b_1 = self.item_cls.objects.get(name='B 1')
self.assertEqual(item_b_1.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_1.position, 1, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/B 1[1]',
'group/a[0]/A 1[2]',
'group/a[0]/A 2[3]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 2[1]',
'group/b[1]/B 3[2]'])
def test_drag_middle_item_between_sections_after_adding_new_item_to_other_section(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.add_inline(indexes=[0], name='A 3')
self.drag_and_drop_item(from_indexes=[1, 1], to_indexes=[0, 1],
screenshot_hack=True)
self.save_form()
item_b_1 = self.item_cls.objects.get(name='B 1')
self.assertEqual(item_b_1.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_1.position, 1, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/B 1[1]',
'group/a[0]/A 1[2]',
'group/a[0]/A 2[3]',
'group/a[0]/A 3[4]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 2[1]'])
def test_drag_new_item_between_sections(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.load_admin(group)
self.add_inline(indexes=[1], name='B 2')
time.sleep(0.01)
self.drag_and_drop_item(from_indexes=[1, 2], to_indexes=[0, 1],
screenshot_hack=True)
self.save_form()
item_b_2 = self.item_cls.objects.get(name='B 2')
self.assertEqual(item_b_2.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_2.position, 1, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/B 2[1]',
'group/a[0]/A 1[2]',
'group/a[0]/A 2[3]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 1[1]'])
def test_delete_item(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.delete_inline(indexes=[1, 1])
self.save_form()
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/A 1[1]',
'group/a[0]/A 2[2]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 2[1]'])
def test_delete_section(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.delete_inline(indexes=[0])
self.save_form()
self.assertEqual(len(self.section_cls.objects.filter(slug='a')), 0, "Section was not deleted")
section_b = self.section_cls.objects.get(slug='b')
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[0]/B 0[0]',
'group/b[0]/B 1[1]',
'group/b[0]/B 2[2]'])
@expected_failure_if_suit
def test_delete_item_undelete_section(self):
"""
Test that, if an item is deleted, then the section is deleted, and
then the section is undeleted, that the item stays deleted.
"""
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.delete_inline(indexes=[0, 1])
self.delete_inline(indexes=[0])
self.undelete_inline(indexes=[0])
self.save_form()
self.assertEqual(len(self.section_cls.objects.filter(slug='a')), 1, "Section should not be deleted")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/A 2[1]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 1[1]',
'group/b[1]/B 2[2]'])
def test_remove_item(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.load_admin(group)
self.add_inline(indexes=[1], name='B 2')
self.remove_inline(indexes=[1, 2])
self.save_form()
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/A 1[1]',
'group/a[0]/A 2[2]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 1[1]'])
def test_drag_item_to_empty_section(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.drag_and_drop_item(from_indexes=[1, 2], to_indexes=[0, 0])
self.save_form()
item_b_2 = self.item_cls.objects.get(name='B 2')
self.assertEqual(item_b_2.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_2.position, 0, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')],
['group/a[0]/B 2[0]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 1[1]'])
def test_drag_item_to_first_position(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.drag_and_drop_item(from_indexes=[1, 2], to_indexes=[0, 0],
screenshot_hack=True)
self.save_form()
item_b_2 = self.item_cls.objects.get(name='B 2')
self.assertEqual(item_b_2.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_2.position, 0, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/B 2[0]',
'group/a[0]/A 0[1]',
'group/a[0]/A 1[2]',
'group/a[0]/A 2[3]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 1[1]'])
def test_drag_item_to_last_position(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
self.drag_and_drop_item(from_indexes=[1, 2], to_indexes=[0, 3])
self.save_form()
item_b_2 = self.item_cls.objects.get(name='B 2')
self.assertEqual(item_b_2.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_2.position, 3, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/A 1[1]',
'group/a[0]/A 2[2]',
'group/a[0]/B 2[3]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 0[0]',
'group/b[1]/B 1[1]'])
def test_drag_item_from_last_position(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.item_cls.objects.create(name='B 2', section=section_b, position=2)
self.load_admin(group)
# Swap top-level last with top-level first
self.drag_and_drop_item(from_indexes=[1], to_indexes=[0])
# Drag last item of (the now) last top-level to the first top-level, in last position
self.drag_and_drop_item(from_indexes=[1, 2], to_indexes=[0, 3])
self.save_form()
section_a.refresh_from_db()
section_b.refresh_from_db()
self.assertEqual(section_b.position, 0, "section b did not change position")
self.assertEqual(section_a.position, 1, "section a did not change position")
item_a_2 = self.item_cls.objects.get(name='A 2')
self.assertEqual(item_a_2.section, section_b, "item is in incorrect section")
self.assertEqual(item_a_2.position, 3, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[1]/A 0[0]',
'group/a[1]/A 1[1]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[0]/B 0[0]',
'group/b[0]/B 1[1]',
'group/b[0]/B 2[2]',
'group/b[0]/A 2[3]'])
# This test fails with the phantomjs driver on Travis, but it passes locally
# and it passes with the Chrome driver, so chalking it up to a fluke
@skipIf(django.VERSION[:2] == (1, 9), "Skipping misbehaving test on travis")
def test_drag_item_to_new_empty_section(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='A 2', section=section_a, position=2)
self.load_admin(group)
self.add_inline(slug="b")
self.drag_and_drop_item(from_indexes=[0, 2], to_indexes=[1, 0])
self.save_form()
item_a_2 = self.item_cls.objects.get(name='A 2')
section_b = self.section_cls.objects.get(slug='b')
self.assertEqual(item_a_2.section, section_b, "item was not moved to the correct section")
self.assertEqual(item_a_2.position, 0, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')],
['group/a[0]/A 0[0]', 'group/a[0]/A 1[1]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')],
['group/b[1]/A 2[0]'])
def test_position_update_bug(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.load_admin(group)
self.add_inline(indexes=[0], name='A 0')
self.add_inline(indexes=[0], name='A 1')
self.add_inline(indexes=[0], name='A 2')
# Move to second position of the first section
self.drag_and_drop_item(from_indexes=[1, 0], to_indexes=[0, 1])
# Move to the last position of the first section
self.drag_and_drop_item(from_indexes=[0, 1], to_indexes=[0, 3])
position_selector = self.get_form_field_selector('position', indexes=[0, 3])
def check_position_is_correct(d):
val = d.execute_script('return $("%s").val()' % position_selector)
return val == '3'
self.wait_until(
check_position_is_correct,
message="Timeout waiting for position to update to correct value")
self.save_form()
item_b_0 = self.item_cls.objects.get(name='B 0')
self.assertEqual(item_b_0.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_0.position, 3, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 0[0]',
'group/a[0]/A 1[1]',
'group/a[0]/A 2[2]',
'group/a[0]/B 0[3]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [])
def test_drag_existing_item_to_new_section_and_back(self):
group = self.root_model.objects.create(slug='test')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.load_admin(group)
self.add_inline(slug="b")
self.drag_and_drop_item(from_indexes=[0, 0], to_indexes=[1, 0])
self.drag_and_drop_item(from_indexes=[1, 0], to_indexes=[0, 0])
self.save_form()
self.assertEqual(len(self.section_cls.objects.all()), 2, "Save failed")
item_a_0 = self.item_cls.objects.get(name='A 0')
self.assertEqual(item_a_0.section, section_a, "Item is in the wrong section")
self.assertEqual(item_a_0.position, 0, "Item has the wrong position")
def test_drag_item_create_invalid_new_item_then_drag_back_after_validation_error_removing_invalid_item(self):
"""
Tests regression of a scenario after encountering a validation error.
Steps to reproduce:
1. Begin with at least two items in each section
2. Drag one of the items from the first section into the second
3. Create an invalid item in the first section
4. Save, encounter a validation error
5. Drag the invalid item back to the first group
6. Remove the invalid item
7. Save, get a 500 Internal Server Error
"""
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.load_admin(group)
# Drag the first item of section 'b' into section 'a'
self.drag_and_drop_item(from_indexes=[1, 0], to_indexes=[0, 1])
# Create invalid item (missing required field 'name')
self.add_inline(indexes=[1])
# We need to manually set the position to trigger the validation error,
# otherwise it will be skipped as an empty inline on save
self.set_field('position', '1', indexes=[1, 1])
# Save
self.save_form()
self.drag_and_drop_item(from_indexes=[1, 1], to_indexes=[0, 0],
screenshot_hack=True)
# Remove invalid item
self.remove_inline(indexes=[0, 0])
# Make a change to test whether save succeeds
self.set_field("name", 'A 0_changed', indexes=[0, 0])
self.save_form()
item_a_0 = self.item_cls.objects.get(section=section_a, position=0)
self.assertEqual(item_a_0.name, 'A 0_changed', 'Save failed')
def test_swap_first_two_items_between_sections(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.load_admin(group)
self.drag_and_drop_item(from_indexes=[1, 0], to_indexes=[0, 0],
screenshot_hack=True)
self.drag_and_drop_item(from_indexes=[0, 1], to_indexes=[1, 0],
screenshot_hack=True)
self.save_form()
item_b_0 = self.item_cls.objects.get(name='B 0')
self.assertEqual(item_b_0.section, section_a, "item was not moved to the correct section")
self.assertEqual(item_b_0.position, 0, "item was not moved to the correct position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/B 0[0]',
'group/a[0]/A 1[1]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/A 0[0]',
'group/b[1]/B 1[1]'])
def test_drag_first_item_to_new_section(self):
"""
Test dragging the first of several items in a pre-existing section into
a newly created section.
"""
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.load_admin(group)
self.add_inline(slug="b")
self.drag_and_drop_item(from_indexes=[0, 0], to_indexes=[1, 0])
self.save_form()
self.assertEqual(len(self.section_cls.objects.all()), 2, "Save failed")
section_b = self.section_cls.objects.get(slug='b')
item_a_0 = self.item_cls.objects.get(name='A 0')
self.assertEqual(item_a_0.section, section_b, "Item is in the wrong section")
self.assertEqual(item_a_0.position, 0, "Item has the wrong position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 1[0]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/A 0[0]'])
def test_drag_first_item_to_new_section_after_removing_item(self):
"""
Test dragging the first of several items in a pre-existing section into
a newly created section after having added two items and then removing
one of those items.
"""
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='A 1', section=section_a, position=1)
self.load_admin(group)
self.add_inline(slug="b")
self.add_inline(indexes=[1], name='B 0')
self.add_inline(indexes=[1], name='B 1')
self.remove_inline(indexes=[1, 0])
self.drag_and_drop_item(from_indexes=[0, 0], to_indexes=[1, 0])
self.save_form()
self.assertEqual(len(self.section_cls.objects.all()), 2, "Save failed")
section_b = self.section_cls.objects.get(slug='b')
item_a_0 = self.item_cls.objects.get(name='A 0')
item_a_1 = self.item_cls.objects.get(name='A 1')
item_b_1 = self.item_cls.objects.get(name='B 1')
self.assertNotEqual(item_a_0.section, section_a, "A0 did not move to new section")
self.assertEqual(item_a_0.position, 0, "A0 has the wrong position")
self.assertEqual(item_a_1.position, 0, "A1 has the wrong position")
self.assertEqual(item_b_1.position, 1, "B1 has the wrong position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/A 1[0]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/A 0[0]', 'group/b[1]/B 1[1]'])
def test_add_remove_items_in_new_section_dragging_existing_items(self):
"""
Tests for a regression that could be reproduced with the following steps:
1. Begin with one section, with at least one item in it.
2. Create a new section
3. Create three items in the new section
4. Remove the first of the new items
5. Drag the first of the existing items into the first position in the
new section.
6. Remove the second item in the new section
8. Save
Expected outcome:
The dragged item from the existing section should have been moved
to the new section.
Outcome with bug:
The item has not moved.
"""
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.load_admin(group)
self.add_inline(slug="b")
self.add_inline(indexes=[1], name='B 0')
self.add_inline(indexes=[1], name='B 1')
self.add_inline(indexes=[1], name='B 2')
self.remove_inline(indexes=[1, 0])
self.drag_and_drop_item(from_indexes=[0, 0], to_indexes=[1, 0])
self.remove_inline(indexes=[1, 1])
self.save_form()
section_b = self.section_cls.objects.get(slug='b')
item_a_0 = self.item_cls.objects.get(name='A 0')
item_b_2 = self.item_cls.objects.get(name='B 2')
self.assertNotEqual(item_a_0.section, section_a, "A0 did not move to new section")
self.assertEqual(item_a_0.position, 0, "A0 has the wrong position")
self.assertEqual(item_b_2.position, 1, "B2 has the wrong position")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/A 0[0]', 'group/b[1]/B 2[1]'])
def test_delete_section_after_dragging_item_away(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.load_admin(group)
# Drag the first item of section 'b' into section 'a'
self.drag_and_drop_item(from_indexes=[1, 0], to_indexes=[0, 0])
# Delete section 'b'
self.delete_inline(indexes=[1])
self.save_form()
self.assertNotEqual(len(self.section_cls.objects.all()), 2, "Save failed")
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/B 0[0]', 'group/a[0]/A 0[1]'])
def test_delete_undelete_section_after_dragging_item_away(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
section_b = self.section_cls.objects.create(slug='b', group=group, position=1)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.item_cls.objects.create(name='B 0', section=section_b, position=0)
self.item_cls.objects.create(name='B 1', section=section_b, position=1)
self.load_admin(group)
# Drag the first item of section 'b' into section 'a'
self.drag_and_drop_item(from_indexes=[1, 0], to_indexes=[0, 0])
# Delete section 'b'
self.delete_inline(indexes=[1])
self.undelete_inline(indexes=[1])
self.save_form()
self.assertEqual(len(self.section_cls.objects.all()), 2)
self.assertEqual(["%s" % i for i in section_a.item_set.all().order_by('position')], [
'group/a[0]/B 0[0]', 'group/a[0]/A 0[1]'])
self.assertEqual(["%s" % i for i in section_b.item_set.all().order_by('position')], [
'group/b[1]/B 1[0]'])
def test_drag_into_new_section_after_adding_and_removing_preceding_section(self):
group = self.root_model.objects.create(slug='group')
section_a = self.section_cls.objects.create(slug='a', group=group, position=0)
self.item_cls.objects.create(name='A 0', section=section_a, position=0)
self.load_admin(group)
self.add_inline(slug="b")
self.add_inline(slug="c")
self.remove_inline(indexes=[1])
self.drag_and_drop_item(from_indexes=[0, 0], to_indexes=[1, 0])
self.save_form()
self.assertEqual(len(self.section_cls.objects.all()), 2, "Save failed (new section wasn't added)")
item_a0 = self.item_cls.objects.get(name='A 0')
section_c = self.section_cls.objects.get(slug='c')
self.assertEqual(item_a0.section, section_c, "Item was not moved to new section")
class TestStackedInlineAdmin(InlineAdminTestCaseMixin, BaseNestedAdminTestCase):
root_model = StackedGroup
nested_models = (StackedSection, StackedItem)
def test_add_item_inline_label_update(self):
if self.has_grappelli:
raise SkipTest("Test does not apply if using django-grappelli")
if self.has_suit:
raise SkipTest("Test does not apply if using django-suit")
group = self.root_model.objects.create(slug='test')
self.section_cls.objects.create(slug='test', group=group, position=0)
self.load_admin(group)
item_verbose_name = self.item_cls._meta.verbose_name.title()
with self.clickable_xpath('//a[contains(string(.), "Add another %s")]' % item_verbose_name) as el:
el.click()
with self.clickable_xpath('//input[@name="section_set-0-item_set-0-name"]') as el:
el.send_keys("Test 1")
with self.clickable_xpath('//a[contains(string(.), "Add another %s")]' % item_verbose_name) as el:
el.click()
with self.clickable_xpath('//input[@name="section_set-0-item_set-0-name"]') as el:
el.send_keys("Test 2")
inline_label = self.get_item([0, 1]).find_element_by_class_name('inline_label')
self.assertEqual(inline_label.text, '#2')
def test_upload_file(self):
group = self.root_model.objects.create(slug='group')
self.load_admin(group)
self.add_inline(slug="a")
self.add_inline(indexes=[0], name='A 0')
fd, path = tempfile.mkstemp()
try:
with os.fdopen(fd, 'w') as tmp:
tmp.write('Test file. Used as a payload for testing file uploads.')
with self.clickable_xpath('//input[@name="section_set-0-item_set-0-upload"]') as el:
el.send_keys(path)
self.save_form()
finally:
os.remove(path)
item_a_0 = self.item_cls.objects.get(name='A 0')
upload_name = 'foo/' + os.path.basename(path)
self.assertEqual(item_a_0.upload.name, upload_name, 'File upload failed')
class TestTabularInlineAdmin(InlineAdminTestCaseMixin, BaseNestedAdminTestCase):
root_model = TabularGroup
nested_models = (TabularSection, TabularItem)
class TestSortablesWithExtra(BaseNestedAdminTestCase):
root_model = SortableWithExtraRoot
nested_models = (SortableWithExtraChild, )
def test_blank_extra_inlines_validation(self):
root = self.root_model.objects.create(slug='a')
self.load_admin(root)
self.set_field('slug', 'b')
self.save_form()
validation_errors = self.selenium.execute_script(
"return $('ul.errorlist li').length")
self.assertEqual(validation_errors, 0, "Unexpected validation errors encountered")
def test_blank_extra_inlines_validation_with_change(self):
root = self.root_model.objects.create(slug='a')
self.load_admin(root)
self.set_field('slug', 'b')
self.set_field('slug', 'a', indexes=[0])
self.save_form()
validation_errors = self.selenium.execute_script(
"return $('ul.errorlist li').length")
self.assertEqual(validation_errors, 0, "Unexpected validation errors encountered")
# refetch from the database
root = self.root_model.objects.get(pk=root.pk)
self.assertEqual(root.slug, 'b', "Root slug did not change")
children = self.nested_models[0].objects.all()
self.assertNotEqual(len(children), 0, "Child object did not save")
self.assertEqual(len(children), 1, "Incorrect number of children saved")
self.assertEqual(children[0].slug, "a", "Child slug incorrect")