2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 05:34:56 +08:00
|
|
|
|
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).
|
|
|
|
self.assertQuerysetEqual(Choice.objects.filter(choice__exact=None), [])
|
|
|
|
|
2013-12-03 17:24:45 +08:00
|
|
|
# The same behavior for iexact query.
|
|
|
|
self.assertQuerysetEqual(Choice.objects.filter(choice__iexact=None), [])
|
|
|
|
|
2010-09-28 16:16:47 +08:00
|
|
|
# Excluding the previous result returns everything.
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-20 07:33:10 +08:00
|
|
|
Choice.objects.exclude(choice=None).order_by('id'),
|
|
|
|
[
|
|
|
|
'<Choice: Choice: Because. in poll Q: Why? >',
|
|
|
|
'<Choice: Choice: Why Not? in poll Q: Why? >'
|
|
|
|
]
|
2010-09-28 16:16:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
# Valid query, but fails because foo isn't a keyword
|
|
|
|
self.assertRaises(FieldError, Choice.objects.filter, foo__exact=None)
|
|
|
|
|
2013-12-03 17:24:45 +08:00
|
|
|
# Can't use None on anything other than __exact and __iexact
|
2010-09-28 16:16:47 +08:00
|
|
|
self.assertRaises(ValueError, Choice.objects.filter, id__gt=None)
|
|
|
|
|
|
|
|
# Related managers use __exact=None implicitly if the object hasn't been saved.
|
|
|
|
p2 = Poll(question="How?")
|
2015-10-06 07:07:34 +08:00
|
|
|
self.assertEqual(repr(p2.choice_set.all()), '<QuerySet []>')
|
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()
|
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 23:15:49 +08:00
|
|
|
OuterA.objects.filter(inner__third=None),
|
2010-09-28 16:16:47 +08:00
|
|
|
['<OuterA: OuterA object>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 23:15:49 +08:00
|
|
|
OuterA.objects.filter(inner__third__data=None),
|
2010-09-28 16:16:47 +08:00
|
|
|
['<OuterA: OuterA object>']
|
|
|
|
)
|
|
|
|
|
2013-10-19 20:31:38 +08:00
|
|
|
Inner.objects.create(first=obj)
|
2010-09-28 16:16:47 +08:00
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 23:15:49 +08:00
|
|
|
Inner.objects.filter(first__inner__third=None),
|
2010-09-28 16:16:47 +08:00
|
|
|
['<Inner: Inner object>']
|
|
|
|
)
|
|
|
|
|
2011-02-08 22:06:02 +08:00
|
|
|
# Ticket #13815: check if <reverse>_isnull=False does not produce
|
|
|
|
# faulty empty lists
|
2013-10-19 20:31:38 +08:00
|
|
|
OuterB.objects.create(data="reverse")
|
2011-02-08 22:06:02 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
OuterB.objects.filter(inner__isnull=False),
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
Inner.objects.create(first=obj)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
OuterB.objects.exclude(inner__isnull=False),
|
|
|
|
['<OuterB: OuterB object>']
|
|
|
|
)
|