2019-04-19 05:43:27 +00:00
|
|
|
;; copyright (c) 2019 Sean Corfield, all rights reserved
|
|
|
|
|
|
2019-01-08 04:38:58 +00:00
|
|
|
(ns next.jdbc-test
|
2019-04-01 06:17:12 +00:00
|
|
|
"Not exactly a test suite -- more a series of examples."
|
2019-05-01 23:21:23 +00:00
|
|
|
(:require [clojure.test :refer [deftest is testing use-fixtures]]
|
2019-04-20 05:51:48 +00:00
|
|
|
[next.jdbc :as jdbc]
|
|
|
|
|
[next.jdbc.test-fixtures :refer [with-test-db ds]]
|
|
|
|
|
[next.jdbc.prepare :as prep]
|
2019-05-22 04:45:05 +00:00
|
|
|
[next.jdbc.result-set :as rs]
|
|
|
|
|
[next.jdbc.specs :as specs])
|
2019-04-18 15:13:16 +00:00
|
|
|
(:import (java.sql ResultSet ResultSetMetaData)))
|
2019-01-08 04:38:58 +00:00
|
|
|
|
2019-05-29 16:04:21 +00:00
|
|
|
(set! *warn-on-reflection* true)
|
|
|
|
|
|
2019-05-01 23:21:23 +00:00
|
|
|
(use-fixtures :once with-test-db)
|
|
|
|
|
|
2019-05-22 04:45:05 +00:00
|
|
|
(specs/instrument)
|
|
|
|
|
|
2019-04-20 05:51:48 +00:00
|
|
|
(deftest basic-tests
|
2019-05-22 23:22:14 +00:00
|
|
|
(testing "plan"
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (= "Apple"
|
|
|
|
|
(reduce (fn [_ row] (reduced (:name row)))
|
|
|
|
|
nil
|
2019-05-22 23:22:14 +00:00
|
|
|
(jdbc/plan
|
2019-04-20 05:51:48 +00:00
|
|
|
(ds)
|
|
|
|
|
["select * from fruit where appearance = ?" "red"])))))
|
|
|
|
|
(testing "execute-one!"
|
|
|
|
|
(is (= "Apple" (:FRUIT/NAME
|
|
|
|
|
(jdbc/execute-one!
|
|
|
|
|
(ds)
|
|
|
|
|
["select * from fruit where appearance = ?" "red"])))))
|
|
|
|
|
(testing "execute!"
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
(ds)
|
|
|
|
|
["select * from fruit where appearance = ?" "red"])]
|
|
|
|
|
(is (= 1 (count rs)))
|
|
|
|
|
(is (= 1 (:FRUIT/ID (first rs)))))
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
(ds)
|
|
|
|
|
["select * from fruit order by id"]
|
2019-04-24 21:22:35 +00:00
|
|
|
{:builder-fn rs/as-maps})]
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 4 (count rs)))
|
|
|
|
|
(is (= 1 (:FRUIT/ID (first rs))))
|
|
|
|
|
(is (= 4 (:FRUIT/ID (last rs)))))
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
(ds)
|
|
|
|
|
["select * from fruit order by id"]
|
2019-04-24 21:22:35 +00:00
|
|
|
{:builder-fn rs/as-arrays})]
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (every? vector? rs))
|
|
|
|
|
(is (= 5 (count rs)))
|
|
|
|
|
(is (every? #(= 5 (count %)) rs))
|
|
|
|
|
;; columns come first
|
|
|
|
|
(is (every? qualified-keyword? (first rs)))
|
|
|
|
|
;; :FRUIT/ID should be first column
|
|
|
|
|
(is (= :FRUIT/ID (ffirst rs)))
|
|
|
|
|
;; and all its corresponding values should be ints
|
|
|
|
|
(is (every? int? (map first (rest rs))))
|
|
|
|
|
(is (every? string? (map second (rest rs)))))
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
(ds)
|
|
|
|
|
["select * from fruit order by id"]
|
2019-04-24 21:22:35 +00:00
|
|
|
{:builder-fn rs/as-unqualified-maps})]
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 4 (count rs)))
|
|
|
|
|
(is (= 1 (:ID (first rs))))
|
|
|
|
|
(is (= 4 (:ID (last rs)))))
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
(ds)
|
|
|
|
|
["select * from fruit order by id"]
|
2019-04-24 21:22:35 +00:00
|
|
|
{:builder-fn rs/as-unqualified-arrays})]
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (every? vector? rs))
|
|
|
|
|
(is (= 5 (count rs)))
|
|
|
|
|
(is (every? #(= 5 (count %)) rs))
|
|
|
|
|
;; columns come first
|
|
|
|
|
(is (every? simple-keyword? (first rs)))
|
|
|
|
|
;; :ID should be first column
|
|
|
|
|
(is (= :ID (ffirst rs)))
|
|
|
|
|
;; and all its corresponding values should be ints
|
|
|
|
|
(is (every? int? (map first (rest rs))))
|
|
|
|
|
(is (every? string? (map second (rest rs))))))
|
|
|
|
|
(testing "prepare"
|
2019-07-09 03:48:56 +00:00
|
|
|
(let [rs (with-open [con (jdbc/get-connection (ds))
|
|
|
|
|
ps (jdbc/prepare
|
|
|
|
|
con
|
|
|
|
|
["select * from fruit order by id"])]
|
|
|
|
|
(jdbc/execute! ps))]
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 4 (count rs)))
|
|
|
|
|
(is (= 1 (:FRUIT/ID (first rs))))
|
|
|
|
|
(is (= 4 (:FRUIT/ID (last rs)))))
|
2019-07-09 03:48:56 +00:00
|
|
|
(let [rs (with-open [con (jdbc/get-connection (ds))
|
|
|
|
|
ps (jdbc/prepare
|
|
|
|
|
con
|
|
|
|
|
["select * from fruit where id = ?"])]
|
|
|
|
|
(jdbc/execute! (prep/set-parameters ps [4])))]
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 1 (count rs)))
|
|
|
|
|
(is (= 4 (:FRUIT/ID (first rs))))))
|
|
|
|
|
(testing "transact"
|
|
|
|
|
(is (= [{:next.jdbc/update-count 1}]
|
|
|
|
|
(jdbc/transact (ds)
|
|
|
|
|
(fn [t] (jdbc/execute! t ["
|
2019-04-22 00:10:29 +00:00
|
|
|
INSERT INTO fruit (name, appearance, cost, grade)
|
|
|
|
|
VALUES ('Pear', 'green', 49, 47)
|
2019-04-20 05:51:48 +00:00
|
|
|
"]))
|
|
|
|
|
{:rollback-only true})))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
2019-07-02 23:45:48 +00:00
|
|
|
(testing "with-transaction rollback-only"
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (= [{:next.jdbc/update-count 1}]
|
|
|
|
|
(jdbc/with-transaction [t (ds) {:rollback-only true}]
|
|
|
|
|
(jdbc/execute! t ["
|
2019-04-22 00:10:29 +00:00
|
|
|
INSERT INTO fruit (name, appearance, cost, grade)
|
|
|
|
|
VALUES ('Pear', 'green', 49, 47)
|
2019-04-20 05:51:48 +00:00
|
|
|
"]))))
|
2019-07-02 23:45:48 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
|
|
|
|
(testing "with-transaction exception"
|
|
|
|
|
(is (thrown? Throwable
|
|
|
|
|
(jdbc/with-transaction [t (ds)]
|
|
|
|
|
(jdbc/execute! t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance, cost, grade)
|
|
|
|
|
VALUES ('Pear', 'green', 49, 47)
|
|
|
|
|
"])
|
|
|
|
|
(throw (ex-info "abort" {})))))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
|
|
|
|
(testing "with-transaction call rollback"
|
|
|
|
|
(is (= [{:next.jdbc/update-count 1}]
|
|
|
|
|
(jdbc/with-transaction [t (ds)]
|
|
|
|
|
(let [result (jdbc/execute! t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance, cost, grade)
|
|
|
|
|
VALUES ('Pear', 'green', 49, 47)
|
|
|
|
|
"])]
|
|
|
|
|
(.rollback t)
|
|
|
|
|
result))))
|
2019-07-03 01:36:00 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
|
|
|
|
(testing "with-transaction with unnamed save point"
|
|
|
|
|
(is (= [{:next.jdbc/update-count 1}]
|
|
|
|
|
(jdbc/with-transaction [t (ds)]
|
|
|
|
|
(let [save-point (.setSavepoint t)
|
|
|
|
|
result (jdbc/execute! t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance, cost, grade)
|
|
|
|
|
VALUES ('Pear', 'green', 49, 47)
|
|
|
|
|
"])]
|
|
|
|
|
(.rollback t save-point)
|
|
|
|
|
result))))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
|
|
|
|
(testing "with-transaction with named save point"
|
|
|
|
|
(is (= [{:next.jdbc/update-count 1}]
|
|
|
|
|
(jdbc/with-transaction [t (ds)]
|
|
|
|
|
(let [save-point (.setSavepoint t (name (gensym)))
|
|
|
|
|
result (jdbc/execute! t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance, cost, grade)
|
|
|
|
|
VALUES ('Pear', 'green', 49, 47)
|
|
|
|
|
"])]
|
|
|
|
|
(.rollback t save-point)
|
|
|
|
|
result))))
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))))
|
2019-06-11 23:47:58 +00:00
|
|
|
|
|
|
|
|
(deftest plan-misuse
|
|
|
|
|
(let [s (pr-str (jdbc/plan (ds) ["select * from fruit"]))]
|
|
|
|
|
(is (re-find #"missing reduction" s)))
|
|
|
|
|
(let [s (pr-str (into [] (jdbc/plan (ds) ["select * from fruit"])))]
|
|
|
|
|
(is (re-find #"missing `map` or `reduce`" s)))
|
|
|
|
|
(let [s (pr-str (into [] (take 3) (jdbc/plan (ds) ["select * from fruit"])))]
|
|
|
|
|
(is (re-find #"missing `map` or `reduce`" s)))
|
|
|
|
|
(is (thrown? IllegalArgumentException
|
|
|
|
|
(doall (take 3 (jdbc/plan (ds) ["select * from fruit"]))))))
|