Skip to content

Commit

Permalink
fix(tests): tests controlling query number and with no sorting were b…
Browse files Browse the repository at this point in the history
…roken
  • Loading branch information
jbarreau committed Dec 14, 2023
1 parent 78b3eff commit 1019828
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 14 deletions.
4 changes: 0 additions & 4 deletions django_forest/resources/utils/queryset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ def join_relations(self, queryset, Model, params, request):
if field["relationship"] is not None and field["relationship"] in ["BelongsTo", "HasOne"]
]

# scope

# filters

# projection
for key, value in params.items():
if re.search(r"fields\[[^\]]+\]", key):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


@mock.patch('django_forest.utils.scope.ScopeManager._has_cache_expired', return_value=False)
@mock.patch('jose.jwt.decode', return_value={'id': 1, 'rendering_id': 1})
@mock.patch('jose.jwt.decode', return_value={'id': 1, 'rendering_id': 1})
class ResourceListFilterDateViewTests(TransactionTestCase):
fixtures = ['question.json']

Expand Down Expand Up @@ -90,7 +90,8 @@ def test_future(self, *args, **kwargs):
'filters': '{"field":"pub_date","operator":"future","value":null}',
'timezone': 'Europe/Paris',
'page[number]': '1',
'page[size]': '15'
'page[size]': '15',
'sort': 'id'
})
data = response.json()
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -241,7 +242,8 @@ def test_after_x_hours_ago(self, *args, **kwargs):
'filters': '{"field":"pub_date","operator":"after_x_hours_ago","value":1}',
'timezone': 'Europe/Paris',
'page[number]': '1',
'page[size]': '15'
'page[size]': '15',
'sort': 'id'
})
data = response.json()
self.assertEqual(response.status_code, 200)
Expand Down
95 changes: 88 additions & 7 deletions django_forest/tests/resources/views/list/test_list_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ def test_get_sort(self, mocked_datetime, mocked_decode):
data = response.json()
self.assertEqual(response.status_code, 200)
self.assertEqual(captured.captured_queries[0]['sql'],
' '.join('''SELECT "tests_question"."id", "tests_question"."question_text", "tests_question"."pub_date", "tests_question"."topic_id"
FROM "tests_question"
ORDER BY "tests_question"."id"
DESC
LIMIT 15'''.replace('\n', ' ').split()))
' '.join('''SELECT "tests_question"."id", "tests_question"."question_text", "tests_question"."pub_date", "tests_question"."topic_id", "tests_topic"."id", "tests_topic"."name"
FROM "tests_question"
LEFT OUTER JOIN "tests_topic" ON ("tests_question"."topic_id" = "tests_topic"."id")
ORDER BY "tests_question"."id"
DESC
LIMIT 15'''.replace('\n', ' ').split()))
self.assertEqual(data, {
'data': [
{
Expand Down Expand Up @@ -145,7 +146,7 @@ def test_get_sort(self, mocked_datetime, mocked_decode):
@mock.patch('jose.jwt.decode', return_value={'id': 1, 'rendering_id': 1})
@mock.patch('django_forest.utils.scope.ScopeManager._has_cache_expired', return_value=False)
def test_get_sort_related_data(self, mocked_scope_has_expired, mocked_decode):
with self._django_assert_num_queries(7) as captured:
with self._django_assert_num_queries(4) as captured:
response = self.client.get(self.reverse_url, {
'fields[tests_choice]': 'id,topic,question,choice_text',
'fields[topic]': 'name',
Expand All @@ -157,9 +158,89 @@ def test_get_sort_related_data(self, mocked_scope_has_expired, mocked_decode):
})
self.assertEqual(response.status_code, 200)
self.assertEqual(captured.captured_queries[0]['sql'],
' '.join('''SELECT "tests_choice"."id", "tests_choice"."question_id", "tests_choice"."choice_text"
' '.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"
FROM "tests_choice"
LEFT OUTER JOIN "tests_question" ON ("tests_choice"."question_id" = "tests_question"."id")
ORDER BY "tests_question"."question_text"
ASC
LIMIT 15'''.replace('\n', ' ').split()))
data = response.json()
self.assertEqual(data, {
"data": [
{
"type": "tests_choice",
"relationships": {
"topic": {
"links": {
"related": "/forest/tests_choice/3/relationships/topic"
},
"data": None,
},
"question": {
"links": {
"related": "/forest/tests_choice/3/relationships/question"
},
"data": {"type": "tests_question", "id": "2"},
},
},
"id": 3,
"attributes": {"choice_text": "good"},
"links": {"self": "/forest/tests_choice/3"},
},
{
"type": "tests_choice",
"relationships": {
"topic": {
"links": {
"related": "/forest/tests_choice/1/relationships/topic"
},
"data": None,
},
"question": {
"links": {
"related": "/forest/tests_choice/1/relationships/question"
},
"data": {"type": "tests_question", "id": "1"},
},
},
"id": 1,
"attributes": {"choice_text": "yes"},
"links": {"self": "/forest/tests_choice/1"},
},
{
"type": "tests_choice",
"relationships": {
"topic": {
"links": {
"related": "/forest/tests_choice/2/relationships/topic"
},
"data": None,
},
"question": {
"links": {
"related": "/forest/tests_choice/2/relationships/question"
},
"data": {"type": "tests_question", "id": "1"},
},
},
"id": 2,
"attributes": {"choice_text": "no"},
"links": {"self": "/forest/tests_choice/2"},
},
],
"included": [
{
"type": "tests_question",
"attributes": {"question_text": "do you like chocolate?"},
"links": {"self": "/forest/tests_question/2"},
"id": 2,
},
{
"type": "tests_question",
"attributes": {"question_text": "what is your favorite color?"},
"links": {"self": "/forest/tests_question/1"},
"id": 1,
},
],
}
)

0 comments on commit 1019828

Please sign in to comment.