Skip to content

Commit aaaae47

Browse files
better post filter and facet tests
1 parent d7ad311 commit aaaae47

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

src/django_elasticsearch_dsl_drf/tests/data_mixins.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ def create_books(cls):
167167
}
168168
)
169169

170+
cls.rejected_count = cls.prefix_count + cls.no_tags_count
171+
170172
cls.all_count = (
171173
cls.published_count +
172174
cls.in_progress_count +

src/django_elasticsearch_dsl_drf/tests/test_faceted_search.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class TestFacetedSearch(BaseRestFrameworkTestCase):
4141

4242
@classmethod
4343
def setUp(cls):
44-
cls.published_count = 10
44+
cls.published_count = 11
4545
cls.published = factories.BookFactory.create_batch(
4646
cls.published_count,
4747
**{
@@ -72,7 +72,7 @@ def _list_results_with_facets(self):
7272
no_args_response = self.client.get(url, data)
7373
self.assertEqual(no_args_response.status_code, status.HTTP_200_OK)
7474

75-
# Should contain 20 results
75+
# Should contain `self.all_count` results
7676
self.assertEqual(len(no_args_response.data['results']), self.all_count)
7777

7878
# Should contain 1 facets
@@ -82,13 +82,19 @@ def _list_results_with_facets(self):
8282
facet_state_response = self.client.get(facet_state_url, data)
8383
self.assertEqual(facet_state_response.status_code, status.HTTP_200_OK)
8484

85-
# Should contain 20 results
85+
# Should contain `self.all_count` results
8686
self.assertEqual(
8787
len(facet_state_response.data['results']), self.all_count
8888
)
8989

9090
# Should contain 2 facets
9191
self.assertEqual(len(facet_state_response.data['facets']), 2)
92+
# With 2 state values
93+
self.assertEqual(
94+
len(facet_state_response.data['facets']
95+
['_filter_state']['state']['buckets']),
96+
2
97+
)
9298

9399
self.assertIn('_filter_publisher', facet_state_response.data['facets'])
94100
self.assertIn(
@@ -111,7 +117,7 @@ def _list_results_with_facets(self):
111117
# )
112118
self.assertIn(
113119
{
114-
"doc_count": 10,
120+
"doc_count": self.published_count,
115121
"key": "published"
116122
},
117123
facet_state_response.data['facets']
@@ -121,7 +127,7 @@ def _list_results_with_facets(self):
121127
)
122128
self.assertIn(
123129
{
124-
"doc_count": 10,
130+
"doc_count": self.not_published_count,
125131
"key": "not_published"
126132
},
127133
facet_state_response.data['facets']
@@ -130,6 +136,28 @@ def _list_results_with_facets(self):
130136
['buckets']
131137
)
132138

139+
filtered_facet_state_url = url + '?facet=state&state={}'.format(
140+
constants.BOOK_PUBLISHING_STATUS_PUBLISHED
141+
)
142+
# Make request
143+
facet_state_response = self.client.get(filtered_facet_state_url, data)
144+
self.assertEqual(facet_state_response.status_code, status.HTTP_200_OK)
145+
146+
# Should contain `self.all_count` results
147+
self.assertEqual(
148+
len(facet_state_response.data['results']), self.published_count
149+
)
150+
151+
# Should contain 2 facets
152+
self.assertEqual(len(facet_state_response.data['facets']), 2)
153+
# With 1 state values
154+
self.assertEqual(
155+
len(facet_state_response.data['facets']
156+
['_filter_state']['state']['buckets']),
157+
1
158+
)
159+
160+
133161
def test_list_results_with_facets(self):
134162
"""Test list results with facets."""
135163
return self._list_results_with_facets()

src/django_elasticsearch_dsl_drf/tests/test_filtering_post_filter.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def _list_results_with_facets(self):
515515
no_args_response = self.client.get(url, data)
516516
self.assertEqual(no_args_response.status_code, status.HTTP_200_OK)
517517

518-
# Should contain 20 results
518+
# Should contain `self.all_count` results
519519
self.assertEqual(len(no_args_response.data['results']), self.all_count)
520520

521521
# Should contain 1 facets
@@ -529,13 +529,19 @@ def _list_results_with_facets(self):
529529
facet_state_response = self.client.get(facet_state_url, data)
530530
self.assertEqual(facet_state_response.status_code, status.HTTP_200_OK)
531531

532-
# Should contain 20 results
532+
# Should contain `self.all_count` results
533533
self.assertEqual(
534534
len(facet_state_response.data['results']), self.all_count
535535
)
536536

537537
# Should contain 2 facets
538538
self.assertEqual(len(facet_state_response.data['facets']), 2)
539+
# With 3 statuses
540+
self.assertEqual(
541+
len(facet_state_response.data['facets']
542+
['_filter_state']['state']['buckets']),
543+
3
544+
)
539545

540546
self.assertIn('_filter_publisher', facet_state_response.data['facets'])
541547
self.assertIn(
@@ -591,30 +597,36 @@ def _list_results_with_facets(self):
591597
# ******************* With facets filtered response ****************
592598
# ******************************************************************
593599

594-
facet_state_filered_url = url + '?facet=state&state=published'
600+
facet_state_filtered_url = url + '?facet=state&state_pf=published'
595601
# Make request
596602
facet_state_filtered_response = self.client.get(
597-
facet_state_filered_url,
603+
facet_state_filtered_url,
598604
data
599605
)
600606
self.assertEqual(
601607
facet_state_filtered_response.status_code,
602608
status.HTTP_200_OK
603609
)
604610

605-
# Should contain 20 results
611+
# Should contain `self.published_count` results
606612
self.assertEqual(
607613
len(facet_state_filtered_response.data['results']),
608614
self.published_count
609615
)
610616

611617
# Should contain 2 facets
612618
self.assertEqual(len(facet_state_filtered_response.data['facets']), 2)
619+
# With 3 statuses
620+
self.assertEqual(
621+
len(facet_state_filtered_response.data['facets']
622+
['_filter_state']['state']['buckets']),
623+
3
624+
)
613625

614626
# Still same facets
615627
self.assertIn(
616628
{
617-
"doc_count": 10,
629+
"doc_count": self.published_count,
618630
"key": "published"
619631
},
620632
facet_state_response.data['facets']
@@ -624,7 +636,7 @@ def _list_results_with_facets(self):
624636
)
625637
self.assertIn(
626638
{
627-
"doc_count": 10,
639+
"doc_count": self.in_progress_count,
628640
"key": "in_progress"
629641
},
630642
facet_state_response.data['facets']
@@ -634,7 +646,7 @@ def _list_results_with_facets(self):
634646
)
635647
self.assertIn(
636648
{
637-
"doc_count": 7,
649+
"doc_count": self.rejected_count,
638650
"key": "rejected"
639651
},
640652
facet_state_response.data['facets']

0 commit comments

Comments
 (0)