Exemple #1
0
    def test_should_create_paginated_query_with_given_paginated_parameters(self):
        with patch("datawinners.search.query.elasticutils") as elasticUtilsMock:
            with patch("datawinners.search.query.QueryBuilder.get_query") as query:
                query.return_value = elasticUtilsMock
                elasticUtilsMock.order_by.return_value = elasticUtilsMock
                query_params = {
                    "start_result_number": 1,
                    "number_of_results": 10,
                    "order": "-",
                    "sort_field": "field_name_to_order_by"
                }

                query_builder = QueryBuilder()
                query_builder.create_paginated_query(query_builder.get_query("database_name", "subject_type"), query_params)

                elasticUtilsMock.__getitem__.assert_called_with(slice(1, 11, None))
Exemple #2
0
    def test_should_create_query_with_index_as_given_database_name(self):
        with patch("datawinners.search.query.elasticutils") as elasticUtilsMock:
            elasticUtilsMock.S.return_value = elasticUtilsMock
            elasticUtilsMock.es.return_value = elasticUtilsMock

            QueryBuilder().get_query("database_name", "entity_type")

            elasticUtilsMock.indexes.assert_called_with("database_name")
Exemple #3
0
    def test_should_return_match_all_query_when_no_query_text_present(self):
        searchMock = Mock()
        query_text = ""
        searchMock.query.return_value = searchMock

        actual_query = QueryBuilder().add_query_criteria([], searchMock,query_text)

        searchMock.query.assert_called_with()
        self.assertEquals(actual_query, searchMock)
Exemple #4
0
    def test_should_create_query_with_given_search_url(self):
        with patch("datawinners.search.query.elasticutils") as elasticUtilsMock:
            with patch("datawinners.search.query.ELASTIC_SEARCH_URL") as searchUrl:
                elasticUtilsMock.S.return_value = elasticUtilsMock

                QueryBuilder().get_query("database_name", "subject_type")

                elasticUtilsMock.S.assert_called_with()
                elasticUtilsMock.es.assert_called_with(urls=searchUrl)
Exemple #5
0
    def test_should_filter_query_with_only_non_void_documents(self):
        with patch("datawinners.search.query.elasticutils") as elasticUtilsMock:
            elasticUtilsMock.S.return_value = elasticUtilsMock
            elasticUtilsMock.es.return_value = elasticUtilsMock
            elasticUtilsMock.indexes.return_value = elasticUtilsMock
            elasticUtilsMock.doctypes.return_value = elasticUtilsMock

            QueryBuilder().get_query("database_name", "subject_type")

            elasticUtilsMock.filter.assert_called_with(void=False)
Exemple #6
0
    def test_should_return_escaped_query_for_given_search_fields_when_query_text_present(self):
        searchMock = Mock()
        query_text = "query_text"

        with patch("datawinners.search.query.ElasticUtilsHelper") as elastic_utils_helper:
            elastic_utils_helper.return_value = elastic_utils_helper
            elastic_utils_helper.replace_special_chars.return_value = "query_text_escaped"

            QueryBuilder().add_query_criteria(["query_field1", "query_field2"], searchMock, query_text)

            elastic_utils_helper.replace_special_chars.assert_called_with('query_text')
            searchMock.query_raw.assert_called_with({
                "query_string": {
                    "fields": ("query_field1", "query_field2"),
                    "query": "query_text_escaped"
                }
            })
 def __init__(self, query_params=None):
     Query.__init__(self, SubjectQueryResponseCreator(), QueryBuilder(),
                    query_params)
 def __init__(self, form_model=None):
     QueryBuilder.__init__(self)
     self.form_model = form_model
Exemple #9
0
 def get_query(self, database_name, doc_type=REPORTER):
     query = QueryBuilder().get_query(database_name=database_name, doc_type=doc_type)
     return query.filter(~ elasticutils.F(short_code_value='test'))