from django.test.testcases import SimpleTestCase

from corehq.apps.es.sms import SMSES
from corehq.apps.es.tests.utils import ElasticTestMixin
from corehq.elastic import SIZE_LIMIT


class TestSMSES(ElasticTestMixin, SimpleTestCase):
    def test_processed_or_incoming(self):
        json_output = {
            "query": {
                "filtered": {
                    "filter": {
                        "and": [
                            {"term": {"domain.exact": "demo"}},
                            {
                                "not": {
                                    "and": (
                                        {"term": {"direction": "o"}},
                                        {"term": {"processed": False}},
                                    )
                                }
                            },
                            {"match_all": {}},
                        ]
                    },
                    "query": {"match_all": {}}
                }
            },
            "size": SIZE_LIMIT
        }

        query = SMSES().domain('demo').processed_or_incoming_messages()

        self.checkQuery(query, json_output)

from django.test.testcases import SimpleTestCase

from corehq.apps.es.sms import SMSES
from corehq.apps.es.tests.utils import ElasticTestMixin
from corehq.elastic import SIZE_LIMIT


class TestSMSES(ElasticTestMixin, SimpleTestCase):
    def test_processed_or_incoming(self):
        json_output = {
            "query": {
                "filtered": {
                    "filter": {
                        "and": [
                            {"term": {"domain.exact": "demo"}},
                            {
                                "or": (
                                    {
                                        "not": {"term": {"direction": "o"}},
                                    },
                                    {
                                        "not": {"term": {"processed": False}},
                                    }
                                ),
                            },
                            {"match_all": {}},
                        ]
                    },
                    "query": {"match_all": {}}
                }
            },
            "size": SIZE_LIMIT
        }

        query = SMSES().domain('demo').processed_or_incoming_messages()

        self.checkQuery(query, json_output)

Fix SMS ES test after not-and rewrite
Fix SMS ES test after not-and rewrite

  from django.test.testcases import SimpleTestCase
  
  from corehq.apps.es.sms import SMSES
  from corehq.apps.es.tests.utils import ElasticTestMixin
  from corehq.elastic import SIZE_LIMIT
  
  
  class TestSMSES(ElasticTestMixin, SimpleTestCase):
      def test_processed_or_incoming(self):
          json_output = {
              "query": {
                  "filtered": {
                      "filter": {
                          "and": [
                              {"term": {"domain.exact": "demo"}},
                              {
-                                 "not": {
+                                 "or": (
-                                     "and": (
-                                         {"term": {"direction": "o"}},
-                                         {"term": {"processed": False}},
-                                     )
+                                     {
+                                         "not": {"term": {"direction": "o"}},
+                                     },
+                                     {
+                                         "not": {"term": {"processed": False}},
+                                     }
-                                 }
+                                 ),
                              },
                              {"match_all": {}},
                          ]
                      },
                      "query": {"match_all": {}}
                  }
              },
              "size": SIZE_LIMIT
          }
  
          query = SMSES().domain('demo').processed_or_incoming_messages()
  
          self.checkQuery(query, json_output)
  
Fix SMS ES test after not-and rewrite
## Code Before:
from django.test.testcases import SimpleTestCase

from corehq.apps.es.sms import SMSES
from corehq.apps.es.tests.utils import ElasticTestMixin
from corehq.elastic import SIZE_LIMIT


class TestSMSES(ElasticTestMixin, SimpleTestCase):
    def test_processed_or_incoming(self):
        json_output = {
            "query": {
                "filtered": {
                    "filter": {
                        "and": [
                            {"term": {"domain.exact": "demo"}},
                            {
                                "not": {
                                    "and": (
                                        {"term": {"direction": "o"}},
                                        {"term": {"processed": False}},
                                    )
                                }
                            },
                            {"match_all": {}},
                        ]
                    },
                    "query": {"match_all": {}}
                }
            },
            "size": SIZE_LIMIT
        }

        query = SMSES().domain('demo').processed_or_incoming_messages()

        self.checkQuery(query, json_output)

## Instruction:
Fix SMS ES test after not-and rewrite
## Code After:
from django.test.testcases import SimpleTestCase

from corehq.apps.es.sms import SMSES
from corehq.apps.es.tests.utils import ElasticTestMixin
from corehq.elastic import SIZE_LIMIT


class TestSMSES(ElasticTestMixin, SimpleTestCase):
    def test_processed_or_incoming(self):
        json_output = {
            "query": {
                "filtered": {
                    "filter": {
                        "and": [
                            {"term": {"domain.exact": "demo"}},
                            {
                                "or": (
                                    {
                                        "not": {"term": {"direction": "o"}},
                                    },
                                    {
                                        "not": {"term": {"processed": False}},
                                    }
                                ),
                            },
                            {"match_all": {}},
                        ]
                    },
                    "query": {"match_all": {}}
                }
            },
            "size": SIZE_LIMIT
        }

        query = SMSES().domain('demo').processed_or_incoming_messages()

        self.checkQuery(query, json_output)
