2013-07-29 19:19:04 +02:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 23:34:56 +02:00
|
|
|
|
2010-09-28 10:16:47 +02:00
|
|
|
from django.test import TestCase
|
|
|
|
from django.core.exceptions import FieldError
|
|
|
|
|
2011-10-13 23:34:56 +02:00
|
|
|
from .models import Poll, Choice, OuterA, Inner, OuterB
|
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).
|
|
|
|
self.assertQuerysetEqual(Choice.objects.filter(choice__exact=None), [])
|
|
|
|
|
2013-12-03 10:24:45 +01:00
|
|
|
# The same behavior for iexact query.
|
|
|
|
self.assertQuerysetEqual(Choice.objects.filter(choice__iexact=None), [])
|
|
|
|
|
2010-09-28 10:16:47 +02:00
|
|
|
# Excluding the previous result returns everything.
|
|
|
|
self.assertQuerysetEqual(
|
2013-10-20 01:33:10 +02: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 10:16:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Valid query, but fails because foo isn't a keyword
|
|
|
|
self.assertRaises(FieldError, Choice.objects.filter, foo__exact=None)
|
|
|
|
|
2013-12-03 10:24:45 +01:00
|
|
|
# Can't use None on anything other than __exact and __iexact
|
2010-09-28 10:16:47 +02: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?")
|
2011-03-03 16:04:39 +01:00
|
|
|
self.assertEqual(repr(p2.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()
|
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 16:15:49 +01:00
|
|
|
OuterA.objects.filter(inner__third=None),
|
2010-09-28 10:16:47 +02:00
|
|
|
['<OuterA: OuterA object>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 16:15:49 +01:00
|
|
|
OuterA.objects.filter(inner__third__data=None),
|
2010-09-28 10:16:47 +02:00
|
|
|
['<OuterA: OuterA object>']
|
|
|
|
)
|
|
|
|
|
2013-10-19 14:31:38 +02:00
|
|
|
Inner.objects.create(first=obj)
|
2010-09-28 10:16:47 +02:00
|
|
|
self.assertQuerysetEqual(
|
2013-02-10 16:15:49 +01:00
|
|
|
Inner.objects.filter(first__inner__third=None),
|
2010-09-28 10:16:47 +02:00
|
|
|
['<Inner: Inner object>']
|
|
|
|
)
|
|
|
|
|
2011-02-08 15:06:02 +01:00
|
|
|
# Ticket #13815: check if <reverse>_isnull=False does not produce
|
|
|
|
# faulty empty lists
|
2013-10-19 14:31:38 +02:00
|
|
|
OuterB.objects.create(data="reverse")
|
2011-02-08 15:06:02 +01: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>']
|
|
|
|
)
|