from datetime import date
from unittest import TestCase

from corehq.apps.es import filters
from corehq.apps.es.es_query import HQESQuery
from corehq.apps.es.tests.utils import ElasticTestMixin
from corehq.elastic import SIZE_LIMIT


class TestFilters(ElasticTestMixin, TestCase):

    def test_nested_filter(self):
        json_output = {
            "query": {
                "filtered": {
                    "filter": {
                        "and": [
                            {"nested": {
                                "path": "actions",
                                "filter": {
                                    "range": {
                                        "actions.date": {
                                            "gte": "2015-01-01",
                                            "lt": "2015-02-01"
                                        }
                                    }
                                }
                            }},
                            {"match_all": {}}
                        ]
                    },
                    "query": {"match_all": {}}
                }
            },
            "size": SIZE_LIMIT
        }

        start, end = date(2015, 1, 1), date(2015, 2, 1)
        query = (HQESQuery('cases')
                 .nested("actions",
                         filters.date_range("actions.date", gte=start, lt=end)))

        self.checkQuery(query, json_output)


class TestSourceFiltering(ElasticTestMixin, TestCase):

    def test_source_include(self):
        json_output = {
            "query": {
                "filtered": {
                    "filter": {
                        "and": [
                            {"match_all": {}}
                        ]
                    },
                    "query": {"match_all": {}}
                }
            },
            "size": SIZE_LIMIT,
            "_source": "source_obj"
        }
        q = HQESQuery('forms').source('source_obj')
        self.checkQuery(q, json_output)

@@ -1438,16 +1438,822 @@
 utput)%0A%0A
+    def test_not_term_filter(self):%0A        json_output = %7B%0A            %22query%22: %7B%0A                %22filtered%22: %7B%0A                    %22filter%22: %7B%0A                        %22and%22: %5B%0A                            %7B%0A                                %22not%22: %7B%0A                                    %22term%22: %7B%0A                                        %22type%22: %22badcasetype%22%0A                                    %7D%0A                                %7D%0A                            %7D,%0A                            %7B%22match_all%22: %7B%7D%7D%0A                        %5D%0A                    %7D,%0A                    %22query%22: %7B%22match_all%22: %7B%7D%7D%0A                %7D%0A            %7D,%0A            %22size%22: SIZE_LIMIT%0A        %7D%0A%0A        query = HQESQuery('cases').filter(filters.not_term('type', 'badcasetype'))%0A%0A        self.checkQuery(query, json_output)%0A%0A
 %0Aclass T

Add test for es query not_term