Skip to content
Snippets Groups Projects
Commit ae2467dc authored by Simon Belak's avatar Simon Belak
Browse files

Add api endpoint tests. Fix a thereby uncoverd bug

parent 16324b37
No related branches found
No related tags found
No related merge requests found
(ns metabase.api.x-ray
(:refer-clojure :exclude [compare])
(:require [compojure.core :refer [GET POST]]
[metabase.api.common :as api]
[metabase.feature-extraction
[core :as fe]
[costs :as costs]]
[metabase.models
[card :refer [Card]]
[card :refer [Card] :as card]
[database :refer [Database] :as database]
[field :refer [Field]]
[metric :refer [Metric]]
......@@ -165,6 +166,17 @@
(api/read-check Segment segment-id)
field))
(api/defendpoint POST "/compare/table/:table-id/segment/:segment-id/field/:field"
"Get comparison x-ray for field named `field` in table with ID
`table-id` and segment with ID `segment-id`."
[table-id segment-id field max_query_cost max_computation_cost]
{max_query_cost MaxQueryCost
max_computation_cost MaxComputationCost}
(compare-filtered-field (max-cost max_query_cost max_computation_cost)
(api/read-check Table table-id)
(api/read-check Segment segment-id)
field))
(api/defendpoint POST "/compare/card/:id/query"
"Get comparison x-ray of card and ad-hoc query."
[id max_query_cost max_computation_cost :as {query :body}]
......
......@@ -139,7 +139,12 @@
(defn- top-contributors
[comparisons]
(if (map? comparisons)
(if (:top-contributors comparisons)
(->> comparisons
:top-contributors
(map (fn [[feature difference]]
{:feature feature
:difference difference})))
(->> comparisons
(comparison/head-tails-breaks (comp :distance val))
(mapcat (fn [[field {:keys [top-contributors distance]}]]
......@@ -147,12 +152,7 @@
{:feature feature
:field field
:contribution (* (math/sqrt distance) difference)})))
(comparison/head-tails-breaks :contribution))
(->> comparisons
:top-contributors
(map (fn [[feature difference]]
{:feature feature
:difference difference})))))
(comparison/head-tails-breaks :contribution))))
(defn compare-features
"Compare feature vectors of two models."
......
(ns metabase.api.x-ray-test
"Tests for /api/x-ray endpoints"
(:require [expectations :refer :all]
[metabase.api.x-ray :refer :all]
[metabase.models
[card :refer [Card]]
[database :refer [Database] :as database]
[field :refer [Field]]
[segment :refer [Segment]]
[table :refer [Table]]]
[metabase.test.data :refer :all]
[metabase.test.data.users :refer :all]
[toucan.util.test :as tt]))
(expect
100.0
(-> ((user->client :rasta) :get 200 (str "x-ray/field/" (id :venues :price)))
:features
:count
:value))
(expect
100.0
(-> ((user->client :rasta) :get 200 (str "x-ray/table/" (id :venues)))
:constituents
:PRICE
:count
:value))
(def ^:private test-query
{:database (id)
:type :query
:query {:source_table (id :checkins)
:aggregation [[:count]]
:breakout [[:datetime-field [:field-id (id :checkins :date)] :month]]}
:parameters []})
(def ^:private test-card
{:name "test card"
:dataset_query test-query})
(def ^:private test-segment
{:name "test segment"
:table_id (id :venues)
:definition {:filter [:= [:field-id (id :venues :category_id)] 7]
:source_table (id :venues)}})
(expect
1000.0
(-> ((user->client :rasta) :post 200 "x-ray/query" test-query)
:constituents
:count
:sum
:value))
(tt/expect-with-temp [Card [{card-id :id} test-card]]
1000.0
(-> ((user->client :rasta) :get 200 (str "x-ray/card/" card-id))
:constituents
:count
:sum
:value))
(tt/expect-with-temp [Segment [{segment-id :id} test-segment]]
10.0
(-> ((user->client :rasta) :get 200 (str "x-ray/segment/" segment-id))
:constituents
:PRICE
:count
:value))
(expect
true
(:significant? ((user->client :rasta) :get 200
(format "x-ray/compare/fields/%s/%s"
(id :venues :longitude)
(id :venues :latitude)))))
(expect
false
(boolean (:significant? ((user->client :rasta) :get 200
(format "x-ray/compare/tables/%s/%s"
(id :venues)
(id :venues))))))
(tt/expect-with-temp [Segment [{segment1-id :id} test-segment]
Segment [{segment2-id :id}
(assoc-in test-segment [:definition :filter 2] 5)]]
true
(:significant? ((user->client :rasta) :get 200
(format "x-ray/compare/segments/%s/%s"
segment1-id
segment2-id))))
(tt/expect-with-temp [Segment [{segment-id :id} test-segment]]
true
(:significant? ((user->client :rasta) :get 200
(format "x-ray/compare/table/%s/segment/%s"
(id :venues)
segment-id))))
(expect
[true
false]
[(-> ((user->client :rasta) :post 200 "x-ray/query" test-query)
:features
:seasonal-decomposition
:value
:trend
:rows
count
pos?)
(-> ((user->client :rasta) :post 200 "x-ray/query?max_computation_cost=linear" test-query)
:features
:seasonal-decomposition
:value
:trend
:rows
count
pos?)])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment