Skip to content

Commit

Permalink
Fix case of empty results
Browse files Browse the repository at this point in the history
POSTHOG-TTG
  • Loading branch information
webjunkie committed Apr 26, 2024
1 parent 5963254 commit f1a3920
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 5 additions & 3 deletions posthog/tasks/exports/csv_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@ def _convert_response_to_csv_data(data: Any) -> Generator[Any, None, None]:
return

if isinstance(results, list):
first_result = results[0]
first_result = next(iter(results), None)

# persons modal like
if len(results) == 1 and set(results[0].keys()) == {"people", "count"}:
if not first_result:
return
elif len(results) == 1 and set(results[0].keys()) == {"people", "count"}:
# persons modal like
yield from results[0].get("people")
return
elif isinstance(first_result, list) or first_result.get("action_id"):
Expand Down
7 changes: 7 additions & 0 deletions posthog/tasks/exports/test/csv_renders/hogql_empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"csv_rows": ["person,id,created_at,person.$delete", ",,,", ""],
"response": {
"columns": ["person", "id", "created_at", "person.$delete"],
"results": []
}
}
2 changes: 1 addition & 1 deletion posthog/tasks/exports/test/test_csv_exporter_renders.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_csv_rendering(mock_settings, mock_process_query, mock_request, filename
if fixture.get("hogql_response"):
# If HogQL has a different response structure, add it to the fixture as `hogql_response`
mock_process_query.return_value = fixture["hogql_response"]
elif fixture["response"].get("results"):
elif fixture["response"].get("results") is not None:
mock_process_query.return_value = fixture["response"]
else:
mock_process_query.return_value = {"results": fixture["response"].pop("result"), **fixture["response"]}
Expand Down

0 comments on commit f1a3920

Please sign in to comment.