from datetime import datetime

from django.core.exceptions import FieldError
from django.db.models import CharField, F, Q
from django.db.models.expressions import SimpleCol
from django.db.models.fields.related_lookups import RelatedIsNull
from django.db.models.functions import Lower
from django.db.models.lookups import Exact, GreaterThan, IsNull, LessThan
from django.db.models.sql.query import Query
from django.db.models.sql.where import OR
from django.test import TestCase
from django.test.utils import register_lookup

from .models import Author, Item, ObjectC, Ranking


class TestQuery(TestCase):
    def test_simple_query(self):
        query = Query(Author)
        where = query.build_where(Q(num__gt=2))
        lookup = where.children[0]
        self.assertIsInstance(lookup, GreaterThan)
        self.assertEqual(lookup.rhs, 2)
        self.assertEqual(lookup.lhs.target, Author._meta.get_field('num'))

    def test_complex_query(self):
        query = Query(Author)
        where = query.build_where(Q(num__gt=2) | Q(num__lt=0))
        self.assertEqual(where.connector, OR)

        lookup = where.children[0]
        self.assertIsInstance(lookup, GreaterThan)
        self.assertEqual(lookup.rhs, 2)
        self.assertEqual(lookup.lhs.target, Author._meta.get_field('num'))

        lookup = where.children[1]
        self.assertIsInstance(lookup, LessThan)
        self.assertEqual(lookup.rhs, 0)
        self.assertEqual(lookup.lhs.target, Author._meta.get_field('num'))

    def test_multiple_fields(self):
        query = Query(Item)
        where = query.build_where(Q(modified__gt=F('created')))
        lookup = where.children[0]
        self.assertIsInstance(lookup, GreaterThan)
        self.assertIsInstance(lookup.rhs, SimpleCol)
        self.assertIsInstance(lookup.lhs, SimpleCol)
        self.assertEqual(lookup.rhs.target, Item._meta.get_field('created'))
        self.assertEqual(lookup.lhs.target, Item._meta.get_field('modified'))

    def test_transform(self):
        query = Query(Author)
        with register_lookup(CharField, Lower):
            where = query.build_where(~Q(name__lower='foo'))
        lookup = where.children[0]
        self.assertIsInstance(lookup, Exact)
        self.assertIsInstance(lookup.lhs, Lower)
        self.assertIsInstance(lookup.lhs.lhs, SimpleCol)
        self.assertEqual(lookup.lhs.lhs.target, Author._meta.get_field('name'))

    def test_negated_nullable(self):
        query = Query(Item)
        where = query.build_where(~Q(modified__lt=datetime(2017, 1, 1)))
        self.assertTrue(where.negated)
        lookup = where.children[0]
        self.assertIsInstance(lookup, LessThan)
        self.assertEqual(lookup.lhs.target, Item._meta.get_field('modified'))
        lookup = where.children[1]
        self.assertIsInstance(lookup, IsNull)
        self.assertEqual(lookup.lhs.target, Item._meta.get_field('modified'))

    def test_foreign_key(self):
        query = Query(Item)
        msg = 'Joined field references are not permitted in this query'
        with self.assertRaisesMessage(FieldError, msg):
            query.build_where(Q(creator__num__gt=2))

    def test_foreign_key_f(self):
        query = Query(Ranking)
        with self.assertRaises(FieldError):
            query.build_where(Q(rank__gt=F('author__num')))

    def test_foreign_key_exclusive(self):
        query = Query(ObjectC)
        where = query.build_where(Q(objecta=None) | Q(objectb=None))
        a_isnull = where.children[0]
        self.assertIsInstance(a_isnull, RelatedIsNull)
        self.assertIsInstance(a_isnull.lhs, SimpleCol)
        self.assertEqual(a_isnull.lhs.target, ObjectC._meta.get_field('objecta'))
        b_isnull = where.children[1]
        self.assertIsInstance(b_isnull, RelatedIsNull)
        self.assertIsInstance(b_isnull.lhs, SimpleCol)
        self.assertEqual(b_isnull.lhs.target, ObjectC._meta.get_field('objectb'))

[((19, 16, 19, 29), 'django.db.models.sql.query.Query', 'Query', ({(19, 22, 19, 28): 'Author'}, {}), '(Author)', False, 'from django.db.models.sql.query import Query\n'), ((27, 16, 27, 29), 'django.db.models.sql.query.Query', 'Query', ({(27, 22, 27, 28): 'Author'}, {}), '(Author)', False, 'from django.db.models.sql.query import Query\n'), ((42, 16, 42, 27), 'django.db.models.sql.query.Query', 'Query', ({(42, 22, 42, 26): 'Item'}, {}), '(Item)', False, 'from django.db.models.sql.query import Query\n'), ((52, 16, 52, 29), 'django.db.models.sql.query.Query', 'Query', ({(52, 22, 52, 28): 'Author'}, {}), '(Author)', False, 'from django.db.models.sql.query import Query\n'), ((62, 16, 62, 27), 'django.db.models.sql.query.Query', 'Query', ({(62, 22, 62, 26): 'Item'}, {}), '(Item)', False, 'from django.db.models.sql.query import Query\n'), ((73, 16, 73, 27), 'django.db.models.sql.query.Query', 'Query', ({(73, 22, 73, 26): 'Item'}, {}), '(Item)', False, 'from django.db.models.sql.query import Query\n'), ((79, 16, 79, 30), 'django.db.models.sql.query.Query', 'Query', ({(79, 22, 79, 29): 'Ranking'}, {}), '(Ranking)', False, 'from django.db.models.sql.query import Query\n'), ((84, 16, 84, 30), 'django.db.models.sql.query.Query', 'Query', ({(84, 22, 84, 29): 'ObjectC'}, {}), '(ObjectC)', False, 'from django.db.models.sql.query import Query\n'), ((20, 34, 20, 46), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import CharField, F, Q\n'), ((53, 13, 53, 46), 'django.test.utils.register_lookup', 'register_lookup', ({(53, 29, 53, 38): 'CharField', (53, 40, 53, 45): 'Lower'}, {}), '(CharField, Lower)', False, 'from django.test.utils import register_lookup\n'), ((28, 34, 28, 46), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import CharField, F, Q\n'), ((28, 49, 28, 61), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import CharField, F, Q\n'), ((76, 30, 76, 51), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import CharField, F, Q\n'), ((85, 34, 85, 49), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import CharField, F, Q\n'), ((85, 52, 85, 67), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import CharField, F, Q\n'), ((43, 49, 43, 61), 'django.db.models.F', 'F', ({(43, 51, 43, 60): '"""created"""'}, {}), "('created')", False, 'from django.db.models import CharField, F, Q\n'), ((54, 39, 54, 59), 'django.db.models.Q', 'Q', (), '', False, 'from django.db.models import CharField, F, Q\n'), ((63, 50, 63, 70), 'datetime.datetime', 'datetime', ({(63, 59, 63, 63): '2017', (63, 65, 63, 66): '1', (63, 68, 63, 69): '1'}, {}), '(2017, 1, 1)', False, 'from datetime import datetime\n'), ((81, 41, 81, 57), 'django.db.models.F', 'F', ({(81, 43, 81, 56): '"""author__num"""'}, {}), "('author__num')", False, 'from django.db.models import CharField, F, Q\n')]