Esempio n. 1
0
    def test_suggest_with_auth(self):
        """Test that calling suggest with no orgs raises an error"""
        Organization.objects.all().delete()

        org0 = Organization(name='testNameWithAuth',
                            filter='testFilterWithAuth')
        org0.save()
        user = XDSUser.objects.create_user('*****@*****.**',
                                           'test1234',
                                           first_name='Jane',
                                           last_name='doe')
        user.organizations.add(org0)
        query = XSEQueries('test', 'test', user=user)

        es = Mock()
        query.search = es

        query.suggest('test')

        self.assertIn(
            org0.filter,
            es.suggest.call_args[1]['completion']['contexts']['filter'])
        self.assertEqual(
            len(es.suggest.call_args[1]['completion']['contexts']['filter']),
            1)
Esempio n. 2
0
    def test_add_search_sort_selected(self):
        """Test that when add_search_sorts is called with a sort parameter\
            then the search object does have a sort attribute"""
        q = Q("bool", should=[Q("match", Test="test")], minimum_should_match=1)
        query = XSEQueries('test', 'test')
        query.search = query.search.query(q)

        with patch('es_api.utils.queries.SearchSortOption.objects') as \
                sortOpts:
            sortOption = SearchSortOption(display_name="test",
                                          field_name="test-field",
                                          xds_ui_configuration=None,
                                          active=True)
            sortOpts.filter.return_value = [sortOption]
            filters = {"sort": "test-field"}
            hasSort = False

            query.add_search_sort(filters=filters)
            result = query.search
            result_dict = result.to_dict()

            if 'sort' in result_dict:
                hasSort = True

            self.assertTrue(hasSort)
Esempio n. 3
0
    def test_suggest_with_orgs(self):
        """Test that calling suggest with orgs filters the query"""
        query = XSEQueries('test', 'test')

        Organization.objects.all().delete()

        org0 = Organization(name='testNameWithOrgs',
                            filter='testFilterWithOrgs')
        org0.save()
        org1 = Organization(name='otherTestNameWithOrgs',
                            filter='otherTestFilterWithOrgs')
        org1.save()

        es = Mock()
        query.search = es

        query.suggest('test')

        print(es.suggest.call_args[1])

        self.assertIn(
            org0.filter,
            es.suggest.call_args[1]['completion']['contexts']['filter'])
        self.assertIn(
            org1.filter,
            es.suggest.call_args[1]['completion']['contexts']['filter'])
        self.assertEqual(
            len(es.suggest.call_args[1]['completion']['contexts']['filter']),
            2)
Esempio n. 4
0
    def test_add_search_filters_with_filters(self):
        """Test that when add_search_filters is called with a filter object\
            then the search object contains every filter passed in"""
        q = Q("bool", should=[Q("match", Test="test")], minimum_should_match=1)
        query = XSEQueries('test', 'test')
        query.search = query.search.query(q)
        filters = {
            "page": 1,
            "color": ["red", "blue"],
            "model": "nike",
            "type": "shirt"
        }
        hasFilter = False
        query.add_search_filters(filters)
        result = query.search
        result_dict = result.to_dict()

        if "filter" in result_dict['query']['bool']:
            hasFilter = True

            for filtr in result_dict['query']['bool']['filter']:
                termObj = filtr['terms']

                for filter_name in termObj:
                    orig_name = filter_name.replace('.keyword', '')
                    self.assertEqual(termObj[filter_name], filters[orig_name])

        self.assertTrue(hasFilter)
Esempio n. 5
0
    def test_user_org_filter_custom_search(self):
        """
        Test that user_organization_filtering returns the same Search
        when given no user
        """
        query = XSEQueries('test', 'test')
        expected_search = Search(using='default', index='custom_testing_index')
        query.search = expected_search
        query.user_organization_filtering()
        result = query.search

        self.assertEqual(result, expected_search)
Esempio n. 6
0
    def test_add_search_filters_no_filter(self):
        """Test that when add_search_filters is called with an empty filter\
            object then no filter gets added to the search object"""
        q = Q("bool", should=[Q("match", Test="test")], minimum_should_match=1)
        filters = {"page": 1}
        hasFilter = False
        query = XSEQueries('test', 'test')
        query.search = query.search.query(q)
        query.add_search_filters(filters)
        result = query.search
        result_dict = result.to_dict()

        if "filter" in result_dict['query']['bool']:
            hasFilter = True

        self.assertFalse(hasFilter)
Esempio n. 7
0
    def test_add_search_aggregations_no_filters(self):
        """Test that when add_search_aggregations is called with an empty\
            filter object, no aggregations are added to the search object"""
        q = Q("bool", should=[Q("match", Test="test")], minimum_should_match=1)
        query = XSEQueries('test', 'test')
        query.search = query.search.query(q)
        filters = []
        hasAggs = False

        query.add_search_aggregations(filters)

        result_dict = query.search.to_dict()

        if 'aggs' in result_dict:
            hasAggs = True

        self.assertFalse(hasAggs)
Esempio n. 8
0
    def test_add_search_sort_none(self):
        """Test that when add_search_sorts is called with no sort parameter\
            then the search object does not have a sort attribute"""
        q = Q("bool", should=[Q("match", Test="test")], minimum_should_match=1)
        query = XSEQueries('test', 'test')
        query.search = query.search.query(q)

        with patch('es_api.utils.queries.SearchSortOption.objects') as \
                sortOpts:
            sortOpts.filter.return_value = []
            filters = {"test": "Test"}
            hasSort = False

            query.add_search_sort(filters=filters)
            result = query.search
            result_dict = result.to_dict()

            if 'sort' in result_dict:
                hasSort = True

            self.assertFalse(hasSort)
Esempio n. 9
0
    def test_add_search_aggregations_with_filters(self):
        """Test that when add_search_aggregations is called with a list of\
            filter object then aggregations are added to the search object"""
        q = Q("bool", should=[Q("match", Test="test")], minimum_should_match=1)
        query = XSEQueries('test', 'test')
        query.search = query.search.query(q)
        config = XDSConfiguration("test")
        uiConfig = XDSUIConfiguration(10, config)
        filterObj = SearchFilter(display_name='type',
                                 field_name='type',
                                 xds_ui_configuration=uiConfig)
        filterObj2 = SearchFilter(display_name='color',
                                  field_name='color',
                                  xds_ui_configuration=uiConfig)
        filters = [
            filterObj,
            filterObj2,
        ]
        hasAggs = False

        query.add_search_aggregations(filters)

        result_dict = query.search.to_dict()

        if 'aggs' in result_dict:
            hasAggs = True

            for filtr in filters:
                hasBucket = False

                if filtr.display_name in result_dict['aggs']:
                    hasBucket = True

                self.assertTrue(hasBucket)

        self.assertTrue(hasAggs)