Skip to content

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

Remove redundant schedule-key conversion (#83353)

Chris Truterf4fd9491ac4d
diff --git a/src/metabase/metabot/tools/subscriptions.clj b/src/metabase/metabot/tools/subscriptions.clj
index aefb8081142..f9dc3dc085d 100644
--- a/src/metabase/metabot/tools/subscriptions.clj
+++ b/src/metabase/metabot/tools/subscriptions.clj
@@ -105,13 +105,7 @@
{:dashboard-id dashboard_id
:email email
:slack-channel slack_channel
- :schedule (-> schedule
- (update :frequency keyword)
- (cond->
- (:day_of_week schedule) (-> (assoc :day-of-week (keyword (:day_of_week schedule)))
- (dissoc :day_of_week))
- (:day_of_month schedule) (-> (assoc :day-of-month (keyword (:day_of_month schedule)))
- (dissoc :day_of_month))))})
+ :schedule schedule})
(set/rename-keys {:error :output}))
(catch Exception e
(metabot.tools.u/handle-agent-or-api-error e))))
diff --git a/test/metabase/metabot/tools/subscriptions_test.clj b/test/metabase/metabot/tools/subscriptions_test.clj
index 4690bb41223..5f6f4f09c27 100644
--- a/test/metabase/metabot/tools/subscriptions_test.clj
+++ b/test/metabase/metabot/tools/subscriptions_test.clj
@@ -5,7 +5,8 @@
[metabase.channel.settings :as channel.settings]
[metabase.metabot.test-util :as test-util]
[metabase.metabot.tools.subscriptions :as agent-subscriptions]
- [metabase.test :as mt]))
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
;;; ------------------------------------------ schema / metadata tests -----------------------------------------------
@@ -74,23 +75,6 @@
:schedule {:frequency "daily" :hour 9}})]
(is (= "no dashboard with this dashboard_id found" (:output result))))))))
-(deftest create-dashboard-subscription-schedule-keywords-test
- (testing "schedule keywords are converted from snake_case to kebab-case"
- (let [captured-args (atom nil)
- sent-schedule (fn [schedule]
- (agent-subscriptions/create-dashboard-subscription-tool
- {:dashboard_id 1
- :schedule schedule})
- (:schedule @captured-args))]
- (mt/with-dynamic-fn-redefs [agent-subscriptions/create-dashboard-subscription
- (fn [args] (reset! captured-args args) {:output "success"})]
- (mt/with-current-user (mt/user->id :crowberto)
- (is (= {:frequency :weekly :hour 9 :day-of-week :monday}
- (sent-schedule {:frequency "weekly" :hour 9 :day_of_week "monday"})))
- (is (= {:frequency :monthly :hour 9 :day-of-month :first-monday}
- (sent-schedule {:frequency "monthly" :hour 9 :day_of_month "first-monday"}))))))))
-
(deftest ^:parallel create-dashboard-subscription-unknown-day-of-week-test
(testing "a day_of_week outside the listed days is rejected at argument validation"
(is (=? #"Invalid tool arguments: `schedule` .*should be either \"sunday\".*"
@@ -122,7 +106,7 @@
(is (= {:output "success"} result)))))))))
(deftest create-dashboard-subscription-slack-monthly-schedule-test
- (testing "Slack subscription with monthly schedule → success"
+ (testing "Slack subscription with monthly schedule → success, stored with the requested schedule"
(mt/with-model-cleanup [:model/Pulse]
(mt/with-dynamic-fn-redefs [channel.settings/slack-configured? (constantly true)
channel.settings/slack-cached-channels-and-usernames
@@ -140,7 +124,12 @@
:schedule {:frequency "monthly"
:day_of_month "last-sunday"
:hour 7}})]
- (is (= {:output "success"} result)))))))))
+ (is (= {:output "success"} result))
+ (is (=? [{:schedule_type :monthly
+ :schedule_hour 7
+ :schedule_day "sun"
+ :schedule_frame :last}]
+ (t2/select :model/PulseChannel :pulse_id (t2/select-one-pk :model/Pulse :dashboard_id dash-id)))))))))))
(deftest create-dashboard-subscription-slack-required-test
(testing "empty slack_channel → error"
diff --git a/test/metabase/metabot/tools/util_test.clj b/test/metabase/metabot/tools/util_test.clj
index 02a2511bc87..c9963b182d7 100644
--- a/test/metabase/metabot/tools/util_test.clj
+++ b/test/metabase/metabot/tools/util_test.clj
@@ -93,6 +93,17 @@
:hour 9
:day-of-week day}))))))
+(deftest ^:parallel schedule->schedule-map-snake-case-string-values-test
+ (testing "snake_case keys and string values normalize to the same result as kebab-case keywords"
+ (are [kebab snake] (= (metabot.tools.util/schedule->schedule-map kebab)
+ (metabot.tools.util/schedule->schedule-map snake))
+ ;; weekly, keyed by day of week
+ {:frequency :weekly, :hour 8, :day-of-week :monday}
+ {:frequency "weekly", :hour 8, :day_of_week "monday"}
+ ;; monthly, keyed by day of month
+ {:frequency :monthly, :hour 6, :day-of-month :first-monday}
+ {:frequency "monthly", :hour 6, :day_of_month "first-monday"})))
+
(deftest metabot-scope-query-test
(testing "metabot-scope-query with collection hierarchy"
(mt/dataset test-data