Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Expanded tests added when fixing #14529.

To make sure changes in 35d1cd0 don't break anything. Refs #19505.

ad769ef from master.
  • Loading branch information...
commit 3b32e5cc2d4b6db22148cd7e51cdb735aeb25b13 1 parent 3ad34c2
Ramiro Morales authored December 24, 2012
44  tests/regressiontests/admin_views/admin.py
@@ -27,7 +27,8 @@
27 27
     Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
28 28
     AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
29 29
     AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
30  
-    RelatedPrepopulated, UndeletableObject, UserMessenger, Simple, Choice)
  30
+    RelatedPrepopulated, UndeletableObject, UserMessenger, Simple, Choice,
  31
+    ShortMessage, Telegram)
31 32
 
32 33
 
33 34
 def callable_year(dt_value):
@@ -439,25 +440,54 @@ class FoodDeliveryAdmin(admin.ModelAdmin):
439 440
     list_editable = ('driver', 'restaurant')
440 441
 
441 442
 
  443
+class CoverLetterAdmin(admin.ModelAdmin):
  444
+    """
  445
+    A ModelAdmin with a custom queryset() method that uses defer(), to test
  446
+    verbose_name display in messages shown after adding/editing CoverLetter
  447
+    instances.
  448
+    Note that the CoverLetter model defines a __unicode__ method.
  449
+    For testing fix for ticket #14529.
  450
+    """
  451
+
  452
+    def queryset(self, request):
  453
+        return super(CoverLetterAdmin, self).queryset(request).defer('date_written')
  454
+
  455
+
442 456
 class PaperAdmin(admin.ModelAdmin):
443 457
     """
444 458
     A ModelAdmin with a custom queryset() method that uses only(), to test
445  
-    verbose_name display in messages shown after adding Paper instances.
  459
+    verbose_name display in messages shown after adding/editing Paper
  460
+    instances.
  461
+    For testing fix for ticket #14529.
446 462
     """
447 463
 
448 464
     def queryset(self, request):
449 465
         return super(PaperAdmin, self).queryset(request).only('title')
450 466
 
451 467
 
452  
-class CoverLetterAdmin(admin.ModelAdmin):
  468
+class ShortMessageAdmin(admin.ModelAdmin):
  469
+    """
  470
+    A ModelAdmin with a custom queryset() method that uses defer(), to test
  471
+    verbose_name display in messages shown after adding/editing ShortMessage
  472
+    instances.
  473
+    For testing fix for ticket #14529.
  474
+    """
  475
+
  476
+    def queryset(self, request):
  477
+        return super(ShortMessageAdmin, self).queryset(request).defer('timestamp')
  478
+
  479
+
  480
+class TelegramAdmin(admin.ModelAdmin):
453 481
     """
454 482
     A ModelAdmin with a custom queryset() method that uses only(), to test
455  
-    verbose_name display in messages shown after adding CoverLetter instances.
456  
-    Note that the CoverLetter model defines a __unicode__ method.
  483
+    verbose_name display in messages shown after adding/editing Telegram
  484
+    instances.
  485
+    Note that the Telegram model defines a __unicode__ method.
  486
+    For testing fix for ticket #14529.
457 487
     """
458 488
 
459 489
     def queryset(self, request):
460  
-        return super(CoverLetterAdmin, self).queryset(request).defer('date_written')
  490
+        return super(TelegramAdmin, self).queryset(request).only('title')
461 491
 
462 492
 
463 493
 class StoryForm(forms.ModelForm):
@@ -665,6 +695,8 @@ class ChoiceList(admin.ModelAdmin):
665 695
 site.register(RowLevelChangePermissionModel, RowLevelChangePermissionModelAdmin)
666 696
 site.register(Paper, PaperAdmin)
667 697
 site.register(CoverLetter, CoverLetterAdmin)
  698
+site.register(ShortMessage, ShortMessageAdmin)
  699
+site.register(Telegram, TelegramAdmin)
668 700
 site.register(Story, StoryAdmin)
669 701
 site.register(OtherStory, OtherStoryAdmin)
670 702
 site.register(Report, ReportAdmin)
