<commit_before>(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.tools.logging :as log]
            [expectations :refer :all]
            (metabase [core :as core]
                      [db :as db]
                      [task :as task]
                      [util :as u])
            [metabase.test.data.datasets :as datasets]))

(declare clear-test-db)

;; # 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
;; 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.
(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

(defn load-test-datasets
  "Call `load-data!` on all the datasets we're testing against."
  []
  (doseq [dataset-name @datasets/test-dataset-names]
    (log/info (format "Loading test data: %s..." (name dataset-name)))
    (datasets/load-data! (datasets/dataset-name->dataset dataset-name))))

(defn test-startup
  {:expectations-options :before-run}
  []
  (log/info "Starting up Metabase unit test runner")

  ;; clear out any previous test data that's lying around
  (log/info "Clearing out test DB...")
  (clear-test-db)
  (log/info "Setting up test DB and running migrations...")
  (db/setup-db :auto-migrate true)

  ;; Load the test datasets
  (load-test-datasets)

  ;; startup test web server
  (core/start-jetty)<commit_msg>tweak failed test output a bit<commit_after>(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.tools.logging :as log]
            [expectations :refer :all]
            (metabase [core :as core]
                      [db :as db]
                      [task :as task]
                      [util :as u])
            [metabase.test.data.datasets :as datasets]))

(declare clear-test-db)

;; # 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
;; 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.
(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           ["\nexpected:\n"
                          (u/pprint-to-str 'green e)
                          "\nwas:\n"
                          (u/pprint-to-str 'red a)]})))


;; # FUNCTIONS THAT GET RUN ON TEST SUITE START / STOP

(defn load-test-datasets
  "Call `load-data!` on all the datasets we're testing against."
  []
  (doseq [dataset-name @datasets/test-dataset-names]
    (log/info (format "Loading test data: %s..." (name dataset-name)))
    (datasets/load-data! (datasets/dataset-name->dataset dataset-name))))

(defn test-startup
  {:expectations-options :before-run}
  []
  (log/info "Starting up Metabase unit test runner")

  ;; clear out any previous test data that's lying around
  (log/info "Clearing out test DB...")
  (clear-test-db)
  (log/info "Setting up test DB and running migrations...")
  (db/setup-db :auto-migrate true)

  ;; Load the test datasets
  (load-test-datasets)

  ;; startup test web server
  (core/start-jetty)
{'fraction_non_alphanumeric': 0.12050256996002284, 'fraction_numerical': 0.0, 'mean_word_length': 2.070718877849211, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7561870', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1569, 'n_words': 525}