Skip to content

Commit

Permalink
Fix tests again after changes from b24b775
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul committed Mar 2, 2018
1 parent b24b775 commit c5be21f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 77 deletions.
34 changes: 18 additions & 16 deletions test/metabase/api/embed_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -516,20 +516,21 @@

;; should be able to fetch values for a Field referenced by a public Card
(expect
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]}
(with-embedding-enabled-and-temp-card-referencing :venues :name [card]
(-> (http/client :get 200 (field-values-url card (data/id :venues :name)))
(update :values (partial take 5)))))
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]
:field_id (data/id :venues :name)}
(with-embedding-enabled-and-temp-card-referencing :venues :name [card]
(-> (http/client :get 200 (field-values-url card (data/id :venues :name)))
(update :values (partial take 5)))))

;; but for Fields that are not referenced we should get an Exception
(expect
"Not found."
(with-embedding-enabled-and-temp-card-referencing :venues :name [card]
(http/client :get 400 (field-values-url card (data/id :venues :price)))))
"Not found."
(with-embedding-enabled-and-temp-card-referencing :venues :name [card]
(http/client :get 400 (field-values-url card (data/id :venues :price)))))

;; Endpoint should fail if embedding is disabled
(expect
Expand Down Expand Up @@ -569,11 +570,12 @@

;; should be able to use it when everything is g2g
(expect
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]}
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]
:field_id (data/id :venues :name)}
(with-embedding-enabled-and-temp-dashcard-referencing :venues :name [dashboard]
(-> (http/client :get 200 (field-values-url dashboard (data/id :venues :name)))
(update :values (partial take 5)))))
Expand Down
76 changes: 38 additions & 38 deletions test/metabase/api/field_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[field-values :refer [FieldValues]]
[table :refer [Table]]]
[metabase.test
[data :refer :all]
[data :as data]
[util :as tu]]
[metabase.test.data.users :refer :all]
[ring.util.codec :as codec]
Expand All @@ -18,7 +18,7 @@
;; Helper Fns

