diff options
author | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 09:53:47 +0100 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 14:36:57 +0100 |
commit | 89f40e36246100df6a11316c31a76712ebc6c501 (patch) | |
tree | 6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/m2m_regress | |
parent | b3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff) | |
download | django-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz |
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/m2m_regress')
-rw-r--r-- | tests/m2m_regress/__init__.py | 0 | ||||
-rw-r--r-- | tests/m2m_regress/models.py | 80 | ||||
-rw-r--r-- | tests/m2m_regress/tests.py | 111 |
3 files changed, 191 insertions, 0 deletions
diff --git a/tests/m2m_regress/__init__.py b/tests/m2m_regress/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/m2m_regress/__init__.py diff --git a/tests/m2m_regress/models.py b/tests/m2m_regress/models.py new file mode 100644 index 0000000000..1a4a6df354 --- /dev/null +++ b/tests/m2m_regress/models.py @@ -0,0 +1,80 @@ +from django.contrib.auth import models as auth +from django.db import models +from django.utils.encoding import python_2_unicode_compatible + +# No related name is needed here, since symmetrical relations are not +# explicitly reversible. +@python_2_unicode_compatible +class SelfRefer(models.Model): + name = models.CharField(max_length=10) + references = models.ManyToManyField('self') + related = models.ManyToManyField('self') + + def __str__(self): + return self.name + +@python_2_unicode_compatible +class Tag(models.Model): + name = models.CharField(max_length=10) + + def __str__(self): + return self.name + +# Regression for #11956 -- a many to many to the base class +@python_2_unicode_compatible +class TagCollection(Tag): + tags = models.ManyToManyField(Tag, related_name='tag_collections') + + def __str__(self): + return self.name + +# A related_name is required on one of the ManyToManyField entries here because +# they are both addressable as reverse relations from Tag. +@python_2_unicode_compatible +class Entry(models.Model): + name = models.CharField(max_length=10) + topics = models.ManyToManyField(Tag) + related = models.ManyToManyField(Tag, related_name="similar") + + def __str__(self): + return self.name + +# Two models both inheriting from a base model with a self-referential m2m field +class SelfReferChild(SelfRefer): + pass + +class SelfReferChildSibling(SelfRefer): + pass + +# Many-to-Many relation between models, where one of the PK's isn't an Autofield +class Line(models.Model): + name = models.CharField(max_length=100) + +class Worksheet(models.Model): + id = models.CharField(primary_key=True, max_length=100) + lines = models.ManyToManyField(Line, blank=True, null=True) + +# Regression for #11226 -- A model with the same name that another one to +# which it has a m2m relation. This shouldn't cause a name clash between +# the automatically created m2m intermediary table FK field names when +# running syncdb +class User(models.Model): + name = models.CharField(max_length=30) + friends = models.ManyToManyField(auth.User) + + +class BadModelWithSplit(models.Model): + name = models.CharField(max_length=1) + + def split(self): + raise RuntimeError('split should not be called') + + class Meta: + abstract = True + + +class RegressionModelSplit(BadModelWithSplit): + """ + Model with a split method should not cause an error in add_lazy_relation + """ + others = models.ManyToManyField('self') diff --git a/tests/m2m_regress/tests.py b/tests/m2m_regress/tests.py new file mode 100644 index 0000000000..884fc097a1 --- /dev/null +++ b/tests/m2m_regress/tests.py @@ -0,0 +1,111 @@ +from __future__ import absolute_import + +from django.core.exceptions import FieldError +from django.test import TestCase +from django.utils import six + +from .models import (SelfRefer, Tag, TagCollection, Entry, SelfReferChild, + SelfReferChildSibling, Worksheet, RegressionModelSplit, Line) + + +class M2MRegressionTests(TestCase): + def test_multiple_m2m(self): + # Multiple m2m references to model must be distinguished when + # accessing the relations through an instance attribute. + + s1 = SelfRefer.objects.create(name='s1') + s2 = SelfRefer.objects.create(name='s2') + s3 = SelfRefer.objects.create(name='s3') + s1.references.add(s2) + s1.related.add(s3) + + e1 = Entry.objects.create(name='e1') + t1 = Tag.objects.create(name='t1') + t2 = Tag.objects.create(name='t2') + + e1.topics.add(t1) + e1.related.add(t2) + + self.assertQuerysetEqual(s1.references.all(), ["<SelfRefer: s2>"]) + self.assertQuerysetEqual(s1.related.all(), ["<SelfRefer: s3>"]) + + self.assertQuerysetEqual(e1.topics.all(), ["<Tag: t1>"]) + self.assertQuerysetEqual(e1.related.all(), ["<Tag: t2>"]) + + def test_internal_related_name_not_in_error_msg(self): + # The secret internal related names for self-referential many-to-many + # fields shouldn't appear in the list when an error is made. + + six.assertRaisesRegex(self, FieldError, + "Choices are: id, name, references, related, selfreferchild, selfreferchildsibling$", + lambda: SelfRefer.objects.filter(porcupine='fred') + ) + + def test_m2m_inheritance_symmetry(self): + # Test to ensure that the relationship between two inherited models + # with a self-referential m2m field maintains symmetry + + sr_child = SelfReferChild(name="Hanna") + sr_child.save() + + sr_sibling = SelfReferChildSibling(name="Beth") + sr_sibling.save() + sr_child.related.add(sr_sibling) + + self.assertQuerysetEqual(sr_child.related.all(), ["<SelfRefer: Beth>"]) + self.assertQuerysetEqual(sr_sibling.related.all(), ["<SelfRefer: Hanna>"]) + + def test_m2m_pk_field_type(self): + # Regression for #11311 - The primary key for models in a m2m relation + # doesn't have to be an AutoField + + w = Worksheet(id='abc') + w.save() + w.delete() + + def test_add_m2m_with_base_class(self): + # Regression for #11956 -- You can add an object to a m2m with the + # base class without causing integrity errors + + t1 = Tag.objects.create(name='t1') + t2 = Tag.objects.create(name='t2') + + c1 = TagCollection.objects.create(name='c1') + c1.tags = [t1, t2] + c1 = TagCollection.objects.get(name='c1') + + self.assertQuerysetEqual(c1.tags.all(), ["<Tag: t1>", "<Tag: t2>"], ordered=False) + self.assertQuerysetEqual(t1.tag_collections.all(), ["<TagCollection: c1>"]) + + def test_manager_class_caching(self): + e1 = Entry.objects.create() + e2 = Entry.objects.create() + t1 = Tag.objects.create() + t2 = Tag.objects.create() + + # Get same manager twice in a row: + self.assertTrue(t1.entry_set.__class__ is t1.entry_set.__class__) + self.assertTrue(e1.topics.__class__ is e1.topics.__class__) + + # Get same manager for different instances + self.assertTrue(e1.topics.__class__ is e2.topics.__class__) + self.assertTrue(t1.entry_set.__class__ is t2.entry_set.__class__) + + def test_m2m_abstract_split(self): + # Regression for #19236 - an abstract class with a 'split' method + # causes a TypeError in add_lazy_relation + m1 = RegressionModelSplit(name='1') + m1.save() + + def test_m2m_filter(self): + worksheet = Worksheet.objects.create(id=1) + line_hi = Line.objects.create(name="hi") + line_bye = Line.objects.create(name="bye") + + worksheet.lines = [line_hi, line_bye] + hi = worksheet.lines.filter(name="hi") + + worksheet.lines = hi + self.assertEqual(1, worksheet.lines.count()) + self.assertEqual(1, hi.count()) + |