예제 #1
0
class TestLinkedCaseClaim(BaseLinkedCaseClaimTest):
    def test_link_enables_case_search(self):
        existing_search_config = CaseSearchConfig(
            domain=self.domain_link.linked_domain, enabled=False)
        existing_search_config.save()
        fuzzy_property = FuzzyProperties(domain=self.domain_link.linked_domain,
                                         case_type='case',
                                         properties=['a', 'b', 'd'])
        fuzzy_property.save()
        existing_search_config.fuzzy_properties.add(fuzzy_property)
        update_case_search_config(self.domain_link)
        search_config = (
            CaseSearchConfig.objects.prefetch_related('fuzzy_properties').get(
                domain=self.domain_link.linked_domain))

        self.assertTrue(search_config.enabled)

        # overwrites the existing fuzzy properties settings
        self.assertEqual(1, search_config.fuzzy_properties.count())
        self.assertEqual(['a', 'b', 'c'],
                         search_config.fuzzy_properties.all()[0].properties)

        self.assertNotEqual(search_config.pk, self.search_config.pk)

    def test_query_addition(self):
        name = 'test query addition'
        addition = {'test': 'addition'}
        self.query_addition = CaseSearchQueryAddition(
            domain=self.domain,
            name=name,
            query_addition=addition,
        )
        self.query_addition.save()

        update_case_search_config(self.domain_link)
        query_addition = CaseSearchQueryAddition.objects.get(
            domain=self.domain_link.linked_domain)
        self.assertEqual(name, query_addition.name)
        self.assertEqual(addition, query_addition.query_addition)
        self.assertNotEqual(query_addition.pk, self.query_addition.pk)
예제 #2
0
class TestLinkedCaseClaim(BaseLinkedCaseClaimTest):
    def test_link_enables_case_search(self):
        existing_search_config = CaseSearchConfig(domain=self.domain_link.linked_domain, enabled=False)
        existing_search_config.save()
        fuzzy_property = FuzzyProperties(domain=self.domain_link.linked_domain, case_type='case',
                                         properties=['a', 'b', 'd'])
        fuzzy_property.save()
        existing_search_config.fuzzy_properties.add(fuzzy_property)
        update_case_search_config(self.domain_link)
        search_config = (CaseSearchConfig.objects
                         .prefetch_related('fuzzy_properties')
                         .get(domain=self.domain_link.linked_domain))

        self.assertTrue(search_config.enabled)

        # overwrites the existing fuzzy properties settings
        self.assertEqual(1, search_config.fuzzy_properties.count())
        self.assertEqual(['a', 'b', 'c'], search_config.fuzzy_properties.all()[0].properties)

        self.assertNotEqual(search_config.pk, self.search_config.pk)

    def test_query_addition(self):
        name = 'test query addition'
        addition = {'test': 'addition'}
        self.query_addition = CaseSearchQueryAddition(
            domain=self.domain,
            name=name,
            query_addition=addition,
        )
        self.query_addition.save()

        update_case_search_config(self.domain_link)
        query_addition = CaseSearchQueryAddition.objects.get(domain=self.domain_link.linked_domain)
        self.assertEqual(name, query_addition.name)
        self.assertEqual(addition, query_addition.query_addition)
        self.assertNotEqual(query_addition.pk, self.query_addition.pk)
    def test_search_query_addition(self, run_query_mock):
        self.maxDiff = None
        new_must_clause = {
            "bool": {
                "should": [{
                    "nested": {
                        "path": "case_properties",
                        "query": {
                            "filtered": {
                                "filter": {
                                    "and": [{
                                        "term": {
                                            "case_properties.key.exact":
                                            "is_inactive"
                                        }
                                    }, {
                                        "term": {
                                            "case_properties.value.exact":
                                            "yes"
                                        }
                                    }]
                                },
                                "query": {
                                    "match_all": {}
                                }
                            }
                        }
                    }
                }, {
                    "nested": {
                        "path": "case_properties",
                        "query": {
                            "filtered": {
                                "filter": {
                                    "and": [{
                                        "term": {
                                            "case_properties.key.exact":
                                            "awaiting_claim"
                                        }
                                    }, {
                                        "term": {
                                            "case_properties.value.exact":
                                            "yes"
                                        }
                                    }]
                                },
                                "query": {
                                    "match_all": {}
                                }
                            }
                        }
                    }
                }]
            }
        }

        query_partial = {"bool": {"must": [new_must_clause]}}

        query_addition = CaseSearchQueryAddition(domain=DOMAIN,
                                                 name="foo",
                                                 query_addition=query_partial)
        query_addition.save()

        client = Client()
        client.login(username=USERNAME, password=PASSWORD)
        url = reverse('remote_search', kwargs={'domain': DOMAIN})
        some_case_name = "wut"
        response = client.get(
            url, {
                'name': some_case_name,
                'case_type': CASE_TYPE,
                SEARCH_QUERY_ADDITION_KEY: query_addition.id
            })

        self.assertEqual(response.status_code, 200)

        expected_query = {
            'query': {
                'filtered': {
                    'filter': {
                        'and': [{
                            'term': {
                                'domain.exact': DOMAIN
                            }
                        }, {
                            'term': {
                                'type.exact': CASE_TYPE
                            }
                        }, {
                            'term': {
                                'closed': False
                            }
                        }, {
                            'match_all': {}
                        }]
                    },
                    'query': {
                        'bool': {
                            'must': [{
                                'nested': {
                                    'path': 'case_properties',
                                    'query': {
                                        'filtered': {
                                            'filter': {
                                                "and": ({
                                                    'term': {
                                                        'case_properties.key.exact':
                                                        'name'
                                                    }
                                                }, {
                                                    'term': {
                                                        'case_properties.value.exact':
                                                        some_case_name
                                                    }
                                                })
                                            },
                                            'query': {
                                                'match_all': {}
                                            }
                                        }
                                    }
                                }
                            }, new_must_clause]
                        }
                    }
                }
            },
            'size': CASE_SEARCH_MAX_RESULTS
        }
        run_query_mock.assert_called_with(
            "case_search",
            expected_query,
            debug_host=None,
            es_instance_alias=ES_DEFAULT_INSTANCE,
        )
    def test_search_query_addition(self, run_query_mock):
        self.maxDiff = None
        new_must_clause = {
            "bool": {
                "should": [
                    {
                        "nested": {
                            "path": "case_properties",
                            "query": {
                                "filtered": {
                                    "filter": {
                                        "and": [
                                            {
                                                "term": {
                                                    "case_properties.key.exact": "is_inactive"
                                                }
                                            },
                                            {
                                                "term": {
                                                    "case_properties.value.exact": "yes"
                                                }
                                            }
                                        ]
                                    },
                                    "query": {
                                        "match_all": {
                                        }
                                    }
                                }
                            }
                        }
                    },
                    {
                        "nested": {
                            "path": "case_properties",
                            "query": {
                                "filtered": {
                                    "filter": {
                                        "and": [
                                            {
                                                "term": {
                                                    "case_properties.key.exact": "awaiting_claim"
                                                }
                                            },
                                            {
                                                "term": {
                                                    "case_properties.value.exact": "yes"
                                                }
                                            }
                                        ]
                                    },
                                    "query": {
                                        "match_all": {
                                        }
                                    }
                                }
                            }
                        }
                    }
                ]
            }
        }

        query_partial = {
            "bool": {
                "must": [
                    new_must_clause
                ]
            }
        }

        query_addition = CaseSearchQueryAddition(domain=DOMAIN, name="foo", query_addition=query_partial)
        query_addition.save()

        client = Client()
        client.login(username=USERNAME, password=PASSWORD)
        url = reverse('remote_search', kwargs={'domain': DOMAIN})
        some_case_name = "wut"
        response = client.get(
            url,
            {'name': some_case_name, 'case_type': CASE_TYPE, SEARCH_QUERY_ADDITION_KEY: query_addition.id}
        )

        self.assertEqual(response.status_code, 200)

        expected_query = {
            'query': {
                'filtered': {
                    'filter': {
                        'and': [
                            {'term': {'domain.exact': DOMAIN}},
                            {'term': {'type.exact': CASE_TYPE}},
                            {'term': {'closed': False}},
                            {'match_all': {}}
                        ]
                    },
                    'query': {
                        'bool': {
                            'must': [
                                {
                                    'nested': {
                                        'path': 'case_properties',
                                        'query': {
                                            'filtered': {
                                                'filter': {
                                                    "and": (
                                                        {
                                                            'term': {'case_properties.key.exact': 'name'}
                                                        },
                                                        {
                                                            'term': {'case_properties.value.exact': some_case_name}
                                                        }
                                                    )
                                                },
                                                'query': {
                                                    'match_all': {
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },
                                new_must_clause
                            ]
                        }
                    }
                }
            },
            'size': CASE_SEARCH_MAX_RESULTS
        }
        run_query_mock.assert_called_with(
            "case_search",
            expected_query,
            debug_host=None,
            es_instance_alias=ES_DEFAULT_INSTANCE,
        )