innovark37 commented on code in PR #40885:
URL: https://github.com/apache/superset/pull/40885#discussion_r3601585917
##########
tests/unit_tests/commands/report/execute_test.py:
##########
@@ -1326,6 +1327,197 @@ def
test_get_csv_data_keeps_screenshot_fallback_without_query_context(
post_chart_data.assert_not_called()
+def test_get_url_for_xlsx_report(mocker: MockerFixture) -> None:
+ """XLSX reports should request post-processed chart data."""
+ report_schedule = create_report_schedule(mocker)
+ report_schedule.chart_id = 1
+ report_schedule.force_screenshot = False
+ report_state = BaseReportState(
+ report_schedule, "January 1, 2021", "execution_id_example"
+ )
+ get_url_path = mocker.patch(
+ "superset.commands.report.execute.get_url_path",
+ return_value="/api/v1/chart/1/data/xlsx",
+ )
+
+ url = report_state._get_url(result_format=ChartDataResultFormat.XLSX)
+
+ assert url == "/api/v1/chart/1/data/xlsx"
+ get_url_path.assert_called_once_with(
+ "ChartDataRestApi.get_data",
+ pk=1,
+ format=ChartDataResultFormat.XLSX.value,
+ type=ChartDataResultType.POST_PROCESSED.value,
+ force="false",
+ )
+
+
+def test_get_chart_data_rejects_non_table_format(mocker: MockerFixture) ->
None:
+ """Chart data retrieval should reject formats it cannot download."""
+ report_state = BaseReportState(
+ create_report_schedule(mocker),
+ "January 1, 2021",
+ "execution_id_example",
+ )
+ get_url = mocker.patch.object(report_state, "_get_url")
+
+ with pytest.raises(
+ ReportScheduleExecuteUnexpectedError,
+ match="Unsupported chart data result format: json",
+ ):
+ report_state._get_chart_data(ChartDataResultFormat.JSON)
+
+ get_url.assert_not_called()
+
+
+def _mock_xlsx_chart_data_dependencies(
+ mocker: MockerFixture,
+ report_state: BaseReportState,
+) -> tuple[MagicMock, dict[str, str]]:
+ """Mock external services used by the chart data download path."""
+ report_state._report_schedule.chart.query_context = None
+ mocker.patch.object(report_state, "_update_query_context")
+ mocker.patch("superset.commands.report.execute.db.session.refresh")
+ get_url = mocker.patch.object(
+ report_state,
+ "_get_url",
+ return_value="/api/v1/chart/1/data/xlsx",
+ )
+ mocker.patch(
+ "superset.commands.report.execute.get_executor",
+ return_value=(None, "report_executor"),
+ )
+ user = mocker.MagicMock(username="report_executor")
+ mocker.patch(
+ "superset.commands.report.execute.security_manager.find_user",
+ return_value=user,
+ )
+ auth_cookies = {"session": "cookie"}
+ auth_provider = mocker.patch(
+ "superset.commands.report.execute.machine_auth_provider_factory"
+ )
+ auth_provider.instance.get_auth_cookies.return_value = auth_cookies
+ return get_url, auth_cookies
+
+
+def test_get_xlsx_data_fetches_chart_data(
+ app: SupersetApp,
+ mocker: MockerFixture,
+) -> None:
+ """XLSX report data should be fetched through the chart data endpoint."""
+ report_state = BaseReportState(
+ create_report_schedule(mocker),
+ "January 1, 2021",
+ "execution_id_example",
+ )
+ get_url, auth_cookies = _mock_xlsx_chart_data_dependencies(mocker,
report_state)
+ get_chart_csv_data = mocker.patch(
+ "superset.commands.report.execute.get_chart_csv_data",
+ return_value=b"xlsx-data",
+ )
+
+ assert report_state._get_xlsx_data() == b"xlsx-data"
+ get_url.assert_called_once_with(result_format=ChartDataResultFormat.XLSX)
+ get_chart_csv_data.assert_called_once_with(
+ chart_url="/api/v1/chart/1/data/xlsx",
+ auth_cookies=auth_cookies,
+ timeout=app.config["ALERT_REPORTS_CSV_REQUEST_TIMEOUT"],
+ )
+
+
[email protected](
+ ("side_effect", "expected_exception", "expected_message"),
+ [
+ (
+ SoftTimeLimitExceeded(),
+ ReportScheduleXlsxTimeout,
+ "timeout occurred while generating an xlsx",
+ ),
+ (
+ RuntimeError("export failed"),
+ ReportScheduleXlsxFailedError,
+ "Failed generating xlsx export failed",
+ ),
+ ],
+)
+def test_get_xlsx_data_maps_errors(
+ app: SupersetApp,
+ mocker: MockerFixture,
+ side_effect: Exception,
+ expected_exception: type[Exception],
+ expected_message: str,
+) -> None:
+ """XLSX generation errors should use XLSX-specific report exceptions."""
+ report_state = BaseReportState(
+ create_report_schedule(mocker),
+ "January 1, 2021",
+ "execution_id_example",
+ )
+ _mock_xlsx_chart_data_dependencies(mocker, report_state)
+ mocker.patch(
+ "superset.commands.report.execute.get_chart_csv_data",
+ side_effect=side_effect,
+ )
+
+ with pytest.raises(expected_exception, match=expected_message) as exc_info:
+ report_state._get_xlsx_data()
+
+ assert exc_info.value.__cause__ is side_effect
+
+
+def test_get_xlsx_data_rejects_empty_result(
+ app: SupersetApp,
+ mocker: MockerFixture,
+) -> None:
+ """An empty XLSX response should fail report generation."""
+ report_state = BaseReportState(
+ create_report_schedule(mocker),
+ "January 1, 2021",
+ "execution_id_example",
+ )
+ _mock_xlsx_chart_data_dependencies(mocker, report_state)
+ mocker.patch(
+ "superset.commands.report.execute.get_chart_csv_data",
+ return_value=None,
+ )
+
+ with pytest.raises(
+ ReportScheduleXlsxFailedError,
+ match="Report Schedule execution failed when generating an xlsx",
+ ) as exc_info:
+ report_state._get_xlsx_data()
+
+ assert exc_info.value.__cause__ is None
+
+
+def test_notification_content_contains_xlsx(mocker: MockerFixture) -> None:
+ """XLSX chart reports should populate the XLSX notification field."""
+ report_schedule = create_report_schedule(mocker)
+ report_schedule.report_format = ReportDataFormat.XLSX
+ report_schedule.force_screenshot = False
+ report_schedule.email_subject = None
+ report_schedule.owners = []
+ report_schedule.recipients = []
+ report_state = BaseReportState(
+ report_schedule,
+ "January 1, 2021",
+ "execution_id_example",
+ )
+ mocker.patch.object(report_state, "_get_url", return_value="/chart/1")
+ mocker.patch.object(report_state, "_get_log_data", return_value={})
+ get_xlsx_data = mocker.patch.object(
+ report_state,
+ "_get_xlsx_data",
+ return_value=b"xlsx-data",
+ )
Review Comment:
Good catch. `_get_xlsx_data` was removed during the merge in favor of
`_get_data(result_format)`, so this test was patching a stale method. I updated
the XLSX tests to call/patch `_get_data(ChartDataResultFormat.XLSX)` and assert
the actual path used by `_get_notification_content`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]