diff --git a/src/metabase/api/metric.clj b/src/metabase/api/metric.clj index 377a03118d4e987493248ae144397d7f1b9f6fd0..1122ff3e30e4249305ab3ee54ee645a7a2fddabb 100644 --- a/src/metabase/api/metric.clj +++ b/src/metabase/api/metric.clj @@ -16,7 +16,7 @@ definition [Required Dict]} (check-superuser) (checkp #(db/exists? Table :id table_id) "table_id" "Table does not exist.") - (check-500 (metric/create-metric table_id name description *current-user-id* definition))) + (check-500 (metric/create-metric! table_id name description *current-user-id* definition))) (defendpoint GET "/:id" @@ -34,7 +34,7 @@ definition [Required Dict]} (check-superuser) (check-404 (metric/exists-metric? id)) - (metric/update-metric + (metric/update-metric! {:id id :name name :description description @@ -49,7 +49,7 @@ {revision_message [Required NonEmptyString]} (check-superuser) (check-404 (metric/exists-metric? id)) - (metric/delete-metric id *current-user-id* revision_message) + (metric/delete-metric! id *current-user-id* revision_message) {:success true}) diff --git a/src/metabase/models/metric.clj b/src/metabase/models/metric.clj index be02f0b141fd7505325f560b42d4bbea111e6383..8904b44313c1794139fe90700ea6de7bd0552521 100644 --- a/src/metabase/models/metric.clj +++ b/src/metabase/models/metric.clj @@ -65,7 +65,7 @@ ;; ## Persistence Functions -(defn create-metric +(defn create-metric! "Create a new `Metric`. Returns the newly created `Metric` or throws an Exception." @@ -111,7 +111,7 @@ (db/sel :many Metric :table_id table-id, :is_active (if (= :active state) true false), (k/order :name :ASC))) (hydrate :creator)))) -(defn update-metric +(defn update-metric! "Update an existing `Metric`. Returns the updated `Metric` or throws an Exception." @@ -129,7 +129,7 @@ (u/prog1 (retrieve-metric id) (events/publish-event :metric-update (assoc <> :actor_id user-id, :revision_message revision_message)))) -(defn delete-metric +(defn delete-metric! "Delete a `Metric`. This does a soft delete and simply marks the `Metric` as deleted but does not actually remove the diff --git a/test/metabase/api/card_test.clj b/test/metabase/api/card_test.clj index 203f6ddbf19767802939f7be527acf7cdfbe19c6..62ad2ade70ebb77d3925325ae333a8b86b510439 100644 --- a/test/metabase/api/card_test.clj +++ b/test/metabase/api/card_test.clj @@ -10,7 +10,7 @@ [table :refer [Table]]) [metabase.test.data :refer :all] [metabase.test.data.users :refer :all] - [metabase.test.util :refer [match-$ expect-eval-actual-first random-name with-temp obj->json->obj]])) + [metabase.test.util :refer [match-$ expect-eval-actual-first random-name with-temp with-temp* obj->json->obj]])) ;; # CARD LIFECYCLE @@ -32,37 +32,38 @@ [true false true] - (with-temp Database [{db-id :id}] - (with-temp Card [{card-1-id :id} {:database_id (id)}] - (with-temp Card [{card-2-id :id} {:database_id db-id}] - (let [card-returned? (fn [database-id card-id] - (contains? (set (for [card ((user->client :rasta) :get 200 "card", :f :database, :model_id database-id)] - (:id card))) - card-id))] - [(card-returned? (id) card-1-id) - (card-returned? db-id card-1-id) - (card-returned? db-id card-2-id)]))))) + (with-temp* [Database [{db-id :id}] + Card [{card-1-id :id} {:database_id (id)}] + Card [{card-2-id :id} {:database_id db-id}]] + (let [card-returned? (fn [database-id card-id] + (contains? (set (for [card ((user->client :rasta) :get 200 "card", :f :database, :model_id database-id)] + (:id card))) + card-id))] + [(card-returned? (id) card-1-id) + (card-returned? db-id card-1-id) + (card-returned? db-id card-2-id)]))) ;; Make sure `id` is required when `f` is :database (expect {:errors {:id "id is required parameter when filter mode is 'database'"}} ((user->client :crowberto) :get 400 "card" :f :database)) ;; Filter cards by table -(expect [true - false - true] - (with-temp Database [{database-id :id}] - (with-temp Table [{table-1-id :id} {:db_id database-id}] - (with-temp Table [{table-2-id :id} {:db_id database-id}] - (with-temp Card [{card-1-id :id} {:table_id table-1-id}] - (with-temp Card [{card-2-id :id} {:table_id table-2-id}] - (let [card-returned? (fn [table-id card-id] - (contains? (set (for [card ((user->client :rasta) :get 200 "card", :f :table, :model_id table-id)] - (:id card))) - card-id))] - [(card-returned? table-1-id card-1-id) - (card-returned? table-2-id card-1-id) - (card-returned? table-2-id card-2-id)]))))))) +(expect + [true + false + true] + (with-temp* [Database [{database-id :id}] + Table [{table-1-id :id} {:db_id database-id}] + Table [{table-2-id :id} {:db_id database-id}] + Card [{card-1-id :id} {:table_id table-1-id}] + Card [{card-2-id :id} {:table_id table-2-id}]] + (let [card-returned? (fn [table-id card-id] + (contains? (set (for [card ((user->client :rasta) :get 200 "card", :f :table, :model_id table-id)] + (:id card))) + card-id))] + [(card-returned? table-1-id card-1-id) + (card-returned? table-2-id card-1-id) + (card-returned? table-2-id card-2-id)]))) ;; Make sure `id` is required when `f` is :table (expect {:errors {:id "id is required parameter when filter mode is 'table'"}} diff --git a/test/metabase/api/dashboard_test.clj b/test/metabase/api/dashboard_test.clj index aeb9a33c973755e6829f311717193155f2cc3dfc..48d0fdc51d587052f55ec50f30cae11a18b68142 100644 --- a/test/metabase/api/dashboard_test.clj +++ b/test/metabase/api/dashboard_test.clj @@ -120,11 +120,10 @@ :series []}] :organization_id nil} ;; fetch a dashboard WITH a dashboard card on it - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard"}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card"}] - (tu/with-temp DashboardCard [_ {:dashboard_id dashboard-id - :card_id card-id}] - (dashboard-response ((user->client :rasta) :get 200 (format "dashboard/%d" dashboard-id))))))) + (tu/with-temp* [Dashboard [{dashboard-id :id} {:name "Test Dashboard"}] + Card [{card-id :id} {:name "Dashboard Test Card"}] + DashboardCard [_ {:dashboard_id dashboard-id, :card_id card-id}]] + (dashboard-response ((user->client :rasta) :get 200 (format "dashboard/%d" dashboard-id))))) ;; ## PUT /api/dashboard/:id @@ -185,15 +184,15 @@ :sizeY 2 :col 4 :row 4}]] - (tu/with-temp Dashboard [{dashboard-id :id}] - (tu/with-temp Card [{card-id :id}] - [(-> ((user->client :rasta) :post 200 (format "dashboard/%d/cards" dashboard-id) {:cardId card-id - :row 4 - :col 4}) - (dissoc :id :dashboard_id :card_id) - (update :created_at #(not (nil? %))) - (update :updated_at #(not (nil? %)))) - (db/sel :many :fields [DashboardCard :sizeX :sizeY :col :row] :dashboard_id dashboard-id)]))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Card [{card-id :id}]] + [(-> ((user->client :rasta) :post 200 (format "dashboard/%d/cards" dashboard-id) {:cardId card-id + :row 4 + :col 4}) + (dissoc :id :dashboard_id :card_id) + (update :created_at #(not (nil? %))) + (update :updated_at #(not (nil? %)))) + (db/sel :many :fields [DashboardCard :sizeX :sizeY :col :row] :dashboard_id dashboard-id)])) ;; new dashboard card w/ additional series (expect @@ -213,16 +212,16 @@ :col 4 :row 4}] [0]] - (tu/with-temp Dashboard [{dashboard-id :id}] - (tu/with-temp Card [{card-id :id}] - (tu/with-temp Card [{series-id-1 :id} {:name "Series Card"}] - (let [dashboard-card ((user->client :rasta) :post 200 (format "dashboard/%d/cards" dashboard-id) {:cardId card-id - :row 4 - :col 4 - :series [{:id series-id-1}]})] - [(remove-ids-and-boolean-timestamps dashboard-card) - (db/sel :many :fields [DashboardCard :sizeX :sizeY :col :row] :dashboard_id dashboard-id) - (db/sel :many :field [DashboardCardSeries :position] :dashboardcard_id (:id dashboard-card))]))))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Card [{card-id :id}] + Card [{series-id-1 :id} {:name "Series Card"}]] + (let [dashboard-card ((user->client :rasta) :post 200 (format "dashboard/%d/cards" dashboard-id) {:cardId card-id + :row 4 + :col 4 + :series [{:id series-id-1}]})] + [(remove-ids-and-boolean-timestamps dashboard-card) + (db/sel :many :fields [DashboardCard :sizeX :sizeY :col :row] :dashboard_id dashboard-id) + (db/sel :many :field [DashboardCardSeries :position] :dashboardcard_id (:id dashboard-card))]))) ;; ## DELETE /api/dashboard/:id/cards @@ -231,16 +230,16 @@ {:success true} 0] ;; fetch a dashboard WITH a dashboard card on it - (tu/with-temp Dashboard [{dashboard-id :id}] - (tu/with-temp Card [{card-id :id}] - (tu/with-temp Card [{series-id-1 :id}] - (tu/with-temp Card [{series-id-2 :id}] - (tu/with-temp DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-1, :position 0}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-2, :position 1}] - [(count (db/sel :many :field [DashboardCard :id] :dashboard_id dashboard-id)) - ((user->client :rasta) :delete 200 (format "dashboard/%d/cards" dashboard-id) :dashcardId dashcard-id) - (count (db/sel :many :field [DashboardCard :id] :dashboard_id dashboard-id))])))))))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Card [{card-id :id}] + Card [{series-id-1 :id}] + Card [{series-id-2 :id}] + DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}] + DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-1, :position 0}] + DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-2, :position 1}]] + [(count (db/sel :many :field [DashboardCard :id] :dashboard_id dashboard-id)) + ((user->client :rasta) :delete 200 (format "dashboard/%d/cards" dashboard-id) :dashcardId dashcard-id) + (count (db/sel :many :field [DashboardCard :id] :dashboard_id dashboard-id))])) ;; ## PUT /api/dashboard/:id/cards @@ -280,26 +279,26 @@ :created_at true :updated_at true}]] ;; fetch a dashboard WITH a dashboard card on it - (tu/with-temp Dashboard [{dashboard-id :id}] - (tu/with-temp Card [{card-id :id}] - (tu/with-temp DashboardCard [{dashcard-id-1 :id} {:dashboard_id dashboard-id, :card_id card-id}] - (tu/with-temp DashboardCard [{dashcard-id-2 :id} {:dashboard_id dashboard-id, :card_id card-id}] - (tu/with-temp Card [{series-id-1 :id} {:name "Series Card"}] - [[(remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-1)) - (remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-2))] - ((user->client :rasta) :put 200 (format "dashboard/%d/cards" dashboard-id) {:cards [{:id dashcard-id-1 - :sizeX 4 - :sizeY 2 - :col 0 - :row 0 - :series [{:id series-id-1}]} - {:id dashcard-id-2 - :sizeX 1 - :sizeY 1 - :col 1 - :row 3}]}) - [(remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-1)) - (remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-2))]])))))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Card [{card-id :id}] + DashboardCard [{dashcard-id-1 :id} {:dashboard_id dashboard-id, :card_id card-id}] + DashboardCard [{dashcard-id-2 :id} {:dashboard_id dashboard-id, :card_id card-id}] + Card [{series-id-1 :id} {:name "Series Card"}]] + [[(remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-1)) + (remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-2))] + ((user->client :rasta) :put 200 (format "dashboard/%d/cards" dashboard-id) {:cards [{:id dashcard-id-1 + :sizeX 4 + :sizeY 2 + :col 0 + :row 0 + :series [{:id series-id-1}]} + {:id dashcard-id-2 + :sizeX 1 + :sizeY 1 + :col 1 + :row 3}]}) + [(remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-1)) + (remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-2))]])) @@ -325,38 +324,34 @@ (dissoc :email :date_joined :last_login :is_superuser :is_qbnewb)) :diff nil :description nil}] - (tu/with-temp Dashboard [{:keys [id]}] - (tu/with-temp Revision [_ {:model "Dashboard" - :model_id id - :user_id (user->id :rasta) - :object {:name "b" - :description nil - :public_perms 0 - :cards [{:sizeX 2 - :sizeY 2 - :row nil - :col nil - :card_id 123 - :series []}]} - :is_creation true - :is_reversion false}] - (tu/with-temp Revision [_ {:model "Dashboard" - :model_id id - :user_id (user->id :crowberto) - :object {:name "c" - :description "something" - :public_perms 0 - :cards [{:sizeX 4 - :sizeY 3 - :row 0 - :col 0 - :card_id 123 - :series [8 9]}]} - :is_creation false - :is_reversion false - :message "updated"}] - (->> ((user->client :crowberto) :get 200 (format "dashboard/%d/revisions" id)) - (mapv #(dissoc % :timestamp :id))))))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Revision [_ {:model "Dashboard" + :model_id dashboard-id + :object {:name "b" + :description nil + :public_perms 0 + :cards [{:sizeX 2 + :sizeY 2 + :row nil + :col nil + :card_id 123 + :series []}]} + :is_creation true}] + Revision [_ {:model "Dashboard" + :model_id dashboard-id + :user_id (user->id :crowberto) + :object {:name "c" + :description "something" + :public_perms 0 + :cards [{:sizeX 4 + :sizeY 3 + :row 0 + :col 0 + :card_id 123 + :series [8 9]}]} + :message "updated"}]] + (doall (for [revision ((user->client :crowberto) :get 200 (format "dashboard/%d/revisions" dashboard-id))] + (dissoc revision :timestamp :id))))) ;; ## POST /api/dashboard/:id/revert @@ -369,7 +364,7 @@ (expect - [;; the api response + [ ;; the api response {:is_reversion true :is_creation false :message nil @@ -402,27 +397,22 @@ (dissoc :email :date_joined :last_login :is_superuser :is_qbnewb)) :diff nil :description nil}]] - (tu/with-temp Dashboard [{:keys [id]}] - (tu/with-temp Revision [{revision-id :id} {:model "Dashboard" - :model_id id - :user_id (user->id :rasta) - :object {:name "a" - :description nil - :public_perms 0 - :cards []} - :is_creation true - :is_reversion false}] - (tu/with-temp Revision [_ {:model "Dashboard" - :model_id id - :user_id (user->id :crowberto) - :object {:name "b" - :description nil - :public_perms 0 - :cards []} - :is_creation false - :is_reversion false - :message "updated"}] - [(-> ((user->client :crowberto) :post 200 (format "dashboard/%d/revert" id) {:revision_id revision-id}) - (dissoc :id :timestamp)) - (->> ((user->client :crowberto) :get 200 (format "dashboard/%d/revisions" id)) - (mapv #(dissoc % :timestamp :id)))])))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Revision [{revision-id :id} {:model "Dashboard" + :model_id dashboard-id + :object {:name "a" + :description nil + :public_perms 0 + :cards []} + :is_creation true}] + Revision [_ {:model "Dashboard" + :model_id dashboard-id + :user_id (user->id :crowberto) + :object {:name "b" + :description nil + :public_perms 0 + :cards []} + :message "updated"}]] + [(dissoc ((user->client :crowberto) :post 200 (format "dashboard/%d/revert" dashboard-id) {:revision_id revision-id}) :id :timestamp) + (doall (for [revision ((user->client :crowberto) :get 200 (format "dashboard/%d/revisions" dashboard-id))] + (dissoc revision :timestamp :id)))])) diff --git a/test/metabase/api/field_test.clj b/test/metabase/api/field_test.clj index 282947ec9b4a22bde4f08e851fc5f8ac57960cff..3d857768d7546918e18b6336d230c58f472fc2ba 100644 --- a/test/metabase/api/field_test.clj +++ b/test/metabase/api/field_test.clj @@ -158,11 +158,11 @@ (expect ["Invalid Request." nil] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Field [{field-id :id} {:table_id table-id}] - [((user->client :crowberto) :put 400 (str "field/" field-id) {:special_type :timestamp_seconds}) - (db/sel :one :field [Field :special_type], :id field-id)])))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Field [{field-id :id} {:table_id table-id}]] + [((user->client :crowberto) :put 400 (str "field/" field-id) {:special_type :timestamp_seconds}) + (db/sel :one :field [Field :special_type], :id field-id)])) (defn- field->field-values diff --git a/test/metabase/api/foreignkey_test.clj b/test/metabase/api/foreignkey_test.clj index 398378b2f4372ee4baf52afb1313a99ef53b7de6..9c39ba8e2d5190319d30c4636b63408b2b0fef70 100644 --- a/test/metabase/api/foreignkey_test.clj +++ b/test/metabase/api/foreignkey_test.clj @@ -30,11 +30,11 @@ (expect [{:success true} nil] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Field [{field-id :id} {:table_id table-id}] - (tu/with-temp ForeignKey [{:keys [id]} {:destination_id field-id - :origin_id field-id - :relationship "whoot"}] - [((user->client :crowberto) :delete 200 (format "foreignkey/%d" id)) - (db/sel :one ForeignKey :id id)]))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Field [{field-id :id} {:table_id table-id}] + ForeignKey [{:keys [id]} {:destination_id field-id + :origin_id field-id + :relationship "whoot"}]] + [((user->client :crowberto) :delete 200 (format "foreignkey/%d" id)) + (db/sel :one ForeignKey :id id)])) diff --git a/test/metabase/api/metric_test.clj b/test/metabase/api/metric_test.clj index 0f0cfcf14afe95903a763d6aa79da8ad74700d1d..389980c9a40fee053c457e7a114f67b20f161be3 100644 --- a/test/metabase/api/metric_test.clj +++ b/test/metabase/api/metric_test.clj @@ -81,13 +81,13 @@ :is_active true :definition {:database 21 :query {:filter ["abc"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (metric-response ((user->client :crowberto) :post 200 "metric" {:name "A Metric" - :description "I did it!" - :table_id id - :definition {:database 21 - :query {:filter ["abc"]}}}))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}]] + (metric-response ((user->client :crowberto) :post 200 "metric" {:name "A Metric" + :description "I did it!" + :table_id id + :definition {:database 21 + :query {:filter ["abc"]}}})))) ;; ## PUT /api/metric @@ -128,20 +128,16 @@ :is_active true :definition {:database 2 :query {:filter ["not" "the toucans you're looking for"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [{:keys [id]} {:creator_id (user->id :rasta) - :table_id table-id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {}}] - (metric-response ((user->client :crowberto) :put 200 (format "metric/%d" id) {:id id - :name "Costa Rica" - :description nil - :table_id 456 - :revision_message "I got me some revisions" - :definition {:database 2 - :query {:filter ["not" "the toucans you're looking for"]}}})))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{:keys [id]} {:table_id table-id}]] + (metric-response ((user->client :crowberto) :put 200 (format "metric/%d" id) {:id id + :name "Costa Rica" + :description nil + :table_id 456 + :revision_message "I got me some revisions" + :definition {:database 2 + :query {:filter ["not" "the toucans you're looking for"]}}})))) ;; ## DELETE /api/metric/:id @@ -168,18 +164,11 @@ :updated_at true :is_active false :definition {}}] - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [{:keys [id]} {:creator_id (user->id :rasta) - :table_id table-id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {}}] - [((user->client :crowberto) :delete 200 (format "metric/%d" id) :revision_message "carryon") - (metric-response (metric/retrieve-metric id))])))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{:keys [id]} {:table_id table-id}]] + [((user->client :crowberto) :delete 200 (format "metric/%d" id) :revision_message "carryon") + (metric-response (metric/retrieve-metric id))])) ;; ## GET /api/metric/:id @@ -190,24 +179,19 @@ (expect - {:name "One Metric to rule them all, one metric to define them" - :description "One metric to bring them all, and in the DataModel bind them" + {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :creator_id (user->id :crowberto) :creator (user-details (fetch-user :crowberto)) :created_at true :updated_at true :is_active true - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [{:keys [id]} {:creator_id (user->id :crowberto) - :table_id table-id - :name "One Metric to rule them all, one metric to define them" - :description "One metric to bring them all, and in the DataModel bind them" - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}}] - (metric-response ((user->client :crowberto) :get 200 (format "metric/%d" id))))))) + :definition {}} + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{:keys [id]} {:creator_id (user->id :crowberto) + :table_id table-id}]] + (metric-response ((user->client :crowberto) :get 200 (format "metric/%d" id))))) ;; ## GET /api/metric/:id/revisions @@ -233,31 +217,27 @@ :diff {:name {:after "b"} :definition {:after {:filter ["AND" [">" 1 25]]}}} :description nil}] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [{:keys [id]} {:creator_id (user->id :crowberto) - :table_id table-id - :name "One Metric to rule them all, one metric to define them" - :description "One metric to bring them all, and in the DataModel bind them" - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}}] - (tu/with-temp Revision [_ {:model "Metric" - :model_id id - :user_id (user->id :rasta) - :object {:name "b" - :definition {:filter ["AND" [">" 1 25]]}} - :is_creation true - :is_reversion false}] - (tu/with-temp Revision [_ {:model "Metric" - :model_id id - :user_id (user->id :crowberto) - :object {:name "c" - :definition {:filter ["AND" [">" 1 25]]}} - :is_creation false - :is_reversion false - :message "updated"}] - (->> ((user->client :crowberto) :get 200 (format "metric/%d/revisions" id)) - (mapv #(dissoc % :timestamp :id))))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{:keys [id]} {:creator_id (user->id :crowberto) + :table_id table-id + :name "One Metric to rule them all, one metric to define them" + :description "One metric to bring them all, and in the DataModel bind them" + :definition {:database 123 + :query {:filter ["In the Land of Metabase where the Datas lie"]}}}] + Revision [_ {:model "Metric" + :model_id id + :object {:name "b" + :definition {:filter ["AND" [">" 1 25]]}} + :is_creation true}] + Revision [_ {:model "Metric" + :model_id id + :user_id (user->id :crowberto) + :object {:name "c" + :definition {:filter ["AND" [">" 1 25]]}} + :message "updated"}]] + (doall (for [revision ((user->client :crowberto) :get 200 (format "metric/%d/revisions" id))] + (dissoc revision :timestamp :id))))) ;; ## POST /api/metric/:id/revert @@ -275,12 +255,11 @@ (expect - [;; the api response + [ ;; the api response {:is_reversion true :is_creation false :message nil - :user (-> (user-details (fetch-user :crowberto)) - (dissoc :email :date_joined :last_login :is_superuser :is_qbnewb)) + :user (dissoc (user-details (fetch-user :crowberto)) :email :date_joined :last_login :is_superuser :is_qbnewb) :diff {:name {:before "Changed Metric Name" :after "One Metric to rule them all, one metric to define them"}} :description "renamed this Metric from \"Changed Metric Name\" to \"One Metric to rule them all, one metric to define them\"."} @@ -288,65 +267,57 @@ [{:is_reversion true :is_creation false :message nil - :user (-> (user-details (fetch-user :crowberto)) - (dissoc :email :date_joined :last_login :is_superuser :is_qbnewb)) + :user (dissoc (user-details (fetch-user :crowberto)) :email :date_joined :last_login :is_superuser :is_qbnewb) :diff {:name {:before "Changed Metric Name" :after "One Metric to rule them all, one metric to define them"}} :description "renamed this Metric from \"Changed Metric Name\" to \"One Metric to rule them all, one metric to define them\"."} {:is_reversion false :is_creation false :message "updated" - :user (-> (user-details (fetch-user :crowberto)) - (dissoc :email :date_joined :last_login :is_superuser :is_qbnewb)) + :user (dissoc (user-details (fetch-user :crowberto)) :email :date_joined :last_login :is_superuser :is_qbnewb) :diff {:name {:after "Changed Metric Name" :before "One Metric to rule them all, one metric to define them"}} :description "renamed this Metric from \"One Metric to rule them all, one metric to define them\" to \"Changed Metric Name\"."} {:is_reversion false :is_creation true :message nil - :user (-> (user-details (fetch-user :rasta)) - (dissoc :email :date_joined :last_login :is_superuser :is_qbnewb)) + :user (dissoc (user-details (fetch-user :rasta)) :email :date_joined :last_login :is_superuser :is_qbnewb) :diff {:name {:after "One Metric to rule them all, one metric to define them"} :description {:after "One metric to bring them all, and in the DataModel bind them"} :definition {:after {:database 123 :query {:filter ["In the Land of Metabase where the Datas lie"]}}}} :description nil}]] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [{:keys [id]} {:creator_id (user->id :crowberto) - :table_id table-id - :name "One Metric to rule them all, one metric to define them" - :description "One metric to bring them all, and in the DataModel bind them" - :definition {:creator_id (user->id :crowberto) - :table_id table-id - :name "Reverted Metric Name" - :description nil - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}}}] - (tu/with-temp Revision [{revision-id :id} {:model "Metric" - :model_id id - :user_id (user->id :rasta) - :object {:creator_id (user->id :crowberto) - :table_id table-id - :name "One Metric to rule them all, one metric to define them" - :description "One metric to bring them all, and in the DataModel bind them" - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}} - :is_creation true - :is_reversion false}] - (tu/with-temp Revision [_ {:model "Metric" - :model_id id - :user_id (user->id :crowberto) - :object {:creator_id (user->id :crowberto) - :table_id table-id - :name "Changed Metric Name" - :description "One metric to bring them all, and in the DataModel bind them" - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}} - :is_creation false - :is_reversion false - :message "updated"}] - [(-> ((user->client :crowberto) :post 200 (format "metric/%d/revert" id) {:revision_id revision-id}) - (dissoc :id :timestamp)) - (->> ((user->client :crowberto) :get 200 (format "metric/%d/revisions" id)) - (mapv #(dissoc % :timestamp :id)))])))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{:keys [id]} {:creator_id (user->id :crowberto) + :table_id table-id + :name "One Metric to rule them all, one metric to define them" + :description "One metric to bring them all, and in the DataModel bind them" + :definition {:creator_id (user->id :crowberto) + :table_id table-id + :name "Reverted Metric Name" + :description nil + :definition {:database 123 + :query {:filter ["In the Land of Metabase where the Datas lie"]}}}}] + Revision [{revision-id :id} {:model "Metric" + :model_id id + :object {:creator_id (user->id :crowberto) + :table_id table-id + :name "One Metric to rule them all, one metric to define them" + :description "One metric to bring them all, and in the DataModel bind them" + :definition {:database 123 + :query {:filter ["In the Land of Metabase where the Datas lie"]}}} + :is_creation true}] + Revision [_ {:model "Metric" + :model_id id + :user_id (user->id :crowberto) + :object {:creator_id (user->id :crowberto) + :table_id table-id + :name "Changed Metric Name" + :description "One metric to bring them all, and in the DataModel bind them" + :definition {:database 123 + :query {:filter ["In the Land of Metabase where the Datas lie"]}}} + :message "updated"}]] + [(dissoc ((user->client :crowberto) :post 200 (format "metric/%d/revert" id) {:revision_id revision-id}) :id :timestamp) + (doall (for [revision ((user->client :crowberto) :get 200 (format "metric/%d/revisions" id))] + (dissoc revision :timestamp :id)))])) diff --git a/test/metabase/api/revision_test.clj b/test/metabase/api/revision_test.clj index 6ae7179a94421bec2bc65906086c40edbb02146e..8f35198c513b349b083ba7d1bd3bec5aac51677c 100644 --- a/test/metabase/api/revision_test.clj +++ b/test/metabase/api/revision_test.clj @@ -9,7 +9,7 @@ [revision-test :refer [with-fake-card]]) [metabase.test.data :refer :all] [metabase.test.data.users :refer :all] - [metabase.test.util :refer [expect-eval-actual-first with-temp random-name]])) + [metabase.test.util :refer [expect-eval-actual-first random-name]])) (def ^:private rasta-revision-info (delay {:id (user->id :rasta) :common_name "Rasta Toucan", :first_name "Rasta", :last_name "Toucan"})) diff --git a/test/metabase/api/segment_test.clj b/test/metabase/api/segment_test.clj index 72337f14089434df9c521c6d4cc983cad16504bd..a6479f9fc66ffc508fa3a342a6ca215c66468510 100644 --- a/test/metabase/api/segment_test.clj +++ b/test/metabase/api/segment_test.clj @@ -81,13 +81,13 @@ :is_active true :definition {:database 21 :query {:filter ["abc"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (segment-response ((user->client :crowberto) :post 200 "segment" {:name "A Segment" - :description "I did it!" - :table_id id - :definition {:database 21 - :query {:filter ["abc"]}}}))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}]] + (segment-response ((user->client :crowberto) :post 200 "segment" {:name "A Segment" + :description "I did it!" + :table_id id + :definition {:database 21 + :query {:filter ["abc"]}}})))) ;; ## PUT /api/segment @@ -128,20 +128,16 @@ :is_active true :definition {:database 2 :query {:filter ["not" "the toucans you're looking for"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Segment [{:keys [id]} {:creator_id (user->id :rasta) - :table_id table-id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {}}] - (segment-response ((user->client :crowberto) :put 200 (format "segment/%d" id) {:id id - :name "Costa Rica" - :description nil - :table_id 456 - :revision_message "I got me some revisions" - :definition {:database 2 - :query {:filter ["not" "the toucans you're looking for"]}}})))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [{:keys [id]} {:table_id table-id}]] + (segment-response ((user->client :crowberto) :put 200 (format "segment/%d" id) {:id id + :name "Costa Rica" + :description nil + :table_id 456 + :revision_message "I got me some revisions" + :definition {:database 2 + :query {:filter ["not" "the toucans you're looking for"]}}})))) ;; ## DELETE /api/segment/:id @@ -168,15 +164,11 @@ :updated_at true :is_active false :definition {}}] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Segment [{:keys [id]} {:creator_id (user->id :rasta) - :table_id table-id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {}}] - [((user->client :crowberto) :delete 200 (format "segment/%d" id) :revision_message "carryon") - (segment-response (segment/retrieve-segment id))])))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [{:keys [id]} {:table_id table-id}]] + [((user->client :crowberto) :delete 200 (format "segment/%d" id) :revision_message "carryon") + (segment-response (segment/retrieve-segment id))])) ;; ## GET /api/segment/:id @@ -187,8 +179,8 @@ (expect - {:name "One Segment to rule them all, one segment to define them" - :description "One segment to bring them all, and in the DataModel bind them" + {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :creator_id (user->id :crowberto) :creator (user-details (fetch-user :crowberto)) :created_at true @@ -196,15 +188,13 @@ :is_active true :definition {:database 123 :query {:filter ["In the Land of Metabase where the Datas lie"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Segment [{:keys [id]} {:creator_id (user->id :crowberto) - :table_id table-id - :name "One Segment to rule them all, one segment to define them" - :description "One segment to bring them all, and in the DataModel bind them" - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}}] - (segment-response ((user->client :crowberto) :get 200 (format "segment/%d" id))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [{:keys [id]} {:creator_id (user->id :crowberto) + :table_id table-id + :definition {:database 123 + :query {:filter ["In the Land of Metabase where the Datas lie"]}}}]] + (segment-response ((user->client :crowberto) :get 200 (format "segment/%d" id))))) ;; ## GET /api/segment/:id/revisions @@ -230,31 +220,25 @@ :diff {:name {:after "b"} :definition {:after {:filter ["AND" [">" 1 25]]}}} :description nil}] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Segment [{:keys [id]} {:creator_id (user->id :crowberto) - :table_id table-id - :name "One Segment to rule them all, one segment to define them" - :description "One segment to bring them all, and in the DataModel bind them" - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}}] - (tu/with-temp Revision [_ {:model "Segment" - :model_id id - :user_id (user->id :rasta) - :object {:name "b" - :definition {:filter ["AND" [">" 1 25]]}} - :is_creation true - :is_reversion false}] - (tu/with-temp Revision [_ {:model "Segment" - :model_id id - :user_id (user->id :crowberto) - :object {:name "c" - :definition {:filter ["AND" [">" 1 25]]}} - :is_creation false - :is_reversion false - :message "updated"}] - (->> ((user->client :crowberto) :get 200 (format "segment/%d/revisions" id)) - (mapv #(dissoc % :timestamp :id))))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [{:keys [id]} {:creator_id (user->id :crowberto) + :table_id table-id + :definition {:database 123 + :query {:filter ["In the Land of Metabase where the Datas lie"]}}}] + Revision [_ {:model "Segment" + :model_id id + :object {:name "b" + :definition {:filter ["AND" [">" 1 25]]}} + :is_creation true}] + Revision [_ {:model "Segment" + :model_id id + :user_id (user->id :crowberto) + :object {:name "c" + :definition {:filter ["AND" [">" 1 25]]}} + :message "updated"}]] + (doall (for [revision ((user->client :crowberto) :get 200 (format "segment/%d/revisions" id))] + (dissoc revision :timestamp :id))))) ;; ## POST /api/segment/:id/revert @@ -272,7 +256,7 @@ (expect - [;; the api response + [ ;; the api response {:is_reversion true :is_creation false :message nil @@ -308,42 +292,37 @@ :definition {:after {:database 123 :query {:filter ["In the Land of Metabase where the Datas lie"]}}}} :description nil}]] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Segment [{:keys [id]} {:creator_id (user->id :crowberto) - :table_id table-id - :name "One Segment to rule them all, one segment to define them" - :description "One segment to bring them all, and in the DataModel bind them" - :definition {:creator_id (user->id :crowberto) - :table_id table-id - :name "Reverted Segment Name" - :description nil - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}}}] - (tu/with-temp Revision [{revision-id :id} {:model "Segment" - :model_id id - :user_id (user->id :rasta) - :object {:creator_id (user->id :crowberto) - :table_id table-id - :name "One Segment to rule them all, one segment to define them" - :description "One segment to bring them all, and in the DataModel bind them" - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}} - :is_creation true - :is_reversion false}] - (tu/with-temp Revision [_ {:model "Segment" - :model_id id - :user_id (user->id :crowberto) - :object {:creator_id (user->id :crowberto) - :table_id table-id - :name "Changed Segment Name" - :description "One segment to bring them all, and in the DataModel bind them" - :definition {:database 123 - :query {:filter ["In the Land of Metabase where the Datas lie"]}}} - :is_creation false - :is_reversion false - :message "updated"}] - [(-> ((user->client :crowberto) :post 200 (format "segment/%d/revert" id) {:revision_id revision-id}) - (dissoc :id :timestamp)) - (->> ((user->client :crowberto) :get 200 (format "segment/%d/revisions" id)) - (mapv #(dissoc % :timestamp :id)))])))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [{:keys [id]} {:creator_id (user->id :crowberto) + :table_id table-id + :name "One Segment to rule them all, one segment to define them" + :description "One segment to bring them all, and in the DataModel bind them" + :definition {:creator_id (user->id :crowberto) + :table_id table-id + :name "Reverted Segment Name" + :description nil + :definition {:database 123 + :query {:filter ["In the Land of Metabase where the Datas lie"]}}}}] + Revision [{revision-id :id} {:model "Segment" + :model_id id + :object {:creator_id (user->id :crowberto) + :table_id table-id + :name "One Segment to rule them all, one segment to define them" + :description "One segment to bring them all, and in the DataModel bind them" + :definition {:database 123 + :query {:filter ["In the Land of Metabase where the Datas lie"]}}} + :is_creation true}] + Revision [_ {:model "Segment" + :model_id id + :user_id (user->id :crowberto) + :object {:creator_id (user->id :crowberto) + :table_id table-id + :name "Changed Segment Name" + :description "One segment to bring them all, and in the DataModel bind them" + :definition {:database 123 + :query {:filter ["In the Land of Metabase where the Datas lie"]}}} + :message "updated"}]] + [(dissoc ((user->client :crowberto) :post 200 (format "segment/%d/revert" id) {:revision_id revision-id}) :id :timestamp) + (doall (for [revision ((user->client :crowberto) :get 200 (format "segment/%d/revisions" id))] + (dissoc revision :timestamp :id)))])) diff --git a/test/metabase/driver/query_processor/macros_test.clj b/test/metabase/driver/query_processor/macros_test.clj index db2806b59c47733af1bbb4be3034f76da204e2fc..980aa47670bbc108c4bc7e8e628392532392f682 100644 --- a/test/metabase/driver/query_processor/macros_test.clj +++ b/test/metabase/driver/query_processor/macros_test.clj @@ -30,21 +30,17 @@ :query {:aggregation ["rows"] :filter ["AND" ["AND" ["=" 5 "abc"]] ["OR" ["AND" ["IS_NULL" 7]] [">" 4 1]]] :breakout [17]}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Segment [{segment1 :id} {:creator_id (user->id :crowberto) - :table_id table-id - :name "Test Segment" - :definition {:filter ["AND" ["=" 5 "abc"]]}}] - (tu/with-temp Segment [{segment2 :id} {:creator_id (user->id :crowberto) - :table_id table-id - :name "Test Segment" - :definition {:filter ["AND" ["IS_NULL" 7]]}}] - (expand-macros {:database 1 - :type :query - :query {:aggregation ["rows"] - :filter ["AND" ["SEGMENT" segment1] ["OR" ["SEGMENT" segment2] [">" 4 1]]] - :breakout [17]}})))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [{segment-1-id :id} {:table_id table-id + :definition {:filter ["AND" ["=" 5 "abc"]]}}] + Segment [{segment-2-id :id} {:table_id table-id + :definition {:filter ["AND" ["IS_NULL" 7]]}}]] + (expand-macros {:database 1 + :type :query + :query {:aggregation ["rows"] + :filter ["AND" ["SEGMENT" segment-1-id] ["OR" ["SEGMENT" segment-2-id] [">" 4 1]]] + :breakout [17]}}))) ;; just a metric (w/out nested segments) (expect @@ -54,19 +50,17 @@ :filter ["AND" ["AND" [">" 4 1]] ["AND" ["=" 5 "abc"]]] :breakout [17] :order_by [[1 "ASC"]]}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [{metric1 :id} {:creator_id (user->id :crowberto) - :table_id table-id - :name "Test Metric" - :definition {:aggregation ["count"] - :filter ["AND" ["=" 5 "abc"]]}}] - (expand-macros {:database 1 - :type :query - :query {:aggregation ["METRIC" metric1] - :filter ["AND" [">" 4 1]] - :breakout [17] - :order_by [[1 "ASC"]]}}))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{metric-1-id :id} {:table_id table-id + :definition {:aggregation ["count"] + :filter ["AND" ["=" 5 "abc"]]}}]] + (expand-macros {:database 1 + :type :query + :query {:aggregation ["METRIC" metric-1-id] + :filter ["AND" [">" 4 1]] + :breakout [17] + :order_by [[1 "ASC"]]}}))) ;; check that when the original filter is empty we simply use our metric filter definition instead (expect @@ -76,19 +70,17 @@ :filter ["AND" ["=" 5 "abc"]] :breakout [17] :order_by [[1 "ASC"]]}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [{metric1 :id} {:creator_id (user->id :crowberto) - :table_id table-id - :name "Test Metric" - :definition {:aggregation ["count"] - :filter ["AND" ["=" 5 "abc"]]}}] - (expand-macros {:database 1 - :type :query - :query {:aggregation ["METRIC" metric1] - :filter [] - :breakout [17] - :order_by [[1 "ASC"]]}}))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{metric-1-id :id} {:table_id table-id + :definition {:aggregation ["count"] + :filter ["AND" ["=" 5 "abc"]]}}]] + (expand-macros {:database 1 + :type :query + :query {:aggregation ["METRIC" metric-1-id] + :filter [] + :breakout [17] + :order_by [[1 "ASC"]]}}))) ;; metric w/ no filter definition (expect @@ -98,18 +90,16 @@ :filter ["AND" ["=" 5 "abc"]] :breakout [17] :order_by [[1 "ASC"]]}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [{metric1 :id} {:creator_id (user->id :crowberto) - :table_id table-id - :name "Test Metric" - :definition {:aggregation ["count"]}}] - (expand-macros {:database 1 - :type :query - :query {:aggregation ["METRIC" metric1] - :filter ["AND" ["=" 5 "abc"]] - :breakout [17] - :order_by [[1 "ASC"]]}}))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{metric-1-id :id} {:table_id table-id + :definition {:aggregation ["count"]}}]] + (expand-macros {:database 1 + :type :query + :query {:aggregation ["METRIC" metric-1-id] + :filter ["AND" ["=" 5 "abc"]] + :breakout [17] + :order_by [[1 "ASC"]]}}))) ;; a metric w/ nested segments (expect @@ -119,24 +109,18 @@ :filter ["AND" ["AND" [">" 4 1] ["AND" ["IS_NULL" 7]]] ["AND" ["=" 5 "abc"] ["AND" ["BETWEEN" 9 0 25]]]] :breakout [17] :order_by [[1 "ASC"]]}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Segment [{segment1 :id} {:creator_id (user->id :crowberto) - :table_id table-id - :name "Test Segment" - :definition {:filter ["AND" ["BETWEEN" 9 0 25]]}}] - (tu/with-temp Segment [{segment2 :id} {:creator_id (user->id :crowberto) - :table_id table-id - :name "Test Segment" - :definition {:filter ["AND" ["IS_NULL" 7]]}}] - (tu/with-temp Metric [{metric1 :id} {:creator_id (user->id :crowberto) - :table_id table-id - :name "Test Metric" - :definition {:aggregation ["sum" 18] - :filter ["AND" ["=" 5 "abc"] ["SEGMENT" segment1]]}}] - (expand-macros {:database 1 - :type :query - :query {:aggregation ["METRIC" metric1] - :filter ["AND" [">" 4 1] ["SEGMENT" segment2]] - :breakout [17] - :order_by [[1 "ASC"]]}}))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [{segment-1-id :id} {:table_id table-id + :definition {:filter ["AND" ["BETWEEN" 9 0 25]]}}] + Segment [{segment-2-id :id} {:table_id table-id + :definition {:filter ["AND" ["IS_NULL" 7]]}}] + Metric [{metric-1-id :id} {:table_id table-id + :definition {:aggregation ["sum" 18] + :filter ["AND" ["=" 5 "abc"] ["SEGMENT" segment-1-id]]}}]] + (expand-macros {:database 1 + :type :query + :query {:aggregation ["METRIC" metric-1-id] + :filter ["AND" [">" 4 1] ["SEGMENT" segment-2-id]] + :breakout [17] + :order_by [[1 "ASC"]]}}))) diff --git a/test/metabase/driver/sync_test.clj b/test/metabase/driver/sync_test.clj index 10dde91e7909c7398b15a37e77f88889ee09b04e..36f94e01a408391d5b252806256077bfee2834f8 100644 --- a/test/metabase/driver/sync_test.clj +++ b/test/metabase/driver/sync_test.clj @@ -82,7 +82,7 @@ :display_name "Id" :base_type :IntegerField :visibility_type :normal - :fk_target_field_id nil + :fk_target_field_id nil} {:description nil :special_type nil :name "studio" @@ -95,7 +95,7 @@ :base_type :TextField :visibility_type :normal :fk_target_field_id nil} - {:description nil + {:description nil :special_type nil :name "title" :active true @@ -196,12 +196,13 @@ :base_type :TextField :visibility_type :normal :fk_target_field_id nil}]} - (tu/with-temp Database [fake-db] - (tu/with-temp Table [fake-table {:name "movie" - :schema "default" - :db_id (:id fake-db)}] - (sync/sync-table! (SyncTestDriver.) fake-table) - (table-details (sel :one Table, :id (:id fake-table)))))) + (tu/with-temp* [Database [fake-db] + Table [fake-table {:name "movie" + :schema "default" + :db_id (:id fake-db)}]] + (sync/sync-table! (SyncTestDriver.) fake-table) + + (table-details (sel :one Table, :id (:id fake-table))))) ;; ## Test that we will remove field-values when they aren't appropriate @@ -209,16 +210,16 @@ (expect [[1 2 3] [1 2 3]] - (tu/with-temp Database [fake-db] - (tu/with-temp Table [fake-table {:db_id (:id fake-db), :name "movie", :schema "default"}] - (sync/sync-table! (SyncTestDriver.) fake-table) - (let [field-id (sel :one :id Field, :table_id (:id fake-table), :name "title")] - (tu/with-temp FieldValues [_ {:field_id field-id - :values "[1,2,3]"}] - (let [initial-field-values (sel :one :field [FieldValues :values], :field_id field-id)] - (sync/sync-table! (SyncTestDriver.) fake-table) - [initial-field-values - (sel :one :field [FieldValues :values], :field_id field-id)])))))) + (tu/with-temp* [Database [fake-db] + Table [fake-table {:db_id (:id fake-db), :name "movie", :schema "default"}]] + (sync/sync-table! (SyncTestDriver.) fake-table) + (let [field-id (sel :one :id Field, :table_id (:id fake-table), :name "title")] + (tu/with-temp FieldValues [_ {:field_id field-id + :values "[1,2,3]"}] + (let [initial-field-values (sel :one :field [FieldValues :values], :field_id field-id)] + (sync/sync-table! (SyncTestDriver.) fake-table) + [initial-field-values + (sel :one :field [FieldValues :values], :field_id field-id)]))))) ;; ## Individual Helper Fns diff --git a/test/metabase/events/activity_feed_test.clj b/test/metabase/events/activity_feed_test.clj index c0b8ba3938a0c770909c6e7b3158111e02b2f030..738fe79a675f191adea4678857b227f299b2f637 100644 --- a/test/metabase/events/activity_feed_test.clj +++ b/test/metabase/events/activity_feed_test.clj @@ -15,7 +15,7 @@ [table :refer [Table]] [user :refer [User]]) [metabase.test.data :refer :all] - [metabase.test.util :refer [expect-eval-actual-first with-temp random-name]] + [metabase.test.util :refer [expect-eval-actual-first random-name]] [metabase.test-setup :refer :all])) diff --git a/test/metabase/events/dependencies_test.clj b/test/metabase/events/dependencies_test.clj index eaafd9cc4aaa88b8658f786796dc36c8dd044f42..c1053966f3e004e242460f6eb46734461dafbe46 100644 --- a/test/metabase/events/dependencies_test.clj +++ b/test/metabase/events/dependencies_test.clj @@ -44,16 +44,14 @@ :dependent_on_id 18} {:dependent_on_model "Segment" :dependent_on_id 35}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [metric {:creator_id (user->id :rasta) - :table_id table-id - :name "Dependencies Test" - :definition {:aggregation ["count"] - :filter ["AND" ["SEGMENT" 18] ["SEGMENT" 35]]}}] - (process-dependencies-event {:topic :metric-create - :item metric}) - (set (db/sel :many :fields [Dependency :dependent_on_model :dependent_on_id], :model "Metric", :model_id (:id metric))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [metric {:table_id table-id + :definition {:aggregation ["count"] + :filter ["AND" ["SEGMENT" 18] ["SEGMENT" 35]]}}]] + (process-dependencies-event {:topic :metric-create + :item metric}) + (set (db/sel :many :fields [Dependency :dependent_on_model :dependent_on_id], :model "Metric", :model_id (:id metric))))) ;; `:card-update` event (expect @@ -61,13 +59,11 @@ :dependent_on_id 18} {:dependent_on_model "Segment" :dependent_on_id 35}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [metric {:creator_id (user->id :rasta) - :table_id table-id - :name "Dependencies Test" - :definition {:aggregation ["count"] - :filter ["AND" ["SEGMENT" 18] ["SEGMENT" 35]]}}] - (process-dependencies-event {:topic :metric-update - :item metric}) - (set (db/sel :many :fields [Dependency :dependent_on_model :dependent_on_id], :model "Metric", :model_id (:id metric))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [metric {:table_id table-id + :definition {:aggregation ["count"] + :filter ["AND" ["SEGMENT" 18] ["SEGMENT" 35]]}}]] + (process-dependencies-event {:topic :metric-update + :item metric}) + (set (db/sel :many :fields [Dependency :dependent_on_model :dependent_on_id], :model "Metric", :model_id (:id metric))))) diff --git a/test/metabase/events/last_login_test.clj b/test/metabase/events/last_login_test.clj index 2b04e179f812a9d227f7890e87ab112557cb2645..9d40e2e86500775cb66dec43fd48252fa766b408 100644 --- a/test/metabase/events/last_login_test.clj +++ b/test/metabase/events/last_login_test.clj @@ -4,7 +4,7 @@ [metabase.events.last-login :refer :all] (metabase.models [user :refer [User]]) [metabase.test.data :refer :all] - [metabase.test.util :refer [expect-eval-actual-first with-temp random-name]] + [metabase.test.util :refer [expect-eval-actual-first random-name]] [metabase.test-setup :refer :all])) diff --git a/test/metabase/events/revision_test.clj b/test/metabase/events/revision_test.clj index dc1ea6ff0c90336da7d3c7232bbb3c48113cfe28..4deba70d6ecbac8654677ae873ec90aebcb30f6a 100644 --- a/test/metabase/events/revision_test.clj +++ b/test/metabase/events/revision_test.clj @@ -14,9 +14,10 @@ [table :refer [Table]]) [metabase.test.data :refer :all] [metabase.test.data.users :refer :all] - [metabase.test.util :refer [expect-eval-actual-first with-temp random-name]] + [metabase.test.util :refer [expect-eval-actual-first random-name]] [metabase.test-setup :refer :all] - [metabase.test.util :as tu])) + [metabase.test.util :as tu] + [metabase.util :as u])) (defn- create-test-card [] (let [rand-name (random-name)] @@ -183,153 +184,129 @@ (expect {:model "Metric" :user_id (user->id :rasta) - :object {:name "ABC" - :description "DEF" + :object {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active true :creator_id (user->id :rasta) :definition {:a "b"}} :is_reversion false :is_creation true :message nil} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Metric [metric {:creator_id (user->id :rasta) - :table_id id - :name "ABC" - :description "DEF" - :definition {:a "b"}}] - (process-revision-event {:topic :metric-create - :item metric}) - (let [revision (db/sel :one :fields [Revision :model :user_id :object :is_reversion :is_creation :message], :model "Metric", :model_id (:id metric))] - (assoc revision :object (dissoc (:object revision) :id :table_id))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}] + Metric [metric {:table_id id, :definition {:a "b"}}]] + (process-revision-event {:topic :metric-create + :item metric}) + + (let [revision (db/sel :one :fields [Revision :model :user_id :object :is_reversion :is_creation :message], :model "Metric", :model_id (:id metric))] + (assoc revision :object (dissoc (:object revision) :id :table_id))))) ;; :metric-update (expect {:model "Metric" :user_id (user->id :crowberto) - :object {:name "ABC" - :description "DEF" + :object {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active true :creator_id (user->id :rasta) :definition {:a "b"}} :is_reversion false :is_creation false :message "updated"} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Metric [metric {:creator_id (user->id :rasta) - :table_id id - :name "ABC" - :description "DEF" - :definition {:a "b"}}] - (process-revision-event {:topic :metric-update - :item (assoc metric - :actor_id (user->id :crowberto) - :revision_message "updated")}) - (let [revision (db/sel :one :fields [Revision :model :user_id :object :is_reversion :is_creation :message], :model "Metric", :model_id (:id metric))] - (assoc revision :object (dissoc (:object revision) :id :table_id))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}] + Metric [metric {:table_id id, :definition {:a "b"}}]] + (process-revision-event {:topic :metric-update + :item (assoc metric + :actor_id (user->id :crowberto) + :revision_message "updated")}) + (let [revision (db/sel :one :fields [Revision :model :user_id :object :is_reversion :is_creation :message], :model "Metric", :model_id (:id metric))] + (assoc revision :object (dissoc (:object revision) :id :table_id))))) ;; :metric-delete (expect {:model "Metric" :user_id (user->id :rasta) - :object {:name "ABC" - :description "DEF" + :object {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active false :creator_id (user->id :rasta) :definition {:a "b"}} :is_reversion false :is_creation false :message nil} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Metric [metric {:creator_id (user->id :rasta) - :table_id id - :name "ABC" - :description "DEF" - :definition {:a "b"} - :is_active false}] - (process-revision-event {:topic :metric-delete - :item metric}) - (let [revision (-> (db/sel :one Revision :model "Metric" :model_id (:id metric)) - (select-keys [:model :user_id :object :is_reversion :is_creation :message]))] - (assoc revision :object (dissoc (:object revision) :id :table_id))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}] + Metric [metric {:table_id id, :definition {:a "b"}, :is_active false}]] + (process-revision-event {:topic :metric-delete + :item metric}) + (let [revision (db/sel :one :fields [Revision :model :user_id :object :is_reversion :is_creation :message], :model "Metric", :model_id (:id metric))] + (assoc revision :object (dissoc (:object revision) :id :table_id))))) ;; :segment-create (expect {:model "Segment" :user_id (user->id :rasta) - :object {:name "ABC" - :description "DEF" + :object {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active true :creator_id (user->id :rasta) :definition {:a "b"}} :is_reversion false :is_creation true :message nil} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Segment [segment {:creator_id (user->id :rasta) - :table_id id - :name "ABC" - :description "DEF" - :definition {:a "b"}}] - (process-revision-event {:topic :segment-create - :item segment}) - (let [revision (-> (db/sel :one Revision :model "Segment" :model_id (:id segment)) - (select-keys [:model :user_id :object :is_reversion :is_creation :message]))] - (assoc revision :object (dissoc (:object revision) :id :table_id))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}] + Segment [segment {:table_id id + :definition {:a "b"}}]] + (process-revision-event {:topic :segment-create + :item segment}) + (let [revision (-> (db/sel :one Revision :model "Segment" :model_id (:id segment)) + (select-keys [:model :user_id :object :is_reversion :is_creation :message]))] + (assoc revision :object (dissoc (:object revision) :id :table_id))))) ;; :segment-update (expect {:model "Segment" :user_id (user->id :crowberto) - :object {:name "ABC" - :description "DEF" + :object {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active true :creator_id (user->id :rasta) :definition {:a "b"}} :is_reversion false :is_creation false :message "updated"} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Segment [segment {:creator_id (user->id :rasta) - :table_id id - :name "ABC" - :description "DEF" - :definition {:a "b"}}] - (process-revision-event {:topic :segment-update - :item (assoc segment - :actor_id (user->id :crowberto) - :revision_message "updated")}) - (let [revision (-> (db/sel :one Revision :model "Segment" :model_id (:id segment)) - (select-keys [:model :user_id :object :is_reversion :is_creation :message]))] - (assoc revision :object (dissoc (:object revision) :id :table_id))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}] + Segment [segment {:table_id id + :definition {:a "b"}}]] + (process-revision-event {:topic :segment-update + :item (assoc segment + :actor_id (user->id :crowberto) + :revision_message "updated")}) + (update (db/sel :one :fields [Revision :model :user_id :object :is_reversion :is_creation :message], :model "Segment", :model_id (:id segment)) + :object (u/rpartial dissoc :id :table_id)))) ;; :segment-delete (expect {:model "Segment" :user_id (user->id :rasta) - :object {:name "ABC" - :description "DEF" + :object {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active false :creator_id (user->id :rasta) :definition {:a "b"}} :is_reversion false :is_creation false :message nil} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Segment [segment {:creator_id (user->id :rasta) - :table_id id - :name "ABC" - :description "DEF" - :definition {:a "b"} - :is_active false}] - (process-revision-event {:topic :segment-delete - :item segment}) - (let [revision (-> (db/sel :one Revision :model "Segment" :model_id (:id segment)) - (select-keys [:model :user_id :object :is_reversion :is_creation :message]))] - (assoc revision :object (dissoc (:object revision) :id :table_id))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}] + Segment [segment {:table_id id + :definition {:a "b"} + :is_active false}]] + (process-revision-event {:topic :segment-delete + :item segment}) + (update (db/sel :one :fields [Revision :model :user_id :object :is_reversion :is_creation :message], :model "Segment", :model_id (:id segment)) + :object (u/rpartial dissoc :id :table_id)))) diff --git a/test/metabase/events/view_log_test.clj b/test/metabase/events/view_log_test.clj index a73564b3a53dac627853694f48333de8da90248e..9899ef6b4985fe0fc66ad0b2ed873d5a3a70e92f 100644 --- a/test/metabase/events/view_log_test.clj +++ b/test/metabase/events/view_log_test.clj @@ -6,7 +6,7 @@ (metabase.models [user :refer [User]] [view-log :refer [ViewLog]]) [metabase.test.data :refer :all] - [metabase.test.util :refer [expect-eval-actual-first with-temp random-name]] + [metabase.test.util :refer [expect-eval-actual-first random-name]] [metabase.test-setup :refer :all])) diff --git a/test/metabase/models/dashboard_card_test.clj b/test/metabase/models/dashboard_card_test.clj index 871740463eb05c53ccab64e8981c475abdcbf531..9e13556f66e794f1462d7758a2b2dc1318ea0bd2 100644 --- a/test/metabase/models/dashboard_card_test.clj +++ b/test/metabase/models/dashboard_card_test.clj @@ -33,11 +33,10 @@ :col nil :row nil :series []} - (tu/with-temp Dashboard [{dashboard-id :id}] - (tu/with-temp Card [{card-id :id}] - (tu/with-temp DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id - :card_id card-id}] - (remove-ids-and-timestamps (retrieve-dashboard-card dashcard-id)))))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Card [{card-id :id}] + DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}]] + (remove-ids-and-timestamps (retrieve-dashboard-card dashcard-id)))) ;; retrieve-dashboard-card ;; dashcard w/ additional series @@ -56,14 +55,14 @@ :display :table :dataset_query {} :visualization_settings {}}]} - (tu/with-temp Dashboard [{dashboard-id :id}] - (tu/with-temp Card [{card-id :id}] - (tu/with-temp Card [{series-id-1 :id} {:name "Additional Series Card 1"}] - (tu/with-temp Card [{series-id-2 :id} {:name "Additional Series Card 2"}] - (tu/with-temp DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-1, :position 0}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-2, :position 1}] - (remove-ids-and-timestamps (retrieve-dashboard-card dashcard-id)))))))))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Card [{card-id :id}] + Card [{series-id-1 :id} {:name "Additional Series Card 1"}] + Card [{series-id-2 :id} {:name "Additional Series Card 2"}] + DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}] + DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-1, :position 0}] + DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-2, :position 1}]] + (remove-ids-and-timestamps (retrieve-dashboard-card dashcard-id)))) ;; update-dashboard-card-series @@ -114,19 +113,19 @@ :display :table :dataset_query {} :visualization_settings {}}]}] - (tu/with-temp Dashboard [{dashboard-id :id}] - (tu/with-temp Card [{card-id :id} {:name "Test Card"}] - (let [dashboard-card (create-dashboard-card {:creator_id (user->id :rasta) - :dashboard_id dashboard-id - :card_id card-id - :sizeX 4 - :sizeY 3 - :row 1 - :col 1 - :series [card-id]})] - ;; first result is return value from function, second is to validate db captured everything - [(remove-ids-and-timestamps dashboard-card) - (remove-ids-and-timestamps (retrieve-dashboard-card (:id dashboard-card)))])))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Card [{card-id :id} {:name "Test Card"}]] + (let [dashboard-card (create-dashboard-card {:creator_id (user->id :rasta) + :dashboard_id dashboard-id + :card_id card-id + :sizeX 4 + :sizeY 3 + :row 1 + :col 1 + :series [card-id]})] + ;; first result is return value from function, second is to validate db captured everything + [(remove-ids-and-timestamps dashboard-card) + (remove-ids-and-timestamps (retrieve-dashboard-card (:id dashboard-card)))]))) ;; update-dashboard-card ;; basic update. we are testing multiple things here @@ -168,22 +167,22 @@ :display :table :dataset_query {} :visualization_settings {}}]}] - (tu/with-temp Dashboard [{dashboard-id :id}] - (tu/with-temp Card [{card-id :id}] - (tu/with-temp DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}] - (tu/with-temp Card [{card-id-1 :id} {:name "Test Card 1"}] - (tu/with-temp Card [{card-id-2 :id} {:name "Test Card 2"}] - ;; first result is the unmodified dashcard - ;; second is the return value from the update call - ;; third is to validate db captured everything - [(remove-ids-and-timestamps (retrieve-dashboard-card dashcard-id)) - (remove-ids-and-timestamps (update-dashboard-card {:id dashcard-id - :actor_id (user->id :rasta) - :dashboard_id nil - :card_id nil - :sizeX 4 - :sizeY 3 - :row 1 - :col 1 - :series [card-id-2 card-id-1]})) - (remove-ids-and-timestamps (retrieve-dashboard-card dashcard-id))])))))) + (tu/with-temp* [Dashboard [{dashboard-id :id}] + Card [{card-id :id}] + DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}] + Card [{card-id-1 :id} {:name "Test Card 1"}] + Card [{card-id-2 :id} {:name "Test Card 2"}]] + ;; first result is the unmodified dashcard + ;; second is the return value from the update call + ;; third is to validate db captured everything + [(remove-ids-and-timestamps (retrieve-dashboard-card dashcard-id)) + (remove-ids-and-timestamps (update-dashboard-card {:id dashcard-id + :actor_id (user->id :rasta) + :dashboard_id nil + :card_id nil + :sizeX 4 + :sizeY 3 + :row 1 + :col 1 + :series [card-id-2 card-id-1]})) + (remove-ids-and-timestamps (retrieve-dashboard-card dashcard-id))])) diff --git a/test/metabase/models/dashboard_test.clj b/test/metabase/models/dashboard_test.clj index e4e34504bbcad629c65c9391f019993fa79aae96..96743d0cfc31d6eb82ee6394c723bf263783adb1 100644 --- a/test/metabase/models/dashboard_test.clj +++ b/test/metabase/models/dashboard_test.clj @@ -24,18 +24,18 @@ :id true :card_id true :series true}]} - (tu/with-temp Dashboard [{dashboard-id :id :as dashboard} {:name "Test Dashboard"}] - (tu/with-temp Card [{card-id :id}] - (tu/with-temp Card [{series-id-1 :id}] - (tu/with-temp Card [{series-id-2 :id}] - (tu/with-temp DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-1, :position 0}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-2, :position 1}] - (update (serialize-dashboard dashboard) :cards (fn [[{:keys [id card_id series], :as card}]] - [(assoc card - :id (= dashcard-id id) - :card_id (= card-id card_id) - :series (= [series-id-1 series-id-2] series))])))))))))) + (tu/with-temp* [Dashboard [{dashboard-id :id :as dashboard} {:name "Test Dashboard"}] + Card [{card-id :id}] + Card [{series-id-1 :id}] + Card [{series-id-2 :id}] + DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}] + DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-1, :position 0}] + DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-2, :position 1}]] + (update (serialize-dashboard dashboard) :cards (fn [[{:keys [id card_id series], :as card}]] + [(assoc card + :id (= dashcard-id id) + :card_id (= card-id card_id) + :series (= [series-id-1 series-id-2] series))])))) ;; diff-dashboards-str @@ -134,29 +134,29 @@ :id false :card_id true :series true}]}] - (tu/with-temp Dashboard [{dashboard-id :id, :as dashboard} {:name "Test Dashboard"}] - (tu/with-temp Card [{card-id :id}] - (tu/with-temp Card [{series-id-1 :id}] - (tu/with-temp Card [{series-id-2 :id}] - (tu/with-temp DashboardCard [{dashcard-id :id :as dashboard-card} {:dashboard_id dashboard-id, :card_id card-id}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-1, :position 0}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-2, :position 1}] - (let [check-ids (fn [[{:keys [id card_id series] :as card}]] - [(assoc card - :id (= dashcard-id id) - :card_id (= card-id card_id) - :series (= [series-id-1 series-id-2] series))]) - serialized-dashboard (serialize-dashboard dashboard)] - ;; delete the dashcard and modify the dash attributes - (dashboard-card/delete-dashboard-card dashboard-card (user->id :rasta)) - (db/upd Dashboard dashboard-id - :name "Revert Test" - :description "something") - ;; capture our updated dashboard state - (let [serialized-dashboard2 (serialize-dashboard (Dashboard dashboard-id))] - ;; now do the reversion - (revert-dashboard dashboard-id (user->id :crowberto) serialized-dashboard) - ;; final output is original-state, updated-state, reverted-state - [(update serialized-dashboard :cards check-ids) - serialized-dashboard2 - (update (serialize-dashboard (Dashboard dashboard-id)) :cards check-ids)])))))))))) + (tu/with-temp* [Dashboard [{dashboard-id :id, :as dashboard} {:name "Test Dashboard"}] + Card [{card-id :id}] + Card [{series-id-1 :id}] + Card [{series-id-2 :id}] + DashboardCard [{dashcard-id :id :as dashboard-card} {:dashboard_id dashboard-id, :card_id card-id}] + DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-1, :position 0}] + DashboardCardSeries [_ {:dashboardcard_id dashcard-id, :card_id series-id-2, :position 1}]] + (let [check-ids (fn [[{:keys [id card_id series] :as card}]] + [(assoc card + :id (= dashcard-id id) + :card_id (= card-id card_id) + :series (= [series-id-1 series-id-2] series))]) + serialized-dashboard (serialize-dashboard dashboard)] + ;; delete the dashcard and modify the dash attributes + (dashboard-card/delete-dashboard-card dashboard-card (user->id :rasta)) + (db/upd Dashboard dashboard-id + :name "Revert Test" + :description "something") + ;; capture our updated dashboard state + (let [serialized-dashboard2 (serialize-dashboard (Dashboard dashboard-id))] + ;; now do the reversion + (revert-dashboard dashboard-id (user->id :crowberto) serialized-dashboard) + ;; final output is original-state, updated-state, reverted-state + [(update serialized-dashboard :cards check-ids) + serialized-dashboard2 + (update (serialize-dashboard (Dashboard dashboard-id)) :cards check-ids)])))) diff --git a/test/metabase/models/dependency_test.clj b/test/metabase/models/dependency_test.clj index 9ff0db3374289ae54c83b056293fc47fecd4f106..1d9d08857c791a79b9dc728452d5c2e73f4a78d5 100644 --- a/test/metabase/models/dependency_test.clj +++ b/test/metabase/models/dependency_test.clj @@ -46,17 +46,17 @@ :model_id 4 :dependent_on_model "foobar" :dependent_on_id 13}} - (tu/with-temp Dependency [_ {:model "Mock" - :model_id 4 - :dependent_on_model "test" - :dependent_on_id 1 - :created_at (u/new-sql-timestamp)}] - (tu/with-temp Dependency [_ {:model "Mock" + (tu/with-temp* [Dependency [_ {:model "Mock" + :model_id 4 + :dependent_on_model "test" + :dependent_on_id 1 + :created_at (u/new-sql-timestamp)}] + Dependency [_ {:model "Mock" :model_id 4 :dependent_on_model "foobar" :dependent_on_id 13 - :created_at (u/new-sql-timestamp)}] - (format-dependencies (retrieve-dependencies Mock 4))))) + :created_at (u/new-sql-timestamp)}]] + (format-dependencies (retrieve-dependencies Mock 4)))) ;; update-dependencies diff --git a/test/metabase/models/field_values_test.clj b/test/metabase/models/field_values_test.clj index da8acd388df23e0ea527118528d510692cdcdd47..8a173fab525fe79d0aea6ff28ba9ad53e4a91ca5 100644 --- a/test/metabase/models/field_values_test.clj +++ b/test/metabase/models/field_values_test.clj @@ -47,11 +47,10 @@ [[1,2,3] {:status 204, :body nil} nil] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Field [{field-id :id} {:table_id table-id}] - (tu/with-temp FieldValues [_ {:field_id field-id - :values "[1,2,3]"}] - [(db/sel :one :field [FieldValues :values] :field_id field-id) - (clear-field-values field-id) - (db/sel :one :field [FieldValues :values] :field_id field-id)]))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Field [{field-id :id} {:table_id table-id}] + FieldValues [_ {:field_id field-id, :values "[1,2,3]"}]] + [(db/sel :one :field [FieldValues :values] :field_id field-id) + (clear-field-values field-id) + (db/sel :one :field [FieldValues :values] :field_id field-id)])) diff --git a/test/metabase/models/metric_test.clj b/test/metabase/models/metric_test.clj index 2d70cf84eca1a980354cff70ed30550ce698e8a7..23422e1f81f26ec68180b7f50beab5c8709508d5 100644 --- a/test/metabase/models/metric_test.clj +++ b/test/metabase/models/metric_test.clj @@ -1,37 +1,33 @@ (ns metabase.models.metric-test - (:require [clojure.tools.macro :refer [symbol-macrolet]] - [expectations :refer :all] + (:require [expectations :refer :all] (metabase.models [database :refer [Database]] [hydrate :refer :all] [metric :refer :all] [table :refer [Table]]) [metabase.test.data :refer :all] [metabase.test.data.users :refer :all] - [metabase.test.util :as tu])) + [metabase.test.util :as tu] + [metabase.util :as u])) -(defn user-details +(defn- user-details [username] - (-> (fetch-user username) - (dissoc :date_joined :last_login))) + (dissoc (fetch-user username) :date_joined :last_login)) -(defn metric-details +(defn- metric-details [{:keys [creator] :as metric}] - (-> metric - (dissoc :id :table_id :created_at :updated_at) - (assoc :creator (dissoc creator :date_joined :last_login)))) + (-> (dissoc metric :id :table_id :created_at :updated_at) + (update :creator (u/rpartial dissoc :date_joined :last_login)))) -(defn create-metric-then-select +(defn- create-metric-then-select! [table name description creator definition] - (-> (create-metric table name description creator definition) - metric-details)) + (metric-details (create-metric! table name description creator definition))) -(defn update-metric-then-select +(defn- update-metric-then-select! [metric] - (-> (update-metric metric (user->id :crowberto)) - metric-details)) + (metric-details (update-metric! metric (user->id :crowberto)))) -;; create-metric +;; create-metric! (expect {:creator_id (user->id :rasta) :creator (user-details :rasta) @@ -39,48 +35,41 @@ :description nil :is_active true :definition {:clause ["a" "b"]}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (create-metric-then-select id "I only want *these* things" nil (user->id :rasta) {:clause ["a" "b"]})))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}]] + (create-metric-then-select! id "I only want *these* things" nil (user->id :rasta) {:clause ["a" "b"]}))) ;; exists-metric? (expect [true false] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Metric [{:keys [id]} {:creator_id (user->id :rasta) - :table_id id - :name "Ivory Tower" - :description "All the glorious things..." - :definition {:database 45 - :query {:filter ["yay"]}}}] - [(exists-metric? id) - (exists-metric? 34)])))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{metric-id :id} {:table_id table-id + :definition {:database 45 + :query {:filter ["yay"]}}}]] + [(exists-metric? metric-id) + (exists-metric? 34)])) ;; retrieve-metric (expect {:creator_id (user->id :rasta) :creator (user-details :rasta) - :name "Ivory Tower" - :description "All the glorious things..." + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active true :definition {:database 45 :query {:filter ["yay"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Metric [{:keys [id]} {:creator_id (user->id :rasta) - :table_id id - :name "Ivory Tower" - :description "All the glorious things..." - :definition {:database 45 - :query {:filter ["yay"]}}}] - (let [{:keys [creator] :as metric} (retrieve-metric id)] - (-> metric - (dissoc :id :table_id :created_at :updated_at) - (assoc :creator (dissoc creator :date_joined :last_login)))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{metric-id :id} {:table_id table-id + :definition {:database 45 + :query {:filter ["yay"]}}}]] + (let [{:keys [creator] :as metric} (retrieve-metric metric-id)] + (update (dissoc metric :id :table_id :created_at :updated_at) + :creator (u/rpartial dissoc :date_joined :last_login))))) ;; retrieve-segements @@ -91,32 +80,19 @@ :description nil :is_active true :definition {}}] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id1 :id} {:db_id database-id}] - (tu/with-temp Table [{table-id2 :id} {:db_id database-id}] - (tu/with-temp Metric [{segement-id1 :id} {:creator_id (user->id :rasta) - :table_id table-id1 - :name "Metric 1" - :definition {}}] - (tu/with-temp Metric [{metric-id2 :id} {:creator_id (user->id :rasta) - :table_id table-id2 - :name "Metric 2" - :definition {}}] - (tu/with-temp Metric [{metric-id3 :id} {:creator_id (user->id :rasta) - :table_id table-id1 - :name "Metric 3" - :is_active false - :definition {}}] - (let [metrics (retrieve-metrics table-id1)] - (assert (= 1 (count metrics))) - (->> metrics - (mapv #(into {} %)) ; expectations doesn't compare our record type properly - (mapv #(dissoc % :id :table_id :created_at :updated_at)) - (mapv (fn [{:keys [creator] :as metric}] - (assoc metric :creator (dissoc creator :date_joined :last_login))))))))))))) - - -;; update-metric + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id-1 :id} {:db_id database-id}] + Table [{table-id-2 :id} {:db_id database-id}] + Metric [{segement-id-1 :id} {:table_id table-id-1, :name "Metric 1", :description nil}] + Metric [{metric-id-2 :id} {:table_id table-id-2}] + Metric [{metric-id3 :id} {:table_id table-id-1, :is_active false}]] + (doall (for [metric (u/prog1 (retrieve-metrics table-id-1) + (assert (= 1 (count <>))))] + (update (dissoc (into {} metric) :id :table_id :created_at :updated_at) + :creator (u/rpartial dissoc :date_joined :last_login)))))) + + +;; update-metric! ;; basic update. we are testing several things here ;; 1. ability to update the Metric name ;; 2. creator_id cannot be changed @@ -131,23 +107,19 @@ :is_active true :definition {:database 2 :query {:filter ["not" "the toucans you're looking for"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Metric [{:keys [id]} {:creator_id (user->id :rasta) - :table_id id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {}}] - (update-metric-then-select {:id id - :name "Costa Rica" - :description nil - :creator_id (user->id :crowberto) - :table_id 456 - :definition {:database 2 - :query {:filter ["not" "the toucans you're looking for"]}} - :revision_message "Just horsing around"}))))) - -;; delete-metric + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{metric-id :id} {:table_id table-id}]] + (update-metric-then-select! {:id metric-id + :name "Costa Rica" + :description nil + :creator_id (user->id :crowberto) + :table_id 456 + :definition {:database 2 + :query {:filter ["not" "the toucans you're looking for"]}} + :revision_message "Just horsing around"}))) + +;; delete-metric! (expect {:creator_id (user->id :rasta) :creator (user-details :rasta) @@ -155,15 +127,11 @@ :description "Lookin' for a blueberry" :is_active false :definition {}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Metric [{:keys [id]} {:creator_id (user->id :rasta) - :table_id id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {}}] - (delete-metric id (user->id :crowberto) "revision message") - (metric-details (retrieve-metric id)))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [{metric-id :id} {:table_id table-id}]] + (delete-metric! metric-id (user->id :crowberto) "revision message") + (metric-details (retrieve-metric metric-id)))) ;; ## Metric Revisions @@ -180,17 +148,14 @@ :definition {:aggregation ["count"] :filter ["AND" [">" 4 "2014-10-19"]]} :is_active true} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Metric [metric {:creator_id (user->id :rasta) - :table_id table-id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {:aggregation ["count"] - :filter ["AND" [">" 4 "2014-10-19"]]}}] - (-> (serialize-metric Metric (:id metric) metric) - (update :id boolean) - (update :table_id boolean)))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [metric {:table_id table-id + :definition {:aggregation ["count"] + :filter ["AND" [">" 4 "2014-10-19"]]}}]] + (-> (serialize-metric Metric (:id metric) metric) + (update :id boolean) + (update :table_id boolean)))) ;; diff-metrics @@ -201,16 +166,14 @@ :after "BBB"} :name {:before "Toucans in the rainforest" :after "Something else"}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Metric [metric {:creator_id (user->id :rasta) - :table_id id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {:filter ["AND" [">" 4 "2014-10-19"]]}}] - (diff-metrics Metric metric (assoc metric :name "Something else" - :description "BBB" - :definition {:filter ["AND" ["BETWEEN" 4 "2014-07-01" "2014-10-19"]]})))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Metric [metric {:table_id table-id + :definition {:filter ["AND" [">" 4 "2014-10-19"]]}}]] + (diff-metrics Metric metric (assoc metric + :name "Something else" + :description "BBB" + :definition {:filter ["AND" ["BETWEEN" 4 "2014-07-01" "2014-10-19"]]})))) ;; test case where definition doesn't change (expect diff --git a/test/metabase/models/pulse_channel_test.clj b/test/metabase/models/pulse_channel_test.clj index c783af3d2096139014ca9ee582e15dceaba9a6a1..0d628c7f6632af95c0520ee703821b0a669c4b4a 100644 --- a/test/metabase/models/pulse_channel_test.clj +++ b/test/metabase/models/pulse_channel_test.clj @@ -128,8 +128,7 @@ :recipients [(user-details :crowberto) {:email "foo@bar.com"} (user-details :rasta)]} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] + (tu/with-temp Pulse [{:keys [id]}] (create-channel-then-select! {:pulse_id id :channel_type :email :schedule_type :daily @@ -144,8 +143,7 @@ :schedule_frame nil :recipients [] :details {:something "random"}} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] + (tu/with-temp Pulse [{:keys [id]}] (create-channel-then-select! {:pulse_id id :channel_type :slack :schedule_type :hourly @@ -162,18 +160,13 @@ :schedule_day nil :schedule_frame nil :recipients [{:email "foo@bar.com"}]} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp PulseChannel [{channel-id :id :as channel} {:pulse_id id - :channel_type :email - :details {} - :schedule_type :daily - :schedule_hour 15}] - (update-channel-then-select! {:id channel-id - :channel_type :email - :schedule_type :daily - :schedule_hour 18 - :recipients [{:email "foo@bar.com"}]})))) + (tu/with-temp* [Pulse [{pulse-id :id}] + PulseChannel [{channel-id :id, :as channel} {:pulse_id pulse-id}]] + (update-channel-then-select! {:id channel-id + :channel_type :email + :schedule_type :daily + :schedule_hour 18 + :recipients [{:email "foo@bar.com"}]}))) ;; monthly schedules require a schedule_frame and can optionally omit they schedule_day (expect @@ -183,20 +176,15 @@ :schedule_day nil :schedule_frame :mid :recipients [{:email "foo@bar.com"} (user-details :rasta)]} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp PulseChannel [{channel-id :id :as channel} {:pulse_id id - :channel_type :email - :details {} - :schedule_type :daily - :schedule_hour 15}] - (update-channel-then-select! {:id channel-id - :channel_type :email - :schedule_type :monthly - :schedule_hour 8 - :schedule_day nil - :schedule_frame :mid - :recipients [{:email "foo@bar.com"} {:id (user->id :rasta)}]})))) + (tu/with-temp* [Pulse [{pulse-id :id}] + PulseChannel [{channel-id :id :as channel} {:pulse_id pulse-id}]] + (update-channel-then-select! {:id channel-id + :channel_type :email + :schedule_type :monthly + :schedule_hour 8 + :schedule_day nil + :schedule_frame :mid + :recipients [{:email "foo@bar.com"} {:id (user->id :rasta)}]}))) ;; weekly schedule should have a day in it, show that we can get full users (expect @@ -206,19 +194,14 @@ :schedule_day "mon" :schedule_frame nil :recipients [{:email "foo@bar.com"} (user-details :rasta)]} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp PulseChannel [{channel-id :id :as channel} {:pulse_id id - :channel_type :email - :details {} - :schedule_type :daily - :schedule_hour 15}] - (update-channel-then-select! {:id channel-id - :channel_type :email - :schedule_type :weekly - :schedule_hour 8 - :schedule_day "mon" - :recipients [{:email "foo@bar.com"} {:id (user->id :rasta)}]})))) + (tu/with-temp* [Pulse [{pulse-id :id}] + PulseChannel [{channel-id :id} {:pulse_id pulse-id}]] + (update-channel-then-select! {:id channel-id + :channel_type :email + :schedule_type :weekly + :schedule_hour 8 + :schedule_day "mon" + :recipients [{:email "foo@bar.com"} {:id (user->id :rasta)}]}))) ;; hourly schedules don't require day/hour settings (should be nil), fully change recipients (expect @@ -228,20 +211,15 @@ :schedule_day nil :schedule_frame nil :recipients [(user-details :crowberto)]} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp PulseChannel [{channel-id :id :as channel} {:pulse_id id - :channel_type :email - :details {:emails ["foo@bar.com"]} - :schedule_type :daily - :schedule_hour 15}] - (update-recipients! channel-id [(user->id :rasta)]) - (update-channel-then-select! {:id channel-id - :channel_type :email - :schedule_type :hourly - :schedule_hour 12 - :schedule_day "tue" - :recipients [{:id (user->id :crowberto)}]})))) + (tu/with-temp* [Pulse [{pulse-id :id}] + PulseChannel [{channel-id :id} {:pulse_id pulse-id, :details {:emails ["foo@bar.com"]}}]] + (update-recipients! channel-id [(user->id :rasta)]) + (update-channel-then-select! {:id channel-id + :channel_type :email + :schedule_type :hourly + :schedule_hour 12 + :schedule_day "tue" + :recipients [{:id (user->id :crowberto)}]}))) ;; custom details for channels that need it (expect @@ -252,20 +230,15 @@ :schedule_frame nil :recipients [{:email "foo@bar.com"} {:email "blah@bar.com"}] :details {:channel "#metabaserocks"}} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp PulseChannel [{channel-id :id :as channel} {:pulse_id id - :channel_type :email - :details {} - :schedule_type :daily - :schedule_hour 15}] - (update-channel-then-select! {:id channel-id - :channel_type :email - :schedule_type :daily - :schedule_hour 12 - :schedule_day "tue" - :recipients [{:email "foo@bar.com"} {:email "blah@bar.com"}] - :details {:channel "#metabaserocks"}})))) + (tu/with-temp* [Pulse [{pulse-id :id}] + PulseChannel [{channel-id :id} {:pulse_id pulse-id}]] + (update-channel-then-select! {:id channel-id + :channel_type :email + :schedule_type :daily + :schedule_hour 12 + :schedule_day "tue" + :recipients [{:email "foo@bar.com"} {:email "blah@bar.com"}] + :details {:channel "#metabaserocks"}}))) ;; update-recipients! (expect @@ -274,21 +247,16 @@ [(user->id :crowberto)] [(user->id :crowberto) (user->id :rasta)] [(user->id :rasta) (user->id :trashbird)]] - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp PulseChannel [{channel-id :id} {:pulse_id id - :channel_type :email - :details {} - :schedule_type :daily}] - (let [upd-recipients (fn [recipients] - (update-recipients! channel-id recipients) - (->> (db/sel :many PulseChannelRecipient :pulse_channel_id channel-id) - (mapv :user_id)))] - [(upd-recipients []) - (upd-recipients [(user->id :rasta)]) - (upd-recipients [(user->id :crowberto)]) - (upd-recipients [(user->id :crowberto) (user->id :rasta)]) - (upd-recipients [(user->id :rasta) (user->id :trashbird)])])))) + (tu/with-temp* [Pulse [{pulse-id :id}] + PulseChannel [{channel-id :id} {:pulse_id pulse-id}]] + (let [upd-recipients! (fn [recipients] + (update-recipients! channel-id recipients) + (db/sel :many :field [PulseChannelRecipient :user_id] :pulse_channel_id channel-id))] + [(upd-recipients! []) + (upd-recipients! [(user->id :rasta)]) + (upd-recipients! [(user->id :crowberto)]) + (upd-recipients! [(user->id :crowberto) (user->id :rasta)]) + (upd-recipients! [(user->id :rasta) (user->id :trashbird)])]))) ;; retrieve-scheduled-channels @@ -300,25 +268,16 @@ {:schedule_type "hourly", :channel_type "slack"}] [{:schedule_type "daily", :channel_type "email"} {:schedule_type "hourly", :channel_type "slack"}]] - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp PulseChannel [_ {:pulse_id id - :channel_type :email - :details {} - :schedule_type :daily - :schedule_hour 15}] - (tu/with-temp PulseChannel [_ {:pulse_id id - :channel_type :slack - :details {} - :schedule_type :hourly - :schedule_hour nil}] - (let [retrieve-channels (fn [hour day] - (->> (retrieve-scheduled-channels hour day :other :other) - (mapv #(dissoc % :id :pulse_id))))] - [(retrieve-channels nil nil) - (retrieve-channels 12 nil) - (retrieve-channels 15 nil) - (retrieve-channels 15 "wed")]))))) + (tu/with-temp* [Pulse [{pulse-id :id}] + PulseChannel [_ {:pulse_id pulse-id}] + PulseChannel [_ {:pulse_id pulse-id, :channel_type :slack, :schedule_type :hourly}]] + (let [retrieve-channels (fn [hour day] + (for [channel (retrieve-scheduled-channels hour day :other :other)] + (dissoc channel :id :pulse_id)))] + [(retrieve-channels nil nil) + (retrieve-channels 12 nil) + (retrieve-channels 15 nil) + (retrieve-channels 15 "wed")]))) ;; more complex scenario with 2 Pulses, including weekly scheduling (expect @@ -329,39 +288,19 @@ {:schedule_type "hourly", :channel_type "slack"}] [{:schedule_type "hourly", :channel_type "slack"} {:schedule_type "weekly", :channel_type "email"}]] - (tu/with-temp Pulse [{pulse1 :id} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp Pulse [{pulse2 :id} {:creator_id (user->id :crowberto) - :name (tu/random-name)}] - (tu/with-temp PulseChannel [_ {:pulse_id pulse1 - :channel_type :email - :details {} - :schedule_type :daily - :schedule_hour 15}] - (tu/with-temp PulseChannel [_ {:pulse_id pulse1 - :channel_type :slack - :details {} - :schedule_type :hourly - :schedule_hour nil}] - (tu/with-temp PulseChannel [_ {:pulse_id pulse2 - :channel_type :slack - :details {} - :schedule_type :daily - :schedule_hour 10 - :schedule_day "wed"}] - (tu/with-temp PulseChannel [_ {:pulse_id pulse2 - :channel_type :email - :details {} - :schedule_type :weekly - :schedule_hour 8 - :schedule_day "mon"}] - (let [retrieve-channels (fn [hour day] - (->> (retrieve-scheduled-channels hour day :other :other) - (mapv #(dissoc % :id :pulse_id))))] - [(retrieve-channels nil nil) - (retrieve-channels 10 nil) - (retrieve-channels 15 nil) - (retrieve-channels 8 "mon")])))))))) + (tu/with-temp* [Pulse [{pulse-1-id :id}] + Pulse [{pulse-2-id :id}] + PulseChannel [_ {:pulse_id pulse-1-id, :channel_type :email, :schedule_type :daily}] + PulseChannel [_ {:pulse_id pulse-1-id, :channel_type :slack, :schedule_type :hourly}] + PulseChannel [_ {:pulse_id pulse-2-id, :channel_type :slack, :schedule_type :daily :schedule_hour 10, :schedule_day "wed"}] + PulseChannel [_ {:pulse_id pulse-2-id, :channel_type :email, :schedule_type :weekly, :schedule_hour 8, :schedule_day "mon"}]] + (let [retrieve-channels (fn [hour day] + (for [channel (retrieve-scheduled-channels hour day :other :other)] + (dissoc channel :id :pulse_id)))] + [(retrieve-channels nil nil) + (retrieve-channels 10 nil) + (retrieve-channels 15 nil) + (retrieve-channels 8 "mon")]))) ;; specific test for various monthly scheduling permutations (expect @@ -372,50 +311,24 @@ [] [{:schedule_type "monthly", :channel_type "slack"}] [{:schedule_type "monthly", :channel_type "email"}]] - (tu/with-temp Pulse [{pulse1 :id} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp Pulse [{pulse2 :id} {:creator_id (user->id :crowberto) - :name (tu/random-name)}] - (tu/with-temp PulseChannel [_ {:pulse_id pulse1 - :channel_type :email - :details {} - :schedule_type :monthly - :schedule_hour 12 - :schedule_day nil - :schedule_frame :first}] - (tu/with-temp PulseChannel [_ {:pulse_id pulse1 - :channel_type :slack - :details {} - :schedule_type :monthly - :schedule_hour 12 - :schedule_day "mon" - :schedule_frame :first}] - (tu/with-temp PulseChannel [_ {:pulse_id pulse2 - :channel_type :slack - :details {} - :schedule_type :monthly - :schedule_hour 16 - :schedule_day nil - :schedule_frame :mid}] - (tu/with-temp PulseChannel [_ {:pulse_id pulse2 - :channel_type :email - :details {} - :schedule_type :monthly - :schedule_hour 8 - :schedule_day "fri" - :schedule_frame :last}] - (let [retrieve-channels (fn [hour weekday monthday monthweek] - (->> (retrieve-scheduled-channels hour weekday monthday monthweek) - (mapv #(dissoc % :id :pulse_id))))] - ;; simple starter which should be empty - [(retrieve-channels nil nil :other :other) - ;; this should capture BOTH first absolute day of month + first monday of month schedules - (retrieve-channels 12 "mon" :first :first) - ;; this should only capture the first monday of the month - (retrieve-channels 12 "mon" :other :first) - ;; this makes sure hour checking is being enforced - (retrieve-channels 8 "mon" :first :first) - ;; middle of the month - (retrieve-channels 16 "fri" :mid :other) - ;; last friday of the month (but not the last day of month) - (retrieve-channels 8 "fri" :other :last)])))))))) + (tu/with-temp* [Pulse [{pulse-1-id :id}] + Pulse [{pulse-2-id :id}] + PulseChannel [_ {:pulse_id pulse-1-id, :channel_type :email, :schedule_type :monthly, :schedule_hour 12, :schedule_frame :first}] + PulseChannel [_ {:pulse_id pulse-1-id, :channel_type :slack, :schedule_type :monthly, :schedule_hour 12, :schedule_day "mon", :schedule_frame :first}] + PulseChannel [_ {:pulse_id pulse-2-id, :channel_type :slack, :schedule_type :monthly, :schedule_hour 16, :schedule_frame :mid}] + PulseChannel [_ {:pulse_id pulse-2-id, :channel_type :email, :schedule_type :monthly, :schedule_hour 8, :schedule_day "fri", :schedule_frame :last}]] + (let [retrieve-channels (fn [hour weekday monthday monthweek] + (for [channel (retrieve-scheduled-channels hour weekday monthday monthweek)] + (dissoc channel :id :pulse_id)))] + ;; simple starter which should be empty + [(retrieve-channels nil nil :other :other) + ;; this should capture BOTH first absolute day of month + first monday of month schedules + (retrieve-channels 12 "mon" :first :first) + ;; this should only capture the first monday of the month + (retrieve-channels 12 "mon" :other :first) + ;; this makes sure hour checking is being enforced + (retrieve-channels 8 "mon" :first :first) + ;; middle of the month + (retrieve-channels 16 "fri" :mid :other) + ;; last friday of the month (but not the last day of month) + (retrieve-channels 8 "fri" :other :last)]))) diff --git a/test/metabase/models/pulse_test.clj b/test/metabase/models/pulse_test.clj index 5ac1d399ec50ed000b8e133e6d7ad48148feb3cf..dc16406178dd0a688c5e2409d332c368023f0fd4 100644 --- a/test/metabase/models/pulse_test.clj +++ b/test/metabase/models/pulse_test.clj @@ -1,6 +1,7 @@ (ns metabase.models.pulse-test (:require [clojure.tools.macro :refer [symbol-macrolet]] [expectations :refer :all] + [medley.core :as m] [metabase.db :as db] (metabase.models [card :refer [Card]] [hydrate :refer :all] @@ -11,12 +12,11 @@ [metabase.test.data :refer :all] [metabase.test.data.users :refer :all] [metabase.test.util :as tu] - [medley.core :as m])) + [metabase.util :as u])) -(defn user-details +(defn- user-details [username] - (-> (fetch-user username) - (dissoc :date_joined :last_login))) + (dissoc (fetch-user username) :date_joined :last_login)) ;; create a channel then select its details (defn create-pulse-then-select @@ -58,26 +58,20 @@ :schedule_day nil, :recipients [{:email "foo@bar.com"} (dissoc (user-details :rasta) :is_superuser :is_qbnewb)]}]} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name "Lodi Dodi"}] - (tu/with-temp PulseChannel [{channel-id :id :as channel} {:pulse_id id - :channel_type :email - :details {:other "stuff" - :emails ["foo@bar.com"]} - :schedule_type :daily - :schedule_hour 15}] - (tu/with-temp Card [{card-id :id} {:name "Test Card"}] - (do - (db/ins PulseCard :pulse_id id :card_id card-id :position 0) - (db/ins PulseChannelRecipient :pulse_channel_id channel-id :user_id (user->id :rasta)) - (let [{:keys [cards channels creator] :as pulse} (retrieve-pulse id)] - (-> pulse - (dissoc :id :pulse_id :created_at :updated_at) - (assoc :creator (dissoc creator :date_joined :last_login)) - (assoc :cards (mapv #(dissoc % :id) cards)) - (assoc :channels (for [channel channels] - (-> (dissoc channel :id :pulse_id :created_at :updated_at) - (m/dissoc-in [:details :emails]))))))))))) + (tu/with-temp* [Pulse [{pulse-id :id} {:name "Lodi Dodi"}] + PulseChannel [{channel-id :id :as channel} {:pulse_id pulse-id + :details {:other "stuff" + :emails ["foo@bar.com"]}}] + Card [{card-id :id} {:name "Test Card"}]] + (db/ins PulseCard, :pulse_id pulse-id, :card_id card-id, :position 0) + (db/ins PulseChannelRecipient, :pulse_channel_id channel-id, :user_id (user->id :rasta)) + (-> (dissoc (retrieve-pulse pulse-id) :id :pulse_id :created_at :updated_at) + (update :creator (u/rpartial dissoc :date_joined :last_login)) + (update :cards (fn [cards] (for [card cards] + (dissoc card :id)))) + (update :channels (fn [channels] (for [channel channels] + (-> (dissoc channel :id :pulse_id :created_at :updated_at) + (m/dissoc-in [:details :emails])))))))) ;; update-pulse-cards @@ -87,21 +81,19 @@ ["card2"] ["card2" "card1"] ["card1" "card3"]] - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (tu/with-temp Card [{card-id-1 :id} {:name "card1"}] - (tu/with-temp Card [{card-id-2 :id} {:name "card2"}] - (tu/with-temp Card [{card-id-3 :id} {:name "card3"}] - (let [upd-cards (fn [cards] - (update-pulse-cards {:id id} cards) - (->> (db/sel :many PulseCard :pulse_id id) - (mapv (fn [{:keys [card_id]}] - (db/sel :one :field [Card :name] :id card_id)))))] - [(upd-cards []) - (upd-cards [card-id-1]) - (upd-cards [card-id-2]) - (upd-cards [card-id-2 card-id-1]) - (upd-cards [card-id-1 card-id-3])])))))) + (tu/with-temp* [Pulse [{pulse-id :id}] + Card [{card-id-1 :id} {:name "card1"}] + Card [{card-id-2 :id} {:name "card2"}] + Card [{card-id-3 :id} {:name "card3"}]] + (let [upd-cards! (fn [cards] + (update-pulse-cards {:id pulse-id} cards) + (doall (for [card-id (db/sel :many :field [PulseCard :card_id] :pulse_id pulse-id)] + (db/sel :one :field [Card :name] :id card-id))))] + [(upd-cards! []) + (upd-cards! [card-id-1]) + (upd-cards! [card-id-2]) + (upd-cards! [card-id-2 card-id-1]) + (upd-cards! [card-id-1 card-id-3])]))) ;; update-pulse-channels (expect @@ -112,17 +104,15 @@ :schedule_frame nil :recipients [{:email "foo@bar.com"} (dissoc (user-details :rasta) :is_superuser :is_qbnewb)]} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name (tu/random-name)}] - (do - (update-pulse-channels {:id id} [{:channel_type :email - :schedule_type :daily - :schedule_hour 4 - :recipients [{:email "foo@bar.com"} {:id (user->id :rasta)}]}]) - (-> (db/sel :one PulseChannel :pulse_id id) - (hydrate :recipients) - (dissoc :id :pulse_id :created_at :updated_at) - (m/dissoc-in [:details :emails]))))) + (tu/with-temp Pulse [{:keys [id]}] + (update-pulse-channels {:id id} [{:channel_type :email + :schedule_type :daily + :schedule_hour 4 + :recipients [{:email "foo@bar.com"} {:id (user->id :rasta)}]}]) + (-> (db/sel :one PulseChannel :pulse_id id) + (hydrate :recipients) + (dissoc :id :pulse_id :created_at :updated_at) + (m/dissoc-in [:details :emails])))) ;; create-pulse ;; simple example with a single card @@ -169,16 +159,15 @@ :schedule_day nil, :recipients [{:email "foo@bar.com"} (dissoc (user-details :crowberto) :is_superuser :is_qbnewb)]}]} - (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) - :name "Lodi Dodi"}] - (tu/with-temp Card [{card-id-1 :id} {:name "Test Card"}] - (tu/with-temp Card [{card-id-2 :id} {:name "Bar Card", :display :bar}] - (update-pulse-then-select {:id id - :name "We like to party" - :creator_id (user->id :crowberto) - :cards [card-id-2 card-id-1] - :channels [{:channel_type :email - :schedule_type :daily - :schedule_hour 18 - :recipients [{:email "foo@bar.com"} - {:id (user->id :crowberto)}]}]}))))) + (tu/with-temp* [Pulse [{pulse-id :id}] + Card [{card-id-1 :id} {:name "Test Card"}] + Card [{card-id-2 :id} {:name "Bar Card", :display :bar}]] + (update-pulse-then-select {:id pulse-id + :name "We like to party" + :creator_id (user->id :crowberto) + :cards [card-id-2 card-id-1] + :channels [{:channel_type :email + :schedule_type :daily + :schedule_hour 18 + :recipients [{:email "foo@bar.com"} + {:id (user->id :crowberto)}]}]}))) diff --git a/test/metabase/models/segment_test.clj b/test/metabase/models/segment_test.clj index 5a93709a836b9299c10756d4d088cffe653ddc1d..d55b77f66abfad76e0f9b534c3648b7f55082f56 100644 --- a/test/metabase/models/segment_test.clj +++ b/test/metabase/models/segment_test.clj @@ -7,7 +7,8 @@ [table :refer [Table]]) [metabase.test.data :refer :all] [metabase.test.data.users :refer :all] - [metabase.test.util :as tu])) + [metabase.test.util :as tu] + [metabase.util :as u])) (defn user-details [username] @@ -39,48 +40,40 @@ :description nil :is_active true :definition {:clause ["a" "b"]}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (create-segment-then-select id "I only want *these* things" nil (user->id :rasta) {:clause ["a" "b"]})))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}]] + (create-segment-then-select table-id "I only want *these* things" nil (user->id :rasta) {:clause ["a" "b"]}))) ;; exists-segment? (expect [true false] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Segment [{:keys [id]} {:creator_id (user->id :rasta) - :table_id id - :name "Ivory Tower" - :description "All the glorious things..." - :definition {:database 45 - :query {:filter ["yay"]}}}] - [(exists-segment? id) - (exists-segment? 34)])))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [{segment-id :id} {:table_id table-id}]] + [(exists-segment? segment-id) + (exists-segment? 3400)])) ;; retrieve-segment (expect {:creator_id (user->id :rasta) :creator (user-details :rasta) - :name "Ivory Tower" - :description "All the glorious things..." + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active true :definition {:database 45 :query {:filter ["yay"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Segment [{:keys [id]} {:creator_id (user->id :rasta) - :table_id id - :name "Ivory Tower" - :description "All the glorious things..." - :definition {:database 45 - :query {:filter ["yay"]}}}] - (let [{:keys [creator] :as segment} (retrieve-segment id)] - (-> segment - (dissoc :id :table_id :created_at :updated_at) - (assoc :creator (dissoc creator :date_joined :last_login)))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [{segment-id :id} {:table_id table-id + :definition {:database 45 + :query {:filter ["yay"]}}}]] + (let [{:keys [creator] :as segment} (retrieve-segment segment-id)] + (-> segment + (dissoc :id :table_id :created_at :updated_at) + (assoc :creator (dissoc creator :date_joined :last_login)))))) ;; retrieve-segements @@ -91,29 +84,16 @@ :description nil :is_active true :definition {}}] - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id1 :id} {:db_id database-id}] - (tu/with-temp Table [{table-id2 :id} {:db_id database-id}] - (tu/with-temp Segment [{segement-id1 :id} {:creator_id (user->id :rasta) - :table_id table-id1 - :name "Segment 1" - :definition {}}] - (tu/with-temp Segment [{segment-id2 :id} {:creator_id (user->id :rasta) - :table_id table-id2 - :name "Segment 2" - :definition {}}] - (tu/with-temp Segment [{segment-id3 :id} {:creator_id (user->id :rasta) - :table_id table-id1 - :name "Segment 3" - :is_active false - :definition {}}] - (let [segments (retrieve-segments table-id1)] - (assert (= 1 (count segments))) - (->> segments - (mapv #(into {} %)) ; expectations doesn't compare our record type properly - (mapv #(dissoc % :id :table_id :created_at :updated_at)) - (mapv (fn [{:keys [creator] :as segment}] - (assoc segment :creator (dissoc creator :date_joined :last_login))))))))))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id-1 :id} {:db_id database-id}] + Table [{table-id-2 :id} {:db_id database-id}] + Segment [{segement-id-1 :id} {:table_id table-id-1, :name "Segment 1", :description nil}] + Segment [{segment-id-2 :id} {:table_id table-id-2}] + Segment [{segment-id3 :id} {:table_id table-id-1, :is_active false}]] + (doall (for [segment (u/prog1 (retrieve-segments table-id-1) + (assert (= 1 (count <>))))] + (-> (dissoc (into {} segment) :id :table_id :created_at :updated_at) + (update :creator (u/rpartial dissoc :date_joined :last_login))))))) ;; update-segment @@ -131,21 +111,17 @@ :is_active true :definition {:database 2 :query {:filter ["not" "the toucans you're looking for"]}}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Segment [{:keys [id]} {:creator_id (user->id :rasta) - :table_id id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {}}] - (update-segment-then-select {:id id - :name "Costa Rica" - :description nil - :creator_id (user->id :crowberto) - :table_id 456 - :definition {:database 2 - :query {:filter ["not" "the toucans you're looking for"]}} - :revision_message "Just horsing around"}))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}] + Segment [{:keys [id]} {:table_id id}]] + (update-segment-then-select {:id id + :name "Costa Rica" + :description nil + :creator_id (user->id :crowberto) + :table_id 456 + :definition {:database 2 + :query {:filter ["not" "the toucans you're looking for"]}} + :revision_message "Just horsing around"}))) ;; delete-segment (expect @@ -155,15 +131,11 @@ :description "Lookin' for a blueberry" :is_active false :definition {}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Segment [{:keys [id]} {:creator_id (user->id :rasta) - :table_id id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {}}] - (delete-segment id (user->id :crowberto) "revision message") - (segment-details (retrieve-segment id)))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{:keys [id]} {:db_id database-id}] + Segment [{:keys [id]} {:table_id id}]] + (delete-segment id (user->id :crowberto) "revision message") + (segment-details (retrieve-segment id)))) ;; ## Segment Revisions @@ -179,16 +151,13 @@ :description "Lookin' for a blueberry" :definition {:filter ["AND",[">",4,"2014-10-19"]]} :is_active true} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{table-id :id} {:db_id database-id}] - (tu/with-temp Segment [segment {:creator_id (user->id :rasta) - :table_id table-id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {:filter ["AND",[">",4,"2014-10-19"]]}}] - (-> (serialize-segment Segment (:id segment) segment) - (update :id boolean) - (update :table_id boolean)))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [segment {:table_id table-id + :definition {:filter ["AND" [">" 4 "2014-10-19"]]}}]] + (-> (serialize-segment Segment (:id segment) segment) + (update :id boolean) + (update :table_id boolean)))) ;; diff-segments @@ -199,16 +168,14 @@ :after "BBB"} :name {:before "Toucans in the rainforest" :after "Something else"}} - (tu/with-temp Database [{database-id :id}] - (tu/with-temp Table [{:keys [id]} {:db_id database-id}] - (tu/with-temp Segment [segment {:creator_id (user->id :rasta) - :table_id id - :name "Toucans in the rainforest" - :description "Lookin' for a blueberry" - :definition {:filter ["AND",[">",4,"2014-10-19"]]}}] - (diff-segments Segment segment (assoc segment :name "Something else" - :description "BBB" - :definition {:filter ["AND",["BETWEEN",4,"2014-07-01","2014-10-19"]]})))))) + (tu/with-temp* [Database [{database-id :id}] + Table [{table-id :id} {:db_id database-id}] + Segment [segment {:table_id table-id + :definition {:filter ["AND" [">" 4 "2014-10-19"]]}}]] + (diff-segments Segment segment (assoc segment + :name "Something else" + :description "BBB" + :definition {:filter ["AND" ["BETWEEN" 4 "2014-07-01" "2014-10-19"]]})))) ;; test case where definition doesn't change (expect @@ -217,30 +184,30 @@ (diff-segments Segment {:name "A" :description "Unchanged" - :definition {:filter ["AND",[">",4,"2014-10-19"]]}} + :definition {:filter ["AND" [">" 4 "2014-10-19"]]}} {:name "B" :description "Unchanged" - :definition {:filter ["AND",[">",4,"2014-10-19"]]}})) + :definition {:filter ["AND" [">" 4 "2014-10-19"]]}})) -;; first version, so comparing against nil +;; first version so comparing against nil (expect {:name {:after "A"} :description {:after "Unchanged"} - :definition {:after {:filter ["AND",[">",4,"2014-10-19"]]}}} + :definition {:after {:filter ["AND" [">" 4 "2014-10-19"]]}}} (diff-segments Segment nil {:name "A" :description "Unchanged" - :definition {:filter ["AND",[">",4,"2014-10-19"]]}})) + :definition {:filter ["AND" [">" 4 "2014-10-19"]]}})) ;; removals only (expect - {:definition {:before {:filter ["AND",[">",4,"2014-10-19"],["=",5,"yes"]]} - :after {:filter ["AND",[">",4,"2014-10-19"]]}}} + {:definition {:before {:filter ["AND" [">" 4 "2014-10-19"] ["=" 5 "yes"]]} + :after {:filter ["AND" [">" 4 "2014-10-19"]]}}} (diff-segments Segment {:name "A" :description "Unchanged" - :definition {:filter ["AND",[">",4,"2014-10-19"],["=",5,"yes"]]}} + :definition {:filter ["AND" [">" 4 "2014-10-19"] ["=" 5 "yes"]]}} {:name "A" :description "Unchanged" - :definition {:filter ["AND",[">",4,"2014-10-19"]]}})) + :definition {:filter ["AND" [">" 4 "2014-10-19"]]}})) diff --git a/test/metabase/models/session_test.clj b/test/metabase/models/session_test.clj index 7e03ee8bb992f72faf0320875eb31cfc7593da7c..ebd0d711c557168855c5ac2b7efbeda6a8217c8d 100644 --- a/test/metabase/models/session_test.clj +++ b/test/metabase/models/session_test.clj @@ -9,21 +9,21 @@ ;; first-session-for-user (expect "the-greatest-day-ever" - (with-temp User [{:keys [id]} {:first_name (random-name) - :last_name (random-name) - :email (str (random-name) "@metabase.com") - :password "nada"}] + (with-temp User [{user-id :id} {:first_name (random-name) + :last_name (random-name) + :email (str (random-name) "@metabase.com") + :password "nada"}] (k/insert Session (k/values [{:id "the-greatest-day-ever" - :user_id id + :user_id user-id :created_at (metabase.util/->Timestamp "1980-10-19")} {:id "the-world-of-bi-changes-forever" - :user_id id + :user_id user-id :created_at (metabase.util/->Timestamp "2015-10-21")} {:id "something-could-have-happened" - :user_id id + :user_id user-id :created_at (metabase.util/->Timestamp "1999-12-31")} {:id "now" - :user_id id + :user_id user-id :created_at (metabase.util/new-sql-timestamp)}])) - (first-session-for-user id))) + (first-session-for-user user-id))) diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.clj index 90529e248b865227bf76698b74f12767b76d2d1f..15a4dabbade85915a034b967b4981165290e113e 100644 --- a/test/metabase/test/util.clj +++ b/test/metabase/test/util.clj @@ -10,6 +10,11 @@ [dashboard :refer [Dashboard]] [database :refer [Database]] [field :refer [Field]] + [metric :refer [Metric]] + [pulse :refer [Pulse]] + [pulse-channel :refer [PulseChannel]] + [revision :refer [Revision]] + [segment :refer [Segment]] [table :refer [Table]]))) (declare $->prop) @@ -82,28 +87,45 @@ (defprotocol ^:private WithTempDefaults (^:private with-temp-defaults [this])) -(u/strict-extend Object WithTempDefaults {:with-temp-defaults (constantly nil)}) -(u/strict-extend (class Card) WithTempDefaults {:with-temp-defaults (fn [_] {:creator_id ((resolve 'metabase.test.data.users/user->id) :rasta) - :dataset_query {} - :display :table - :name (random-name) - :public_perms common/perms-none - :visualization_settings {}})}) -(u/strict-extend (class Dashboard) WithTempDefaults {:with-temp-defaults (fn [_] {:creator_id ((resolve 'metabase.test.data.users/user->id) :rasta) - :name (random-name) - :public_perms 0})}) -(u/strict-extend (class Database) WithTempDefaults {:with-temp-defaults (fn [_] {:details {} - :engine :yeehaw - :is_sample false - :name (random-name)})}) -(u/strict-extend (class Field) WithTempDefaults {:with-temp-defaults (fn [_] {:active true - :base_type :TextField - :field_type :info - :name (random-name) - :position 1 - :preview_display true})}) -(u/strict-extend (class Table) WithTempDefaults {:with-temp-defaults (fn [_] {:active true - :name (random-name)})}) +(u/strict-extend Object WithTempDefaults {:with-temp-defaults (constantly nil)}) +(u/strict-extend (class Card) WithTempDefaults {:with-temp-defaults (fn [_] {:creator_id ((resolve 'metabase.test.data.users/user->id) :rasta) + :dataset_query {} + :display :table + :name (random-name) + :public_perms common/perms-none + :visualization_settings {}})}) +(u/strict-extend (class Dashboard) WithTempDefaults {:with-temp-defaults (fn [_] {:creator_id ((resolve 'metabase.test.data.users/user->id) :rasta) + :name (random-name) + :public_perms 0})}) +(u/strict-extend (class Database) WithTempDefaults {:with-temp-defaults (fn [_] {:details {} + :engine :yeehaw + :is_sample false + :name (random-name)})}) +(u/strict-extend (class Field) WithTempDefaults {:with-temp-defaults (fn [_] {:active true + :base_type :TextField + :field_type :info + :name (random-name) + :position 1 + :preview_display true})}) +(u/strict-extend (class Metric) WithTempDefaults {:with-temp-defaults (fn [_] {:creator_id ((resolve 'metabase.test.data.users/user->id) :rasta) + :definition {} + :description "Lookin' for a blueberry" + :name "Toucans in the rainforest"})}) +(u/strict-extend (class Pulse) WithTempDefaults {:with-temp-defaults (fn [_] {:creator_id ((resolve 'metabase.test.data.users/user->id) :rasta) + :name (random-name)})}) +(u/strict-extend (class PulseChannel) WithTempDefaults {:with-temp-defaults (constantly {:channel_type :email + :details {} + :schedule_type :daily + :schedule_hour 15})}) +(u/strict-extend (class Revision) WithTempDefaults {:with-temp-defaults (fn [_] {:user_id ((resolve 'metabase.test.data.users/user->id) :rasta) + :is_creation false + :is_reversion false})}) +(u/strict-extend (class Segment) WithTempDefaults {:with-temp-defaults (fn [_] {:creator_id ((resolve 'metabase.test.data.users/user->id) :rasta) + :definition {} + :description "Lookin' for a blueberry" + :name "Toucans in the rainforest"})}) +(u/strict-extend (class Table) WithTempDefaults {:with-temp-defaults (fn [_] {:active true + :name (random-name)})}) (defn do-with-temp