regression test for the use of none as a query value none is interpreted as an sql null but only in exact and iexact queries set up some initial polls and choices exact query with value none returns nothing is null in sql but every id field has a value the same behavior for iexact query excluding the previous result returns everything valid query but fails because foo isn t a keyword can t use none on anything other than exact and iexact querying across reverse relations and then another relation should insert outer joins correctly so as not to exclude results ticket 13815 check if reverseisnullfalse does not produce faulty empty lists regression test for the use of none as a query value none is interpreted as an sql null but only in __exact and __iexact queries set up some initial polls and choices exact query with value none returns nothing is null in sql but every id field has a value the same behavior for iexact query excluding the previous result returns everything valid query but fails because foo isn t a keyword can t use none on anything other than __exact and __iexact querying across reverse relations and then another relation should insert outer joins correctly so as not to exclude results ticket 13815 check if reverse _isnull false does not produce faulty empty lists
from django.core.exceptions import FieldError
from django.test import TestCase

from .models import Choice, Inner, OuterA, OuterB, Poll


class NullQueriesTests(TestCase):
    def test_none_as_null(self):
        
        p1 = Poll(question="Why?")
        p1.save()
        c1 = Choice(poll=p1, choice="Because.")
        c1.save()
        c2 = Choice(poll=p1, choice="Why Not?")
        c2.save()

        
        
        self.assertSequenceEqual(Choice.objects.filter(choice__exact=None), [])

        
        self.assertSequenceEqual(Choice.objects.filter(choice__iexact=None), [])

        
        self.assertSequenceEqual(
            Choice.objects.exclude(choice=None).order_by("id"), [c1, c2]
        )

        
        msg = (
            "Cannot resolve keyword 'foo' into field. Choices are: choice, id, poll, "
            "poll_id"
        )
        with self.assertRaisesMessage(FieldError, msg):
            Choice.objects.filter(foo__exact=None)

        
        with self.assertRaisesMessage(ValueError, "Cannot use None as a query value"):
            Choice.objects.filter(id__gt=None)

    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()

    def test_reverse_relations(self):
        
        obj = OuterA.objects.create()
        self.assertSequenceEqual(OuterA.objects.filter(inner__third=None), [obj])
        self.assertSequenceEqual(OuterA.objects.filter(inner__third__data=None), [obj])

        inner = Inner.objects.create(first=obj)
        self.assertSequenceEqual(
            Inner.objects.filter(first__inner__third=None), [inner]
        )

        
        
        outerb = OuterB.objects.create(data="reverse")
        self.assertSequenceEqual(OuterB.objects.filter(inner__isnull=False), [])
        Inner.objects.create(first=obj)
        self.assertSequenceEqual(OuterB.objects.exclude(inner__isnull=False), [outerb])