Skip to content
Snippets Groups Projects
Commit 4c0fd545 authored by Cam Saül's avatar Cam Saül
Browse files

Merge pull request #477 from metabase/more_unit_test_format_improvements

additional improvements to failed unit test formatting
parents 832e02c4 81c46667
Branches
Tags
No related merge requests found
(ns metabase.test-setup
"Functions that run before + after unit tests (setup DB, start web server, load test data)."
(:require [clojure.java.io :as io]
[clojure.set :as set]
[clojure.tools.logging :as log]
[expectations :refer :all]
(metabase [core :as core]
......@@ -11,37 +12,58 @@
(declare clear-test-db)
;; # SETTINGS
;; # ---------------------------------------- EXPECTAIONS FRAMEWORK SETTINGS ------------------------------
;; ## GENERAL SETTINGS
;; Don't run unit tests whenever JVM shuts down
;; it's pretty annoying to have our DB reset all the time
(expectations/disable-run-on-shutdown)
;; ## EXPECTATIONS FORMATTING OVERRIDES
;; This overrides the methods Expectations usually uses for printing a failed test comparing two maps.
;; This is basically the same as the original implementation, but colorizes and pretty-prints the
;; These overrides the methods Expectations usually uses for printing failed tests.
;; These are basically the same as the original implementations, but they colorize and pretty-print the
;; output, which makes it an order of magnitude easier to read, especially for tests that compare a
;; lot of data, like Query Processor or API tests.
(defn- format-failure [e a str-e str-a]
{:type :fail
:expected-message (when-let [in-e (first (clojure.data/diff e a))]
(format "\nin expected, not actual:\n%s" (u/pprint-to-str 'green in-e)))
:actual-message (when-let [in-a (first (clojure.data/diff a e))]
(format "\nin actual, not expected:\n%s" (u/pprint-to-str 'red in-a)))
:raw [str-e str-a]
:result ["expected:\n"
(u/pprint-to-str 'green e)
"was:\n"
(u/pprint-to-str 'red a)]})
(defmethod compare-expr :expectations/maps [e a str-e str-a]
(let [[in-e in-a] (clojure.data/diff e a)]
(if (and (nil? in-e) (nil? in-a))
{:type :pass}
{:type :fail
:expected-message (some->> in-e
u/pprint-to-str
(u/format-color 'green "\nin expected, not actual:\n%s"))
:actual-message (some->> in-a
u/pprint-to-str
(u/format-color 'red "\nin actual, not expected:\n%s"))
:raw [str-e str-a]
:result ["expected:\n"
(u/pprint-to-str 'green e)
"\n was:\n"
(u/pprint-to-str 'red a)]})))
;; # FUNCTIONS THAT GET RUN ON TEST SUITE START / STOP
(format-failure e a str-e str-a))))
(defmethod compare-expr :expectations/sets [e a str-e str-a]
(format-failure e a str-e str-a))
(defmethod compare-expr :expectations/sequentials [e a str-e str-a]
(let [diff-fn (fn [e a] (seq (set/difference (set e) (set a))))]
(assoc (format-failure e a str-e str-a)
:message (cond
(and (= (set e) (set a))
(= (count e) (count a))
(= (count e) (count (set a)))) "lists appear to contain the same items with different ordering"
(and (= (set e) (set a))
(< (count e) (count a))) "some duplicate items in actual are not expected"
(and (= (set e) (set a))
(> (count e) (count a))) "some duplicate items in expected are not actual"
(< (count e) (count a)) "actual is larger than expected"
(> (count e) (count a)) "expected is larger than actual"))))
;; # ------------------------------ FUNCTIONS THAT GET RUN ON TEST SUITE START / STOP ------------------------------
(defn load-test-datasets
"Call `load-data!` on all the datasets we're testing against."
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment