def test_regex_replace(self): """ https://sentry.io/dimagi/commcarehq/issues/314043351/ https://manage.dimagi.com/default.asp?262513 https://manage.dimagi.com/default.asp?265167#1419523 """ initial_custom_query = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "__thing_1" } } } } } } } expected = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "boop-beep+boop" } } } } } } } search_criteria = { "{}__thing_1".format(SEARCH_QUERY_CUSTOM_VALUE): "boop-beep+\\boop", "name": "Jon Snow", } self.maxDiff = None self.assertDictEqual( expected, replace_custom_query_variables(initial_custom_query, search_criteria, None))
def _add_case_search_additions(self): query_addition_id = self.criteria.pop(SEARCH_QUERY_ADDITION_KEY, None) if query_addition_id: ignore_patterns = self.config.ignore_patterns.filter(domain=self.domain, case_type=self.case_type) query_addition = CaseSearchQueryAddition.objects.get( id=query_addition_id, domain=self.domain).query_addition query_addition = replace_custom_query_variables(query_addition, self.criteria, ignore_patterns) self.query_addition_debug_details['original_query'] = self.search_es.get_query() self.query_addition_debug_details['query_addition'] = query_addition new_query = merge_queries(self.search_es.get_query(), query_addition) self.query_addition_debug_details['new_query'] = new_query self.search_es = self.search_es.set_query(new_query)
def _add_case_search_additions(self): query_addition_id = self.criteria.pop(SEARCH_QUERY_ADDITION_KEY, None) if query_addition_id: ignore_patterns = self.config.ignore_patterns.filter( domain=self.domain, case_type=self.case_type) query_addition = CaseSearchQueryAddition.objects.get( id=query_addition_id, domain=self.domain).query_addition query_addition = replace_custom_query_variables( query_addition, self.criteria, ignore_patterns) self.query_addition_debug_details[ 'original_query'] = self.search_es.get_query() self.query_addition_debug_details[ 'query_addition'] = query_addition new_query = merge_queries(self.search_es.get_query(), query_addition) self.query_addition_debug_details['new_query'] = new_query self.search_es = self.search_es.set_query(new_query)
def test_remove_unneeded_custom_values(self): config, created = CaseSearchConfig.objects.get_or_create(pk='domain', enabled=True) initial_custom_query = { "include_if": "__thing_1", "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "__thing_1" } } } } } } } search_criteria = { "{}__thing_2".format(SEARCH_QUERY_CUSTOM_VALUE): "boop", "name": "Jon Snow", } expected = {} self.assertDictEqual( expected, replace_custom_query_variables( initial_custom_query, search_criteria, config.ignore_patterns.filter(domain='domain', case_type='case_type'))) search_criteria = { "{}__thing_1".format(SEARCH_QUERY_CUSTOM_VALUE): "boop", "name": "Jon Snow", } expected = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "boop" } } } } } } } self.assertDictEqual( expected, replace_custom_query_variables( initial_custom_query, search_criteria, config.ignore_patterns.filter(domain='domain', case_type='case_type')))
def test_custom_values(self): config, created = CaseSearchConfig.objects.get_or_create(pk='domain', enabled=True) rc = IgnorePatterns( domain='domain', case_type='case_type', case_property='thing_1', regex=' removed', ) rc.save() config.ignore_patterns.add(rc) initial_custom_query = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "__thing_1" } } } } } } } search_criteria = { "{}__thing_1".format(SEARCH_QUERY_CUSTOM_VALUE): "boop removed", "name": "Jon Snow", } expected = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "boop" } } } } } } } self.assertDictEqual( expected, replace_custom_query_variables( initial_custom_query, search_criteria, config.ignore_patterns.filter(domain='domain', case_type='case_type') ) )
def test_regex_replace(self): """ https://sentry.io/dimagi/commcarehq/issues/314043351/ https://manage.dimagi.com/default.asp?262513 https://manage.dimagi.com/default.asp?265167#1419523 """ initial_custom_query = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "__thing_1" } } } } } } } expected = { "nested": { "path": "case_properties", "query": { "filtered": { "filter": { "term": { "case_properties.key": "thing_1" } }, "query": { "match": { "case_properties.value": { "fuzziness": "0", "query": "boop-beep+boop" } } } } } } } search_criteria = { "{}__thing_1".format(SEARCH_QUERY_CUSTOM_VALUE): "boop-beep+\\boop", "name": "Jon Snow", } self.maxDiff = None self.assertDictEqual( expected, replace_custom_query_variables( initial_custom_query, search_criteria, None ) )