Skip to content

Commit

Permalink
Merge pull request #2549 from onaio/fix-charts-endpoint-programming-e…
Browse files Browse the repository at this point in the history
…rror

Fix SQL syntax error when grouping by select one
  • Loading branch information
KipSigei authored Feb 1, 2024
2 parents 4914a3c + fe50698 commit 49e8c99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions onadata/apps/api/tests/viewsets/test_charts_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,35 @@ def test_charts_caching(self):
)
self.assertEqual(response.status_code, 200)
self.assertNotEqual(response.data, initial_data)

def test_charts_group_by_select_one(self):
"""
Test that the chart endpoint works correctly
when grouping with select one field
"""
data = {"field_name": "gender", "group_by": "pizza_fan"}
request = self.factory.get("/charts", data)
force_authenticate(request, user=self.user)
initial_data = {
"data": [
{"gender": ["Male"], "items": [{"pizza_fan": ["No"], "count": 1}]},
{
"gender": ["Female"],
"items": [
{"pizza_fan": ["No"], "count": 1},
{"pizza_fan": ["Yes"], "count": 1},
],
},
],
"data_type": "categorized",
"field_label": "Gender",
"field_xpath": "gender",
"field_name": "gender",
"field_type": "select one",
"grouped_by": "pizza_fan",
"xform": self.xform.pk,
}

response = self.view(request, pk=self.xform.id)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, initial_data)
2 changes: 1 addition & 1 deletion onadata/libs/data/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _postgres_count_group_field_n_group_by(field, name, xform, group_by, data_vi
"count(*) as count "
"FROM %(table)s WHERE "
+ restricted_string
+ "AND deleted_at IS NULL "
+ " AND deleted_at IS NULL "
+ additional_filters
+ " GROUP BY %(json)s, %(group_by)s"
+ " ORDER BY %(json)s, %(group_by)s"
Expand Down

0 comments on commit 49e8c99

Please sign in to comment.