| | 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 | class 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>"]) |