PageRenderTime 16ms CodeModel.GetById 6ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/m2m_regress/tests.py

https://code.google.com/p/mango-py/
Python | 75 lines | 45 code | 20 blank | 10 comment | 0 complexity | d7f4ef172bfd33a61cde86f7a774e8db MD5 | raw file
 1from django.core.exceptions import FieldError
 2from django.test import TestCase
 3
 4from models import (SelfRefer, Tag, TagCollection, Entry, SelfReferChild,
 5    SelfReferChildSibling, Worksheet)
 6
 7
 8class 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>"])