2020-01-01 21:13:41 +00:00
|
|
|
;; copyright (c) 2019-2020 Sean Corfield, all rights reserved
|
2019-04-19 05:43:27 +00:00
|
|
|
|
2019-01-08 04:38:58 +00:00
|
|
|
(ns next.jdbc-test
|
2019-11-15 01:10:49 +00:00
|
|
|
"Basic tests for the primary API of `next.jdbc`."
|
2020-06-27 00:38:58 +00:00
|
|
|
(:require [clojure.core.reducers :as r]
|
|
|
|
|
[clojure.string :as str]
|
2019-10-02 16:19:31 +00:00
|
|
|
[clojure.test :refer [deftest is testing use-fixtures]]
|
2019-04-20 05:51:48 +00:00
|
|
|
[next.jdbc :as jdbc]
|
2019-10-02 16:19:31 +00:00
|
|
|
[next.jdbc.connection :as c]
|
2020-06-24 19:33:32 +00:00
|
|
|
[next.jdbc.test-fixtures
|
|
|
|
|
:refer [with-test-db db ds column
|
|
|
|
|
default-options stored-proc?
|
|
|
|
|
derby? hsqldb? jtds? mssql? mysql? postgres?]]
|
2019-04-20 05:51:48 +00:00
|
|
|
[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-11-15 01:10:49 +00:00
|
|
|
(:import (java.sql ResultSet)))
|
2019-01-08 04:38:58 +00:00
|
|
|
|
2019-05-29 16:04:21 +00:00
|
|
|
(set! *warn-on-reflection* true)
|
|
|
|
|
|
2020-06-27 20:46:38 +00:00
|
|
|
;; around each test because of the folding tests using 1,000 rows
|
|
|
|
|
(use-fixtures :each with-test-db)
|
2019-05-01 23:21:23 +00:00
|
|
|
|
2019-05-22 04:45:05 +00:00
|
|
|
(specs/instrument)
|
|
|
|
|
|
2019-04-20 05:51:48 +00:00
|
|
|
(deftest basic-tests
|
2020-06-22 23:38:13 +00:00
|
|
|
;; use ds-opts instead of (ds) anywhere you want default options applied:
|
|
|
|
|
(let [ds-opts (jdbc/with-options (ds) (default-options))]
|
|
|
|
|
(testing "plan"
|
|
|
|
|
(is (= "Apple"
|
|
|
|
|
(reduce (fn [_ row] (reduced (:name row)))
|
|
|
|
|
nil
|
2020-07-05 23:11:43 +00:00
|
|
|
(jdbc/plan
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit where appearance = ?" "red"]))))
|
|
|
|
|
(is (= "Banana"
|
|
|
|
|
(reduce (fn [_ row] (reduced (:no-such-column row "Banana")))
|
|
|
|
|
nil
|
2020-06-22 23:38:13 +00:00
|
|
|
(jdbc/plan
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit where appearance = ?" "red"])))))
|
|
|
|
|
(testing "execute-one!"
|
|
|
|
|
(is (nil? (jdbc/execute-one!
|
|
|
|
|
(ds)
|
|
|
|
|
["select * from fruit where appearance = ?" "neon-green"])))
|
|
|
|
|
(is (= "Apple" ((column :FRUIT/NAME)
|
|
|
|
|
(jdbc/execute-one!
|
|
|
|
|
ds-opts
|
2020-07-08 18:52:22 +00:00
|
|
|
["select * from fruit where appearance = ?" "red"]))))
|
|
|
|
|
(is (= "red" (:fruit/looks-like
|
|
|
|
|
(jdbc/execute-one!
|
|
|
|
|
ds-opts
|
|
|
|
|
["select appearance as looks_like from fruit where id = ?" 1]
|
|
|
|
|
jdbc/snake-kebab-opts))))
|
|
|
|
|
(is (= "red" (:looks-like
|
|
|
|
|
(jdbc/execute-one!
|
|
|
|
|
ds-opts
|
|
|
|
|
["select appearance as looks_like from fruit where id = ?" 1]
|
|
|
|
|
jdbc/unqualified-snake-kebab-opts)))))
|
2020-06-22 23:38:13 +00:00
|
|
|
(testing "execute!"
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit where appearance = ?" "neon-green"])]
|
|
|
|
|
(is (vector? rs))
|
|
|
|
|
(is (= [] rs)))
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit where appearance = ?" "red"])]
|
|
|
|
|
(is (= 1 (count rs)))
|
|
|
|
|
(is (= 1 ((column :FRUIT/ID) (first rs)))))
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit order by id"]
|
|
|
|
|
{:builder-fn rs/as-maps})]
|
|
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 4 (count rs)))
|
|
|
|
|
(is (= 1 ((column :FRUIT/ID) (first rs))))
|
|
|
|
|
(is (= 4 ((column :FRUIT/ID) (last rs)))))
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit order by id"]
|
|
|
|
|
{:builder-fn rs/as-arrays})]
|
|
|
|
|
(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 (= (column :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))))))
|
|
|
|
|
(testing "execute! with adapter"
|
|
|
|
|
(let [rs (jdbc/execute! ; test again, with adapter and lower columns
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit order by id"]
|
|
|
|
|
{:builder-fn (rs/as-arrays-adapter
|
|
|
|
|
rs/as-lower-arrays
|
|
|
|
|
(fn [^ResultSet rs _ ^Integer i]
|
|
|
|
|
(.getObject rs i)))})]
|
|
|
|
|
(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))))))
|
|
|
|
|
(testing "execute! with unqualified"
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
(ds)
|
|
|
|
|
["select * from fruit order by id"]
|
|
|
|
|
{:builder-fn rs/as-unqualified-maps})]
|
|
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 4 (count rs)))
|
|
|
|
|
(is (= 1 ((column :ID) (first rs))))
|
|
|
|
|
(is (= 4 ((column :ID) (last rs)))))
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit order by id"]
|
|
|
|
|
{:builder-fn rs/as-unqualified-arrays})]
|
|
|
|
|
(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 (= (column :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 "execute! with :max-rows / :maxRows"
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit order by id"]
|
|
|
|
|
{:max-rows 2})]
|
|
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 2 (count rs)))
|
|
|
|
|
(is (= 1 ((column :FRUIT/ID) (first rs))))
|
|
|
|
|
(is (= 2 ((column :FRUIT/ID) (last rs)))))
|
|
|
|
|
(let [rs (jdbc/execute!
|
|
|
|
|
ds-opts
|
|
|
|
|
["select * from fruit order by id"]
|
|
|
|
|
{:statement {:maxRows 2}})]
|
|
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 2 (count rs)))
|
|
|
|
|
(is (= 1 ((column :FRUIT/ID) (first rs))))
|
|
|
|
|
(is (= 2 ((column :FRUIT/ID) (last rs)))))))
|
2019-04-20 05:51:48 +00:00
|
|
|
(testing "prepare"
|
2020-06-22 23:38:13 +00:00
|
|
|
;; default options do not flow over get-connection
|
2019-07-09 03:48:56 +00:00
|
|
|
(let [rs (with-open [con (jdbc/get-connection (ds))
|
|
|
|
|
ps (jdbc/prepare
|
|
|
|
|
con
|
2019-11-16 06:37:42 +00:00
|
|
|
["select * from fruit order by id"]
|
|
|
|
|
(default-options))]
|
2019-07-09 03:48:56 +00:00
|
|
|
(jdbc/execute! ps))]
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 4 (count rs)))
|
2019-11-15 23:38:51 +00:00
|
|
|
(is (= 1 ((column :FRUIT/ID) (first rs))))
|
|
|
|
|
(is (= 4 ((column :FRUIT/ID) (last rs)))))
|
2020-06-22 23:38:13 +00:00
|
|
|
;; default options do not flow over get-connection
|
2019-07-09 03:48:56 +00:00
|
|
|
(let [rs (with-open [con (jdbc/get-connection (ds))
|
|
|
|
|
ps (jdbc/prepare
|
|
|
|
|
con
|
2019-11-16 06:37:42 +00:00
|
|
|
["select * from fruit where id = ?"]
|
|
|
|
|
(default-options))]
|
2019-11-15 00:15:52 +00:00
|
|
|
(jdbc/execute! (prep/set-parameters ps [4]) nil {}))]
|
|
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 1 (count rs)))
|
2019-11-15 23:38:51 +00:00
|
|
|
(is (= 4 ((column :FRUIT/ID) (first rs))))))
|
2019-11-15 00:15:52 +00:00
|
|
|
(testing "statement"
|
2020-06-22 23:38:13 +00:00
|
|
|
;; default options do not flow over get-connection
|
2019-11-15 00:15:52 +00:00
|
|
|
(let [rs (with-open [con (jdbc/get-connection (ds))]
|
2019-12-20 23:45:22 +00:00
|
|
|
(jdbc/execute! (prep/statement con (default-options))
|
2019-11-15 00:15:52 +00:00
|
|
|
["select * from fruit order by id"]))]
|
|
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 4 (count rs)))
|
2019-12-20 23:45:22 +00:00
|
|
|
(is (= 1 ((column :FRUIT/ID) (first rs))))
|
|
|
|
|
(is (= 4 ((column :FRUIT/ID) (last rs)))))
|
2020-06-22 23:38:13 +00:00
|
|
|
;; default options do not flow over get-connection
|
2019-11-15 00:15:52 +00:00
|
|
|
(let [rs (with-open [con (jdbc/get-connection (ds))]
|
2019-12-20 23:45:22 +00:00
|
|
|
(jdbc/execute! (prep/statement con (default-options))
|
2019-11-15 00:15:52 +00:00
|
|
|
["select * from fruit where id = 4"]))]
|
2019-04-20 05:51:48 +00:00
|
|
|
(is (every? map? rs))
|
|
|
|
|
(is (every? meta rs))
|
|
|
|
|
(is (= 1 (count rs)))
|
2019-12-20 23:45:22 +00:00
|
|
|
(is (= 4 ((column :FRUIT/ID) (first rs))))))
|
2019-04-20 05:51:48 +00:00
|
|
|
(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-12-12 00:03:41 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))
|
|
|
|
|
(with-open [con (jdbc/get-connection (ds))]
|
|
|
|
|
(let [ac (.getAutoCommit con)]
|
|
|
|
|
(is (= [{:next.jdbc/update-count 1}]
|
|
|
|
|
(jdbc/with-transaction [t con {:rollback-only true}]
|
|
|
|
|
(jdbc/execute! t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance, cost, grade)
|
|
|
|
|
VALUES ('Pear', 'green', 49, 47)
|
|
|
|
|
"]))))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! con ["select * from fruit"]))))
|
|
|
|
|
(is (= ac (.getAutoCommit con))))))
|
2019-07-02 23:45:48 +00:00
|
|
|
(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" {})))))
|
2019-12-12 00:03:41 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))
|
|
|
|
|
(with-open [con (jdbc/get-connection (ds))]
|
|
|
|
|
(let [ac (.getAutoCommit con)]
|
|
|
|
|
(is (thrown? Throwable
|
|
|
|
|
(jdbc/with-transaction [t con]
|
|
|
|
|
(jdbc/execute! t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance, cost, grade)
|
|
|
|
|
VALUES ('Pear', 'green', 49, 47)
|
|
|
|
|
"])
|
|
|
|
|
(throw (ex-info "abort" {})))))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! con ["select * from fruit"]))))
|
|
|
|
|
(is (= ac (.getAutoCommit con))))))
|
2019-07-02 23:45:48 +00:00
|
|
|
(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-12-12 00:03:41 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))
|
|
|
|
|
(with-open [con (jdbc/get-connection (ds))]
|
|
|
|
|
(let [ac (.getAutoCommit con)]
|
|
|
|
|
(is (= [{:next.jdbc/update-count 1}]
|
|
|
|
|
(jdbc/with-transaction [t con]
|
|
|
|
|
(let [result (jdbc/execute! t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance, cost, grade)
|
|
|
|
|
VALUES ('Pear', 'green', 49, 47)
|
|
|
|
|
"])]
|
|
|
|
|
(.rollback t)
|
|
|
|
|
result))))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! con ["select * from fruit"]))))
|
|
|
|
|
(is (= ac (.getAutoCommit con))))))
|
2019-07-03 01:36:00 +00:00
|
|
|
(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))))
|
2019-12-12 00:03:41 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))
|
|
|
|
|
(with-open [con (jdbc/get-connection (ds))]
|
|
|
|
|
(let [ac (.getAutoCommit con)]
|
|
|
|
|
(is (= [{:next.jdbc/update-count 1}]
|
|
|
|
|
(jdbc/with-transaction [t con]
|
|
|
|
|
(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! con ["select * from fruit"]))))
|
|
|
|
|
(is (= ac (.getAutoCommit con))))))
|
2019-07-03 01:36:00 +00:00
|
|
|
(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-12-12 00:03:41 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))
|
|
|
|
|
(with-open [con (jdbc/get-connection (ds))]
|
|
|
|
|
(let [ac (.getAutoCommit con)]
|
|
|
|
|
(is (= [{:next.jdbc/update-count 1}]
|
|
|
|
|
(jdbc/with-transaction [t con]
|
|
|
|
|
(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))))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! con ["select * from fruit"]))))
|
|
|
|
|
(is (= ac (.getAutoCommit con)))))))
|
2019-06-11 23:47:58 +00:00
|
|
|
|
2020-06-27 02:03:57 +00:00
|
|
|
(deftest folding-test
|
2020-06-27 20:46:38 +00:00
|
|
|
(jdbc/execute-one! (ds) ["delete from fruit"])
|
2020-06-27 21:09:54 +00:00
|
|
|
(with-open [con (jdbc/get-connection (ds))
|
|
|
|
|
ps (jdbc/prepare con ["insert into fruit(name) values (?)"])]
|
|
|
|
|
(prep/execute-batch! ps (mapv #(vector (str "Fruit-" %)) (range 1 1001))))
|
2020-06-27 02:03:57 +00:00
|
|
|
(testing "foldable result set"
|
|
|
|
|
(testing "from a Connection"
|
|
|
|
|
(let [result
|
|
|
|
|
(with-open [con (jdbc/get-connection (ds))]
|
2020-06-27 20:46:38 +00:00
|
|
|
(r/foldcat
|
|
|
|
|
(r/map (column :FRUIT/NAME)
|
|
|
|
|
(jdbc/plan con ["select * from fruit order by id"]
|
|
|
|
|
(default-options)))))]
|
|
|
|
|
(is (= 1000 (count result)))
|
|
|
|
|
(is (= "Fruit-1" (first result)))
|
|
|
|
|
(is (= "Fruit-1000" (last result)))))
|
2020-06-27 02:03:57 +00:00
|
|
|
(testing "from a DataSource"
|
2020-06-27 20:46:38 +00:00
|
|
|
(doseq [n [1 2 3 4 5 100 300 500 700 900 1000 1100]]
|
|
|
|
|
(testing (str "folding with n = " n)
|
|
|
|
|
(let [result
|
|
|
|
|
(r/fold n r/cat r/append!
|
|
|
|
|
(r/map (column :FRUIT/NAME)
|
|
|
|
|
(jdbc/plan (ds) ["select * from fruit order by id"]
|
|
|
|
|
(default-options))))]
|
|
|
|
|
(is (= 1000 (count result)))
|
|
|
|
|
(is (= "Fruit-1" (first result)))
|
|
|
|
|
(is (= "Fruit-1000" (last result)))))))
|
2020-06-27 02:03:57 +00:00
|
|
|
(testing "from a PreparedStatement"
|
|
|
|
|
(let [result
|
|
|
|
|
(with-open [con (jdbc/get-connection (ds))
|
|
|
|
|
stmt (jdbc/prepare con
|
|
|
|
|
["select * from fruit order by id"]
|
|
|
|
|
(default-options))]
|
2020-06-27 20:46:38 +00:00
|
|
|
(r/foldcat
|
|
|
|
|
(r/map (column :FRUIT/NAME)
|
|
|
|
|
(jdbc/plan stmt nil (default-options)))))]
|
|
|
|
|
(is (= 1000 (count result)))
|
|
|
|
|
(is (= "Fruit-1" (first result)))
|
|
|
|
|
(is (= "Fruit-1000" (last result)))))
|
2020-06-27 02:03:57 +00:00
|
|
|
(testing "from a Statement"
|
|
|
|
|
(let [result
|
|
|
|
|
(with-open [con (jdbc/get-connection (ds))
|
|
|
|
|
stmt (prep/statement con (default-options))]
|
2020-06-27 20:46:38 +00:00
|
|
|
(r/foldcat
|
|
|
|
|
(r/map (column :FRUIT/NAME)
|
|
|
|
|
(jdbc/plan stmt ["select * from fruit order by id"]
|
|
|
|
|
(default-options)))))]
|
|
|
|
|
(is (= 1000 (count result)))
|
|
|
|
|
(is (= "Fruit-1" (first result)))
|
|
|
|
|
(is (= "Fruit-1000" (last result)))))))
|
2020-06-27 00:38:58 +00:00
|
|
|
|
2019-10-02 16:19:31 +00:00
|
|
|
(deftest connection-tests
|
|
|
|
|
(testing "datasource via jdbcUrl"
|
|
|
|
|
(when-not (postgres?)
|
|
|
|
|
(let [[url etc] (#'c/spec->url+etc (db))
|
|
|
|
|
ds (jdbc/get-datasource (assoc etc :jdbcUrl url))]
|
2019-11-15 23:38:51 +00:00
|
|
|
(cond (derby?) (is (= {:create true} etc))
|
2019-11-16 06:37:42 +00:00
|
|
|
(mssql?) (is (= #{:user :password} (set (keys etc))))
|
2020-03-16 22:19:21 +00:00
|
|
|
(mysql?) (is (= #{:user :password :useSSL}
|
|
|
|
|
(disj (set (keys etc)) :disableMariaDbDriver)))
|
2019-11-15 23:38:51 +00:00
|
|
|
:else (is (= {} etc)))
|
2019-10-02 16:19:31 +00:00
|
|
|
(is (instance? javax.sql.DataSource ds))
|
2019-11-16 06:37:42 +00:00
|
|
|
(is (str/index-of (pr-str ds) (str "jdbc:"
|
2020-06-07 00:06:11 +00:00
|
|
|
(cond (jtds?)
|
2020-06-07 00:16:17 +00:00
|
|
|
"jtds:sqlserver"
|
2020-06-07 00:06:11 +00:00
|
|
|
(mssql?)
|
|
|
|
|
"sqlserver"
|
|
|
|
|
:else
|
|
|
|
|
(:dbtype (db))))))
|
2019-10-02 16:19:31 +00:00
|
|
|
;; checks get-datasource on a DataSource is identity
|
|
|
|
|
(is (identical? ds (jdbc/get-datasource ds)))
|
|
|
|
|
(with-open [con (jdbc/get-connection ds {})]
|
|
|
|
|
(is (instance? java.sql.Connection con)))))))
|
|
|
|
|
|
2020-06-06 01:34:08 +00:00
|
|
|
(deftest multi-rs
|
2020-06-26 22:38:35 +00:00
|
|
|
(when (mssql?)
|
|
|
|
|
(testing "script with multiple result sets"
|
|
|
|
|
(let [multi-rs
|
|
|
|
|
(jdbc/execute! (ds)
|
|
|
|
|
[(str "begin"
|
|
|
|
|
" select * from fruit;"
|
|
|
|
|
" select * from fruit where id < 4;"
|
|
|
|
|
" end")]
|
|
|
|
|
{:multi-rs true})]
|
|
|
|
|
(is (= 2 (count multi-rs)))
|
|
|
|
|
(is (= 4 (count (first multi-rs))))
|
|
|
|
|
(is (= 3 (count (second multi-rs)))))))
|
2020-06-06 01:34:08 +00:00
|
|
|
(when (stored-proc?)
|
|
|
|
|
(testing "stored proc; multiple result sets"
|
|
|
|
|
(try
|
2020-06-24 19:33:32 +00:00
|
|
|
(let [multi-rs
|
|
|
|
|
(jdbc/execute! (ds)
|
|
|
|
|
[(if (mssql?) "EXEC FRUITP" "CALL FRUITP()")]
|
|
|
|
|
{:multi-rs true})
|
|
|
|
|
zero-updates [{:next.jdbc/update-count 0}]]
|
|
|
|
|
(cond (postgres?) ; does not support multiple result sets yet
|
|
|
|
|
(do
|
|
|
|
|
(is (= 1 (count multi-rs)))
|
|
|
|
|
(is (= zero-updates (first multi-rs))))
|
|
|
|
|
(hsqldb?)
|
|
|
|
|
(do
|
|
|
|
|
(is (= 3 (count multi-rs)))
|
|
|
|
|
(is (= zero-updates (first multi-rs))))
|
|
|
|
|
(mysql?)
|
|
|
|
|
(do
|
|
|
|
|
(is (= 3 (count multi-rs)))
|
|
|
|
|
(is (= zero-updates (last multi-rs))))
|
|
|
|
|
:else
|
|
|
|
|
(is (= 2 (count multi-rs)))))
|
2020-06-06 01:34:08 +00:00
|
|
|
(catch Throwable t
|
|
|
|
|
(println 'call-proc (:dbtype (db)) (ex-message t) (some-> t (ex-cause) (ex-message))))))))
|
|
|
|
|
|
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)))
|
2019-08-02 19:42:00 +00:00
|
|
|
;; this may succeed or not, depending on how the driver handles things
|
|
|
|
|
;; most drivers will error because the ResultSet was closed before pr-str
|
|
|
|
|
;; is invoked (which will attempt to print each row)
|
2019-11-16 06:37:42 +00:00
|
|
|
(let [s (pr-str (into [] (take 3) (jdbc/plan (ds) ["select * from fruit"]
|
|
|
|
|
(default-options))))]
|
2019-08-02 19:42:00 +00:00
|
|
|
(is (or (re-find #"missing `map` or `reduce`" s)
|
|
|
|
|
(re-find #"(?i)^\[#:fruit\{.*:id.*\}\]$" s))))
|
|
|
|
|
(is (every? #(re-find #"(?i)^#:fruit\{.*:id.*\}$" %)
|
2019-11-16 06:37:42 +00:00
|
|
|
(into [] (map str) (jdbc/plan (ds) ["select * from fruit"]
|
|
|
|
|
(default-options)))))
|
2019-08-02 19:42:00 +00:00
|
|
|
(is (every? #(re-find #"(?i)^#:fruit\{.*:id.*\}$" %)
|
2019-11-16 06:37:42 +00:00
|
|
|
(into [] (map pr-str) (jdbc/plan (ds) ["select * from fruit"]
|
|
|
|
|
(default-options)))))
|
2019-06-11 23:47:58 +00:00
|
|
|
(is (thrown? IllegalArgumentException
|
|
|
|
|
(doall (take 3 (jdbc/plan (ds) ["select * from fruit"]))))))
|