(defn- db-details []
(tu/match-$ (db)
(tu/match-$ (data/db)
{:created_at $
:engine "h2"
:caveats nil
Expand All @@ -38,13 +38,13 @@

;; ## GET /api/field/:id
(expect
(tu/match-$ (Field (id :users :name))
(tu/match-$ (Field (data/id :users :name))
{:description nil
:table_id (id :users)
:table_id (data/id :users)
:raw_column_id $
:fingerprint $
:fingerprint_version $
:table (tu/match-$ (Table (id :users))
:table (tu/match-$ (Table (data/id :users))
{:description nil
:entity_type nil
:visibility_type nil
Expand All @@ -56,8 +56,8 @@
:updated_at $
:entity_name nil
:active true
:id (id :users)
:db_id (id)
:id (data/id :users)
:db_id (data/id)
:caveats nil
:points_of_interest nil
:show_in_getting_started false
Expand All @@ -71,7 +71,7 @@
:updated_at $
:last_analyzed $
:active true
:id (id :users :name)
:id (data/id :users :name)
:visibility_type "normal"
:position 0
:preview_display true
Expand All @@ -83,13 +83,13 @@
:parent_id nil
:dimensions []
:name_field nil})
((user->client :rasta) :get 200 (format "field/%d" (id :users :name))))
((user->client :rasta) :get 200 (format "field/%d" (data/id :users :name))))


;; ## GET /api/field/:id/summary
(expect [["count" 75] ; why doesn't this come back as a dictionary ?
["distincts" 75]]
((user->client :rasta) :get 200 (format "field/%d/summary" (id :categories :name))))
((user->client :rasta) :get 200 (format "field/%d/summary" (data/id :categories :name))))


;; ## PUT /api/field/:id
Expand Down Expand Up @@ -157,41 +157,41 @@
(defn- field->field-values
"Fetch the `FieldValues` object that corresponds to a given `Field`."
[table-kw field-kw]
(FieldValues :field_id (id table-kw field-kw)))
(FieldValues :field_id (data/id table-kw field-kw)))

(defn- field-values-id [table-key field-key]
(:id (field->field-values table-key field-key)))

;; ## GET /api/field/:id/values
;; Should return something useful for a field that has special_type :type/Category
(expect
{:values [[1] [2] [3] [4]]}
{:values [[1] [2] [3] [4]], :field_id (data/id :venues :price)}
(do
;; clear out existing human_readable_values in case they're set
(db/update! FieldValues (field-values-id :venues :price)
:human_readable_values nil)
;; now update the values via the API
(tu/boolean-ids-and-timestamps ((user->client :rasta) :get 200 (format "field/%d/values" (id :venues :price))))))
((user->client :rasta) :get 200 (format "field/%d/values" (data/id :venues :price)))))

;; Should return nothing for a field whose special_type is *not* :type/Category
(expect
{:values []}
((user->client :rasta) :get 200 (format "field/%d/values" (id :venues :id))))
{:values [], :field_id (data/id :venues :id)}
((user->client :rasta) :get 200 (format "field/%d/values" (data/id :venues :id))))

;; Sensisitive fields do not have field values and should return empty
(expect
{:values []}
((user->client :rasta) :get 200 (format "field/%d/values" (id :users :password))))
{:values [], :field_id (data/id :users :password)}
((user->client :rasta) :get 200 (format "field/%d/values" (data/id :users :password))))

(def ^:private category-field {:name "Field Test" :base_type :type/Integer :special_type :type/Category})

;; ## POST /api/field/:id/values

;; Human readable values are optional
(expect
[{:values [[5] [6] [7] [8] [9]]}
[{:values [[5] [6] [7] [8] [9]], :field_id true}
{:status "success"}
{:values [[1] [2] [3] [4]]}]
{:values [[1] [2] [3] [4]], :field_id true}]
(tt/with-temp* [Field [{field-id :id} category-field]
FieldValues [{field-value-id :id} {:values (range 5 10), :field_id field-id}]]
(mapv tu/boolean-ids-and-timestamps
Expand All @@ -202,9 +202,9 @@

;; Existing field values can be updated (with their human readable values)
(expect
[{:values [[1] [2] [3] [4]]}
[{:values [[1] [2] [3] [4]], :field_id true}
{:status "success"}
{:values [[1 "$"] [2 "$$"] [3 "$$$"] [4 "$$$$"]]}]
{:values [[1 "$"] [2 "$$"] [3 "$$$"] [4 "$$$$"]], :field_id true}]
(tt/with-temp* [Field [{field-id :id} category-field]
FieldValues [{field-value-id :id} {:values (range 1 5), :field_id field-id}]]
(mapv tu/boolean-ids-and-timestamps
Expand All @@ -215,9 +215,9 @@

;; Field values are created when not present
(expect
[{:values []}
[{:values [], :field_id true}
{:status "success"}
{:values [[1 "$"] [2 "$$"] [3 "$$$"] [4 "$$$$"]]}]
{:values [[1 "$"] [2 "$$"] [3 "$$$"] [4 "$$$$"]], :field_id true}]
(tt/with-temp* [Field [{field-id :id} category-field]]
(mapv tu/boolean-ids-and-timestamps
[((user->client :crowberto) :get 200 (format "field/%d/values" field-id))
Expand All @@ -227,22 +227,22 @@

;; Can unset values
(expect
[{:values [[1] [2] [3] [4]]}
[{:values [[1] [2] [3] [4]], :field_id true}
{:status "success"}
{:values []}]
{:values [], :field_id true}]
(tt/with-temp* [Field [{field-id :id} category-field]
FieldValues [{field-value-id :id} {:values (range 1 5), :field_id field-id}]]
(mapv tu/boolean-ids-and-timestamps
[((user->client :crowberto) :get 200 (format "field/%d/values" field-id))
((user->client :crowberto) :post 200 (format "field/%d/values" field-id)
{:values []})
{:values [], :field_id true})
((user->client :crowberto) :get 200 (format "field/%d/values" field-id))])))

;; Can unset just human readable values
(expect
[{:values [[1 "$"] [2 "$$"] [3 "$$$"] [4 "$$$$"]]}
[{:values [[1 "$"] [2 "$$"] [3 "$$$"] [4 "$$$$"]], :field_id true}
{:status "success"}
{:values [[1] [2] [3] [4]]}]
{:values [[1] [2] [3] [4]], :field_id true}]
(tt/with-temp* [Field [{field-id :id} category-field]
FieldValues [{field-value-id :id} {:values (range 1 5), :field_id field-id
:human_readable_values ["$" "$$" "$$$" "$$$$"]}]]
Expand All @@ -258,8 +258,8 @@
(tt/with-temp* [Field [{field-id :id} {:name "Field Test" :base_type :type/Integer :special_type :type/Category}]]
((user->client :crowberto) :post 400 (format "field/%d/values" field-id)
{:values [[1 "$"] [2 "$$"] [3] [4]]})))

;; ## PUT /api/field/:id/dimension
, :field_id true
;; ## PUT /api/field/:id/dimensio, :field_id truen

(defn- dimension-for-field [field-id]
(-> (Field :id field-id)
Expand Down Expand Up @@ -306,13 +306,13 @@
;; test that we can do basic field update work, including unsetting some fields such as special-type
(expect
[[]
{:id true
:created_at true
:updated_at true
:type :external
:name "some dimension name"
{:id true
:created_at true
:updated_at true
:type :external
:name "some dimension name"
:human_readable_field_id true
:field_id true}]
:field_id true}]
(tt/with-temp* [Field [{field-id-1 :id} {:name "Field Test 1"}]
Field [{field-id-2 :id} {:name "Field Test 2"}]]
(let [before-creation (dimension-for-field field-id-1)
Expand All @@ -326,8 +326,8 @@
clojure.lang.ExceptionInfo
(tt/with-temp* [Field [{field-id-1 :id} {:name "Field Test 1"}]]
(create-dimension-via-API! field-id-1 {:name "some dimension name", :type "external"})))

;; Non-admin users can't update dimensions
, :field_id true
;; Non-admin users can't update dimension, :field_id trues
(expect
"You don't have permissions to do that."
(tt/with-temp* [Field [{field-id :id} {:name "Field Test 1"}]]
Expand Down
50 changes: 27 additions & 23 deletions test/metabase/api/public_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -504,22 +504,24 @@

;; We should be able to get values for a Field referenced by a Card
(expect
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]}
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]
:field_id (data/id :venues :name)}
(tt/with-temp Card [card (mbql-card-referencing :venues :name)]
(-> (public-api/card-and-field-id->values (u/get-id card) (data/id :venues :name))
(update :values (partial take 5)))))

;; SQL param field references should work just as well as MBQL field referenced
(expect
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]}
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]
:field_id (data/id :venues :name)}
(tt/with-temp Card [card (sql-card-referencing-venue-name)]
(-> (public-api/card-and-field-id->values (u/get-id card) (data/id :venues :name))
(update :values (partial take 5)))))
Expand Down Expand Up @@ -557,14 +559,15 @@

;; should be able to fetch values for a Field referenced by a public Card
(expect
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]}
(with-sharing-enabled-and-temp-card-referencing :venues :name [card]
(-> (http/client :get 200 (field-values-url card (data/id :venues :name)))
(update :values (partial take 5)))))
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]
:field_id (data/id :venues :name)}
(with-sharing-enabled-and-temp-card-referencing :venues :name [card]
(-> (http/client :get 200 (field-values-url card (data/id :venues :name)))
(update :values (partial take 5)))))

;; but for Fields that are not referenced we should get an Exception
(expect
Expand Down Expand Up @@ -603,11 +606,12 @@

;; should be able to use it when everything is g2g
(expect
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]}
{:values [["20th Century Cafe"]
["25°"]
["33 Taps"]
["800 Degrees Neapolitan Pizzeria"]
["BCD Tofu House"]]
:field_id (data/id :venues :name)}
(with-sharing-enabled-and-temp-dashcard-referencing :venues :name [dashboard]
(-> (http/client :get 200 (field-values-url dashboard (data/id :venues :name)))
(update :values (partial take 5)))))
Expand Down

0 comments on commit c5be21f

Please sign in to comment.