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] Fixed #14458 -- converted m2m_regress tests from doctests to …

…unittests. We have always been at war with doctests. Thanks to Gabriel Hurley for the patch. Backport of [14205].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14206 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1df25941b388d88e4360d2ebe478b3cd41e3eb28 1 parent 55b6836
Alex Gaynor authored October 14, 2010
67  tests/regressiontests/m2m_regress/models.py
@@ -56,70 +56,3 @@ class Worksheet(models.Model):
56 56
 class User(models.Model):
57 57
     name = models.CharField(max_length=30)
58 58
     friends = models.ManyToManyField(auth.User)
59  
-
60  
-__test__ = {"regressions": """
61  
-# Multiple m2m references to the same model or a different model must be
62  
-# distinguished when accessing the relations through an instance attribute.
63  
-
64  
->>> s1 = SelfRefer.objects.create(name='s1')
65  
->>> s2 = SelfRefer.objects.create(name='s2')
66  
->>> s3 = SelfRefer.objects.create(name='s3')
67  
->>> s1.references.add(s2)
68  
->>> s1.related.add(s3)
69  
-
70  
->>> e1 = Entry.objects.create(name='e1')
71  
->>> t1 = Tag.objects.create(name='t1')
72  
->>> t2 = Tag.objects.create(name='t2')
73  
->>> e1.topics.add(t1)
74  
->>> e1.related.add(t2)
75  
-
76  
->>> s1.references.all()
77  
-[<SelfRefer: s2>]
78  
->>> s1.related.all()
79  
-[<SelfRefer: s3>]
80  
-
81  
->>> e1.topics.all()
82  
-[<Tag: t1>]
83  
->>> e1.related.all()
84  
-[<Tag: t2>]
85  
-
86  
-# The secret internal related names for self-referential many-to-many fields
87  
-# shouldn't appear in the list when an error is made.
88  
->>> SelfRefer.objects.filter(porcupine='fred')
89  
-Traceback (most recent call last):
90  
-...
91  
-FieldError: Cannot resolve keyword 'porcupine' into field. Choices are: id, name, references, related, selfreferchild, selfreferchildsibling
92  
-
93  
-# Test to ensure that the relationship between two inherited models
94  
-# with a self-referential m2m field maintains symmetry
95  
->>> sr_child = SelfReferChild(name="Hanna")
96  
->>> sr_child.save()
97  
-
98  
->>> sr_sibling = SelfReferChildSibling(name="Beth")
99  
->>> sr_sibling.save()
100  
->>> sr_child.related.add(sr_sibling)
101  
->>> sr_child.related.all()
102  
-[<SelfRefer: Beth>]
103  
->>> sr_sibling.related.all()
104  
-[<SelfRefer: Hanna>]
105  
-
106  
-# Regression for #11311 - The primary key for models in a m2m relation
107  
-# doesn't have to be an AutoField
108  
->>> w = Worksheet(id='abc')
109  
->>> w.save()
110  
->>> w.delete()
111  
-
112  
-# Regression for #11956 -- You can add an object to a m2m with the
113  
-# base class without causing integrity errors
114  
->>> c1 = TagCollection.objects.create(name='c1')
115  
->>> c1.tags = [t1,t2]
116  
-
117  
->>> c1 = TagCollection.objects.get(name='c1')
118  
->>> c1.tags.all()
119  
-[<Tag: t1>, <Tag: t2>]
120  
-
121  
->>> t1.tag_collections.all()
122  
-[<TagCollection: c1>]
123  
-
124  
-"""
125  
-}
75  tests/regressiontests/m2m_regress/tests.py
... ...
@@ -0,0 +1,75 @@
  1
+from django.core.exceptions import FieldError
  2
+from django.test import TestCase
  3
+
  4
+from models import (SelfRefer, Tag, TagCollection, Entry, SelfReferChild,
  5
+    SelfReferChildSibling, Worksheet)
  6
+
  7
+
  8
+class M2MRegressionTests(TestCase):
  9
+    def test_multiple_m2m(self):
  10
+        # Multiple m2m references to model must be distinguished when
  11
+        # accessing the relations through an instance attribute.
  12
+
  13
+        s1 = SelfRefer.objects.create(name='s1')
  14
+        s2 = SelfRefer.objects.create(name='s2')
  15
+        s3 = SelfRefer.objects.create(name='s3')
  16
+        s1.references.add(s2)
  17
+        s1.related.add(s3)
  18
+
  19
+        e1 = Entry.objects.create(name='e1')
  20
+        t1 = Tag.objects.create(name='t1')
  21
+        t2 = Tag.objects.create(name='t2')
  22
+
  23
+        e1.topics.add(t1)
  24
+        e1.related.add(t2)
  25
+
  26
+        self.assertQuerysetEqual(s1.references.all(), ["<SelfRefer: s2>"])
  27
+        self.assertQuerysetEqual(s1.related.all(), ["<SelfRefer: s3>"])
  28
+
  29
+        self.assertQuerysetEqual(e1.topics.all(), ["<Tag: t1>"])
  30
+        self.assertQuerysetEqual(e1.related.all(), ["<Tag: t2>"])
  31
+
  32
+    def test_internal_related_name_not_in_error_msg(self):
  33
+        # The secret internal related names for self-referential many-to-many
  34
+        # fields shouldn't appear in the list when an error is made.
  35
+
  36
+        self.assertRaisesRegexp(FieldError,
  37
+            "Choices are: id, name, references, related, selfreferchild, selfreferchildsibling$",
  38
+            lambda: SelfRefer.objects.filter(porcupine='fred')
  39
+        )
  40
+
  41
+    def test_m2m_inheritance_symmetry(self):
  42
+        # Test to ensure that the relationship between two inherited models
  43
+        # with a self-referential m2m field maintains symmetry
  44
+
  45
+        sr_child = SelfReferChild(name="Hanna")
  46
+        sr_child.save()
  47
+
  48
+        sr_sibling = SelfReferChildSibling(name="Beth")
  49
+        sr_sibling.save()
  50
+        sr_child.related.add(sr_sibling)
  51
+
  52
+        self.assertQuerysetEqual(sr_child.related.all(), ["<SelfRefer: Beth>"])
  53
+        self.assertQuerysetEqual(sr_sibling.related.all(), ["<SelfRefer: Hanna>"])
  54
+
  55
+    def test_m2m_pk_field_type(self):
  56
+        # Regression for #11311 - The primary key for models in a m2m relation
  57
+        # doesn't have to be an AutoField
  58
+
  59
+        w = Worksheet(id='abc')
  60
+        w.save()
  61
+        w.delete()
  62
+
  63
+    def test_add_m2m_with_base_class(self):
  64
+        # Regression for #11956 -- You can add an object to a m2m with the
  65
+        # base class without causing integrity errors
  66
+
  67
+        t1 = Tag.objects.create(name='t1')
  68
+        t2 = Tag.objects.create(name='t2')
  69
+
  70
+        c1 = TagCollection.objects.create(name='c1')
  71
+        c1.tags = [t1,t2]
  72
+        c1 = TagCollection.objects.get(name='c1')
  73
+
  74
+        self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"])
  75
+        self.assertQuerysetEqual(t1.tag_collections.all(), ["<TagCollection: c1>"])

0 notes on commit 1df2594

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