Skip to content

Commit a7213b8

Browse files
committed
fix(tests): tests controlling query number and with no sorting were broken
1 parent 78b3eff commit a7213b8

File tree

2 files changed

+88
-11
lines changed

2 files changed

+88
-11
lines changed

django_forest/resources/utils/queryset/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ def join_relations(self, queryset, Model, params, request):
3939
if field["relationship"] is not None and field["relationship"] in ["BelongsTo", "HasOne"]
4040
]
4141

42-
# scope
43-
44-
# filters
45-
4642
# projection
4743
for key, value in params.items():
4844
if re.search(r"fields\[[^\]]+\]", key):

django_forest/tests/resources/views/list/test_list_sort.py

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ def test_get_sort(self, mocked_datetime, mocked_decode):
7575
data = response.json()
7676
self.assertEqual(response.status_code, 200)
7777
self.assertEqual(captured.captured_queries[0]['sql'],
78-
' '.join('''SELECT "tests_question"."id", "tests_question"."question_text", "tests_question"."pub_date", "tests_question"."topic_id"
79-
FROM "tests_question"
80-
ORDER BY "tests_question"."id"
81-
DESC
82-
LIMIT 15'''.replace('\n', ' ').split()))
78+
' '.join('''SELECT "tests_question"."id", "tests_question"."question_text", "tests_question"."pub_date", "tests_question"."topic_id", "tests_topic"."id", "tests_topic"."name"
79+
FROM "tests_question"
80+
LEFT OUTER JOIN "tests_topic" ON ("tests_question"."topic_id" = "tests_topic"."id")
81+
ORDER BY "tests_question"."id"
82+
DESC
83+
LIMIT 15'''.replace('\n', ' ').split()))
8384
self.assertEqual(data, {
8485
'data': [
8586
{
@@ -145,7 +146,7 @@ def test_get_sort(self, mocked_datetime, mocked_decode):
145146
@mock.patch('jose.jwt.decode', return_value={'id': 1, 'rendering_id': 1})
146147
@mock.patch('django_forest.utils.scope.ScopeManager._has_cache_expired', return_value=False)
147148
def test_get_sort_related_data(self, mocked_scope_has_expired, mocked_decode):
148-
with self._django_assert_num_queries(7) as captured:
149+
with self._django_assert_num_queries(4) as captured:
149150
response = self.client.get(self.reverse_url, {
150151
'fields[tests_choice]': 'id,topic,question,choice_text',
151152
'fields[topic]': 'name',
@@ -157,9 +158,89 @@ def test_get_sort_related_data(self, mocked_scope_has_expired, mocked_decode):
157158
})
158159
self.assertEqual(response.status_code, 200)
159160
self.assertEqual(captured.captured_queries[0]['sql'],
160-
' '.join('''SELECT "tests_choice"."id", "tests_choice"."question_id", "tests_choice"."choice_text"
161+
' '.join('''SELECT "tests_choice"."id", "tests_choice"."question_id", "tests_choice"."choice_text", "tests_question"."id", "tests_question"."question_text", "tests_question"."pub_date", "tests_question"."topic_id"
161162
FROM "tests_choice"
162163
LEFT OUTER JOIN "tests_question" ON ("tests_choice"."question_id" = "tests_question"."id")
163164
ORDER BY "tests_question"."question_text"
164165
ASC
165166
LIMIT 15'''.replace('\n', ' ').split()))
167+
data = response.json()
168+
self.assertEqual(data, {
169+
"data": [
170+
{
171+
"type": "tests_choice",
172+
"relationships": {
173+
"topic": {
174+
"links": {
175+
"related": "/forest/tests_choice/3/relationships/topic"
176+
},
177+
"data": None,
178+
},
179+
"question": {
180+
"links": {
181+
"related": "/forest/tests_choice/3/relationships/question"
182+
},
183+
"data": {"type": "tests_question", "id": "2"},
184+
},
185+
},
186+
"id": 3,
187+
"attributes": {"choice_text": "good"},
188+
"links": {"self": "/forest/tests_choice/3"},
189+
},
190+
{
191+
"type": "tests_choice",
192+
"relationships": {
193+
"topic": {
194+
"links": {
195+
"related": "/forest/tests_choice/1/relationships/topic"
196+
},
197+
"data": None,
198+
},
199+
"question": {
200+
"links": {
201+
"related": "/forest/tests_choice/1/relationships/question"
202+
},
203+
"data": {"type": "tests_question", "id": "1"},
204+
},
205+
},
206+
"id": 1,
207+
"attributes": {"choice_text": "yes"},
208+
"links": {"self": "/forest/tests_choice/1"},
209+
},
210+
{
211+
"type": "tests_choice",
212+
"relationships": {
213+
"topic": {
214+
"links": {
215+
"related": "/forest/tests_choice/2/relationships/topic"
216+
},
217+
"data": None,
218+
},
219+
"question": {
220+
"links": {
221+
"related": "/forest/tests_choice/2/relationships/question"
222+
},
223+
"data": {"type": "tests_question", "id": "1"},
224+
},
225+
},
226+
"id": 2,
227+
"attributes": {"choice_text": "no"},
228+
"links": {"self": "/forest/tests_choice/2"},
229+
},
230+
],
231+
"included": [
232+
{
233+
"type": "tests_question",
234+
"attributes": {"question_text": "do you like chocolate?"},
235+
"links": {"self": "/forest/tests_question/2"},
236+
"id": 2,
237+
},
238+
{
239+
"type": "tests_question",
240+
"attributes": {"question_text": "what is your favorite color?"},
241+
"links": {"self": "/forest/tests_question/1"},
242+
"id": 1,
243+
},
244+
],
245+
}
246+
)

0 commit comments

Comments
 (0)