JasonD28 commented on a change in pull request #10605:
URL:
https://github.com/apache/incubator-superset/pull/10605#discussion_r478669106
##########
File path: tests/alerts_tests.py
##########
@@ -41,95 +50,204 @@
def setup_database():
with app.app_context():
slice_id = db.session.query(Slice).all()[0].id
- database_id = utils.get_example_database().id
+ database = utils.get_example_database()
+ database_id = database.id
+ database.get_sqla_engine().execute("CREATE TABLE test_table AS SELECT
2 as id")
+ common_data = dict(
+ active=True,
+ crontab="* * * * *",
+ slice_id=slice_id,
+ recipients="[email protected]",
+ slack_channel="#test_channel",
+ )
alerts = [
- Alert(
- id=1,
- label="alert_1",
- active=True,
- crontab="*/1 * * * *",
- sql="SELECT 0",
- alert_type="email",
- slice_id=slice_id,
- database_id=database_id,
+ Alert(**common_data, id=1, label="alert_1"),
+ Alert(**common_data, id=2, label="alert_2"),
+ Alert(**common_data, id=3, label="alert_3"),
+ Alert(**common_data, id=4, label="alert_4"),
+ Alert(id=5, crontab="* * * * *", active=False, label="alert_5"),
+ Alert(id=6, crontab="* * * * *", active=False, label="alert_6"),
+ ]
+ observers = [
+ SQLObserver(
+ name="observer_1", sql="SELECT 0", alert_id=1,
database_id=database_id
),
- Alert(
- id=2,
- label="alert_2",
- active=True,
- crontab="*/1 * * * *",
- sql="SELECT 55",
- alert_type="email",
- slice_id=slice_id,
- recipients="[email protected]",
- slack_channel="#test_channel",
+ SQLObserver(
+ name="observer_2",
+ sql="SELECT id FROM test_table WHERE id = -1",
+ alert_id=2,
database_id=database_id,
),
- Alert(
- id=3,
- label="alert_3",
- active=False,
- crontab="*/1 * * * *",
- sql="UPDATE 55",
- alert_type="email",
- slice_id=slice_id,
- database_id=database_id,
+ SQLObserver(
+ name="observer_3", sql="$%^&", alert_id=3,
database_id=database_id
+ ),
+ SQLObserver(
+ name="observer_4", sql="SELECT 55", alert_id=4,
database_id=database_id
),
- Alert(id=4, active=False, label="alert_4", database_id=-1),
- Alert(id=5, active=False, label="alert_5",
database_id=database_id),
]
db.session.bulk_save_objects(alerts)
- db.session.commit()
+ db.session.bulk_save_objects(observers)
yield db.session
+ db.session.query(SQLObservation).delete()
+ db.session.query(SQLObserver).delete()
+ db.session.query(Validator).delete()
db.session.query(AlertLog).delete()
db.session.query(Alert).delete()
+def test_alert_observer(setup_database):
+ dbsession = setup_database
+
+ # Test SQLObserver with empty SQL return
+ alert2 = dbsession.query(Alert).filter_by(id=2).one()
+ observe(alert2.id)
+ assert alert2.sql_observers[0].observations[-1].value is None
+
+ # Test SQLObserver with non-empty SQL return
+ alert4 = dbsession.query(Alert).filter_by(id=4).one()
+ observe(alert4.id)
+ assert alert4.sql_observers[0].observations[-1].value == 55
+
+
+def test_alert_error(setup_database):
+ dbsession = setup_database
+
+ # Test error with Observer SQL statement
+ alert3 = dbsession.query(Alert).filter_by(id=3).one()
+ check_alert(alert3.id, alert3.label)
+ assert alert3.logs[-1].state == AlertState.ERROR
+
+ # Test error with alert lacking observer
+ alert5 = dbsession.query(Alert).filter_by(id=5).one()
+ check_alert(alert5.id, alert5.label)
+ assert alert5.logs[-1].state == AlertState.ERROR
Review comment:
i chose to abstract that into the different validator tests since an
successful alert is based on its SQLObserver value plus its specific validator
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]