2010-09-28 16:16:47 +08:00
|
|
|
from django.core.exceptions import FieldError
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.test import TestCase
|
2010-09-28 16:16:47 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import Choice, Inner, OuterA, OuterB, Poll
|
2010-09-28 16:16:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class NullQueriesTests(TestCase):
|
|
|
|
def test_none_as_null(self):
|
|
|
|
"""
|
|
|
|
Regression test for the use of None as a query value.
|
|
|
|
|
2013-12-03 17:24:45 +08:00
|
|
|
None is interpreted as an SQL NULL, but only in __exact and __iexact
|
|
|
|
queries.
|
2010-09-28 16:16:47 +08:00
|
|
|
Set up some initial polls and choices
|
|
|
|
"""
|
|
|
|
p1 = Poll(question="Why?")
|
|
|
|
p1.save()
|
|
|
|
c1 = Choice(poll=p1, choice="Because.")
|
|
|
|
c1.save()
|
|
|
|
c2 = Choice(poll=p1, choice="Why Not?")
|
|
|
|
c2.save()
|
|
|
|
|
|
|
|
# Exact query with value None returns nothing ("is NULL" in sql,
|
|
|
|
# but every 'id' field has a value).
|
2017-04-10 20:59:29 +08:00
|
|
|
self.assertSequenceEqual(Choice.objects.filter(choice__exact=None), [])
|
2010-09-28 16:16:47 +08:00
|
|
|
|
2013-12-03 17:24:45 +08:00
|
|
|
# The same behavior for iexact query.
|
2017-04-10 20:59:29 +08:00
|
|
|
self.assertSequenceEqual(Choice.objects.filter(choice__iexact=None), [])
|
2013-12-03 17:24:45 +08:00
|
|
|
|
2010-09-28 16:16:47 +08:00
|
|
|
# Excluding the previous result returns everything.
|
2017-04-10 20:59:29 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Choice.objects.exclude(choice=None).order_by("id"), [c1, c2]
|
|
|
|
)
|
2010-09-28 16:16:47 +08:00
|
|
|
|
|
|
|
# Valid query, but fails because foo isn't a keyword
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = (
|
|
|
|
"Cannot resolve keyword 'foo' into field. Choices are: choice, id, poll, "
|
|
|
|
"poll_id"
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
Choice.objects.filter(foo__exact=None)
|
2010-09-28 16:16:47 +08:00
|
|
|
|
2013-12-03 17:24:45 +08:00
|
|
|
# Can't use None on anything other than __exact and __iexact
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, "Cannot use None as a query value"):
|
2016-01-17 19:26:39 +08:00
|
|
|
Choice.objects.filter(id__gt=None)
|
2010-09-28 16:16:47 +08:00
|
|
|
|
2022-01-14 20:44:17 +08:00
|
|
|
def test_unsaved(self):
|
|
|
|
poll = Poll(question="How?")
|
|
|
|
msg = (
|
|
|
|
"'Poll' instance needs to have a primary key value before this "
|
|
|
|
"relationship can be used."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
poll.choice_set.all()
|
2010-09-28 16:16:47 +08:00
|
|
|
|
|
|
|
def test_reverse_relations(self):
|
|
|
|
"""
|
|
|
|
Querying across reverse relations and then another relation should
|
|
|
|
insert outer joins correctly so as not to exclude results.
|
|
|
|
"""
|
|
|
|
obj = OuterA.objects.create()
|
2017-04-10 20:59:29 +08:00
|
|
|
self.assertSequenceEqual(OuterA.objects.filter(inner__third=None), [obj])
|
|
|
|
self.assertSequenceEqual(OuterA.objects.filter(inner__third__data=None), [obj])
|
2010-09-28 16:16:47 +08:00
|
|
|
|
2017-04-10 20:59:29 +08:00
|
|
|
inner = Inner.objects.create(first=obj)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Inner.objects.filter(first__inner__third=None), [inner]
|
|
|
|
)
|
2010-09-28 16:16:47 +08:00
|
|
|
|
2011-02-08 22:06:02 +08:00
|
|
|
# Ticket #13815: check if <reverse>_isnull=False does not produce
|
|
|
|
# faulty empty lists
|
2017-04-10 20:59:29 +08:00
|
|
|
outerb = OuterB.objects.create(data="reverse")
|
|
|
|
self.assertSequenceEqual(OuterB.objects.filter(inner__isnull=False), [])
|
2011-02-08 22:06:02 +08:00
|
|
|
Inner.objects.create(first=obj)
|
2017-04-10 20:59:29 +08:00
|
|
|
self.assertSequenceEqual(OuterB.objects.exclude(inner__isnull=False), [outerb])
|