2010-10-12 08:55:55 +08:00
|
|
|
from django.test import TestCase
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2014-09-24 00:29:17 +08:00
|
|
|
from .models import Article, Car, Driver, Reporter
|
2010-10-12 08:55:55 +08:00
|
|
|
|
|
|
|
|
2011-06-08 04:51:59 +08:00
|
|
|
class ManyToOneNullTests(TestCase):
|
2018-11-24 10:24:25 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2010-10-12 08:55:55 +08:00
|
|
|
# Create a Reporter.
|
2018-11-24 10:24:25 +08:00
|
|
|
cls.r = Reporter(name="John Smith")
|
|
|
|
cls.r.save()
|
2010-10-12 08:55:55 +08:00
|
|
|
# Create an Article.
|
2018-11-24 10:24:25 +08:00
|
|
|
cls.a = Article(headline="First", reporter=cls.r)
|
|
|
|
cls.a.save()
|
2010-10-12 08:55:55 +08:00
|
|
|
# Create an Article via the Reporter object.
|
2018-11-24 10:24:25 +08:00
|
|
|
cls.a2 = cls.r.article_set.create(headline="Second")
|
2010-10-12 08:55:55 +08:00
|
|
|
# Create an Article with no Reporter by passing "reporter=None".
|
2018-11-24 10:24:25 +08:00
|
|
|
cls.a3 = Article(headline="Third", reporter=None)
|
|
|
|
cls.a3.save()
|
2010-10-12 08:55:55 +08:00
|
|
|
# Create another article and reporter
|
2018-11-24 10:24:25 +08:00
|
|
|
cls.r2 = Reporter(name="Paul Jones")
|
|
|
|
cls.r2.save()
|
|
|
|
cls.a4 = cls.r2.article_set.create(headline="Fourth")
|
2010-10-12 08:55:55 +08:00
|
|
|
|
|
|
|
def test_get_related(self):
|
|
|
|
self.assertEqual(self.a.reporter.id, self.r.id)
|
|
|
|
# Article objects have access to their related Reporter objects.
|
|
|
|
r = self.a.reporter
|
|
|
|
self.assertEqual(r.id, self.r.id)
|
|
|
|
|
|
|
|
def test_created_via_related_set(self):
|
|
|
|
self.assertEqual(self.a2.reporter.id, self.r.id)
|
|
|
|
|
|
|
|
def test_related_set(self):
|
|
|
|
# Reporter objects have access to their related Article objects.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r.article_set.all(), [self.a, self.a2])
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.r.article_set.filter(headline__startswith="Fir"), [self.a]
|
|
|
|
)
|
2010-10-12 08:55:55 +08:00
|
|
|
self.assertEqual(self.r.article_set.count(), 2)
|
|
|
|
|
|
|
|
def test_created_without_related(self):
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(self.a3.reporter)
|
2010-10-12 08:55:55 +08:00
|
|
|
# Need to reget a3 to refresh the cache
|
2010-10-12 08:56:40 +08:00
|
|
|
a3 = Article.objects.get(pk=self.a3.pk)
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(a3.reporter, "id")
|
2010-10-12 08:55:55 +08:00
|
|
|
# Accessing an article's 'reporter' attribute returns None
|
|
|
|
# if the reporter is set to None.
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(a3.reporter)
|
2010-10-12 08:55:55 +08:00
|
|
|
# To retrieve the articles with no reporters set, use "reporter__isnull=True".
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Article.objects.filter(reporter__isnull=True), [self.a3]
|
|
|
|
)
|
2010-10-12 08:55:55 +08:00
|
|
|
# We can achieve the same thing by filtering for the case where the
|
|
|
|
# reporter is None.
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(Article.objects.filter(reporter=None), [self.a3])
|
2010-10-12 08:55:55 +08:00
|
|
|
# Set the reporter for the Third article
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r.article_set.all(), [self.a, self.a2])
|
2010-10-12 08:55:55 +08:00
|
|
|
self.r.article_set.add(a3)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2016-04-08 10:04:45 +08:00
|
|
|
self.r.article_set.all(),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.a, self.a2, self.a3],
|
2016-04-08 10:04:45 +08:00
|
|
|
)
|
2010-10-12 08:55:55 +08:00
|
|
|
# Remove an article from the set, and check that it was removed.
|
|
|
|
self.r.article_set.remove(a3)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r.article_set.all(), [self.a, self.a2])
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Article.objects.filter(reporter__isnull=True), [self.a3]
|
|
|
|
)
|
2010-10-12 08:55:55 +08:00
|
|
|
|
|
|
|
def test_remove_from_wrong_set(self):
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [self.a4])
|
2010-10-12 08:55:55 +08:00
|
|
|
# Try to remove a4 from a set it does not belong to
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(Reporter.DoesNotExist):
|
|
|
|
self.r.article_set.remove(self.a4)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [self.a4])
|
2010-10-12 08:55:55 +08:00
|
|
|
|
2015-01-30 02:15:27 +08:00
|
|
|
def test_set(self):
|
|
|
|
# Use manager.set() to allocate ForeignKey. Null is legal, so existing
|
|
|
|
# members of the set that are not in the assignment set are set to null.
|
|
|
|
self.r2.article_set.set([self.a2, self.a3])
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [self.a2, self.a3])
|
2015-01-30 02:15:27 +08:00
|
|
|
# Use manager.set(clear=True)
|
|
|
|
self.r2.article_set.set([self.a3, self.a4], clear=True)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [self.a4, self.a3])
|
2015-01-30 02:15:27 +08:00
|
|
|
# Clear the rest of the set
|
|
|
|
self.r2.article_set.set([])
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [])
|
|
|
|
self.assertSequenceEqual(
|
2016-04-08 10:04:45 +08:00
|
|
|
Article.objects.filter(reporter__isnull=True),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.a4, self.a2, self.a3],
|
2016-04-08 10:04:45 +08:00
|
|
|
)
|
2015-01-30 02:15:27 +08:00
|
|
|
|
2018-06-24 04:39:22 +08:00
|
|
|
def test_set_clear_non_bulk(self):
|
|
|
|
# 2 queries for clear(), 1 for add(), and 1 to select objects.
|
|
|
|
with self.assertNumQueries(4):
|
|
|
|
self.r.article_set.set([self.a], bulk=False, clear=True)
|
|
|
|
|
2010-10-12 08:55:55 +08:00
|
|
|
def test_assign_clear_related_set(self):
|
|
|
|
# Use descriptor assignment to allocate ForeignKey. Null is legal, so
|
2015-01-30 02:15:27 +08:00
|
|
|
# existing members of the set that are not in the assignment set are
|
|
|
|
# set to null.
|
2015-10-09 05:17:10 +08:00
|
|
|
self.r2.article_set.set([self.a2, self.a3])
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r2.article_set.all(), [self.a2, self.a3])
|
2010-10-12 08:55:55 +08:00
|
|
|
# Clear the rest of the set
|
|
|
|
self.r.article_set.clear()
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.r.article_set.all(), [])
|
|
|
|
self.assertSequenceEqual(
|
2016-04-08 10:04:45 +08:00
|
|
|
Article.objects.filter(reporter__isnull=True),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.a, self.a4],
|
2016-04-08 10:04:45 +08:00
|
|
|
)
|
2011-06-08 04:51:59 +08:00
|
|
|
|
2014-03-27 19:32:48 +08:00
|
|
|
def test_assign_with_queryset(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# Querysets used in reverse FK assignments are pre-evaluated
|
2014-03-27 19:32:48 +08:00
|
|
|
# so their value isn't affected by the clearing operation in
|
2015-10-09 05:17:10 +08:00
|
|
|
# RelatedManager.set() (#19816).
|
|
|
|
self.r2.article_set.set([self.a2, self.a3])
|
2014-03-27 19:32:48 +08:00
|
|
|
|
2014-03-30 19:48:27 +08:00
|
|
|
qs = self.r2.article_set.filter(headline="Second")
|
2015-10-09 05:17:10 +08:00
|
|
|
self.r2.article_set.set(qs)
|
2014-03-27 19:32:48 +08:00
|
|
|
|
|
|
|
self.assertEqual(1, self.r2.article_set.count())
|
|
|
|
self.assertEqual(1, qs.count())
|
|
|
|
|
2015-03-15 09:25:33 +08:00
|
|
|
def test_add_efficiency(self):
|
|
|
|
r = Reporter.objects.create()
|
|
|
|
articles = []
|
|
|
|
for _ in range(3):
|
|
|
|
articles.append(Article.objects.create())
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
r.article_set.add(*articles)
|
|
|
|
self.assertEqual(r.article_set.count(), 3)
|
|
|
|
|
2011-06-08 04:59:07 +08:00
|
|
|
def test_clear_efficiency(self):
|
2011-06-08 04:51:59 +08:00
|
|
|
r = Reporter.objects.create()
|
2012-07-21 00:53:11 +08:00
|
|
|
for _ in range(3):
|
2011-06-08 04:51:59 +08:00
|
|
|
r.article_set.create()
|
|
|
|
with self.assertNumQueries(1):
|
|
|
|
r.article_set.clear()
|
2012-07-21 00:53:11 +08:00
|
|
|
self.assertEqual(r.article_set.count(), 0)
|
2014-09-24 00:29:17 +08:00
|
|
|
|
|
|
|
def test_related_null_to_field(self):
|
|
|
|
c1 = Car.objects.create()
|
|
|
|
d1 = Driver.objects.create()
|
|
|
|
self.assertIs(d1.car, None)
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.assertEqual(list(c1.drivers.all()), [])
|
2022-01-14 20:44:17 +08:00
|
|
|
|
|
|
|
def test_unsaved(self):
|
|
|
|
msg = (
|
|
|
|
"'Car' instance needs to have a primary key value before this relationship "
|
|
|
|
"can be used."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
Car(make="Ford").drivers.all()
|
|
|
|
|
|
|
|
def test_related_null_to_field_related_managers(self):
|
|
|
|
car = Car.objects.create(make=None)
|
|
|
|
driver = Driver.objects.create()
|
|
|
|
msg = (
|
|
|
|
f'"{car!r}" needs to have a value for field "make" before this '
|
|
|
|
f"relationship can be used."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
car.drivers.add(driver)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
car.drivers.create()
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
car.drivers.get_or_create()
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
car.drivers.update_or_create()
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
car.drivers.remove(driver)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
car.drivers.clear()
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
car.drivers.set([driver])
|
|
|
|
|
|
|
|
with self.assertNumQueries(0):
|
|
|
|
self.assertEqual(car.drivers.count(), 0)
|