Author: Alex
Date: 2010-10-08 18:47:11 -0500 (Fri, 08 Oct 2010)
New Revision: 14043

Added:
   django/trunk/tests/modeltests/order_with_respect_to/tests.py
Modified:
   django/trunk/tests/modeltests/order_with_respect_to/models.py
Log:
Converted order_with_respect_to to unittests.  We have always been at war with 
doctests.

Modified: django/trunk/tests/modeltests/order_with_respect_to/models.py
===================================================================
--- django/trunk/tests/modeltests/order_with_respect_to/models.py       
2010-10-08 23:37:48 UTC (rev 14042)
+++ django/trunk/tests/modeltests/order_with_respect_to/models.py       
2010-10-08 23:47:11 UTC (rev 14043)
@@ -4,6 +4,7 @@
 
 from django.db import models
 
+
 class Question(models.Model):
     text = models.CharField(max_length=200)
 
@@ -16,63 +17,3 @@
 
     def __unicode__(self):
         return unicode(self.text)
-
-__test__ = {'API_TESTS': """
->>> q1 = Question(text="Which Beatle starts with the letter 'R'?")
->>> q1.save()
->>> q2 = Question(text="What is your name?")
->>> q2.save()
->>> Answer(text="John", question=q1).save()
->>> Answer(text="Jonno",question=q2).save()
->>> Answer(text="Paul", question=q1).save()
->>> Answer(text="Paulo", question=q2).save()
->>> Answer(text="George", question=q1).save()
->>> Answer(text="Ringo", question=q1).save()
-
-The answers will always be ordered in the order they were inserted.
-
->>> q1.answer_set.all()
-[<Answer: John>, <Answer: Paul>, <Answer: George>, <Answer: Ringo>]
-
-We can retrieve the answers related to a particular object, in the order
-they were created, once we have a particular object.
-
->>> a1 = Answer.objects.filter(question=q1)[0]
->>> a1
-<Answer: John>
->>> a2 = a1.get_next_in_order()
->>> a2
-<Answer: Paul>
->>> a4 = list(Answer.objects.filter(question=q1))[-1]
->>> a4
-<Answer: Ringo>
->>> a4.get_previous_in_order()
-<Answer: George>
-
-Determining (and setting) the ordering for a particular item is also possible.
-
->>> id_list = [o.pk for o in q1.answer_set.all()]
->>> a2.question.get_answer_order() == id_list
-True
-
->>> a5 = Answer(text="Number five", question=q1)
->>> a5.save()
-
-It doesn't matter which answer we use to check the order, it will always be 
the same.
-
->>> a2.question.get_answer_order() == a5.question.get_answer_order()
-True
-
-The ordering can be altered:
-
->>> id_list = [o.pk for o in q1.answer_set.all()]
->>> x = id_list.pop()
->>> id_list.insert(-1, x)
->>> a5.question.get_answer_order() == id_list
-False
->>> a5.question.set_answer_order(id_list)
->>> q1.answer_set.all()
-[<Answer: John>, <Answer: Paul>, <Answer: George>, <Answer: Number five>, 
<Answer: Ringo>]
-
-"""
-}

Added: django/trunk/tests/modeltests/order_with_respect_to/tests.py
===================================================================
--- django/trunk/tests/modeltests/order_with_respect_to/tests.py                
                (rev 0)
+++ django/trunk/tests/modeltests/order_with_respect_to/tests.py        
2010-10-08 23:47:11 UTC (rev 14043)
@@ -0,0 +1,62 @@
+from operator import attrgetter
+
+from django.test import TestCase
+
+from models import Question, Answer
+
+
+class OrderWithRespectToTests(TestCase):
+    def test_basic(self):
+        q1 = Question.objects.create(text="Which Beatle starts with the letter 
'R'?")
+        q2 = Question.objects.create(text="What is your name?")
+        
+        Answer.objects.create(text="John", question=q1)
+        Answer.objects.create(text="Jonno", question=q2)
+        Answer.objects.create(text="Paul", question=q1)
+        Answer.objects.create(text="Paulo", question=q2)
+        Answer.objects.create(text="George", question=q1)
+        Answer.objects.create(text="Ringo", question=q1)
+        
+        # The answers will always be ordered in the order they were inserted.
+        self.assertQuerysetEqual(
+            q1.answer_set.all(), [
+                "John", "Paul", "George", "Ringo",
+            ],
+            attrgetter("text"),
+        )
+        
+        # We can retrieve the answers related to a particular object, in the
+        # order they were created, once we have a particular object.
+        a1 = Answer.objects.filter(question=q1)[0]
+        self.assertEqual(a1.text, "John")
+        a2 = a1.get_next_in_order()
+        self.assertEqual(a2.text, "Paul")
+        a4 = list(Answer.objects.filter(question=q1))[-1]
+        self.assertEqual(a4.text, "Ringo")
+        self.assertEqual(a4.get_previous_in_order().text, "George")
+        
+        # Determining (and setting) the ordering for a particular item is also
+        # possible.
+        id_list = [o.pk for o in q1.answer_set.all()]
+        self.assertEqual(a2.question.get_answer_order(), id_list)
+        
+        a5 = Answer.objects.create(text="Number five", question=q1)
+        
+        # It doesn't matter which answer we use to check the order, it will
+        # always be the same.
+        self.assertEqual(
+            a2.question.get_answer_order(), a5.question.get_answer_order()
+        )
+        
+        # The ordering can be altered:
+        id_list = [o.pk for o in q1.answer_set.all()]
+        x = id_list.pop()
+        id_list.insert(-1, x)
+        self.assertNotEqual(a5.question.get_answer_order(), id_list)
+        a5.question.set_answer_order(id_list)
+        self.assertQuerysetEqual(
+            q1.answer_set.all(), [
+                "John", "Paul", "George", "Number five", "Ringo"
+            ],
+            attrgetter("text")
+        )

-- 
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