22  tests/regressiontests/admin_views/models.py
@@ -548,18 +548,32 @@ class Meta:
548 548
         unique_together = (("driver", "restaurant"),)
549 549
 
550 550
 
  551
+@python_2_unicode_compatible
  552
+class CoverLetter(models.Model):
  553
+    author = models.CharField(max_length=30)
  554
+    date_written = models.DateField(null=True, blank=True)
  555
+
  556
+    def __str__(self):
  557
+        return self.author
  558
+
  559
+
551 560
 class Paper(models.Model):
552 561
     title = models.CharField(max_length=30)
553 562
     author = models.CharField(max_length=30, blank=True, null=True)
554 563
 
555 564
 
  565
+class ShortMessage(models.Model):
  566
+    content = models.CharField(max_length=140)
  567
+    timestamp = models.DateTimeField(null=True, blank=True)
  568
+
  569
+
556 570
 @python_2_unicode_compatible
557  
-class CoverLetter(models.Model):
558  
-    author = models.CharField(max_length=30)
559  
-    date_written = models.DateField(null=True, blank=True)
  571
+class Telegram(models.Model):
  572
+    title = models.CharField(max_length=30)
  573
+    date_sent = models.DateField(null=True, blank=True)
560 574
 
561 575
     def __str__(self):
562  
-        return self.author
  576
+        return self.title
563 577
 
564 578
 
565 579
 class Story(models.Model):
163  tests/regressiontests/admin_views/tests.py
@@ -47,7 +47,7 @@
47 47
     OtherStory, ComplexSortedPerson, Parent, Child, AdminOrderedField,
48 48
     AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
49 49
     Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
50  
-    Simple, UndeletableObject, Choice)
  50
+    Simple, UndeletableObject, Choice, ShortMessage, Telegram)
51 51
 
52 52
 
53 53
 ERROR_MESSAGE = "Please enter the correct username and password \
@@ -2505,27 +2505,93 @@ def test_change_view(self):
2505 2505
             else:
2506 2506
                 self.assertEqual(response.status_code, 404)
2507 2507
 
  2508
+    def test_add_model_modeladmin_defer_qs(self):
  2509
+        # Test for #14529. defer() is used in ModelAdmin.queryset()
  2510
+
  2511
+        # model has __unicode__ method
  2512
+        self.assertEqual(CoverLetter.objects.count(), 0)
  2513
+        # Emulate model instance creation via the admin
  2514
+        post_data = {
  2515
+            "author": "Candidate, Best",
  2516
+            "_save": "Save",
  2517
+        }
  2518
+        response = self.client.post('/test_admin/admin/admin_views/coverletter/add/',
  2519
+                                    post_data, follow=True)
  2520
+        self.assertEqual(response.status_code, 200)
  2521
+        self.assertEqual(CoverLetter.objects.count(), 1)
  2522
+        # Message should contain non-ugly model verbose name
  2523
+        self.assertContains(
  2524
+            response,
  2525
+            '<li class="info">The cover letter &quot;Candidate, Best&quot; was added successfully.</li>',
  2526
+            html=True
  2527
+        )
  2528
+
  2529
+        # model has no __unicode__ method
  2530
+        self.assertEqual(ShortMessage.objects.count(), 0)
  2531
+        # Emulate model instance creation via the admin
  2532
+        post_data = {
  2533
+            "content": "What's this SMS thing?",
  2534
+            "_save": "Save",
  2535
+        }
  2536
+        response = self.client.post('/test_admin/admin/admin_views/shortmessage/add/',
  2537
+                post_data, follow=True)
  2538
+        self.assertEqual(response.status_code, 200)
  2539
+        self.assertEqual(ShortMessage.objects.count(), 1)
  2540
+        # Message should contain non-ugly model verbose name
  2541
+        self.assertContains(
  2542
+            response,
  2543
+            '<li class="info">The short message &quot;ShortMessage object&quot; was added successfully.</li>',
  2544
+            html=True
  2545
+        )
  2546
+
2508 2547
     def test_add_model_modeladmin_only_qs(self):
