import unittest
from src.services.analysis.query_request import ElasticQuery


class TestQueryRequest(unittest.TestCase):
    class Client():
        def search(self, index, body):
            resp = {'hits': {'hits': "", 'total': {'value': ""}}}
            return resp

    def setUp(self) -> None:
        self.query = ElasticQuery()

    def test_match_from_timestamp(self):
        self.query.match_from_timestamp('0', '0')
        self.assertTrue('timestamp' in self.query.final_query['query']['bool']['must'][0]['range'])

    def test_match_from_field(self):
        self.query.match_from_field('test', '0', '0')
        self.assertTrue('test' in self.query.final_query['query']['bool']['must'][0]['range'])

    def test_send(self):
        client = TestQueryRequest.Client()
        self.query.client = client
        resp = self.query.send('index')
        self.assertIsNotNone(resp)

    def test_from_string(self):
        query_str = "{'query': {'bool': {'must': [{'range': {'timestamp': {'gte': '2022-04-21T11:13:28', " \
                    "'lte': '2022-04-22T11:14:03'}}}, {'range': {'percent': {'gte': 0, 'lte': 87}}}]}}} "
        query_str = query_str.replace("'", '"')
        query = ElasticQuery.from_string(query_str)
        self.assertIsNotNone(query)


if __name__ == '__main__':
    unittest.main()
