Ticket #14458: m2m_regress_test_conversion.diff

File m2m_regress_test_conversion.diff, 5.7 KB (added by Gabriel Hurley, 14 years ago)
  • tests/regressiontests/m2m_regress/models.py

     
    5656class User(models.Model):
    5757    name = models.CharField(max_length=30)
    5858    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 }
  • tests/regressiontests/m2m_regress/tests.py

     
     1from django.core.exceptions import FieldError
     2from django.test import TestCase
     3
     4from models import SelfRefer, Tag, TagCollection, Entry, SelfReferChild, \
     5                    SelfReferChildSibling, Worksheet
     6
     7class M2MRegressionTests(TestCase):
     8    def test_multiple_m2m(self):
     9        # Multiple m2m references to model must be distinguished when
     10        # accessing the relations through an instance attribute.
     11       
     12        s1 = SelfRefer.objects.create(name='s1')
     13        s2 = SelfRefer.objects.create(name='s2')
     14        s3 = SelfRefer.objects.create(name='s3')
     15        s1.references.add(s2)
     16        s1.related.add(s3)
     17       
     18        e1 = Entry.objects.create(name='e1')
     19        t1 = Tag.objects.create(name='t1')
     20        t2 = Tag.objects.create(name='t2')
     21       
     22        e1.topics.add(t1)
     23        e1.related.add(t2)
     24       
     25        self.assertQuerysetEqual(s1.references.all(), ["<SelfRefer: s2>"])
     26        self.assertQuerysetEqual(s1.related.all(), ["<SelfRefer: s3>"])
     27       
     28        self.assertQuerysetEqual(e1.topics.all(), ["<Tag: t1>"])
     29        self.assertQuerysetEqual(e1.related.all(), ["<Tag: t2>"])
     30       
     31    def test_internal_related_name_not_in_error_msg(self):
     32        # The secret internal related names for self-referential many-to-many
     33        # fields shouldn't appear in the list when an error is made.
     34       
     35        try:
     36            SelfRefer.objects.filter(porcupine='fred')
     37        except FieldError, e:
     38            self.assertEqual(unicode(e), u"Cannot resolve keyword 'porcupine' into field. Choices are: id, name, references, related, selfreferchild, selfreferchildsibling")
     39       
     40    def test_m2m_inheritance_symmetry(self):
     41        # Test to ensure that the relationship between two inherited models
     42        # with a self-referential m2m field maintains symmetry
     43       
     44        sr_child = SelfReferChild(name="Hanna")
     45        sr_child.save()
     46       
     47        sr_sibling = SelfReferChildSibling(name="Beth")
     48        sr_sibling.save()
     49        sr_child.related.add(sr_sibling)
     50       
     51        self.assertQuerysetEqual(sr_child.related.all(), ["<SelfRefer: Beth>"])
     52        self.assertQuerysetEqual(sr_sibling.related.all(), ["<SelfRefer: Hanna>"])
     53   
     54    def test_m2m_pk_field_type(self):
     55        # Regression for #11311 - The primary key for models in a m2m relation
     56        # doesn't have to be an AutoField
     57       
     58        w = Worksheet(id='abc')
     59        w.save()
     60        w.delete()
     61   
     62    def test_add_m2m_with_base_class(self):
     63        # Regression for #11956 -- You can add an object to a m2m with the
     64        # base class without causing integrity errors
     65       
     66        t1 = Tag.objects.create(name='t1')
     67        t2 = Tag.objects.create(name='t2')
     68       
     69        c1 = TagCollection.objects.create(name='c1')
     70        c1.tags = [t1,t2]
     71        c1 = TagCollection.objects.get(name='c1')
     72       
     73        self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"])
     74        self.assertQuerysetEqual(t1.tag_collections.all(), ["<TagCollection: c1>"])
Back to Top