2509  
-        # only() is used in ModelAdmin.queryset()
2510  
-        p = Paper.objects.create(title="My Paper Title")
2511  
-        self.assertEqual(Paper.objects.count(), 1)
2512  
-        response = self.client.get('/test_admin/admin/admin_views/paper/%s/' % p.pk)
  2548
+        # Test for #14529. only() is used in ModelAdmin.queryset()
  2549
+
  2550
+        # model has __unicode__ method
  2551
+        self.assertEqual(Telegram.objects.count(), 0)
  2552
+        # Emulate model instance creation via the admin
  2553
+        post_data = {
  2554
+            "title": "Urgent telegram",
  2555
+            "_save": "Save",
  2556
+        }
  2557
+        response = self.client.post('/test_admin/admin/admin_views/telegram/add/',
  2558
+                post_data, follow=True)
2513 2559
         self.assertEqual(response.status_code, 200)
  2560
+        self.assertEqual(Telegram.objects.count(), 1)
  2561
+        # Message should contain non-ugly model verbose name
  2562
+        self.assertContains(
  2563
+            response,
  2564
+            '<li class="info">The telegram &quot;Urgent telegram&quot; was added successfully.</li>',
  2565
+            html=True
  2566
+        )
  2567
+
  2568
+        # model has no __unicode__ method
  2569
+        self.assertEqual(Paper.objects.count(), 0)
  2570
+        # Emulate model instance creation via the admin
2514 2571
         post_data = {
2515 2572
             "title": "My Modified Paper Title",
2516 2573
             "_save": "Save",
2517 2574
         }
2518  
-        response = self.client.post('/test_admin/admin/admin_views/paper/%s/' % p.pk,
  2575
+        response = self.client.post('/test_admin/admin/admin_views/paper/add/',
2519 2576
                 post_data, follow=True)
2520 2577
         self.assertEqual(response.status_code, 200)
2521  
-        # Message should contain non-ugly model name. Instance representation is set by six.text_type() (ugly)
2522  
-        self.assertContains(response, '<li class="info">The paper &quot;Paper_Deferred_author object&quot; was changed successfully.</li>', html=True)
  2578
+        self.assertEqual(Paper.objects.count(), 1)
  2579
+        # Message should contain non-ugly model verbose name
  2580
+        self.assertContains(
  2581
+            response,
  2582
+            '<li class="info">The paper &quot;Paper object&quot; was added successfully.</li>',
  2583
+            html=True
  2584
+        )
  2585
+
  2586
+    def test_edit_model_modeladmin_defer_qs(self):
  2587
+        # Test for #14529. defer() is used in ModelAdmin.queryset()
2523 2588
 
2524  
-        # defer() is used in ModelAdmin.queryset()
  2589
+        # model has __unicode__ method
2525 2590
         cl = CoverLetter.objects.create(author="John Doe")
2526 2591
         self.assertEqual(CoverLetter.objects.count(), 1)
2527 2592
         response = self.client.get('/test_admin/admin/admin_views/coverletter/%s/' % cl.pk)
2528 2593
         self.assertEqual(response.status_code, 200)
  2594
+        # Emulate model instance edit via the admin
2529 2595
         post_data = {
2530 2596
             "author": "John Doe II",
2531 2597
             "_save": "Save",
@@ -2533,8 +2599,83 @@ def test_add_model_modeladmin_only_qs(self):
2533 2599
         response = self.client.post('/test_admin/admin/admin_views/coverletter/%s/' % cl.pk,
2534 2600
                 post_data, follow=True)
2535 2601
         self.assertEqual(response.status_code, 200)
2536  
-        # Message should contain non-ugly model name. Instance representation is set by model's __unicode__()
2537  
-        self.assertContains(response, '<li class="info">The cover letter &quot;John Doe II&quot; was changed successfully.</li>', html=True)
  2602
+        self.assertEqual(CoverLetter.objects.count(), 1)
  2603
+        # Message should contain non-ugly model verbose name. Instance
  2604
+        # representation is set by model's __unicode__()
  2605
+        self.assertContains(
  2606
+            response,
  2607
+            '<li class="info">The cover letter &quot;John Doe II&quot; was changed successfully.</li>',
  2608
+            html=True
  2609
+        )
  2610
+
  2611
+        # model has no __unicode__ method
  2612
+        sm = ShortMessage.objects.create(content="This is expensive")
  2613
+        self.assertEqual(ShortMessage.objects.count(), 1)
  2614
+        response = self.client.get('/test_admin/admin/admin_views/shortmessage/%s/' % sm.pk)
  2615
+        self.assertEqual(response.status_code, 200)
  2616
+        # Emulate model instance edit via the admin
  2617
+        post_data = {
  2618
+            "content": "Too expensive",
  2619
+            "_save": "Save",
  2620
+        }
  2621
+        response = self.client.post('/test_admin/admin/admin_views/shortmessage/%s/' % sm.pk,
  2622
+                post_data, follow=True)
  2623
+        self.assertEqual(response.status_code, 200)
  2624
+        self.assertEqual(ShortMessage.objects.count(), 1)
  2625
+        # Message should contain non-ugly model verbose name. The ugly(!)
  2626
+        # instance representation is set by six.text_type()
  2627
+        self.assertContains(
  2628
+            response,
  2629
+            '<li class="info">The short message &quot;ShortMessage_Deferred_timestamp object&quot; was changed successfully.</li>',
  2630
+            html=True
  2631
+        )
  2632
+
  2633
+    def test_edit_model_modeladmin_only_qs(self):
  2634
+        # Test for #14529. only() is used in ModelAdmin.queryset()
  2635
+
  2636
+        # model has __unicode__ method
  2637
+        t = Telegram.objects.create(title="Frist Telegram")
  2638
+        self.assertEqual(Telegram.objects.count(), 1)
  2639
+        response = self.client.get('/test_admin/admin/admin_views/telegram/%s/' % t.pk)
  2640
+        self.assertEqual(response.status_code, 200)
  2641
+        # Emulate model instance edit via the admin
  2642
+        post_data = {
  2643
+            "title": "Telegram without typo",
  2644
+            "_save": "Save",
  2645
+        }
  2646
+        response = self.client.post('/test_admin/admin/admin_views/telegram/%s/' % t.pk,
  2647
+                post_data, follow=True)
  2648
+        self.assertEqual(response.status_code, 200)
  2649
+        self.assertEqual(Telegram.objects.count(), 1)
  2650
+        # Message should contain non-ugly model verbose name. The instance
  2651
+        # representation is set by model's __unicode__()
  2652
+        self.assertContains(
  2653
+            response,
  2654
+            '<li class="info">The telegram &quot;Telegram without typo&quot; was changed successfully.</li>',
  2655
+            html=True
  2656
+        )
  2657
+
  2658
+        # model has no __unicode__ method
  2659
+        p = Paper.objects.create(title="My Paper Title")
  2660
+        self.assertEqual(Paper.objects.count(), 1)
  2661
+        response = self.client.get('/test_admin/admin/admin_views/paper/%s/' % p.pk)
  2662
+        self.assertEqual(response.status_code, 200)
  2663
+        # Emulate model instance edit via the admin
  2664
+        post_data = {
  2665
+            "title": "My Modified Paper Title",
  2666
+            "_save": "Save",
  2667
+        }
  2668
+        response = self.client.post('/test_admin/admin/admin_views/paper/%s/' % p.pk,
  2669
+                post_data, follow=True)
  2670
+        self.assertEqual(response.status_code, 200)
  2671
+        self.assertEqual(Paper.objects.count(), 1)
  2672
+        # Message should contain non-ugly model verbose name. The ugly(!)
  2673
+        # instance representation is set by six.text_type()
  2674
+        self.assertContains(
  2675
+            response,
  2676
+            '<li class="info">The paper &quot;Paper_Deferred_author object&quot; was changed successfully.</li>',
  2677
+            html=True
  2678
+        )
2538 2679
 
2539 2680
 
2540 2681
 @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))

0 notes on commit 3b32e5c

Please sign in to comment.
Something went wrong with that request. Please try again.