Author: russellm
Date: 2010-09-12 15:09:23 -0500 (Sun, 12 Sep 2010)
New Revision: 13791

Added:
   django/branches/releases/1.2.X/tests/modeltests/custom_managers/tests.py
Modified:
   django/branches/releases/1.2.X/tests/modeltests/custom_managers/models.py
Log:
[1.2.X] Migrated the custom_managers tests. Thanks to Alex Gaynor.

Backport of r13774 from trunk.

Modified: 
django/branches/releases/1.2.X/tests/modeltests/custom_managers/models.py
===================================================================
--- django/branches/releases/1.2.X/tests/modeltests/custom_managers/models.py   
2010-09-12 20:05:17 UTC (rev 13790)
+++ django/branches/releases/1.2.X/tests/modeltests/custom_managers/models.py   
2010-09-12 20:09:23 UTC (rev 13791)
@@ -57,51 +57,3 @@
 
     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>]
-"""}

Added: django/branches/releases/1.2.X/tests/modeltests/custom_managers/tests.py
===================================================================
--- django/branches/releases/1.2.X/tests/modeltests/custom_managers/tests.py    
                        (rev 0)
+++ django/branches/releases/1.2.X/tests/modeltests/custom_managers/tests.py    
2010-09-12 20:09:23 UTC (rev 13791)
@@ -0,0 +1,71 @@
+from django.test import TestCase
+
+from models import Person, Book, Car, PersonManager, PublishedBookManager
+
+
+class CustomManagerTests(TestCase):
+    def test_manager(self):
+        p1 = Person.objects.create(first_name="Bugs", last_name="Bunny", 
fun=True)
+        p2 = Person.objects.create(first_name="Droopy", last_name="Dog", 
fun=False)
+
+        self.assertQuerysetEqual(
+            Person.objects.get_fun_people(), [
+                "Bugs Bunny"
+            ],
+            unicode
+        )
+        # The RelatedManager used on the 'books' descriptor extends the default
+        # manager
+        self.assertTrue(isinstance(p2.books, PublishedBookManager))
+
+        b1 = Book.published_objects.create(
+            title="How to program", author="Rodney Dangerfield", 
is_published=True
+        )
+        b2 = Book.published_objects.create(
+            title="How to be smart", author="Albert Einstein", 
is_published=False
+        )
+
+        # The default manager, "objects", doesn't exist, because a custom one
+        # was provided.
+        self.assertRaises(AttributeError, lambda: Book.objects)
+
+        # The RelatedManager used on the 'authors' descriptor extends the
+        # default manager
+        self.assertTrue(isinstance(b2.authors, PersonManager))
+
+        self.assertQuerysetEqual(
+            Book.published_objects.all(), [
+                "How to program",
+            ],
+            lambda b: b.title
+        )
+
+        c1 = Car.cars.create(name="Corvette", mileage=21, top_speed=180)
+        c2 = Car.cars.create(name="Neon", mileage=31, top_speed=100)
+
+        self.assertQuerysetEqual(
+            Car.cars.order_by("name"), [
+                "Corvette",
+                "Neon",
+            ],
+            lambda c: c.name
+        )
+
+        self.assertQuerysetEqual(
+            Car.fast_cars.all(), [
+                "Corvette",
+            ],
+            lambda c: c.name
+        )
+
+        # 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"), [
+                "Corvette",
+                "Neon",
+            ],
+            lambda c: c.name
+        )

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to