|
1 | | -import unittest |
2 | 1 | from django.test import TestCase |
3 | 2 | from api.views.v1.opensearch_query_builder. \ |
4 | 3 | production_locations_query_builder import ProductionLocationsQueryBuilder |
@@ -26,20 +25,6 @@ def test_add_match(self): |
26 | 25 | self.builder.query_body['query']['bool']['must'] |
27 | 26 | ) |
28 | 27 |
|
29 | | - def test_add_multi_match(self): |
30 | | - self.builder.add_multi_match('test query') |
31 | | - expected = { |
32 | | - 'multi_match': { |
33 | | - 'query': 'test query', |
34 | | - 'fields': ['name^2', 'address', 'description', 'local_name'], |
35 | | - 'fuzziness': 2 |
36 | | - } |
37 | | - } |
38 | | - self.assertIn( |
39 | | - expected, |
40 | | - self.builder.query_body['query']['bool']['must'] |
41 | | - ) |
42 | | - |
43 | 28 | def test_add_terms_for_standard_field(self): |
44 | 29 | self.builder.add_terms('country', ['US', 'CA']) |
45 | 30 | expected = {'terms': {'country.alpha_2': ['US', 'CA']}} |
@@ -181,6 +166,11 @@ def test_add_sort(self): |
181 | 166 | expected = {'name.keyword': {'order': 'desc'}} |
182 | 167 | self.assertIn(expected, self.builder.query_body['sort']) |
183 | 168 |
|
| 169 | + def test_add_sort_with_default_order(self): |
| 170 | + self.builder.add_sort('name') |
| 171 | + expected = {'name.keyword': {'order': 'asc'}} |
| 172 | + self.assertIn(expected, self.builder.query_body['sort']) |
| 173 | + |
184 | 174 | def test_add_search_after(self): |
185 | 175 | search_after_value = 'test_value' |
186 | 176 | search_after_id = 'test_id' |
@@ -214,6 +204,55 @@ def test_get_final_query_body(self): |
214 | 204 | } |
215 | 205 | self.assertEqual(final_query, expected) |
216 | 206 |
|
| 207 | + def test_add_multi_match(self): |
| 208 | + self.builder.add_multi_match( |
| 209 | + 'test query' |
| 210 | + ) |
| 211 | + expected = { |
| 212 | + 'multi_match': { |
| 213 | + 'query': 'test query', |
| 214 | + 'fields': ['name^2', 'address', 'description', 'local_name'], |
| 215 | + 'fuzziness': 2, |
| 216 | + } |
| 217 | + } |
| 218 | + self.assertIn( |
| 219 | + expected, self.builder.query_body['query']['bool']['must'] |
| 220 | + ) |
| 221 | + |
| 222 | + def test_add_aggregations_with_precision(self): |
| 223 | + aggregation = 'geohex_grid' |
| 224 | + geohex_grid_precision = 5 |
| 225 | + self.builder.add_aggregations( |
| 226 | + aggregation, |
| 227 | + geohex_grid_precision |
| 228 | + ) |
| 229 | + expected = { |
| 230 | + 'grouped': { |
| 231 | + 'geohex_grid': { |
| 232 | + 'field': 'coordinates', |
| 233 | + 'precision': geohex_grid_precision |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + self.assertIn('aggregations', self.builder.query_body) |
| 238 | + self.assertEqual(expected, self.builder.query_body['aggregations']) |
| 239 | + |
| 240 | + def test_add_aggregations_without_precision(self): |
| 241 | + aggregation = 'geohex_grid' |
| 242 | + self.builder.add_aggregations( |
| 243 | + aggregation |
| 244 | + ) |
| 245 | + expected = { |
| 246 | + 'grouped': { |
| 247 | + 'geohex_grid': {'field': 'coordinates'} |
| 248 | + } |
| 249 | + } |
| 250 | + self.assertIn('aggregations', self.builder.query_body) |
| 251 | + self.assertEqual(expected, self.builder.query_body['aggregations']) |
217 | 252 |
|
218 | | -if __name__ == '__main__': |
219 | | - unittest.main() |
| 253 | + def test_add_aggregations_where_aggregation_is_not_geohex_grid(self): |
| 254 | + aggregation = 'test_aggregation' |
| 255 | + self.builder.add_aggregations( |
| 256 | + aggregation |
| 257 | + ) |
| 258 | + self.assertNotIn('aggregations', self.builder.query_body) |
0 commit comments