Back
with-test (clj)
(source)macro
(with-test definition & body)
Takes any definition form (that returns a Var) as the first argument.
Remaining body goes in the :test metadata function for that Var.
When *load-tests* is false, only evaluates the definition, ignoring
the tests.
Examples
metabase/metabase
(ns metabase.test.transforms
(:require
[clojure.test :refer :all]
[metabase.transforms.specs :as tf.specs]))
(defmacro with-test-transform-specs
"Evaluate `body` in a context where `transforms.specs/transform-specs` have been swapped for `test-transform-specs`"
[& body]
`(testing "with-test-transform-specs\n"
(with-redefs [tf.specs/transform-specs (delay [test-transform-spec])]
~@body)))
metabase/metabase
(ns metabase.transforms.core-test
(:require
[clojure.test :refer :all]
[medley.core :as m]
[metabase.domain-entities.core :as de]
[metabase.domain-entities.specs :as de.specs]
[metabase.models.card :as card :refer [Card]]
[metabase.models.collection :refer [Collection]]
[metabase.models.interface :as mi]
[metabase.models.table :as table :refer [Table]]
[metabase.query-processor :as qp]
[metabase.test :as mt]
[metabase.test.domain-entities :refer [with-test-domain-entity-specs]]
[metabase.test.transforms :refer [test-transform-spec with-test-transform-specs]]
[metabase.transforms.core :as tf]
[metabase.transforms.specs :as tf.specs]
[metabase.util :as u]
[toucan2.core :as t2]
[toucan2.tools.with-temp :as t2.with-temp]))
(def ^:private test-bindings
(delay
(with-test-domain-entity-specs
(let [table (m/find-first (comp #{(mt/id :venues)} u/the-id) (#'tf/tableset (mt/id) "PUBLIC"))]
{"Venues" {:dimensions (m/map-vals de/mbql-reference (get-in table [:domain_entity :dimensions]))
:entity table}}))))
(deftest find-tables-with-domain-entity-test
(with-test-domain-entity-specs
(testing "Can we filter a tableset by domain entity?"
(is (= [(mt/id :venues)]
(map u/the-id (#'tf/find-tables-with-domain-entity (#'tf/tableset (mt/id) "PUBLIC")
(@de.specs/domain-entity-specs "Venues"))))))
(testing "Gracefully handle no-match"
(with-test-domain-entity-specs
(is (= nil
(not-empty
(#'tf/find-tables-with-domain-entity [] (@de.specs/domain-entity-specs "Venues")))))))))
(deftest resulting-entities-test
(testing "Can we extract results from the final bindings?"
(with-test-transform-specs
(is (= [(mt/id :venues)]
(map u/the-id (#'tf/resulting-entities {"VenuesEnhanced" {:entity (t2/select-one Table :id (mt/id :venues))
:dimensions {"D1" [:field 1 nil]}}}
(first @tf.specs/transform-specs))))))))
(deftest tables-matching-requirements-test
(testing "Can we find a table set matching requirements of a given spec?"
(with-test-transform-specs
(with-test-domain-entity-specs
(is (= [(mt/id :venues)]
(map u/the-id (#'tf/tables-matching-requirements (#'tf/tableset (mt/id) "PUBLIC")
(first @tf.specs/transform-specs)))))))))
(deftest tableset->bindings-test
(testing "Can we turn a tableset into corresponding bindings?"
(with-test-domain-entity-specs
(is (= @test-bindings
(#'tf/tableset->bindings (filter (comp #{(mt/id :venues)} u/the-id) (#'tf/tableset (mt/id) "PUBLIC"))))))))
(deftest validation-test
(with-test-domain-entity-specs
(with-test-transform-specs
(testing "Is the validation of results working?"
(is (#'tf/validate-results {"VenuesEnhanced" {:entity (mi/instance
Card
{:result_metadata [{:name "AvgPrice"}
{:name "MaxPrice"}
{:name "MinPrice"}]})
:dimensions {"D1" [:field 1 nil]}}}
(first @tf.specs/transform-specs))))
(deftest transform-test
(testing "Run the transform and make sure it produces the correct result"
(mt/with-test-user :rasta
(with-test-domain-entity-specs
(is (= [[1 "Red Medicine" 4 10.065 -165.374 3 1.5 4 3 2 1]
[2 "Stout Burgers & Beers" 11 34.1 -118.329 2 1.1 11 2 1 1]
[3 "The Apple Pan" 11 34.041 -118.428 2 1.1 11 2 1 1]]
(mt/formatted-rows [int str int 3.0 3.0 int 1.0 int int int int]
(-> (tf/apply-transform! (mt/id) "PUBLIC" test-transform-spec)
first
:dataset_query
qp/process-query))))))))
(deftest correct-transforms-for-table-test
(testing "Can we find the right transform(s) for a given table"
(with-test-transform-specs
(with-test-domain-entity-specs
(is (= "Test transform"
(-> (tf/candidates (t2/select-one Table :id (mt/id :venues)))
first
:name)))))))
metabase/metabase
(ns metabase.test.domain-entities
(:require
[clojure.test :refer :all]
[metabase.domain-entities.specs :as de.specs]))
(defmacro with-test-domain-entity-specs
"Evaluate `body` in a context where `domain-entities.specs/domain-entity-specs` have been swapped for
`test-domain-entity-specs`"
[& body]
`(testing "with-test-domain-entity-specs\n"
(with-redefs [de.specs/domain-entity-specs (delay test-domain-entity-specs)]
~@body)))
metabase/metabase
(ns metabase.pulse.render.color-test
(:require
[clojure.test :refer :all]
[metabase.pulse.render.color :as color]
[metabase.pulse.render.js-engine :as js]))
(defmacro ^:private with-test-js-engine
"Setup a javascript engine with a stubbed script useful making sure `get-background-color` works independently from
the real color picking script"
[script & body]
`(with-redefs [color/js-engine (let [delay# (delay (doto (js/context)
(js/load-js-string ~script ~(name (gensym "color-src")))))]
(fn [] @delay#))]
~@body))
(deftest color-test
(testing "The test script above should return red on even rows, green on odd rows"
(with-test-js-engine test-script
(let [color-selector (color/make-color-selector {:cols [{:name "test"}]
:rows [[1] [2] [3] [4]]}
{"even" red, "odd" green})]
(is (= [red green red green]
(for [row-index (range 0 4)]
(color/get-background-color color-selector "any value" "any column" row-index))))))))
(deftest convert-keywords-test
(testing (str "Same test as above, but make sure we convert any keywords as keywords don't get converted to "
"strings automatically when passed to a JavaScript function")
(with-test-js-engine test-script
(let [color-selector (color/make-color-selector {:cols [{:name "test"}]
:rows [[1] [2] [3] [4]]}
{:even red, :odd green})]
(is (= [red green red green]
(for [row-index (range 0 4)]
(color/get-background-color color-selector "any value" "any column" row-index))))))))
metabase/metabase
(ns ^:mb/once metabase.util.embed-test
(:require
[buddy.sign.jwt :as jwt]
[clojure.test :refer :all]
[crypto.random :as crypto-random]
[metabase.config :as config]
[metabase.public-settings.premium-features :as premium-features]
[metabase.public-settings.premium-features-test
:as premium-features-test]
[metabase.test :as mt]
[metabase.util.embed :as embed]))
(deftest show-static-embed-terms-test
(mt/with-test-user :crowberto
(mt/with-temporary-setting-values [show-static-embed-terms nil]
(testing "Check if the user needs to accept the embedding licensing terms before static embedding"
(when-not config/ee-available?
(testing "should return true when user is OSS and has not accepted licensing terms"
(is (= (embed/show-static-embed-terms) true)))
(testing "should return false when user is OSS and has already accepted licensing terms"
(embed/show-static-embed-terms! false)
(is (= (embed/show-static-embed-terms) false))))
(when config/ee-available?
(testing "should return false when an EE user has a valid token"
(with-redefs [premium-features/fetch-token-status (fn [_x]
{:valid true
:status "fake"
:features ["test" "fixture"]
:trial false})]
(mt/with-temporary-setting-values [premium-embedding-token premium-features-test/random-fake-token]
(is (= (embed/show-static-embed-terms) false))
(embed/show-static-embed-terms! false)
(is (= (embed/show-static-embed-terms) false)))))
(testing "when an EE user doesn't have a valid token"
(mt/with-temporary-setting-values [premium-embedding-token nil show-static-embed-terms nil]
(testing "should return true when the user has not accepted licensing terms"
(is (= (embed/show-static-embed-terms) true)))
(testing "should return false when the user has already accepted licensing terms"
(embed/show-static-embed-terms! false)
(is (= (embed/show-static-embed-terms) false))))))))))