2019-04-19 05:43:19 +00:00
|
|
|
;; copyright (c) 2019 Sean Corfield, all rights reserved
|
|
|
|
|
|
|
|
|
|
(ns next.jdbc.test-fixtures
|
2019-05-26 02:16:30 +00:00
|
|
|
"Multi-database testing fixtures."
|
2019-04-19 05:43:19 +00:00
|
|
|
(:require [next.jdbc :as jdbc]
|
2019-07-25 00:32:58 +00:00
|
|
|
[next.jdbc.sql :as sql])
|
|
|
|
|
(:import (com.opentable.db.postgres.embedded EmbeddedPostgres)
|
|
|
|
|
(javax.sql DataSource)))
|
2019-04-19 05:43:19 +00:00
|
|
|
|
2019-05-29 16:04:21 +00:00
|
|
|
(set! *warn-on-reflection* true)
|
|
|
|
|
|
2019-04-22 00:10:29 +00:00
|
|
|
(def ^:private test-derby {:dbtype "derby" :dbname "clojure_test_derby" :create true})
|
|
|
|
|
|
|
|
|
|
(def ^:private test-h2-mem {:dbtype "h2:mem" :dbname "clojure_test_h2_mem"})
|
|
|
|
|
|
|
|
|
|
(def ^:private test-h2 {:dbtype "h2" :dbname "clojure_test_h2"})
|
|
|
|
|
|
|
|
|
|
(def ^:private test-hsql {:dbtype "hsqldb" :dbname "clojure_test_hsqldb"})
|
|
|
|
|
|
|
|
|
|
(def ^:private test-sqlite {:dbtype "sqlite" :dbname "clojure_test_sqlite"})
|
|
|
|
|
|
2019-07-25 00:32:58 +00:00
|
|
|
;; this is just a dummy db-spec -- it's handled in with-test-db below
|
|
|
|
|
(def ^:private test-postgres {:dbtype "embedded-postgres"})
|
2019-11-11 01:53:23 +00:00
|
|
|
;; it takes a while to spin up so we kick it off at startup
|
|
|
|
|
(defonce embedded-pg (future (EmbeddedPostgres/start)))
|
2019-07-25 00:32:58 +00:00
|
|
|
|
|
|
|
|
(def ^:private test-db-specs
|
|
|
|
|
[test-derby test-h2-mem test-h2 test-hsql test-sqlite test-postgres])
|
2019-04-22 00:10:29 +00:00
|
|
|
|
|
|
|
|
(def ^:private test-db-spec (atom nil))
|
|
|
|
|
|
|
|
|
|
(defn derby? [] (= "derby" (:dbtype @test-db-spec)))
|
|
|
|
|
|
2019-07-25 00:32:58 +00:00
|
|
|
(defn postgres? [] (= "embedded-postgres" (:dbtype @test-db-spec)))
|
|
|
|
|
|
2019-04-22 00:10:29 +00:00
|
|
|
(defn sqlite? [] (= "sqlite" (:dbtype @test-db-spec)))
|
2019-04-19 05:43:19 +00:00
|
|
|
|
|
|
|
|
(def ^:private test-datasource (atom nil))
|
|
|
|
|
|
2019-10-02 16:19:31 +00:00
|
|
|
(defn db
|
|
|
|
|
"Tests should call this to get the db-spec to use inside a fixture."
|
|
|
|
|
[]
|
|
|
|
|
@test-db-spec)
|
|
|
|
|
|
2019-04-19 05:43:19 +00:00
|
|
|
(defn ds
|
|
|
|
|
"Tests should call this to get the DataSource to use inside a fixture."
|
|
|
|
|
[]
|
|
|
|
|
@test-datasource)
|
|
|
|
|
|
|
|
|
|
(defn with-test-db
|
|
|
|
|
"Given a test function (or suite), run it in the context of an in-memory
|
|
|
|
|
H2 database set up with a simple fruit table containing four rows of data.
|
|
|
|
|
|
|
|
|
|
Tests can reach into here and call ds (above) to get a DataSource for use
|
|
|
|
|
in test functions (that operate inside this fixture)."
|
|
|
|
|
[t]
|
2019-04-22 00:10:29 +00:00
|
|
|
(doseq [db test-db-specs]
|
|
|
|
|
(reset! test-db-spec db)
|
2019-07-25 00:32:58 +00:00
|
|
|
(if (= "embedded-postgres" (:dbtype db))
|
2019-11-11 01:53:23 +00:00
|
|
|
(reset! test-datasource
|
|
|
|
|
(.getPostgresDatabase ^EmbeddedPostgres @embedded-pg))
|
2019-07-25 00:32:58 +00:00
|
|
|
(reset! test-datasource (jdbc/get-datasource db)))
|
2019-04-22 00:10:29 +00:00
|
|
|
(let [auto-inc-pk
|
|
|
|
|
(cond (or (derby?) (= "hsqldb" (:dbtype db)))
|
|
|
|
|
(str "GENERATED ALWAYS AS IDENTITY"
|
|
|
|
|
" (START WITH 1, INCREMENT BY 1)"
|
|
|
|
|
" PRIMARY KEY")
|
2019-07-25 00:32:58 +00:00
|
|
|
(postgres?)
|
|
|
|
|
(str "GENERATED ALWAYS AS IDENTITY"
|
|
|
|
|
" PRIMARY KEY")
|
2019-04-22 00:10:29 +00:00
|
|
|
(sqlite?)
|
|
|
|
|
"PRIMARY KEY AUTOINCREMENT"
|
|
|
|
|
:else
|
|
|
|
|
"AUTO_INCREMENT PRIMARY KEY")]
|
|
|
|
|
(with-open [con (jdbc/get-connection (ds))]
|
|
|
|
|
(try
|
|
|
|
|
(jdbc/execute-one! con ["DROP TABLE FRUIT"])
|
|
|
|
|
(catch Exception _))
|
|
|
|
|
(jdbc/execute-one! con [(str "
|
|
|
|
|
CREATE TABLE FRUIT (
|
|
|
|
|
ID INTEGER " auto-inc-pk ",
|
|
|
|
|
NAME VARCHAR(32),
|
2019-05-26 02:16:30 +00:00
|
|
|
APPEARANCE VARCHAR(32) DEFAULT NULL,
|
|
|
|
|
COST INT DEFAULT NULL,
|
|
|
|
|
GRADE REAL DEFAULT NULL
|
2019-04-22 00:10:29 +00:00
|
|
|
)")])
|
|
|
|
|
(sql/insert-multi! con :fruit
|
|
|
|
|
[:name :appearance :cost :grade]
|
2019-05-26 02:16:30 +00:00
|
|
|
[["Apple" "red" 59 nil]
|
|
|
|
|
["Banana" "yellow" nil 92.2]
|
|
|
|
|
["Peach" nil 139 90.0]
|
|
|
|
|
["Orange" "juicy" 89 88.6]]
|
2019-04-22 00:10:29 +00:00
|
|
|
{:return-keys false})
|
|
|
|
|
(t)))))
|