Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Test every Metabot alert and subscription frequency (#84893)

Andrei Simionescu8188a724636d
diff --git a/test/metabase/metabot/tools_test.clj b/test/metabase/metabot/tools_test.clj
index 11ba97856a7..79997201861 100644
--- a/test/metabase/metabot/tools_test.clj
+++ b/test/metabase/metabot/tools_test.clj
@@ -336,26 +336,45 @@
(is (not-any? #(= :queries_state (first %)) (rest params)))
(is (not-any? #(= :charts_state (first %)) (rest params))))))
-(deftest slackbot-schedules-keep-their-day-test
+(deftest notification-tools-store-requested-schedules-test
(mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
channel.settings/slack-cached-channels-and-usernames
(constantly {:channels [{:display-name "#data-team" :name "data-team" :id "C123"}]})]
- (mt/with-model-cleanup [:model/Notification :model/Pulse]
- (mt/with-temp [:model/Card {card-id :id} {}
- :model/Dashboard {dash-id :id} {}]
+ (mt/with-temp [:model/Card {card-id :id} {}
+ :model/Dashboard {slack-dash-id :id} {}
+ :model/DashboardCard _ {:dashboard_id slack-dash-id :card_id card-id}
+ :model/Dashboard {inapp-dash-id :id} {}
+ :model/DashboardCard _ {:dashboard_id inapp-dash-id :card_id card-id}]
+ (let [alert-schedules #(->> (models.notification/notifications-for-card card-id)
+ (mapcat :subscriptions)
+ (mapv :cron_schedule))
+ subscription-schedules (fn [dash-id]
+ (->> (t2/hydrate (t2/select :model/Pulse :dashboard_id dash-id) :channels)
+ (mapcat :channels)
+ (mapv (juxt :schedule_type :schedule_hour :schedule_day :schedule_frame))))]
(binding [shared/*memory-atom* (atom {:context {:slack_channel_id "C123"}})]
(mt/with-current-user (mt/user->id :crowberto)
- (doseq [schedule [{:frequency "weekly" :day_of_week "monday" :hour 9}
- {:frequency "monthly" :day_of_month "first-monday" :hour 9}]]
- (agent-tools/create-alert-tool {:card_id card-id :send_condition "has_result" :schedule schedule})
- (agent-tools/slackbot-create-dashboard-subscription-tool {:dashboard_id dash-id :schedule schedule}))))
- (testing "alerts"
- (is (= #{"0 0 9 ? * 2 *" "0 0 9 ? * 2#1 *"}
- (->> (models.notification/notifications-for-card card-id)
- (mapcat :subscriptions)
- (into #{} (map :cron_schedule))))))
- (testing "dashboard subscriptions"
- (is (= #{[:weekly "mon" nil] [:monthly "mon" :first]}
- (->> (t2/hydrate (t2/select :model/Pulse :dashboard_id dash-id) :channels)
- (mapcat :channels)
- (into #{} (map (juxt :schedule_type :schedule_day :schedule_frame)))))))))))
+ (doseq [[schedule expected-cron expected-subscription]
+ [[{:frequency "hourly"}
+ "0 0 * * * ? *" [:hourly nil nil nil]]
+ [{:frequency "daily" :hour 9}
+ "0 0 9 * * ? *" [:daily 9 nil nil]]
+ [{:frequency "weekly" :day_of_week "monday" :hour 9}
+ "0 0 9 ? * 2 *" [:weekly 9 "mon" nil]]
+ [{:frequency "monthly" :day_of_month "first-monday" :hour 9}
+ "0 0 9 ? * 2#1 *" [:monthly 9 "mon" :first]]]]
+ (testing (:frequency schedule)
+ (mt/with-model-cleanup [:model/Notification :model/Pulse]
+ (doseq [[tool args stored-schedules expected]
+ [[#'agent-tools/create-alert-tool
+ {:card_id card-id :send_condition "has_result"}
+ alert-schedules expected-cron]
+ [#'agent-tools/slackbot-create-dashboard-subscription-tool
+ {:dashboard_id slack-dash-id}
+ #(subscription-schedules slack-dash-id) expected-subscription]
+ [#'agent-tools/create-dashboard-subscription-tool
+ {:dashboard_id inapp-dash-id :slack_channel "data-team"}
+ #(subscription-schedules inapp-dash-id) expected-subscription]]]
+ (testing (str tool)
+ (is (= {:output "success"} (tool (assoc args :schedule schedule))))
+ (is (= [expected] (stored-schedules))))))))))))))