feat: send data embedded in report email (#15805)

* feat: send data embedded in report email

* Prettify table

* Change post-processing to use new endpoint

* Show text option only for text viz

* Show TEXT option only to text-based vizs

* Fix test

* Add email test

* Add unit test
This commit is contained in:
Beto Dealmeida
2021-07-28 09:43:04 -07:00
committed by GitHub
parent 32d2aa0c40
commit 3adf8e85cd
10 changed files with 155 additions and 15 deletions

View File

@@ -181,6 +181,7 @@ class TestReportSchedulesApi(SupersetTestCase):
"chart": {
"id": report_schedule.chart.id,
"slice_name": report_schedule.chart.slice_name,
"viz_type": report_schedule.chart.viz_type,
},
"context_markdown": report_schedule.context_markdown,
"crontab": report_schedule.crontab,

View File

@@ -230,6 +230,20 @@ def create_report_email_chart_with_csv():
cleanup_report_schedule(report_schedule)
@pytest.fixture()
def create_report_email_chart_with_text():
with app.app_context():
chart = db.session.query(Slice).first()
chart.query_context = '{"mock": "query_context"}'
report_schedule = create_report_notification(
email_target="target@email.com",
chart=chart,
report_format=ReportDataFormat.TEXT,
)
yield report_schedule
cleanup_report_schedule(report_schedule)
@pytest.fixture()
def create_report_email_chart_with_csv_no_query_context():
with app.app_context():
@@ -721,6 +735,59 @@ def test_email_chart_report_schedule_with_csv_no_query_context(
screenshot_mock.assert_called_once()
@pytest.mark.usefixtures(
"load_birth_names_dashboard_with_slices", "create_report_email_chart_with_text"
)
@patch("superset.utils.csv.urllib.request.urlopen")
@patch("superset.utils.csv.urllib.request.OpenerDirector.open")
@patch("superset.reports.notifications.email.send_email_smtp")
@patch("superset.utils.csv.get_chart_csv_data")
def test_email_chart_report_schedule_with_text(
csv_mock, email_mock, mock_open, mock_urlopen, create_report_email_chart_with_text,
):
"""
ExecuteReport Command: Test chart email report schedule with CSV
"""
# setup csv mock
response = Mock()
mock_open.return_value = response
mock_urlopen.return_value = response
mock_urlopen.return_value.getcode.return_value = 200
response.read.return_value = CSV_FILE
with freeze_time("2020-01-01T00:00:00Z"):
AsyncExecuteReportScheduleCommand(
TEST_ID, create_report_email_chart_with_text.id, datetime.utcnow()
).run()
# assert that the data is embedded correctly
table_html = """<table>
<thead>
<tr>
<th>t1</th>
<th>t2</th>
<th>t3__sum</th>
</tr>
</thead>
<tbody>
<tr>
<td>c11</td>
<td>c12</td>
<td>c13</td>
</tr>
<tr>
<td>c21</td>
<td>c22</td>
<td>c23</td>
</tr>
</tbody>
</table>"""
assert table_html in email_mock.call_args[0][2]
# Assert logs are correct
assert_log(ReportState.SUCCESS)
@pytest.mark.usefixtures(
"load_birth_names_dashboard_with_slices", "create_report_email_dashboard"
)