Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.2.X] Migrated m2m_multiple doctests. Thanks to Alex Gaynor.

Backport of r13789 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@13805 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit cbcdd408c0310ec72655e86c31481f0b27e5e23a 1 parent a4a19b8
Russell Keith-Magee authored September 12, 2010
49  tests/modeltests/m2m_multiple/models.py
@@ -28,52 +28,3 @@ class Meta:
28 28
     def __unicode__(self):
29 29
         return self.headline
30 30
 
31  
-__test__ = {'API_TESTS':"""
32  
->>> from datetime import datetime
33  
-
34  
->>> c1 = Category(name='Sports')
35  
->>> c1.save()
36  
->>> c2 = Category(name='News')
37  
->>> c2.save()
38  
->>> c3 = Category(name='Crime')
39  
->>> c3.save()
40  
->>> c4 = Category(name='Life')
41  
->>> c4.save()
42  
-
43  
->>> a1 = Article(headline='Area man steals', pub_date=datetime(2005, 11, 27))
44  
->>> a1.save()
45  
->>> a1.primary_categories.add(c2, c3)
46  
->>> a1.secondary_categories.add(c4)
47  
-
48  
->>> a2 = Article(headline='Area man runs', pub_date=datetime(2005, 11, 28))
49  
->>> a2.save()
50  
->>> a2.primary_categories.add(c1, c2)
51  
->>> a2.secondary_categories.add(c4)
52  
-
53  
->>> a1.primary_categories.all()
54  
-[<Category: Crime>, <Category: News>]
55  
-
56  
->>> a2.primary_categories.all()
57  
-[<Category: News>, <Category: Sports>]
58  
-
59  
->>> a1.secondary_categories.all()
60  
-[<Category: Life>]
61  
-
62  
-
63  
->>> c1.primary_article_set.all()
64  
-[<Article: Area man runs>]
65  
->>> c1.secondary_article_set.all()
66  
-[]
67  
->>> c2.primary_article_set.all()
68  
-[<Article: Area man steals>, <Article: Area man runs>]
69  
->>> c2.secondary_article_set.all()
70  
-[]
71  
->>> c3.primary_article_set.all()
72  
-[<Article: Area man steals>]
73  
->>> c3.secondary_article_set.all()
74  
-[]
75  
->>> c4.primary_article_set.all()
76  
-[]
77  
->>> c4.secondary_article_set.all()
78  
-[<Article: Area man steals>, <Article: Area man runs>]
79  
-"""}
84  tests/modeltests/m2m_multiple/tests.py
... ...
@@ -0,0 +1,84 @@
  1
+from datetime import datetime
  2
+
  3
+from django.test import TestCase
  4
+
  5
+from models import Article, Category
  6
+
  7
+
  8
+class M2MMultipleTests(TestCase):
  9
+    def test_multiple(self):
  10
+        c1, c2, c3, c4 = [
  11
+            Category.objects.create(name=name)
  12
+            for name in ["Sports", "News", "Crime", "Life"]
  13
+        ]
  14
+
  15
+        a1 = Article.objects.create(
  16
+            headline="Area man steals", pub_date=datetime(2005, 11, 27)
  17
+        )
  18
+        a1.primary_categories.add(c2, c3)
  19
+        a1.secondary_categories.add(c4)
  20
+
  21
+        a2 = Article.objects.create(
  22
+            headline="Area man runs", pub_date=datetime(2005, 11, 28)
  23
+        )
  24
+        a2.primary_categories.add(c1, c2)
  25
+        a2.secondary_categories.add(c4)
  26
+
  27
+        self.assertQuerysetEqual(
  28
+            a1.primary_categories.all(), [
  29
+                "Crime",
  30
+                "News",
  31
+            ],
  32
+            lambda c: c.name
  33
+        )
  34
+        self.assertQuerysetEqual(
  35
+            a2.primary_categories.all(), [
  36
+                "News",
  37
+                "Sports",
  38
+            ],
  39
+            lambda c: c.name
  40
+        )
  41
+        self.assertQuerysetEqual(
  42
+            a1.secondary_categories.all(), [
  43
+                "Life",
  44
+            ],
  45
+            lambda c: c.name
  46
+        )
  47
+        self.assertQuerysetEqual(
  48
+            c1.primary_article_set.all(), [
  49
+                "Area man runs",
  50
+            ],
  51
+            lambda a: a.headline
  52
+        )
  53
+        self.assertQuerysetEqual(
  54
+            c1.secondary_article_set.all(), []
  55
+        )
  56
+        self.assertQuerysetEqual(
  57
+            c2.primary_article_set.all(), [
  58
+                "Area man steals",
  59
+                "Area man runs",
  60
+            ],
  61
+            lambda a: a.headline
  62
+        )
  63
+        self.assertQuerysetEqual(
  64
+            c2.secondary_article_set.all(), []
  65
+        )
  66
+        self.assertQuerysetEqual(
  67
+            c3.primary_article_set.all(), [
  68
+                "Area man steals",
  69
+            ],
  70
+            lambda a: a.headline
  71
+        )
  72
+        self.assertQuerysetEqual(
  73
+            c3.secondary_article_set.all(), []
  74
+        )
  75
+        self.assertQuerysetEqual(
  76
+            c4.primary_article_set.all(), []
  77
+        )
  78
+        self.assertQuerysetEqual(
  79
+            c4.secondary_article_set.all(), [
  80
+                "Area man steals",
  81
+                "Area man runs",
  82
+            ],
  83
+            lambda a: a.headline
  84
+        )

0 notes on commit cbcdd40

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