From 3f2e2c9976b766fdc54fd5a212d626682655c798 Mon Sep 17 00:00:00 2001 From: Lily Kuang Date: Thu, 18 Mar 2021 02:17:50 -0700 Subject: [PATCH] fix(alert|report): allow null on chart and dashboard field (#13680) * allow null on chart and dashboard field * update api test --- superset/reports/schemas.py | 4 +-- tests/reports/api_tests.py | 55 ++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/superset/reports/schemas.py b/superset/reports/schemas.py index 94c99b618..58eab2ce5 100644 --- a/superset/reports/schemas.py +++ b/superset/reports/schemas.py @@ -148,8 +148,8 @@ class ReportSchedulePostSchema(Schema): sql = fields.String( description=sql_description, example="SELECT value FROM time_series_table" ) - chart = fields.Integer(required=False) - dashboard = fields.Integer(required=False) + chart = fields.Integer(required=False, allow_none=True) + dashboard = fields.Integer(required=False, allow_none=True) database = fields.Integer(required=False) owners = fields.List(fields.Integer(description=owners_description)) validator_type = fields.String( diff --git a/tests/reports/api_tests.py b/tests/reports/api_tests.py index ddd528a8f..fde006028 100644 --- a/tests/reports/api_tests.py +++ b/tests/reports/api_tests.py @@ -496,13 +496,16 @@ class TestReportSchedulesApi(SupersetTestCase): db.session.delete(created_model) db.session.commit() - @pytest.mark.usefixtures("create_report_schedules") + @pytest.mark.usefixtures( + "load_birth_names_dashboard_with_slices", "create_report_schedules" + ) def test_create_report_schedule_schema(self): """ ReportSchedule Api: Test create report schedule schema check """ self.login(username="admin") chart = db.session.query(Slice).first() + dashboard = db.session.query(Dashboard).first() example_db = get_example_database() # Check that a report does not have a database reference @@ -590,6 +593,56 @@ class TestReportSchedulesApi(SupersetTestCase): rv = self.client.post(uri, json=report_schedule_data) assert rv.status_code == 400 + # Test that report can be created with null dashboard + report_schedule_data = { + "type": ReportScheduleType.ALERT, + "name": "new4", + "description": "description", + "crontab": "0 9 * * *", + "recipients": [ + { + "type": ReportRecipientType.EMAIL, + "recipient_config_json": {"target": "target@superset.org"}, + }, + { + "type": ReportRecipientType.SLACK, + "recipient_config_json": {"target": "channel"}, + }, + ], + "working_timeout": 3600, + "chart": chart.id, + "dashboard": None, + "database": example_db.id, + } + uri = "api/v1/report/" + rv = self.client.post(uri, json=report_schedule_data) + assert rv.status_code == 201 + + # Test that report can be created with null chart + report_schedule_data = { + "type": ReportScheduleType.ALERT, + "name": "new5", + "description": "description", + "crontab": "0 9 * * *", + "recipients": [ + { + "type": ReportRecipientType.EMAIL, + "recipient_config_json": {"target": "target@superset.org"}, + }, + { + "type": ReportRecipientType.SLACK, + "recipient_config_json": {"target": "channel"}, + }, + ], + "working_timeout": 3600, + "chart": None, + "dashboard": dashboard.id, + "database": example_db.id, + } + uri = "api/v1/report/" + rv = self.client.post(uri, json=report_schedule_data) + assert rv.status_code == 201 + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") def test_create_report_schedule_chart_dash_validation(self): """