示例#1
0
    def test_search_with_aggregation_params(self, mock_es):
        def side_effect(*args, **kwargs):
            if "size" in kwargs:
                return self.es_search_response
            else:
                return self.es_aggregation_response

        mock_es.side_effect = side_effect

        response = self.app.get(
            '/api/search?q=act1&category=gene&go_names=cytoplasm')

        es_query = build_search_query(
            "act1", self.search_fields, "gene", self.category_filters,
            ImmutableMultiDict([("q", "act1"), ("category", "gene"),
                                ("go_names", "cytoplasm")]))

        mock_es.assert_has_calls([
            mock.call(index=self.index,
                      body=build_es_search_body_request(
                          "act1", "gene", es_query, self.json_response_fields,
                          self.search_fields, ""),
                      size=10,
                      from_=0,
                      preference='p_act1')
        ])

        mock_es.assert_has_calls([
            mock.call(index=self.index,
                      body=build_es_aggregation_body_request(
                          es_query, "gene", self.category_filters))
        ])

        self.assertEqual(response.status_code, 200)
示例#2
0
    def test_search_default_params(self, mock_es):
        def side_effect(*args, **kwargs):
            if "size" in kwargs:
                return self.es_search_response
            else:
                return self.es_aggregation_response

        mock_es.side_effect = side_effect

        response = self.app.get('/api/search')

        es_query = build_search_query('', self.search_fields, '',
                                      self.category_filters, {})

        mock_es.assert_has_calls([
            mock.call(index=self.index,
                      body=build_es_search_body_request(
                          '', '', es_query, self.json_response_fields,
                          self.search_fields, ''),
                      size=10,
                      from_=0,
                      preference='p_')
        ])

        mock_es.assert_has_calls([
            mock.call(index=self.index,
                      body=build_es_aggregation_body_request(
                          es_query, '', self.category_filters))
        ])

        self.assertEqual(response.status_code, 200)
示例#3
0
    def test_build_es_aggregation_body_request_should_aggregate_each_subcategory(self):
        query = ""
        fields = ["name", "symbol"]
        category = "genes"
        category_filters = {
            "genes": ['go_ids', 'go_names'],
            "go": ['gene'],
        }
        args = ImmutableMultiDict([
            ('go_names', 'A'),
            ('go_names', 'B'),
            ('go_names', 'C')
        ])

        es_query = build_search_query(query, fields, category, category_filters, args)

        self.assertEqual(build_es_aggregation_body_request(es_query, category, category_filters), {
            'query': es_query,
            'size': 0,
            'aggs': {
                'go_ids': {
                    'terms': {'field': 'go_ids.raw', 'size': 999}
                },
                'go_names': {
                    'terms': {'field': 'go_names.raw', 'size': 999}
                }
            }
        })
示例#4
0
    def test_search_with_custom_params(self, mock_es):
        def side_effect(*args, **kwargs):
            if "size" in kwargs:
                return self.es_search_response
            else:
                return self.es_aggregation_response

        mock_es.side_effect = side_effect

        response = self.app.get(
            '/api/search?q=act1&category=gene&limit=25&offset=10&sort_by=alphabetical'
        )

        es_query = build_search_query(
            "act1", self.search_fields, "gene", self.category_filters,
            ImmutableMultiDict([("q", "act1"), ("category", "gene"),
                                ("limit", 25), ("offset", 10),
                                ("sort_by", "alphabetical")]))

        mock_es.assert_has_calls([
            mock.call(index=self.index,
                      body=build_es_search_body_request(
                          "act1", "gene", es_query, self.json_response_fields,
                          self.search_fields, "alphabetical"),
                      size=25,
                      from_=10)
        ])

        mock_es.assert_has_calls([
            mock.call(index=self.index,
                      body=build_es_aggregation_body_request(
                          es_query, "gene", self.category_filters))
        ])

        self.assertEqual(response.status_code, 200)
示例#5
0
    def test_build_es_aggregation_body_request_should_return_empty_for_invalid_category(self):
        query = ""
        fields = ["name", "symbol"]
        category = "invalid_category"
        category_filters = {
            "genes": ['go_ids', 'go_names'],
            "go": ['gene'],
        }
        args = ImmutableMultiDict([
            ('go_names', 'A'),
            ('go_names', 'B'),
            ('go_names', 'C')
        ])

        es_query = build_search_query(query, fields, category, category_filters, args)

        self.assertEqual(build_es_aggregation_body_request(es_query, category, category_filters), {})