Skip to content

Commit

Permalink
Add a few tests for time granularity parameters
Browse files Browse the repository at this point in the history
The API/model schemas for dashboard parameters are general enough
that they just work for this new type, which doesn't look very different
from a value parameter right now.
  • Loading branch information
bshepherdson committed May 2, 2024
1 parent 4fa5778 commit cdb5348
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions test/metabase/api/dashboard_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@
:dashboard_id dashboard-id}]
(testing "User's set parameter is saved and sent back in the dashboard response."
;; api request mimicking a user setting a parameter value
(mt/user-http-request :rasta :post (format "dashboard/%d/dashcard/%s/card/%s/query" dashboard-id dashcard-id card-id)
{:parameters [{:id "a" :value ["new value"]}]})
(is (some? (mt/user-http-request :rasta :post (format "dashboard/%d/dashcard/%s/card/%s/query" dashboard-id dashcard-id card-id)
{:parameters [{:id "a" :value ["new value"]}]})))
(is (= {:a ["new value"]}
(:last_used_param_values (mt/user-http-request :rasta :get 200 (format "dashboard/%d" dashboard-id)))))))))))

Expand Down Expand Up @@ -875,6 +875,25 @@
:width
first)))))))))

(deftest update-dashboard-add-time-granularity-param
(testing "PUT /api/dashboard/:id"
(testing "We can add a time granularity parameter to a dashboard"
(t2.with-temp/with-temp [Dashboard dashboard {}]
(with-dashboards-in-writeable-collection [dashboard]
(testing "the dashboard starts with no parameters."
(is (= []
(t2/select-one-fn :parameters Dashboard :id (u/the-id dashboard)))))

(testing "adding a new time granularity parameter works."
(let [params [{:name "Time Unit"
:slug "time_unit"
:id "927e929"
:type :temporal-unit
:sectionId "temporal-unit"}]]
(mt/user-http-request :rasta :put 200 (str "dashboard/" (u/the-id dashboard)) {:parameters params})
(is (= params
(t2/select-one-fn :parameters Dashboard :id (u/the-id dashboard)))))))))))

;;; +----------------------------------------------------------------------------------------------------------------+
;;; | UPDATING DASHBOARD CARD IN DASHCARD |
;;; +----------------------------------------------------------------------------------------------------------------+
Expand Down Expand Up @@ -1540,24 +1559,35 @@
:parameters [{:name "Category ID"
:slug "category_id"
:id "_CATEGORY_ID_"
:type :category}]}
:type :category}
{:name "Unit"
:slug "unit"
:id "_unit_"
:type :temporal-unit}]}
Card {card-id :id} {}
Card {card-id2 :id} {}
DashboardCard {dashcard-id :id} {:dashboard_id dashboard-id
:card_id card-id
:parameter_mappings [{:parameter_id "random-id"
:card_id card-id
:target [:dimension [:field (mt/id :venues :name) nil]]}]}
:target [:dimension [:field (mt/id :venues :name) nil]]}
{:parameter_id "also-random"
:card_id card-id
:target [:dimension [:field (mt/id :orders :created_at) {:temporal-unit "month"}]]}]}
DashboardCard _ {:dashboard_id dashboard-id, :card_id card-id2}] {}
(let [copy-id (u/the-id (mt/user-http-request :rasta :post 200 (format "dashboard/%d/copy" dashboard-id)))]
(try
(is (= 2
(count (t2/select-pks-set DashboardCard, :dashboard_id copy-id))))
(is (=? [{:name "Category ID" :slug "category_id" :id "_CATEGORY_ID_" :type :category}]
(is (=? [{:name "Category ID" :slug "category_id" :id "_CATEGORY_ID_" :type :category}
{:name "Unit", :slug "unit", :id "_unit_", :type :temporal-unit}]
(t2/select-one-fn :parameters Dashboard :id copy-id)))
(is (=? [{:parameter_id "random-id"
:card_id card-id
:target [:dimension [:field (mt/id :venues :name) nil]]}]
:target [:dimension [:field (mt/id :venues :name) nil]]}
{:parameter_id "also-random"
:card_id card-id
:target [:dimension [:field (mt/id :orders :created_at) {:temporal-unit :month}]]}]
(t2/select-one-fn :parameter_mappings DashboardCard :id dashcard-id)))
(finally
(t2/delete! Dashboard :id copy-id))))))))
Expand Down

0 comments on commit cdb5348

Please sign in to comment.