1. Luke Plant
  2. django

Commits

Pa...@bcc190cf-cafb-0310-a4f2-bffc1f526a37  committed 000bb6f

[soc2010/test-refactor] Converted custom_managers doctests to unittests.

  • Participants
  • Parent commits 683ac4d
  • Branches soc2010/test-refactor

Comments (0)

Files changed (3)

File tests/modeltests/custom_managers/fixtures/custom_managers_testdata.json

View file
  • Ignore whitespace
+[
+    {
+        "pk": 1, 
+        "model": "custom_managers.person", 
+        "fields": {
+            "fun": true, 
+            "first_name": "Bugs", 
+            "last_name": "Bunny"
+        }
+    }, 
+    {
+        "pk": 2, 
+        "model": "custom_managers.person", 
+        "fields": {
+            "fun": false, 
+            "first_name": "Droopy", 
+            "last_name": "Dog"
+        }
+    }, 
+    {
+        "pk": 1, 
+        "model": "custom_managers.book", 
+        "fields": {
+            "authors": [], 
+            "is_published": true, 
+            "author": "Rodney Dangerfield", 
+            "title": "How to program"
+        }
+    }, 
+    {
+        "pk": 1, 
+        "model": "custom_managers.car", 
+        "fields": {
+            "mileage": 21, 
+            "top_speed": 180, 
+            "name": "Corvette"
+        }
+    }, 
+    {
+        "pk": 2, 
+        "model": "custom_managers.car", 
+        "fields": {
+            "mileage": 31, 
+            "top_speed": 100, 
+            "name": "Neon"
+        }
+    }
+]

File tests/modeltests/custom_managers/models.py

View file
  • Ignore whitespace
 
     def __unicode__(self):
         return self.name
-
-__test__ = {'API_TESTS':"""
->>> p1 = Person(first_name='Bugs', last_name='Bunny', fun=True)
->>> p1.save()
->>> p2 = Person(first_name='Droopy', last_name='Dog', fun=False)
->>> p2.save()
->>> Person.objects.get_fun_people()
-[<Person: Bugs Bunny>]
-
-# The RelatedManager used on the 'books' descriptor extends the default manager
->>> from modeltests.custom_managers.models import PublishedBookManager
->>> isinstance(p2.books, PublishedBookManager)
-True
-
->>> b1 = Book(title='How to program', author='Rodney Dangerfield', is_published=True)
->>> b1.save()
->>> b2 = Book(title='How to be smart', author='Albert Einstein', is_published=False)
->>> b2.save()
-
-# The default manager, "objects", doesn't exist,
-# because a custom one was provided.
->>> Book.objects
-Traceback (most recent call last):
-    ...
-AttributeError: type object 'Book' has no attribute 'objects'
-
-# The RelatedManager used on the 'authors' descriptor extends the default manager
->>> from modeltests.custom_managers.models import PersonManager
->>> isinstance(b2.authors, PersonManager)
-True
-
->>> Book.published_objects.all()
-[<Book: How to program>]
-
->>> c1 = Car(name='Corvette', mileage=21, top_speed=180)
->>> c1.save()
->>> c2 = Car(name='Neon', mileage=31, top_speed=100)
->>> c2.save()
->>> Car.cars.order_by('name')
-[<Car: Corvette>, <Car: Neon>]
->>> Car.fast_cars.all()
-[<Car: Corvette>]
-
-# Each model class gets a "_default_manager" attribute, which is a reference
-# to the first manager defined in the class. In this case, it's "cars".
->>> Car._default_manager.order_by('name')
-[<Car: Corvette>, <Car: Neon>]
-"""}

File tests/modeltests/custom_managers/tests.py

View file
  • Ignore whitespace
+from django.test import TestCase
+
+from models import PersonManager, PublishedBookManager
+from models import Person, Book, Car
+
+class CustomManagersTestCase(TestCase):
+    fixtures = ['custom_managers_testdata.json']
+
+    def test_related_manager(self):
+        self.assertQuerysetEqual(Person.objects.get_fun_people(),
+                                 ['<Person: Bugs Bunny>'])
+        
+
+        # The RelatedManager used on the 'books' descriptor extends
+        # the default manager
+        p2 = Person.objects.get(first_name='Droopy')
+        self.assertTrue(isinstance(p2.books, PublishedBookManager))
+
+
+        # The default manager, "objects", doesn't exist, because a
+        # custom one was provided.
+        self.assertRaises(AttributeError,
+                          getattr,
+                          Book, 'objects')
+
+
+        # The RelatedManager used on the 'authors' descriptor extends
+        # the default manager
+        b2 = Book(title='How to be smart', 
+                  author='Albert Einstein', 
+                  is_published=False)
+        b2.save()
+
+        self.assertTrue(isinstance(b2.authors, PersonManager))
+        self.assertQuerysetEqual(Book.published_objects.all(),
+                                 ['<Book: How to program>'])
+
+        self.assertQuerysetEqual(Car.cars.order_by('name'),
+                                 ['<Car: Corvette>', '<Car: Neon>'])
+        self.assertQuerysetEqual(Car.fast_cars.all(),
+                                 ['<Car: Corvette>'])
+
+        # Each model class gets a "_default_manager" attribute, which
+        # is a reference to the first manager defined in the class. In
+        # this case, it's "cars".
+        self.assertQuerysetEqual(Car._default_manager.order_by('name'),
+                                 ['<Car: Corvette>', '<Car: Neon>'])