Skip to content
Snippets Groups Projects
Commit 729c9b68 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

tidy up the unit test lifecycle functions so that we just have a single setup...

tidy up the unit test lifecycle functions so that we just have a single setup & teardown function which reuse as much of the normal app setup process as possible.
parent 9c4303b4
Branches
Tags
No related merge requests found
......@@ -3,14 +3,11 @@
(: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]
[db :as db]
[test-data :refer :all])))
(declare load-test-data
setup-test-db
start-jetty)
(declare clear-test-db)
;; # SETTINGS
......@@ -24,57 +21,36 @@
(defn test-setup
{:expectations-options :before-run}
[]
;; Disable debug logging since it clutters up our output
(setup-test-db)
(load-test-data)
(start-jetty))
(log/info "Starting up Metabase unit test runner")
;; clear out any previous test data that's lying around
(clear-test-db)
;; setup the db and migrate up to current schema
(db/setup-db :auto-migrate true)
;; this causes the test data to be loaded
@test-db
;; startup test web server
(core/start-jetty))
(defn test-teardown
{:expectations-options :after-run}
[]
(log/info "Shutting down Metabase unit test runner")
(core/stop-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."
(defn- clear-test-db
"Delete the test db file if it's still lying around."
[]
(let [filename (-> (re-find #"file:(\w+\.db).*" (db-file)) second)] ; db-file is prefixed with "file:", so we strip that off
(let [filename (-> (re-find #"file:(\w+\.db).*" (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"))
(defn- load-test-data []
@test-db)
;; ## Jetty (Web) Server
(def ^:private jetty-instance
(atom nil))
(defn- start-jetty
"Start the Jetty web server."
[]
(log/info "STARTING THE JETTY SERVER...")
(when-not @jetty-instance
(try (->> (ring/run-jetty core/app {:port 3000
:join? false}) ; detach the thread
(reset! jetty-instance))
(catch java.net.BindException e ; assume server is already running if port's already bound
(log/warn "ALREADY RUNNING!"))))) ; e.g. if someone is running `lein ring server` locally. Tests should still work normally.
(defn stop-jetty
"Stop the Jetty web server."
{:expectations-options :after-run}
[]
(when @jetty-instance
(.stop ^org.eclipse.jetty.server.Server @jetty-instance)
(reset! jetty-instance nil)))
".lock.db"])))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment