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