2010-09-28 10:16:47 +02:00
|
|
|
from django.core.exceptions import FieldError
|
2015-01-28 13:35:27 +01:00
|
|
|
from django.test import TestCase
|
2010-09-28 10:16:47 +02:00
|
|
|
|
2015-01-28 13:35:27 +01:00
|
|
|
from .models import Choice, Inner, OuterA, OuterB, Poll
|
2010-09-28 10:16:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
class NullQueriesTests(TestCase):
|
|
|
|
def test_none_as_null(self):
|
|
|
|
"""
|
|
|
|
Regression test for the use of None as a query value.
|
|
|
|
|
2013-12-03 10:24:45 +01:00
|
|
|
None is interpreted as an SQL NULL, but only in __exact and __iexact
|
|
|
|
queries.
|
2010-09-28 10:16:47 +02: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 14:59:29 +02:00
|
|
|
self.assertSequenceEqual(Choice.objects.filter(choice__exact=None), [])
|
2010-09-28 10:16:47 +02:00
|
|
|
|
2013-12-03 10:24:45 +01:00
|
|
|
# The same behavior for iexact query.
|
2017-04-10 14:59:29 +02:00
|
|
|
self.assertSequenceEqual(Choice.objects.filter(choice__iexact=None), [])
|
2013-12-03 10:24:45 +01:00
|
|
|
|
2010-09-28 10:16:47 +02:00
|
|
|
# Excluding the previous result returns everything.
|
2017-04-10 14:59:29 +02:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Choice.objects.exclude(choice=None).order_by("id"), [c1, c2]
|
|
|
|
)
|
2010-09-28 10:16:47 +02:00
|
|
|
|
|
|
|
# Valid query, but fails because foo isn't a keyword
|
2017-05-28 21:37:21 +02:00
|
|
|
msg = (
|
|
|
|
"Cannot resolve keyword 'foo' into field. Choices are: choice, id, poll, "
|
|
|
|
"poll_id"
|
2022-02-04 08:08:27 +01:00
|
|
|
)
|
2017-05-28 21:37:21 +02:00
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
2016-01-17 12:26:39 +01:00
|
|
|
Choice.objects.filter(foo__exact=None)
|
2010-09-28 10:16:47 +02:00
|
|
|
|
2013-12-03 10:24:45 +01:00
|
|
|
# Can't use None on anything other than __exact and __iexact
|
2017-05-28 21:37:21 +02:00
|
|
|
with self.assertRaisesMessage(ValueError, "Cannot use None as a query value"):
|
2016-01-17 12:26:39 +01:00
|
|
|
Choice.objects.filter(id__gt=None)
|
2010-09-28 10:16:47 +02:00
|
|
|
|
2022-01-14 13:44:17 +01: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 10:16:47 +02: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 14:59:29 +02:00
|
|
|
self.assertSequenceEqual(OuterA.objects.filter(inner__third=None), [obj])
|
|
|
|
self.assertSequenceEqual(OuterA.objects.filter(inner__third__data=None), [obj])
|
2010-09-28 10:16:47 +02:00
|
|
|
|
2017-04-10 14:59:29 +02:00
|
|
|
inner = Inner.objects.create(first=obj)
|
|
|
|
self.assertSequenceEqual(
|
|
|
|
Inner.objects.filter(first__inner__third=None), [inner]
|
|
|
|
)
|
2010-09-28 10:16:47 +02:00
|
|
|
|
2011-02-08 15:06:02 +01:00
|
|
|
# Ticket #13815: check if <reverse>_isnull=False does not produce
|
|
|
|
# faulty empty lists
|
2017-04-10 14:59:29 +02:00
|
|
|
outerb = OuterB.objects.create(data="reverse")
|
|
|
|
self.assertSequenceEqual(OuterB.objects.filter(inner__isnull=False), [])
|
2011-02-08 15:06:02 +01:00
|
|
|
Inner.objects.create(first=obj)
|
2017-04-10 14:59:29 +02:00
|
|
|
self.assertSequenceEqual(OuterB.objects.exclude(inner__isnull=False), [outerb])
|