<commit_before>(ns metabase.test-utils
  (:require [clojure.java.io :as io]
            [clojure.tools.logging :as log]
            [expectations :refer :all]
            [ring.adapter.jetty :as ring]
            (metabase [core :as core]
                      [db :refer :all]
                      [test-data :refer :all])))

(declare set-test-logging-level
         setup-test-db
         start-jetty)

;; # 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)


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

(defn test-setup
  {:expectations-options :before-run}
  []
  (set-test-logging-level)
  (setup-test-db)
  (start-jetty))


;; ## Logging Setup

(defn set-test-logging-level
  "Disable debug logging since it clutters up our output."
  []
  (.setLevel (org.apache.log4j.Logger/getLogger "metabase") org.apache.log4j.Level/INFO))


;; ## DB Setup
;; WARNING: BY RUNNING ANY UNIT TESTS THAT REQUIRE THIS FILE OR BY RUNNING YOUR ENTIRE TEST SUITE YOU WILL EFFECTIVELY BE WIPING OUT YOUR DATABASE.
;; SETUP-DB DELETES YOUR DATABASE FILE, AND GETS RAN AUTOMATICALLY BY EXPECTATIONS. USE AT YOUR OWN RISK!

(defn setup-test-db
  "Setup database schema."
  []
  (let [filename (-> (re-find #"file:(\w+\.db).*" (db-file)) second)] ; db-file is prefixed with "file:", so we strip that off
    (map (fn [file-extension]                                         ; delete the database files, e.g. `metabase.db.h2.db`, `metabase.db.trace.db`, etc.
           (let [file (str filename file-extension)]
             (when (.exists (io/file file))
               (io/delete-file file))))
         [".h2.db"
          ".trace.db"
          ".lock.db"]))
  (log/info "tearing down database and resetting to empty schema")
  (migrate (setup-jdbc-db) :down)
  (log/info "setting up database and running all migrations")
  (setup-db :auto-migrate true)
  (log/info "database setup complete")<commit_msg>remove unneeded fn<commit_after>(ns metabase.test-utils
  (:require [clojure.java.io :as io]
            [clojure.tools.logging :as log]
            [expectations :refer :all]
            [ring.adapter.jetty :as ring]
            (metabase [core :as core]
                      [db :refer :all]
                      [test-data :refer :all])))

(declare set-test-logging-level
         setup-test-db
         start-jetty)

;; # 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)


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

(defn test-setup
  {:expectations-options :before-run}
  []
  ;; Disable debug logging since it clutters up our output
  (.setLevel (org.apache.log4j.Logger/getLogger "metabase") org.apache.log4j.Level/INFO)
  (setup-test-db)
  (start-jetty))

;; ## DB Setup
;; WARNING: BY RUNNING ANY UNIT TESTS THAT REQUIRE THIS FILE OR BY RUNNING YOUR ENTIRE TEST SUITE YOU WILL EFFECTIVELY BE WIPING OUT YOUR DATABASE.
;; SETUP-DB DELETES YOUR DATABASE FILE, AND GETS RAN AUTOMATICALLY BY EXPECTATIONS. USE AT YOUR OWN RISK!

(defn setup-test-db
  "Setup database schema."
  []
  (let [filename (-> (re-find #"file:(\w+\.db).*" (db-file)) second)] ; db-file is prefixed with "file:", so we strip that off
    (map (fn [file-extension]                                         ; delete the database files, e.g. `metabase.db.h2.db`, `metabase.db.trace.db`, etc.
           (let [file (str filename file-extension)]
             (when (.exists (io/file file))
               (io/delete-file file))))
         [".h2.db"
          ".trace.db"
          ".lock.db"]))
  (log/info "tearing down database and resetting to empty schema")
  (migrate (setup-jdbc-db) :down)
  (log/info "setting up database and running all migrations")
  (setup-db :auto-migrate true)
  (log/info "database setup complete")
{'fraction_non_alphanumeric': 0.12808759867583397, 'fraction_numerical': 0.002037178507766743, 'mean_word_length': 2.5102770330652366, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2986147', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1274, 'n_words': 394}