fix schedules tests (#12870)

Co-authored-by: Ville Brofeldt <ville.v.brofeldt@gmail.com>
This commit is contained in:
Karol Kostrzewa 2021-02-23 07:21:19 +01:00 committed by GitHub
parent af3a46048d
commit a5a31e0e60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 43 deletions

View File

@ -23,9 +23,7 @@ import pytest
from selenium.common.exceptions import WebDriverException
from slack import errors, WebClient
from tests.fixtures.world_bank_dashboard import (
load_world_bank_dashboard_with_slices_module_scope,
)
from tests.fixtures.world_bank_dashboard import load_world_bank_dashboard_with_slices
from tests.test_app import app
from superset import db
from superset.models.dashboard import Dashboard
@ -44,9 +42,6 @@ from superset.tasks.schedules import (
from superset.models.slice import Slice
from tests.base_tests import SupersetTestCase
from tests.utils import read_fixture
from tests.fixtures.birth_names_dashboard import (
load_birth_names_dashboard_with_slices_module_scope,
)
class TestSchedules(SupersetTestCase):
@ -55,31 +50,31 @@ class TestSchedules(SupersetTestCase):
BCC = "bcc@superset.com"
CSV = read_fixture("trends.csv")
@classmethod
def setUpClass(cls):
@pytest.fixture()
def add_schedule_slice_and_dashboard(self):
with app.app_context():
cls.common_data = dict(
self.common_data = dict(
active=True,
crontab="* * * * *",
recipients=cls.RECIPIENTS,
recipients=self.RECIPIENTS,
deliver_as_group=True,
delivery_type=EmailDeliveryType.inline,
)
# Pick up a sample slice and dashboard
slce = db.session.query(Slice).filter_by(slice_name="Participants").one()
slice = db.session.query(Slice).filter_by(slice_name="Region Filter").one()
dashboard = (
db.session.query(Dashboard)
.filter_by(dashboard_title="World Bank's Data")
.one()
)
dashboard_schedule = DashboardEmailSchedule(**cls.common_data)
dashboard_schedule = DashboardEmailSchedule(**self.common_data)
dashboard_schedule.dashboard_id = dashboard.id
dashboard_schedule.user_id = 1
db.session.add(dashboard_schedule)
slice_schedule = SliceEmailSchedule(**cls.common_data)
slice_schedule.slice_id = slce.id
slice_schedule = SliceEmailSchedule(**self.common_data)
slice_schedule.slice_id = slice.id
slice_schedule.user_id = 1
slice_schedule.email_format = SliceEmailReportFormat.data
slice_schedule.slack_channel = "#test_channel"
@ -87,21 +82,20 @@ class TestSchedules(SupersetTestCase):
db.session.add(slice_schedule)
db.session.commit()
cls.slice_schedule = slice_schedule.id
cls.dashboard_schedule = dashboard_schedule.id
self.slice_schedule = slice_schedule.id
self.dashboard_schedule = dashboard_schedule.id
yield
@classmethod
def tearDownClass(cls):
with app.app_context():
db.session.query(SliceEmailSchedule).filter_by(
id=cls.slice_schedule
id=self.slice_schedule
).delete()
db.session.query(DashboardEmailSchedule).filter_by(
id=cls.dashboard_schedule
id=self.dashboard_schedule
).delete()
db.session.commit()
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
def test_crontab_scheduler(self):
crontab = "* * * * *"
@ -129,7 +123,6 @@ class TestSchedules(SupersetTestCase):
self.assertEqual(schedules[-1], stop_at - timedelta(seconds=12 * 60))
self.assertEqual(len(schedules), 5)
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
def test_wider_schedules(self):
crontab = "*/15 2,10 * * *"
@ -145,10 +138,6 @@ class TestSchedules(SupersetTestCase):
else:
self.assertEqual(len(schedules), 0)
@pytest.mark.usefixtures(
"load_world_bank_dashboard_with_slices_module_scope",
"load_birth_names_dashboard_with_slices_module_scope",
)
def test_complex_schedule(self):
# Run the job on every Friday of March and May
# On these days, run the job at
@ -176,7 +165,6 @@ class TestSchedules(SupersetTestCase):
self.assertEqual(schedules[59], datetime.strptime("2018-03-30 17:40:00", fmt))
self.assertEqual(schedules[60], datetime.strptime("2018-05-04 17:10:00", fmt))
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
@patch("superset.tasks.schedules.firefox.webdriver.WebDriver")
def test_create_driver(self, mock_driver_class):
mock_driver = Mock()
@ -186,7 +174,9 @@ class TestSchedules(SupersetTestCase):
create_webdriver(db.session)
mock_driver.add_cookie.assert_called_once()
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
@pytest.mark.usefixtures(
"load_world_bank_dashboard_with_slices", "add_schedule_slice_and_dashboard"
)
@patch("superset.tasks.schedules.firefox.webdriver.WebDriver")
@patch("superset.tasks.schedules.send_email_smtp")
@patch("superset.tasks.schedules.time")
@ -220,7 +210,9 @@ class TestSchedules(SupersetTestCase):
driver.screenshot.assert_not_called()
send_email_smtp.assert_called_once()
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
@pytest.mark.usefixtures(
"load_world_bank_dashboard_with_slices", "add_schedule_slice_and_dashboard"
)
@patch("superset.tasks.schedules.firefox.webdriver.WebDriver")
@patch("superset.tasks.schedules.send_email_smtp")
@patch("superset.tasks.schedules.time")
@ -264,7 +256,9 @@ class TestSchedules(SupersetTestCase):
element.screenshot_as_png,
)
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
@pytest.mark.usefixtures(
"load_world_bank_dashboard_with_slices", "add_schedule_slice_and_dashboard"
)
@patch("superset.tasks.schedules.firefox.webdriver.WebDriver")
@patch("superset.tasks.schedules.send_email_smtp")
@patch("superset.tasks.schedules.time")
@ -308,7 +302,9 @@ class TestSchedules(SupersetTestCase):
driver.screenshot.return_value,
)
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
@pytest.mark.usefixtures(
"load_world_bank_dashboard_with_slices", "add_schedule_slice_and_dashboard"
)
@patch("superset.tasks.schedules.firefox.webdriver.WebDriver")
@patch("superset.tasks.schedules.send_email_smtp")
@patch("superset.tasks.schedules.time")
@ -350,7 +346,9 @@ class TestSchedules(SupersetTestCase):
self.assertEqual(send_email_smtp.call_count, 2)
self.assertEqual(send_email_smtp.call_args[1]["bcc"], self.BCC)
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
@pytest.mark.usefixtures(
"load_world_bank_dashboard_with_slices", "add_schedule_slice_and_dashboard"
)
@patch("superset.tasks.slack_util.WebClient.files_upload")
@patch("superset.tasks.schedules.firefox.webdriver.WebDriver")
@patch("superset.tasks.schedules.send_email_smtp")
@ -399,12 +397,14 @@ class TestSchedules(SupersetTestCase):
{
"channels": "#test_channel",
"file": element.screenshot_as_png,
"initial_comment": f"\n *Participants*\n\n <http://0.0.0.0:8080/superset/slice/{schedule.slice_id}/|Explore in Superset>\n ",
"title": "[Report] Participants",
"initial_comment": f"\n *Region Filter*\n\n <http://0.0.0.0:8080/superset/slice/{schedule.slice_id}/|Explore in Superset>\n ",
"title": "[Report] Region Filter",
},
)
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
@pytest.mark.usefixtures(
"load_world_bank_dashboard_with_slices", "add_schedule_slice_and_dashboard"
)
@patch("superset.tasks.slack_util.WebClient.files_upload")
@patch("superset.tasks.schedules.firefox.webdriver.WebDriver")
@patch("superset.tasks.schedules.send_email_smtp")
@ -454,12 +454,14 @@ class TestSchedules(SupersetTestCase):
{
"channels": "#test_channel",
"file": element.screenshot_as_png,
"initial_comment": f"\n *Participants*\n\n <http://0.0.0.0:8080/superset/slice/{schedule.slice_id}/|Explore in Superset>\n ",
"title": "[Report] Participants",
"initial_comment": f"\n *Region Filter*\n\n <http://0.0.0.0:8080/superset/slice/{schedule.slice_id}/|Explore in Superset>\n ",
"title": "[Report] Region Filter",
},
)
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
@pytest.mark.usefixtures(
"load_world_bank_dashboard_with_slices", "add_schedule_slice_and_dashboard"
)
@patch("superset.tasks.slack_util.WebClient.files_upload")
@patch("superset.tasks.schedules.urllib.request.OpenerDirector.open")
@patch("superset.tasks.schedules.urllib.request.urlopen")
@ -501,12 +503,14 @@ class TestSchedules(SupersetTestCase):
{
"channels": "#test_channel",
"file": self.CSV,
"initial_comment": f"\n *Participants*\n\n <http://0.0.0.0:8080/superset/slice/{schedule.slice_id}/|Explore in Superset>\n ",
"title": "[Report] Participants",
"initial_comment": f"\n *Region Filter*\n\n <http://0.0.0.0:8080/superset/slice/{schedule.slice_id}/|Explore in Superset>\n ",
"title": "[Report] Region Filter",
},
)
@pytest.mark.usefixtures("load_world_bank_dashboard_with_slices_module_scope")
@pytest.mark.usefixtures(
"load_world_bank_dashboard_with_slices", "add_schedule_slice_and_dashboard"
)
@patch("superset.tasks.slack_util.WebClient.files_upload")
@patch("superset.tasks.schedules.urllib.request.urlopen")
@patch("superset.tasks.schedules.urllib.request.OpenerDirector.open")
@ -546,8 +550,8 @@ class TestSchedules(SupersetTestCase):
{
"channels": "#test_channel",
"file": self.CSV,
"initial_comment": f"\n *Participants*\n\n <http://0.0.0.0:8080/superset/slice/{schedule.slice_id}/|Explore in Superset>\n ",
"title": "[Report] Participants",
"initial_comment": f"\n *Region Filter*\n\n <http://0.0.0.0:8080/superset/slice/{schedule.slice_id}/|Explore in Superset>\n ",
"title": "[Report] Region Filter",
},
)