diff --git a/src/metabase/db/migrations.clj b/src/metabase/db/migrations.clj index 164abba674b687130e6053e22a8e47e2a9eb0ca6..8bc386c062bfddb376bf1631e4f6d978169ed04a 100644 --- a/src/metabase/db/migrations.clj +++ b/src/metabase/db/migrations.clj @@ -33,8 +33,7 @@ (@migration-var) (k/insert "data_migrations" (k/values {:id migration-name - :timestamp (u/new-sql-timestamp)})) - (log/info "[ok]")))) + :timestamp (u/new-sql-timestamp)}))))) (def ^:private data-migrations (atom [])) diff --git a/test/metabase/api/activity_test.clj b/test/metabase/api/activity_test.clj index 55eb3136508d2c0d4d920c555b8551cd2b08ee6c..ffdeac24ac98609b0bd56d0408329ee66fa558e9 100644 --- a/test/metabase/api/activity_test.clj +++ b/test/metabase/api/activity_test.clj @@ -10,7 +10,7 @@ [view-log :refer [ViewLog]]) [metabase.test.data :refer :all] [metabase.test.data.users :refer :all] - [metabase.test.util :refer [match-$ expect-eval-actual-first random-name with-temp]] + [metabase.test.util :refer [match-$ expect-eval-actual-first random-name]] [metabase.util :as u])) ;; GET / diff --git a/test/metabase/api/card_test.clj b/test/metabase/api/card_test.clj index 5f4b098b029f98745b42d40331ec12a18a957f84..203f6ddbf19767802939f7be527acf7cdfbe19c6 100644 --- a/test/metabase/api/card_test.clj +++ b/test/metabase/api/card_test.clj @@ -32,31 +32,16 @@ [true false true] - (with-temp Database [{dbid :id} {:name (random-name) - :engine :h2 - :details {}}] - (with-temp Card [{id1 :id} {:name (random-name) - :public_perms common/perms-none - :creator_id (user->id :crowberto) - :display :table - :dataset_query {} - :visualization_settings {} - :database_id (id)}] - (with-temp Card [{id2 :id} {:name (random-name) - :public_perms common/perms-none - :creator_id (user->id :crowberto) - :display :table - :dataset_query {} - :visualization_settings {} - :database_id dbid}] + (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? (->> ((user->client :crowberto) :get 200 "card" :f :database :model_id database-id) - (map :id) - set) + (contains? (set (for [card ((user->client :rasta) :get 200 "card", :f :database, :model_id database-id)] + (:id card))) card-id))] - [(card-returned? (id) id1) - (card-returned? dbid id1) - (card-returned? dbid id2)]))))) + [(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'"}} @@ -66,38 +51,18 @@ (expect [true false true] - (with-temp Database [{database-id :id} {:name "Card API Test" - :engine :yeehaw - :details {} - :is_sample false}] - (with-temp Table [{table1 :id} {:name "Card API Table 1" - :db_id database-id - :active true}] - (with-temp Table [{table2 :id} {:name "Card API Table 2" - :db_id database-id - :active true}] - (with-temp Card [{id1 :id} {:name (random-name) - :public_perms common/perms-none - :creator_id (user->id :crowberto) - :display :table - :dataset_query {} - :visualization_settings {} - :table_id table1}] - (with-temp Card [{id2 :id} {:name (random-name) - :public_perms common/perms-none - :creator_id (user->id :crowberto) - :display :table - :dataset_query {} - :visualization_settings {} - :table_id table2}] + (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? (->> ((user->client :crowberto) :get 200 "card" :f :table :model_id table-id) - (map :id) - set) + (contains? (set (for [card ((user->client :rasta) :get 200 "card", :f :table, :model_id table-id)] + (:id card))) card-id))] - [(card-returned? table1 id1) - (card-returned? table2 id1) - (card-returned? table2 id2)]))))))) + [(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'"}} @@ -106,17 +71,11 @@ ;; Check that only the creator of a private Card can see it (expect [true false] - (with-temp Card [{:keys [id]} {:name (random-name) - :public_perms common/perms-none - :creator_id (user->id :crowberto) - :display :table - :dataset_query {} - :visualization_settings {}}] + (with-temp Card [{card-id :id} {:creator_id (user->id :crowberto)}] (let [can-see-card? (fn [user] - (contains? (->> ((user->client user) :get 200 "card" :f :all) - (map :id) - set) - id))] + (contains? (set (for [card ((user->client user) :get 200 "card", :f :all)] + (:id card))) + card-id))] [(can-see-card? :crowberto) (can-see-card? :rasta)]))) diff --git a/test/metabase/api/dashboard_test.clj b/test/metabase/api/dashboard_test.clj index 9cc0679ddc39626e4425e82cc63f395c53eeb991..aeb9a33c973755e6829f311717193155f2cc3dfc 100644 --- a/test/metabase/api/dashboard_test.clj +++ b/test/metabase/api/dashboard_test.clj @@ -113,26 +113,18 @@ :creator_id (user->id :rasta) :creator (user-details (fetch-user :rasta)) :organization_id nil - :display "scalar" + :display "table" :query_type nil - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}} + :dataset_query {} + :visualization_settings {} } :series []}] :organization_id nil} ;; fetch a dashboard WITH a dashboard card on it - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] + (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}] - (-> ((user->client :rasta) :get 200 (format "dashboard/%d" dashboard-id)) - dashboard-response))))) + (dashboard-response ((user->client :rasta) :get 200 (format "dashboard/%d" dashboard-id))))))) ;; ## PUT /api/dashboard/:id @@ -159,25 +151,20 @@ :updated_at true :created_at true :organization_id nil}] - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (->> [(Dashboard dashboard-id) - ((user->client :rasta) :put 200 (format "dashboard/%d" dashboard-id) {:name "My Cool Dashboard" - :description "Some awesome description" - ;; these things should fail to update - :public_perms 2 - :creator_id (user->id :trashbird)}) - (Dashboard dashboard-id)] - (mapv dashboard-response)))) + (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard"}] + (mapv dashboard-response [(Dashboard dashboard-id) + ((user->client :rasta) :put 200 (str "dashboard/" dashboard-id) {:name "My Cool Dashboard" + :description "Some awesome description" + ;; these things should fail to update + :public_perms 2 + :creator_id (user->id :trashbird)}) + (Dashboard dashboard-id)]))) ;; ## DELETE /api/dashboard/:id (expect [nil nil] - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] + (tu/with-temp Dashboard [{dashboard-id :id}] [((user->client :rasta) :delete 204 (format "dashboard/%d" dashboard-id)) (Dashboard dashboard-id)])) @@ -198,15 +185,8 @@ :sizeY 2 :col 4 :row 4}]] - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] + (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}) @@ -223,8 +203,8 @@ :row 4 :series [{:name "Series Card" :description nil - :display "scalar" - :dataset_query {:something "simple"} + :display "table" + :dataset_query {} :visualization_settings {}}] :created_at true :updated_at true} @@ -233,25 +213,13 @@ :col 4 :row 4}] [0]] - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {}}] - (tu/with-temp Card [{series-id1 :id} {:name "Series Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :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 "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-id1}]})] + :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))]))))) @@ -263,35 +231,13 @@ {:success true} 0] ;; fetch a dashboard WITH a dashboard card on it - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (tu/with-temp Card [{series-id1 :id} {:name "Additional Series Card 1" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (tu/with-temp Card [{series-id2 :id} {:name "Additional Series Card 2" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (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-id1 - :position 0}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id - :card_id series-id2 - :position 1}] + (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))])))))))) @@ -320,8 +266,9 @@ :row 0 :series [{:name "Series Card" :description nil - :display :scalar - :dataset_query {:something "simple"} + + :display :table + :dataset_query {} :visualization_settings {}}] :created_at true :updated_at true} @@ -333,40 +280,26 @@ :created_at true :updated_at true}]] ;; fetch a dashboard WITH a dashboard card on it - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (tu/with-temp DashboardCard [{dashcard-id1 :id} {:dashboard_id dashboard-id - :card_id card-id}] - (tu/with-temp DashboardCard [{dashcard-id2 :id} {:dashboard_id dashboard-id - :card_id card-id}] - (tu/with-temp Card [{series-id1 :id} {:name "Series Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {}}] - [[(remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id1)) - (remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id2))] - ((user->client :rasta) :put 200 (format "dashboard/%d/cards" dashboard-id) {:cards [{:id dashcard-id1 + (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-id1}]} - {:id dashcard-id2 + :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-id1)) - (remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id2))]])))))) + [(remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-1)) + (remove-ids-and-boolean-timestamps (retrieve-dashboard-card dashcard-id-2))]])))))) @@ -392,9 +325,7 @@ (dissoc :email :date_joined :last_login :is_superuser :is_qbnewb)) :diff nil :description nil}] - (tu/with-temp Dashboard [{:keys [id]} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] + (tu/with-temp Dashboard [{:keys [id]}] (tu/with-temp Revision [_ {:model "Dashboard" :model_id id :user_id (user->id :rasta) @@ -471,9 +402,7 @@ (dissoc :email :date_joined :last_login :is_superuser :is_qbnewb)) :diff nil :description nil}]] - (tu/with-temp Dashboard [{:keys [id]} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] + (tu/with-temp Dashboard [{:keys [id]}] (tu/with-temp Revision [{revision-id :id} {:model "Dashboard" :model_id id :user_id (user->id :rasta) diff --git a/test/metabase/api/dataset_test.clj b/test/metabase/api/dataset_test.clj index a0a20a92f6f1bfb2e953d01ecab01a276cdb31fd..796a523cda639d41fcef1ea696f9691cec0ddd77 100644 --- a/test/metabase/api/dataset_test.clj +++ b/test/metabase/api/dataset_test.clj @@ -128,11 +128,11 @@ :description nil :public_perms 0 :creator (user-details (fetch-user :rasta)) - :display "scalar" + :display "table" :query_type "query" :dataset_query (-> (ql/wrap-inner-query (query checkins - (ql/aggregation (ql/count)))) + (ql/aggregation (ql/count)))) (assoc :type "query") (assoc-in [:query :aggregation] {:aggregation-type "count"})) :visualization_settings {} @@ -148,7 +148,7 @@ :uuid true :json_query (-> (ql/wrap-inner-query (query checkins - (ql/aggregation (ql/count)))) + (ql/aggregation (ql/count)))) (assoc :type "query") (assoc-in [:query :aggregation] {:aggregation-type "count"}) (assoc :constraints dataset-query-api-constraints)) @@ -164,7 +164,7 @@ :raw_query "" :json_query (-> (ql/wrap-inner-query (query checkins - (ql/aggregation (ql/count)))) + (ql/aggregation (ql/count)))) (assoc :type "query") (assoc-in [:query :aggregation] {:aggregation-type "count"}) (assoc :constraints dataset-query-api-constraints)) @@ -172,14 +172,10 @@ :finished_at true :running_time true :version 0}] - (tu/with-temp Card [{card-id :id} {:name "Dataset Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query (ql/wrap-inner-query - (query checkins - (ql/aggregation (ql/count)))) - :visualization_settings {}}] + (tu/with-temp Card [{card-id :id} {:name "Dataset Test Card" + :dataset_query (ql/wrap-inner-query + (query checkins + (ql/aggregation (ql/count))))}] (let [result ((user->client :rasta) :get 200 (format "dataset/card/%d" card-id))] [(-> result (update :card remove-ids-and-boolean-timestamps) diff --git a/test/metabase/api/field_test.clj b/test/metabase/api/field_test.clj index 866cfdfca06d022cada23ff60151de106c895eaa..282947ec9b4a22bde4f08e851fc5f8ac57960cff 100644 --- a/test/metabase/api/field_test.clj +++ b/test/metabase/api/field_test.clj @@ -158,22 +158,11 @@ (expect ["Invalid Request." nil] - (tu/with-temp Database [{database-id :id} {:name "Field Test" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Field Test" - :db_id database-id - :active true}] - (tu/with-temp Field [{field-id :id} {:table_id table-id - :name "Field Test" - :base_type :TextField - :field_type :info - :active true - :preview_display true - :position 1}] - [((user->client :crowberto) :put 400 (format "field/%d" field-id) {:special_type :timestamp_seconds}) - (db/sel :one :field [Field :special_type] :id field-id)])))) + (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)])))) (defn- field->field-values diff --git a/test/metabase/api/foreignkey_test.clj b/test/metabase/api/foreignkey_test.clj new file mode 100644 index 0000000000000000000000000000000000000000..398378b2f4372ee4baf52afb1313a99ef53b7de6 --- /dev/null +++ b/test/metabase/api/foreignkey_test.clj @@ -0,0 +1,40 @@ +(ns metabase.api.foreignkey-test + "tests for /api/foreignkey api endpoints" + (:require [clojure.tools.macro :refer [symbol-macrolet]] + [expectations :refer :all] + [metabase.db :as db] + (metabase [http-client :as http] + [middleware :as middleware]) + (metabase.models [database :refer [Database]] + [field :refer [Field]] + [foreign-key :refer :all] + [table :refer [Table]]) + [metabase.test.util :as tu] + [metabase.test.data.users :refer :all])) + + +;; ## /api/segment/* AUTHENTICATION Tests +;; We assume that all endpoints for a given context are enforced by the same middleware, so we don't run the same +;; authentication test on every single individual endpoint + +(expect (get middleware/response-unauthentic :body) (http/client :delete 401 "foreignkey/123")) + + +;; ## DELETE /api/foreignkey/:id + +;; test security. requires superuser perms +(expect "You don't have permissions to do that." + ((user->client :rasta) :delete 403 "foreignkey/1")) + + +(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)]))))) diff --git a/test/metabase/api/metric_test.clj b/test/metabase/api/metric_test.clj index 53275f0bfdd46ae9d9ce9cce3b16e141dc032e6a..0f0cfcf14afe95903a763d6aa79da8ad74700d1d 100644 --- a/test/metabase/api/metric_test.clj +++ b/test/metabase/api/metric_test.clj @@ -81,18 +81,13 @@ :is_active true :definition {:database 21 :query {:filter ["abc"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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"]}}}))))) + :description "I did it!" + :table_id id + :definition {:database 21 + :query {:filter ["abc"]}}}))))) ;; ## PUT /api/metric @@ -124,7 +119,7 @@ :definition "foobar"})) (expect - {:name "Tatooine" + {:name "Costa Rica" :description nil :creator_id (user->id :rasta) :creator (user-details (fetch-user :rasta)) @@ -132,26 +127,21 @@ :updated_at true :is_active true :definition {:database 2 - :query {:filter ["not" "the droids you're looking for"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :active true}] + :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 "Droids in the desert" - :description "Lookin' for a jedi" + :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 "Tatooine" + :name "Costa Rica" :description nil :table_id 456 :revision_message "I got me some revisions" :definition {:database 2 - :query {:filter ["not" "the droids you're looking for"]}}})))))) + :query {:filter ["not" "the toucans you're looking for"]}}})))))) ;; ## DELETE /api/metric/:id @@ -170,8 +160,8 @@ (expect [{:success true} - {:name "Droids in the desert" - :description "Lookin' for a jedi" + {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :creator_id (user->id :rasta) :creator (user-details (fetch-user :rasta)) :created_at true @@ -182,13 +172,11 @@ :engine :yeehaw :details {} :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :active true}] + (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 "Droids in the desert" - :description "Lookin' for a jedi" + :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))])))) @@ -211,19 +199,14 @@ :is_active true :definition {:database 123 :query {:filter ["In the Land of Metabase where the Datas lie"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :active true}] + (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"]}}}] + :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))))))) @@ -250,13 +233,8 @@ :diff {:name {:after "b"} :definition {:after {:filter ["AND" [">" 1 25]]}}} :description nil}] - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -333,13 +311,8 @@ :definition {:after {:database 123 :query {:filter ["In the Land of Metabase where the Datas lie"]}}}} :description nil}]] - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :active true}] + (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" diff --git a/test/metabase/api/segment_test.clj b/test/metabase/api/segment_test.clj index f50e7760819d57a7baf664f363c1e426deba1d37..72337f14089434df9c521c6d4cc983cad16504bd 100644 --- a/test/metabase/api/segment_test.clj +++ b/test/metabase/api/segment_test.clj @@ -81,13 +81,8 @@ :is_active true :definition {:database 21 :query {:filter ["abc"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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 @@ -124,7 +119,7 @@ :definition "foobar"})) (expect - {:name "Tatooine" + {:name "Costa Rica" :description nil :creator_id (user->id :rasta) :creator (user-details (fetch-user :rasta)) @@ -132,26 +127,21 @@ :updated_at true :is_active true :definition {:database 2 - :query {:filter ["not" "the droids you're looking for"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :active true}] + :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 "Droids in the desert" - :description "Lookin' for a jedi" + :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 "Tatooine" + :name "Costa Rica" :description nil :table_id 456 :revision_message "I got me some revisions" :definition {:database 2 - :query {:filter ["not" "the droids you're looking for"]}}})))))) + :query {:filter ["not" "the toucans you're looking for"]}}})))))) ;; ## DELETE /api/segment/:id @@ -170,25 +160,20 @@ (expect [{:success true} - {:name "Droids in the desert" - :description "Lookin' for a jedi" + {:name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :creator_id (user->id :rasta) :creator (user-details (fetch-user :rasta)) :created_at true :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} {:name "Stuff" - :db_id database-id - :active true}] + (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 "Droids in the desert" - :description "Lookin' for a jedi" + :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))])))) @@ -211,13 +196,8 @@ :is_active true :definition {:database 123 :query {:filter ["In the Land of Metabase where the Datas lie"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -250,13 +230,8 @@ :diff {:name {:after "b"} :definition {:after {:filter ["AND" [">" 1 25]]}}} :description nil}] - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -333,13 +308,8 @@ :definition {:after {:database 123 :query {:filter ["In the Land of Metabase where the Datas lie"]}}}} :description nil}]] - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :active true}] + (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" diff --git a/test/metabase/driver/query_processor/macros_test.clj b/test/metabase/driver/query_processor/macros_test.clj index 9912370a6452b7f504e5f33c0e70de19a87d2e9a..db2806b59c47733af1bbb4be3034f76da204e2fc 100644 --- a/test/metabase/driver/query_processor/macros_test.clj +++ b/test/metabase/driver/query_processor/macros_test.clj @@ -30,13 +30,8 @@ :query {:aggregation ["rows"] :filter ["AND" ["AND" ["=" 5 "abc"]] ["OR" ["AND" ["IS_NULL" 7]] [">" 4 1]]] :breakout [17]}} - (tu/with-temp Database [{database-id :id} {:name "Macro Expansion Test" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Macro Expansion Table" - :db_id database-id - :active true}] + (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" @@ -59,13 +54,8 @@ :filter ["AND" ["AND" [">" 4 1]] ["AND" ["=" 5 "abc"]]] :breakout [17] :order_by [[1 "ASC"]]}} - (tu/with-temp Database [{database-id :id} {:name "Macro Expansion Test" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Macro Expansion Table" - :db_id database-id - :active true}] + (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" @@ -86,13 +76,8 @@ :filter ["AND" ["=" 5 "abc"]] :breakout [17] :order_by [[1 "ASC"]]}} - (tu/with-temp Database [{database-id :id} {:name "Macro Expansion Test" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Macro Expansion Table" - :db_id database-id - :active true}] + (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" @@ -113,13 +98,8 @@ :filter ["AND" ["=" 5 "abc"]] :breakout [17] :order_by [[1 "ASC"]]}} - (tu/with-temp Database [{database-id :id} {:name "Macro Expansion Test" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Macro Expansion Table" - :db_id database-id - :active true}] + (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" @@ -139,13 +119,8 @@ :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} {:name "Macro Expansion Test" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Macro Expansion Table" - :db_id database-id - :active true}] + (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" diff --git a/test/metabase/driver/sync_test.clj b/test/metabase/driver/sync_test.clj index efe64073b16e02b494ed31d9f887dc1d2eecf768..10dde91e7909c7398b15a37e77f88889ee09b04e 100644 --- a/test/metabase/driver/sync_test.clj +++ b/test/metabase/driver/sync_test.clj @@ -16,7 +16,7 @@ (metabase.test.data [datasets :as datasets] [interface :refer [create-database-definition]]))) -(def sync-test-tables +(def ^:private ^:const sync-test-tables {"movie" {:name "movie" :schema "default" :fields #{{:name "id" @@ -41,227 +41,196 @@ driver/IDriver (merge driver/IDriverDefaultsMixin {:analyze-table (constantly nil) - :describe-database (fn [_ _] - {:tables (set (->> (vals sync-test-tables) - (map #(dissoc % :fields))))}) + :describe-database (constantly {:tables (set (for [table (vals sync-test-tables)] + (dissoc table :fields)))}) :describe-table (fn [_ table] (get sync-test-tables (:name table))) - :descrite-table-fks (fn [_ _] - #{{:fk-column-name "studio" - :dest-table-name "studio" - :dest-column-name "studio"}})})) + :descrite-table-fks (constantly #{{:fk-column-name "studio" + :dest-table-name "studio" + :dest-column-name "studio"}})})) -;(driver/register-driver! :sync-test (SyncTestDriver.)) +(def ^:private users-table (delay (sel :one Table, :name "USERS"))) +(def ^:private venues-table (delay (Table (id :venues)))) +(def ^:private korma-users-table (delay (korma-entity @users-table))) +(def ^:private users-name-field (delay (Field (id :users :name)))) -(def users-table - (delay (sel :one Table :name "USERS"))) -(def venues-table - (delay (Table (id :venues)))) - -(def korma-users-table - (delay (korma-entity @users-table))) - -(def users-name-field - (delay (Field (id :users :name)))) - - -(defn table-details [table] +(defn- table-details [table] (into {} (-> (dissoc table :id :db :db_id :created_at :updated_at :pk_field :field_values) - (assoc :fields (->> (sel :many Field :table_id (:id table) (k/order :name)) - (map #(dissoc % :table_id :table :db :children :qualified-name :qualified-name-components :created_at :updated_at :id :values :target)) - (map #(into {} %))))))) + (assoc :fields (for [field (sel :many Field, :table_id (:id table), (k/order :name))] + (into {} (dissoc field :table_id :table :db :children :qualified-name :qualified-name-components :created_at :updated_at :id :values :target))))))) ;; ## SYNC DATABASE (expect - [{:schema "default" - :name "movie" - :display_name "Movie" - :description nil - :entity_type nil - :entity_name nil + [{:schema "default" + :name "movie" + :display_name "Movie" + :description nil + :entity_type nil + :entity_name nil :visibility_type nil - :rows nil - :active true - :fields [{:description nil, - :special_type :id, - :name "id", - :active true, - :parent_id nil, - :field_type :info, - :position 0, - :preview_display true, - :display_name "Id", - :base_type :IntegerField, - :visibility_type :normal - :fk_target_field_id nil} - {:description nil, - :special_type nil, - :name "studio", - :active true, - :parent_id nil, - :field_type :info, - :position 0, - :preview_display true, - :display_name "Studio", - :base_type :TextField, - :visibility_type :normal - :fk_target_field_id nil} - {:description nil, - :special_type nil, - :name "title", - :active true, - :parent_id nil, - :field_type :info, - :position 0, - :preview_display true, - :display_name "Title", - :base_type :TextField, - :visibility_type :normal - :fk_target_field_id nil}]} - {:schema nil - :name "studio" - :display_name "Studio" - :description nil - :entity_type nil - :entity_name nil + :rows nil + :active true + :fields [{:description nil + :special_type :id + :name "id" + :active true + :parent_id nil + :field_type :info + :position 0 + :preview_display true + :display_name "Id" + :base_type :IntegerField + :visibility_type :normal + :fk_target_field_id nil + {:description nil + :special_type nil + :name "studio" + :active true + :parent_id nil + :field_type :info + :position 0 + :preview_display true + :display_name "Studio" + :base_type :TextField + :visibility_type :normal + :fk_target_field_id nil} + {:description nil + :special_type nil + :name "title" + :active true + :parent_id nil + :field_type :info + :position 0 + :preview_display true + :display_name "Title" + :base_type :TextField + :visibility_type :normal + :fk_target_field_id nil}]} + {:schema nil + :name "studio" + :display_name "Studio" + :description nil + :entity_type nil + :entity_name nil :visibility_type nil - :rows nil - :active true - :fields [{:description nil, - :special_type :name, - :name "name", - :active true, - :parent_id nil, - :field_type :info, - :position 0, - :preview_display true, - :display_name "Name", - :base_type :TextField, - :visibility_type :normal - :fk_target_field_id nil} - {:description nil, - :special_type :id, - :name "studio", - :active true, - :parent_id nil, - :field_type :info, - :position 0, - :preview_display true, - :display_name "Studio", - :base_type :TextField, - :visibility_type :normal - :fk_target_field_id nil}]}] - (tu/with-temp Database [fake-db {:name "sync-test" - :engine :sync-test - :details {}}] + :rows nil + :active true + :fields [{:description nil + :special_type :name + :name "name" + :active true + :parent_id nil + :field_type :info + :position 0 + :preview_display true + :display_name "Name" + :base_type :TextField + :visibility_type :normal + :fk_target_field_id nil} + {:description nil + :special_type :id + :name "studio" + :active true + :parent_id nil + :field_type :info + :position 0 + :preview_display true + :display_name "Studio" + :base_type :TextField + :visibility_type :normal + :fk_target_field_id nil}]}] + (tu/with-temp Database [fake-db] (sync/sync-database! (SyncTestDriver.) fake-db) ;; we are purposely running the sync twice to test for possible logic issues which only manifest ;; on resync of a database, such as adding tables that already exist or duplicating fields (sync/sync-database! (SyncTestDriver.) fake-db) - (->> (sel :many Table :db_id (:id fake-db) (k/order :name)) - (mapv table-details)))) + (mapv table-details (sel :many Table, :db_id (:id fake-db), (k/order :name))))) ;; ## SYNC TABLE (expect - {:schema "default" - :name "movie" - :display_name "Movie" - :description nil - :entity_type nil - :entity_name nil + {:schema "default" + :name "movie" + :display_name "Movie" + :description nil + :entity_type nil + :entity_name nil :visibility_type nil - :rows nil - :active true - :fields [{:description nil, - :special_type :id, - :name "id", - :active true, - :parent_id nil, - :field_type :info, - :position 0, - :preview_display true, - :display_name "Id", - :base_type :IntegerField, - :visibility_type :normal - :fk_target_field_id nil} - {:description nil, - :special_type nil, - :name "studio", - :active true, - :parent_id nil, - :field_type :info, - :position 0, - :preview_display true, - :display_name "Studio", - :base_type :TextField, - :visibility_type :normal - :fk_target_field_id nil} - {:description nil, - :special_type nil, - :name "title", - :active true, - :parent_id nil, - :field_type :info, - :position 0, - :preview_display true, - :display_name "Title", - :base_type :TextField, - :visibility_type :normal - :fk_target_field_id nil}]} - (tu/with-temp Database [fake-db {:name "sync-test" - :engine :sync-test - :details {}}] - (tu/with-temp Table [fake-table {:name "movie" + :rows nil + :active true + :fields [{:description nil + :special_type :id + :name "id" + :active true + :parent_id nil + :field_type :info + :position 0 + :preview_display true + :display_name "Id" + :base_type :IntegerField + :visibility_type :normal + :fk_target_field_id nil} + {:description nil + :special_type nil + :name "studio" + :active true + :parent_id nil + :field_type :info + :position 0 + :preview_display true + :display_name "Studio" + :base_type :TextField + :visibility_type :normal + :fk_target_field_id nil} + {:description nil + :special_type nil + :name "title" + :active true + :parent_id nil + :field_type :info + :position 0 + :preview_display true + :display_name "Title" + :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) - :active true}] + :db_id (:id fake-db)}] (sync/sync-table! (SyncTestDriver.) fake-table) - (table-details (sel :one Table :id (:id fake-table)))))) + (table-details (sel :one Table, :id (:id fake-table)))))) ;; ## Test that we will remove field-values when they aren't appropriate (expect - [[1,2,3] - [1,2,3]] - (tu/with-temp Database [fake-db {:name "sync-test" - :engine :sync-test - :details {}}] - (tu/with-temp Table [fake-table {:name "movie" - :schema "default" - :db_id (:id fake-db) - :active true}] + [[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 [{:keys [id]} (sel :one Field :table_id (:id fake-table) :name "title")] - (tu/with-temp FieldValues [_ {:field_id id + (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 [starting (sel :one :field [FieldValues :values] :field_id id)] + (let [initial-field-values (sel :one :field [FieldValues :values], :field_id field-id)] (sync/sync-table! (SyncTestDriver.) fake-table) - [starting - (sel :one :field [FieldValues :values] :field_id id)])))))) + [initial-field-values + (sel :one :field [FieldValues :values], :field_id field-id)])))))) ;; ## Individual Helper Fns ;; infer-field-special-type -(expect nil (sync/infer-field-special-type {:name "whatever" - :base-type :foo})) -(expect :id (sync/infer-field-special-type {:name "whatever" - :base-type :TextField - :pk? :id})) -(expect :id (sync/infer-field-special-type {:name "id" - :base-type :IntegerField})) -(expect :category (sync/infer-field-special-type {:name "whatever" - :base-type :IntegerField - :special-type :category})) -(expect :country (sync/infer-field-special-type {:name "country" - :base-type :TextField})) -(expect :state (sync/infer-field-special-type {:name "state" - :base-type :TextField})) +(expect nil (sync/infer-field-special-type {:name "whatever", :base-type :foo})) +(expect :id (sync/infer-field-special-type {:name "whatever", :base-type :TextField, :pk? :id})) +(expect :id (sync/infer-field-special-type {:name "id", :base-type :IntegerField})) +(expect :category (sync/infer-field-special-type {:name "whatever", :base-type :IntegerField, :special-type :category})) +(expect :country (sync/infer-field-special-type {:name "country", :base-type :TextField})) +(expect :state (sync/infer-field-special-type {:name "state", :base-type :TextField})) ;; ## TEST PK SYNCING diff --git a/test/metabase/events/dependencies_test.clj b/test/metabase/events/dependencies_test.clj index 152e9020e25416b65410211921a0abe1ac54dc14..eaafd9cc4aaa88b8658f786796dc36c8dd044f42 100644 --- a/test/metabase/events/dependencies_test.clj +++ b/test/metabase/events/dependencies_test.clj @@ -20,36 +20,23 @@ :dependent_on_id 2} {:dependent_on_model "Segment" :dependent_on_id 3}} - (tu/with-temp Card [card {:name "Simple Test" - :creator_id (user->id :rasta) - :public_perms 2 - :display "table" - :dataset_query {:database (id) - :type :query - :query {:source_table (id :categories) - :filter ["AND" [">" 4 "2014-10-19"] ["=" 5 "yes"] ["SEGMENT" 2] ["SEGMENT" 3]]}} - :visualization_settings {}}] + (tu/with-temp Card [card {:dataset_query {:database (id) + :type :query + :query {:source_table (id :categories) + :filter ["AND" [">" 4 "2014-10-19"] ["=" 5 "yes"] ["SEGMENT" 2] ["SEGMENT" 3]]}}}] (process-dependencies-event {:topic :card-create :item card}) - (->> (db/sel :many Dependency :model "Card" :model_id (:id card)) - (mapv #(select-keys % [:dependent_on_model :dependent_on_id])) - set))) + (set (db/sel :many :fields [Dependency :dependent_on_model :dependent_on_id], :model "Card", :model_id (:id card))))) ;; `:card-update` event (expect [] - (tu/with-temp Card [card {:name "Simple Test" - :creator_id (user->id :rasta) - :public_perms 2 - :display "table" - :dataset_query {:database (id) - :type :query - :query {:source_table (id :categories)}} - :visualization_settings {}}] + (tu/with-temp Card [card {:dataset_query {:database (id) + :type :query + :query {:source_table (id :categories)}}}] (process-dependencies-event {:topic :card-create :item card}) - (->> (db/sel :many Dependency :model "Card" :model_id (:id card)) - (mapv #(select-keys % [:dependent_on_model :dependent_on_id]))))) + (db/sel :many :fields [Dependency :dependent_on_model :dependent_on_id], :model "Card", :model_id (:id card)))) ;; `:metric-create` event (expect @@ -57,13 +44,8 @@ :dependent_on_id 18} {:dependent_on_model "Segment" :dependent_on_id 35}} - (tu/with-temp Database [{database-id :id} {:name "Dependencies Test" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Dependencies Test" - :db_id database-id - :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 "Dependencies Test" @@ -71,9 +53,7 @@ :filter ["AND" ["SEGMENT" 18] ["SEGMENT" 35]]}}] (process-dependencies-event {:topic :metric-create :item metric}) - (->> (db/sel :many Dependency :model "Metric" :model_id (:id metric)) - (mapv #(select-keys % [:dependent_on_model :dependent_on_id])) - set))))) + (set (db/sel :many :fields [Dependency :dependent_on_model :dependent_on_id], :model "Metric", :model_id (:id metric))))))) ;; `:card-update` event (expect @@ -81,13 +61,8 @@ :dependent_on_id 18} {:dependent_on_model "Segment" :dependent_on_id 35}} - (tu/with-temp Database [{database-id :id} {:name "Dependencies Test" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Dependencies Test" - :db_id database-id - :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 "Dependencies Test" @@ -95,6 +70,4 @@ :filter ["AND" ["SEGMENT" 18] ["SEGMENT" 35]]}}] (process-dependencies-event {:topic :metric-update :item metric}) - (->> (db/sel :many Dependency :model "Metric" :model_id (:id metric)) - (mapv #(select-keys % [:dependent_on_model :dependent_on_id])) - set))))) + (set (db/sel :many :fields [Dependency :dependent_on_model :dependent_on_id], :model "Metric", :model_id (:id metric))))))) diff --git a/test/metabase/events/revision_test.clj b/test/metabase/events/revision_test.clj index 3f1ca27ce829b74b64f82910ac7476a9fd068658..dc1ea6ff0c90336da7d3c7232bbb3c48113cfe28 100644 --- a/test/metabase/events/revision_test.clj +++ b/test/metabase/events/revision_test.clj @@ -191,22 +191,16 @@ :is_reversion false :is_creation true :message nil} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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"}}] + :table_id id + :name "ABC" + :description "DEF" + :definition {:a "b"}}] (process-revision-event {:topic :metric-create :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]))] + (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 @@ -221,24 +215,18 @@ :is_reversion false :is_creation false :message "updated"} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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"}}] + :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 Revision :model "Metric" :model_id (:id metric)) - (select-keys [:model :user_id :object :is_reversion :is_creation :message]))] + :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 @@ -253,13 +241,8 @@ :is_reversion false :is_creation false :message nil} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -285,13 +268,8 @@ :is_reversion false :is_creation true :message nil} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -315,13 +293,8 @@ :is_reversion false :is_creation false :message "updated"} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -329,8 +302,8 @@ :definition {:a "b"}}] (process-revision-event {:topic :segment-update :item (assoc segment - :actor_id (user->id :crowberto) - :revision_message "updated")}) + :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))))))) @@ -347,13 +320,8 @@ :is_reversion false :is_creation false :message nil} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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" diff --git a/test/metabase/models/dashboard_card_test.clj b/test/metabase/models/dashboard_card_test.clj index 524f2545690e1800f47638f58a92d914e8ab1188..871740463eb05c53ccab64e8981c475abdcbf531 100644 --- a/test/metabase/models/dashboard_card_test.clj +++ b/test/metabase/models/dashboard_card_test.clj @@ -33,19 +33,11 @@ :col nil :row nil :series []} - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] + (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}] - (-> (retrieve-dashboard-card dashcard-id) - remove-ids-and-timestamps))))) + (remove-ids-and-timestamps (retrieve-dashboard-card dashcard-id)))))) ;; retrieve-dashboard-card ;; dashcard w/ additional series @@ -56,45 +48,22 @@ :row nil :series [{:name "Additional Series Card 1" :description nil - :display :scalar - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}} + :display :table + :dataset_query {} + :visualization_settings {}} {:name "Additional Series Card 2" :description nil - :display :scalar - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}]} - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (tu/with-temp Card [{series-id1 :id} {:name "Additional Series Card 1" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (tu/with-temp Card [{series-id2 :id} {:name "Additional Series Card 2" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (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-id1 - :position 0}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id - :card_id series-id2 - :position 1}] - (-> (retrieve-dashboard-card dashcard-id) - remove-ids-and-timestamps))))))))) + :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)))))))))) ;; update-dashboard-card-series @@ -104,45 +73,24 @@ ["card2"] ["card2" "card1"] ["card1" "card3"]] - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:creator_id (user->id :rasta) - :name "Test Card" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] - (tu/with-temp DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id - :card_id card-id}] - (tu/with-temp Card [{card-id1 :id} {:creator_id (user->id :rasta) - :name "card1" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] - (tu/with-temp Card [{card-id2 :id} {:creator_id (user->id :rasta) - :name "card2" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] - (tu/with-temp Card [{card-id3 :id} {:creator_id (user->id :rasta) - :name "card3" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] - (let [upd-series (fn [series] - (update-dashboard-card-series {:id dashcard-id} series) - (->> (db/sel :many :field [DashboardCardSeries :card_id] :dashboardcard_id dashcard-id) - (mapv (fn [card_id] - (db/sel :one :field [Card :name] :id card_id)))))] - [(upd-series []) - (upd-series [card-id1]) - (upd-series [card-id2]) - (upd-series [card-id2 card-id1]) - (upd-series [card-id1 card-id3])])))))))) + (tu/with-temp* [Dashboard [{dashboard-id :id} {:name "Test Dashboard" + :public_perms 0 + :creator_id (user->id :rasta)}] + Card [{card-id :id}] + DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id, :card_id card-id}] + Card [{card-id-1 :id} {:name "card1"}] + Card [{card-id-2 :id} {:name "card2"}] + Card [{card-id3 :id} {:name "card3"}]] + (let [upd-series (fn [series] + (update-dashboard-card-series {:id dashcard-id} series) + (->> (db/sel :many :field [DashboardCardSeries :card_id] :dashboardcard_id dashcard-id) + (mapv (fn [card_id] + (db/sel :one :field [Card :name] :id card_id)))))] + [(upd-series []) + (upd-series [card-id-1]) + (upd-series [card-id-2]) + (upd-series [card-id-2 card-id-1]) + (upd-series [card-id-1 card-id3])]))) ;; create-dashboard-card @@ -155,7 +103,7 @@ :series [{:name "Test Card" :description nil :display :table - :dataset_query {:type "native"} + :dataset_query {} :visualization_settings {}}]} {:sizeX 4 :sizeY 3 @@ -164,17 +112,10 @@ :series [{:name "Test Card" :description nil :display :table - :dataset_query {:type "native"} + :dataset_query {} :visualization_settings {}}]}] - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:creator_id (user->id :rasta) - :name "Test Card" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :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 @@ -205,13 +146,13 @@ :row 1 :series [{:name "Test Card 2" :description nil - :display :bar - :dataset_query {:type "native"} + :display :table + :dataset_query {} :visualization_settings {}} {:name "Test Card 1" :description nil :display :table - :dataset_query {:type "native"} + :dataset_query {} :visualization_settings {}}]} {:sizeX 4 :sizeY 3 @@ -219,37 +160,19 @@ :row 1 :series [{:name "Test Card 2" :description nil - :display :bar - :dataset_query {:type "native"} + :display :table + :dataset_query {} :visualization_settings {}} {:name "Test Card 1" :description nil :display :table - :dataset_query {:type "native"} + :dataset_query {} :visualization_settings {}}]}] - (tu/with-temp Dashboard [{dashboard-id :id} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:creator_id (user->id :rasta) - :name "Test Card" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] - (tu/with-temp DashboardCard [{dashcard-id :id} {:dashboard_id dashboard-id - :card_id card-id}] - (tu/with-temp Card [{card-id1 :id} {:creator_id (user->id :rasta) - :name "Test Card 1" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] - (tu/with-temp Card [{card-id2 :id} {:creator_id (user->id :rasta) - :name "Test Card 2" - :display :bar - :public_perms 0 - :dataset_query {:type :native} - :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 @@ -262,5 +185,5 @@ :sizeY 3 :row 1 :col 1 - :series [card-id2 card-id1]})) + :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 f4749cc5166897483f98e408e310bd9fa301e7a1..e4e34504bbcad629c65c9391f019993fa79aae96 100644 --- a/test/metabase/models/dashboard_test.clj +++ b/test/metabase/models/dashboard_test.clj @@ -1,15 +1,14 @@ (ns metabase.models.dashboard-test (:require [expectations :refer :all] [metabase.db :as db] - [metabase.models.card :refer [Card]] - [metabase.models.dashboard :refer :all] - [metabase.models.dashboard-card :refer [DashboardCard]] - [metabase.models.dashboard-card-series :refer [DashboardCardSeries]] - [metabase.models.hydrate :refer :all] + (metabase.models [card :refer [Card]] + [dashboard :refer :all] + [dashboard-card :refer [DashboardCard], :as dashboard-card] + [dashboard-card-series :refer [DashboardCardSeries]] + [hydrate :refer :all]) [metabase.test.data :refer :all] [metabase.test.data.users :refer :all] - [metabase.test.util :as tu] - [metabase.models.dashboard-card :as dashboard-card])) + [metabase.test.util :as tu])) ;; ## Dashboard Revisions @@ -25,40 +24,18 @@ :id true :card_id true :series true}]} - (tu/with-temp Dashboard [{dashboard-id :id :as dashboard} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (tu/with-temp Card [{series-id1 :id} {:name "Additional Series Card 1" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (tu/with-temp Card [{series-id2 :id} {:name "Additional Series Card 2" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (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-id1 - :position 0}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id - :card_id series-id2 - :position 1}] - (update-in (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-id1 series-id2] series))])))))))))) + (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))])))))))))) ;; diff-dashboards-str @@ -157,51 +134,29 @@ :id false :card_id true :series true}]}] - (tu/with-temp Dashboard [{dashboard-id :id :as dashboard} {:name "Test Dashboard" - :public_perms 0 - :creator_id (user->id :rasta)}] - (tu/with-temp Card [{card-id :id} {:name "Dashboard Test Card" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (tu/with-temp Card [{series-id1 :id} {:name "Additional Series Card 1" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (tu/with-temp Card [{series-id2 :id} {:name "Additional Series Card 2" - :creator_id (user->id :rasta) - :public_perms 0 - :display "scalar" - :dataset_query {:something "simple"} - :visualization_settings {:global {:title nil}}}] - (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-id1 - :position 0}] - (tu/with-temp DashboardCardSeries [_ {:dashboardcard_id dashcard-id - :card_id series-id2 - :position 1}] + (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-id1 series-id2] series))]) + :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") + :name "Revert Test" + :description "something") ;; capture our updated dashboard state - (let [serialized-dashboard2 (serialize-dashboard (db/sel :one Dashboard :id dashboard-id))] + (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 (db/sel :one Dashboard :id dashboard-id)) :cards check-ids)])))))))))) + (update (serialize-dashboard (Dashboard dashboard-id)) :cards check-ids)])))))))))) diff --git a/test/metabase/models/field_values_test.clj b/test/metabase/models/field_values_test.clj index 5d510d4e29b466ae38cd530cefbe69e723373b23..da8acd388df23e0ea527118528d510692cdcdd47 100644 --- a/test/metabase/models/field_values_test.clj +++ b/test/metabase/models/field_values_test.clj @@ -47,20 +47,9 @@ [[1,2,3] {:status 204, :body nil} nil] - (tu/with-temp Database [{database-id :id} {:name "FieldValues Test" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "FieldValues Test" - :db_id database-id - :active true}] - (tu/with-temp Field [{field-id :id} {:table_id table-id - :name "FieldValues Test" - :base_type :TextField - :field_type :info - :active true - :preview_display true - :position 1}] + (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) diff --git a/test/metabase/models/metric_test.clj b/test/metabase/models/metric_test.clj index d2a10805ce6fd4fd2e6051dca836c98b21598d77..2d70cf84eca1a980354cff70ed30550ce698e8a7 100644 --- a/test/metabase/models/metric_test.clj +++ b/test/metabase/models/metric_test.clj @@ -39,13 +39,8 @@ :description nil :is_active true :definition {:clause ["a" "b"]}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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"]})))) @@ -53,13 +48,8 @@ (expect [true false] - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -79,13 +69,8 @@ :is_active true :definition {:database 45 :query {:filter ["yay"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -106,16 +91,9 @@ :description nil :is_active true :definition {}}] - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id1 :id} {:name "Table 1" - :db_id database-id - :active true}] - (tu/with-temp Table [{table-id2 :id} {:name "Table 2" - :db_id database-id - :active true}] + (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" @@ -148,51 +126,41 @@ (expect {:creator_id (user->id :rasta) :creator (user-details :rasta) - :name "Tatooine" + :name "Costa Rica" :description nil :is_active true :definition {:database 2 - :query {:filter ["not" "the droids you're looking for"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + :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 "Droids in the desert" - :description "Lookin' for a jedi" + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :definition {}}] (update-metric-then-select {:id id - :name "Tatooine" + :name "Costa Rica" :description nil :creator_id (user->id :crowberto) :table_id 456 :definition {:database 2 - :query {:filter ["not" "the droids you're looking for"]}} + :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) - :name "Droids in the desert" - :description "Lookin' for a jedi" + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active false :definition {}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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 "Droids in the desert" - :description "Lookin' for a jedi" + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :definition {}}] (delete-metric id (user->id :crowberto) "revision message") (metric-details (retrieve-metric id)))))) @@ -207,24 +175,19 @@ {:id true :table_id true :creator_id (user->id :rasta) - :name "Droids in the desert" - :description "Lookin' for a jedi" + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :definition {:aggregation ["count"] - :filter ["AND",[">",4,"2014-10-19"]]} + :filter ["AND" [">" 4 "2014-10-19"]]} :is_active true} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :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 "Droids in the desert" - :description "Lookin' for a jedi" + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :definition {:aggregation ["count"] - :filter ["AND",[">",4,"2014-10-19"]]}}] + :filter ["AND" [">" 4 "2014-10-19"]]}}] (-> (serialize-metric Metric (:id metric) metric) (update :id boolean) (update :table_id boolean)))))) @@ -234,25 +197,20 @@ (expect {:definition {:before {:filter ["AND" [">" 4 "2014-10-19"]]} :after {:filter ["AND" ["BETWEEN" 4 "2014-07-01" "2014-10-19"]]}} - :description {:before "Lookin' for a jedi" + :description {:before "Lookin' for a blueberry" :after "BBB"} - :name {:before "Droids in the desert" + :name {:before "Toucans in the rainforest" :after "Something else"}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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 "Droids in the desert" - :description "Lookin' for a jedi" - :definition {:filter ["AND",[">",4,"2014-10-19"]]}}] + :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"]]})))))) + :definition {:filter ["AND" ["BETWEEN" 4 "2014-07-01" "2014-10-19"]]})))))) ;; test case where definition doesn't change (expect @@ -261,33 +219,33 @@ (diff-metrics Metric {: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 (expect {:name {:after "A"} :description {:after "Unchanged"} - :definition {:after {:filter ["AND",[">",4,"2014-10-19"]]}}} + :definition {:after {:filter ["AND" [">" 4 "2014-10-19"]]}}} (diff-metrics Metric 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-metrics Metric {: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/pulse_test.clj b/test/metabase/models/pulse_test.clj index 45cfc6e21ed96b7a979ff505add04b8571c20ddc..5ac1d399ec50ed000b8e133e6d7ad48148feb3cf 100644 --- a/test/metabase/models/pulse_test.clj +++ b/test/metabase/models/pulse_test.clj @@ -66,12 +66,7 @@ :emails ["foo@bar.com"]} :schedule_type :daily :schedule_hour 15}] - (tu/with-temp Card [{card-id :id} {:creator_id (user->id :rasta) - :name "Test Card" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] + (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)) @@ -84,55 +79,6 @@ (-> (dissoc channel :id :pulse_id :created_at :updated_at) (m/dissoc-in [:details :emails]))))))))))) -;; retrieve-pulses -;; some crappy problem with expectations where tests with vectors of maps are failing without explanation :( -;(expect -; [{:creator_id (user->id :crowberto) -; :creator (user-details :crowberto) -; :name "ABC" -; :public_perms 2 -; :cards [] -; :channels []} -; {:creator_id (user->id :rasta) -; :creator (user-details :rasta) -; :name "Loddi-doddi" -; :public_perms 2 -; :cards [{:name "Test Card", -; :description nil, -; :display "table"}] -; :channels [{:schedule_type :daily, -; :schedule_hour 15, -; :channel_type :email, -; :details {:other "stuff"}, -; :schedule_day nil, -; :recipients [{:email "foo@bar.com"} -; (dissoc (user-details :rasta) :is_superuser)]}]}] -; (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) -; :name "Loddi-doddi"}] -; (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} {:creator_id (user->id :rasta) -; :name "Test Card" -; :display :table -; :public_perms 0 -; :dataset_query {:type :native} -; :visualization_settings {}}] -; (tu/with-temp Pulse [{pulse2 :id} {:creator_id (user->id :crowberto) -; :name "ABC"}] -; (let [format-pulse (fn [{:keys [cards channels creator] :as pulse}] -; (-> 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 (mapv #(dissoc % :id :pulse_id :created_at :updated_at) channels))))] -; (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)) -; (mapv format-pulse (retrieve-pulses))))))))) ;; update-pulse-cards (expect @@ -143,34 +89,19 @@ ["card1" "card3"]] (tu/with-temp Pulse [{:keys [id]} {:creator_id (user->id :rasta) :name (tu/random-name)}] - (tu/with-temp Card [{card-id1 :id} {:creator_id (user->id :rasta) - :name "card1" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] - (tu/with-temp Card [{card-id2 :id} {:creator_id (user->id :rasta) - :name "card2" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] - (tu/with-temp Card [{card-id3 :id} {:creator_id (user->id :rasta) - :name "card3" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] + (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-id1]) - (upd-cards [card-id2]) - (upd-cards [card-id2 card-id1]) - (upd-cards [card-id1 card-id3])])))))) + (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 @@ -207,12 +138,7 @@ :cards [{:name "Test Card" :description nil :display "table"}]} - (tu/with-temp Card [{:keys [id]} {:creator_id (user->id :rasta) - :name "Test Card" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] + (tu/with-temp Card [{:keys [id]} {:name "Test Card"}] (create-pulse-then-select "Booyah!" (user->id :rasta) [id] [{:channel_type :email :schedule_type :daily :schedule_hour 18 @@ -245,22 +171,12 @@ (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-id1 :id} {:creator_id (user->id :rasta) - :name "Test Card" - :display :table - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] - (tu/with-temp Card [{card-id2 :id} {:creator_id (user->id :rasta) - :name "Bar Card" - :display :bar - :public_perms 0 - :dataset_query {:type :native} - :visualization_settings {}}] + (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-id2 card-id1] + :cards [card-id-2 card-id-1] :channels [{:channel_type :email :schedule_type :daily :schedule_hour 18 diff --git a/test/metabase/models/segment_test.clj b/test/metabase/models/segment_test.clj index 2955b43c50eea2346da28a57ae785cc843ebe21f..5a93709a836b9299c10756d4d088cffe653ddc1d 100644 --- a/test/metabase/models/segment_test.clj +++ b/test/metabase/models/segment_test.clj @@ -39,13 +39,8 @@ :description nil :is_active true :definition {:clause ["a" "b"]}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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"]})))) @@ -53,13 +48,8 @@ (expect [true false] - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -79,13 +69,8 @@ :is_active true :definition {:database 45 :query {:filter ["yay"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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" @@ -106,16 +91,9 @@ :description nil :is_active true :definition {}}] - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id1 :id} {:name "Table 1" - :db_id database-id - :active true}] - (tu/with-temp Table [{table-id2 :id} {:name "Table 2" - :db_id database-id - :active true}] + (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" @@ -148,51 +126,41 @@ (expect {:creator_id (user->id :rasta) :creator (user-details :rasta) - :name "Tatooine" + :name "Costa Rica" :description nil :is_active true :definition {:database 2 - :query {:filter ["not" "the droids you're looking for"]}}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + :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 "Droids in the desert" - :description "Lookin' for a jedi" + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :definition {}}] (update-segment-then-select {:id id - :name "Tatooine" + :name "Costa Rica" :description nil :creator_id (user->id :crowberto) :table_id 456 :definition {:database 2 - :query {:filter ["not" "the droids you're looking for"]}} + :query {:filter ["not" "the toucans you're looking for"]}} :revision_message "Just horsing around"}))))) ;; delete-segment (expect {:creator_id (user->id :rasta) :creator (user-details :rasta) - :name "Droids in the desert" - :description "Lookin' for a jedi" + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :is_active false :definition {}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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 "Droids in the desert" - :description "Lookin' for a jedi" + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :definition {}}] (delete-segment id (user->id :crowberto) "revision message") (segment-details (retrieve-segment id)))))) @@ -207,21 +175,16 @@ {:id true :table_id true :creator_id (user->id :rasta) - :name "Droids in the desert" - :description "Lookin' for a jedi" + :name "Toucans in the rainforest" + :description "Lookin' for a blueberry" :definition {:filter ["AND",[">",4,"2014-10-19"]]} :is_active true} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{table-id :id} {:name "Stuff" - :db_id database-id - :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 "Droids in the desert" - :description "Lookin' for a jedi" + :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) @@ -232,21 +195,16 @@ (expect {:definition {:before {:filter ["AND" [">" 4 "2014-10-19"]]} :after {:filter ["AND" ["BETWEEN" 4 "2014-07-01" "2014-10-19"]]}} - :description {:before "Lookin' for a jedi" + :description {:before "Lookin' for a blueberry" :after "BBB"} - :name {:before "Droids in the desert" + :name {:before "Toucans in the rainforest" :after "Something else"}} - (tu/with-temp Database [{database-id :id} {:name "Hillbilly" - :engine :yeehaw - :details {} - :is_sample false}] - (tu/with-temp Table [{:keys [id]} {:name "Stuff" - :db_id database-id - :active true}] + (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 "Droids in the desert" - :description "Lookin' for a jedi" + :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" diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.clj index c700284021c27d73c4e486ad026b3198ccf43e7b..90529e248b865227bf76698b74f12767b76d2d1f 100644 --- a/test/metabase/test/util.clj +++ b/test/metabase/test/util.clj @@ -4,7 +4,13 @@ [expectations :refer :all] [medley.core :as m] (metabase [db :refer :all] - [util :as u]))) + [util :as u]) + (metabase.models [card :refer [Card]] + [common :as common] + [dashboard :refer [Dashboard]] + [database :refer [Database]] + [field :refer [Field]] + [table :refer [Table]]))) (declare $->prop) @@ -73,9 +79,45 @@ (->> (repeatedly 20 #(-> (rand-int 26) (+ (int \A)) char)) (apply str))) - -;; ## with-temp -;; +(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)})}) + + +(defn do-with-temp + "Internal implementation of `with-temp` (don't call this directly)." + [entity attributes f] + (let [temp-object (m/mapply ins entity (merge (with-temp-defaults entity) + attributes))] + (try + (f temp-object) + (finally + (cascade-delete entity :id (:id temp-object)))))) + + +;;; # with-temp (defmacro with-temp "Create a temporary instance of ENTITY bound to BINDING-FORM, execute BODY, then delete it via `cascade-delete`. @@ -93,15 +135,22 @@ :organization_id @org-id}] ...)" [entity [binding-form & [options-map]] & body] - `(let [object# (m/mapply ins ~entity ~options-map) - ~binding-form object# - delete-fn# (fn [] (cascade-delete ~entity :id (:id object#)))] - (let [result# (try (do ~@body) - (catch Throwable e# - (delete-fn#) - (throw e#)))] - (delete-fn#) - result#))) + `(do-with-temp ~entity ~options-map (fn [~binding-form] + ~@body))) + +(defmacro with-temp* + "Like `with-temp` but establishes multiple temporary objects at the same time. + + (with-temp* [Database [{database-id :id}] + Table [table {:db_id database-id}]] + ...)" + [entity-bindings & body] + (loop [[pair & more] (reverse (partition 2 entity-bindings)), body `(do ~@body)] + (let [body `(with-temp ~@pair + ~body)] + (if (seq more) + (recur more body) + body)))) ;; ## resolve-private-fns