2010-10-14 01:10:57 +00:00
|
|
|
from django.core.exceptions import FieldError
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import (
|
2015-03-26 18:47:07 +00:00
|
|
|
Entry,
|
|
|
|
Line,
|
|
|
|
Post,
|
|
|
|
RegressionModelSplit,
|
|
|
|
SelfRefer,
|
|
|
|
SelfReferChild,
|
2015-01-28 12:35:27 +00:00
|
|
|
SelfReferChildSibling,
|
|
|
|
Tag,
|
|
|
|
TagCollection,
|
|
|
|
Worksheet,
|
|
|
|
)
|
2010-10-14 01:10:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(s1.references.all(), [s2])
|
|
|
|
self.assertSequenceEqual(s1.related.all(), [s3])
|
2010-10-14 01:10:57 +00:00
|
|
|
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(e1.topics.all(), [t1])
|
|
|
|
self.assertSequenceEqual(e1.related.all(), [t2])
|
2010-10-14 01:10:57 +00:00
|
|
|
|
2021-08-01 11:13:35 +00:00
|
|
|
def test_m2m_managers_reused(self):
|
|
|
|
s1 = SelfRefer.objects.create(name="s1")
|
|
|
|
e1 = Entry.objects.create(name="e1")
|
|
|
|
self.assertIs(s1.references, s1.references)
|
|
|
|
self.assertIs(s1.related, s1.related)
|
|
|
|
self.assertIs(e1.topics, e1.topics)
|
|
|
|
self.assertIs(e1.related, e1.related)
|
|
|
|
|
2010-10-14 01:10:57 +00:00
|
|
|
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.
|
2019-04-28 13:15:19 +00:00
|
|
|
msg = (
|
2016-12-07 22:42:31 +00:00
|
|
|
"Choices are: id, name, references, related, selfreferchild, "
|
|
|
|
"selfreferchildsibling"
|
2019-04-28 13:15:19 +00:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
SelfRefer.objects.filter(porcupine="fred")
|
2010-10-14 01:10:57 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(sr_child.related.all(), [sr_sibling.selfrefer_ptr])
|
|
|
|
self.assertSequenceEqual(sr_sibling.related.all(), [sr_child.selfrefer_ptr])
|
2010-10-14 01:10:57 +00:00
|
|
|
|
|
|
|
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")
|
2015-10-08 21:17:10 +00:00
|
|
|
c1.tags.set([t1, t2])
|
2010-10-14 01:10:57 +00:00
|
|
|
c1 = TagCollection.objects.get(name="c1")
|
|
|
|
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertCountEqual(c1.tags.all(), [t1, t2])
|
|
|
|
self.assertCountEqual(t1.tag_collections.all(), [c1])
|
2011-09-30 10:41:25 +00:00
|
|
|
|
|
|
|
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:
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIs(t1.entry_set.__class__, t1.entry_set.__class__)
|
|
|
|
self.assertIs(e1.topics.__class__, e1.topics.__class__)
|
2011-09-30 10:41:25 +00:00
|
|
|
|
|
|
|
# Get same manager for different instances
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIs(e1.topics.__class__, e2.topics.__class__)
|
|
|
|
self.assertIs(t1.entry_set.__class__, t2.entry_set.__class__)
|
2012-11-04 21:43:54 +00:00
|
|
|
|
|
|
|
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()
|
2014-12-24 13:13:58 +00:00
|
|
|
|
|
|
|
def test_assigning_invalid_data_to_m2m_doesnt_clear_existing_relations(self):
|
|
|
|
t1 = Tag.objects.create(name="t1")
|
|
|
|
t2 = Tag.objects.create(name="t2")
|
|
|
|
c1 = TagCollection.objects.create(name="c1")
|
2015-10-08 21:17:10 +00:00
|
|
|
c1.tags.set([t1, t2])
|
2014-12-24 13:13:58 +00:00
|
|
|
|
2017-05-28 19:37:21 +00:00
|
|
|
with self.assertRaisesMessage(TypeError, "'int' object is not iterable"):
|
2015-10-08 21:17:10 +00:00
|
|
|
c1.tags.set(7)
|
2014-12-24 13:13:58 +00:00
|
|
|
|
|
|
|
c1.refresh_from_db()
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(c1.tags.order_by("name"), [t1, t2])
|
2015-03-26 18:47:07 +00:00
|
|
|
|
|
|
|
def test_multiple_forwards_only_m2m(self):
|
|
|
|
# Regression for #24505 - Multiple ManyToManyFields to same "to"
|
|
|
|
# model with related_name set to '+'.
|
|
|
|
foo = Line.objects.create(name="foo")
|
|
|
|
bar = Line.objects.create(name="bar")
|
|
|
|
post = Post.objects.create()
|
|
|
|
post.primary_lines.add(foo)
|
|
|
|
post.secondary_lines.add(bar)
|
2020-10-18 16:29:52 +00:00
|
|
|
self.assertSequenceEqual(post.primary_lines.all(), [foo])
|
|
|
|
self.assertSequenceEqual(post.secondary_lines.all(), [bar])
|