2021-01-30 00:13:05 +00:00
|
|
|
;; copyright (c) 2019-2021 Sean Corfield, all rights reserved
|
2019-04-18 21:15:15 +00:00
|
|
|
|
|
|
|
|
(ns next.jdbc.prepare-test
|
2019-04-19 04:51:58 +00:00
|
|
|
"Stub test namespace for PreparedStatement creation etc.
|
|
|
|
|
|
2019-07-09 23:00:10 +00:00
|
|
|
Most of this functionality is core to all of the higher-level stuff
|
|
|
|
|
so it gets tested that way, but there are some specific tests for
|
|
|
|
|
`execute-batch!` here."
|
|
|
|
|
(:require [clojure.test :refer [deftest is testing use-fixtures]]
|
|
|
|
|
[next.jdbc :as jdbc]
|
2020-06-06 21:30:27 +00:00
|
|
|
[next.jdbc.test-fixtures
|
2020-07-22 22:32:26 +00:00
|
|
|
:refer [with-test-db ds jtds? mssql? sqlite?]]
|
2019-07-09 23:00:10 +00:00
|
|
|
[next.jdbc.prepare :as prep]
|
|
|
|
|
[next.jdbc.specs :as specs]))
|
2019-05-29 16:04:21 +00:00
|
|
|
|
|
|
|
|
(set! *warn-on-reflection* true)
|
2019-07-09 23:00:10 +00:00
|
|
|
|
|
|
|
|
(use-fixtures :once with-test-db)
|
|
|
|
|
|
|
|
|
|
(specs/instrument)
|
|
|
|
|
|
|
|
|
|
(deftest execute-batch-tests
|
|
|
|
|
(testing "simple batch insert"
|
|
|
|
|
(is (= [1 1 1 1 1 1 1 1 1 13]
|
|
|
|
|
(jdbc/with-transaction [t (ds) {:rollback-only true}]
|
|
|
|
|
(with-open [ps (jdbc/prepare t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance) VALUES (?,?)
|
|
|
|
|
"])]
|
|
|
|
|
(let [result (prep/execute-batch! ps [["fruit1" "one"]
|
|
|
|
|
["fruit2" "two"]
|
|
|
|
|
["fruit3" "three"]
|
|
|
|
|
["fruit4" "four"]
|
|
|
|
|
["fruit5" "five"]
|
|
|
|
|
["fruit6" "six"]
|
|
|
|
|
["fruit7" "seven"]
|
|
|
|
|
["fruit8" "eight"]
|
|
|
|
|
["fruit9" "nine"]])]
|
|
|
|
|
(conj result (count (jdbc/execute! t ["select * from fruit"]))))))))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
|
|
|
|
(testing "small batch insert"
|
|
|
|
|
(is (= [1 1 1 1 1 1 1 1 1 13]
|
|
|
|
|
(jdbc/with-transaction [t (ds) {:rollback-only true}]
|
|
|
|
|
(with-open [ps (jdbc/prepare t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance) VALUES (?,?)
|
|
|
|
|
"])]
|
|
|
|
|
(let [result (prep/execute-batch! ps [["fruit1" "one"]
|
|
|
|
|
["fruit2" "two"]
|
|
|
|
|
["fruit3" "three"]
|
|
|
|
|
["fruit4" "four"]
|
|
|
|
|
["fruit5" "five"]
|
|
|
|
|
["fruit6" "six"]
|
|
|
|
|
["fruit7" "seven"]
|
|
|
|
|
["fruit8" "eight"]
|
|
|
|
|
["fruit9" "nine"]]
|
|
|
|
|
{:batch-size 3})]
|
|
|
|
|
(conj result (count (jdbc/execute! t ["select * from fruit"]))))))))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
|
|
|
|
(testing "big batch insert"
|
|
|
|
|
(is (= [1 1 1 1 1 1 1 1 1 13]
|
|
|
|
|
(jdbc/with-transaction [t (ds) {:rollback-only true}]
|
|
|
|
|
(with-open [ps (jdbc/prepare t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance) VALUES (?,?)
|
|
|
|
|
"])]
|
|
|
|
|
(let [result (prep/execute-batch! ps [["fruit1" "one"]
|
|
|
|
|
["fruit2" "two"]
|
|
|
|
|
["fruit3" "three"]
|
|
|
|
|
["fruit4" "four"]
|
|
|
|
|
["fruit5" "five"]
|
|
|
|
|
["fruit6" "six"]
|
|
|
|
|
["fruit7" "seven"]
|
|
|
|
|
["fruit8" "eight"]
|
|
|
|
|
["fruit9" "nine"]]
|
|
|
|
|
{:batch-size 8})]
|
|
|
|
|
(conj result (count (jdbc/execute! t ["select * from fruit"]))))))))
|
|
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
|
|
|
|
|
(testing "large batch insert"
|
2020-07-22 20:01:51 +00:00
|
|
|
(when-not (or (jtds?) (sqlite?))
|
2019-07-09 23:00:10 +00:00
|
|
|
(is (= [1 1 1 1 1 1 1 1 1 13]
|
|
|
|
|
(jdbc/with-transaction [t (ds) {:rollback-only true}]
|
|
|
|
|
(with-open [ps (jdbc/prepare t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance) VALUES (?,?)
|
|
|
|
|
"])]
|
|
|
|
|
(let [result (prep/execute-batch! ps [["fruit1" "one"]
|
|
|
|
|
["fruit2" "two"]
|
|
|
|
|
["fruit3" "three"]
|
|
|
|
|
["fruit4" "four"]
|
|
|
|
|
["fruit5" "five"]
|
|
|
|
|
["fruit6" "six"]
|
|
|
|
|
["fruit7" "seven"]
|
|
|
|
|
["fruit8" "eight"]
|
|
|
|
|
["fruit9" "nine"]]
|
|
|
|
|
{:batch-size 4
|
|
|
|
|
:large true})]
|
|
|
|
|
(conj result (count (jdbc/execute! t ["select * from fruit"]))))))))
|
2020-07-22 20:01:51 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))))
|
|
|
|
|
(testing "return generated keys"
|
|
|
|
|
(when-not (mssql?)
|
|
|
|
|
(let [results
|
|
|
|
|
(jdbc/with-transaction [t (ds) {:rollback-only true}]
|
|
|
|
|
(with-open [ps (jdbc/prepare t ["
|
|
|
|
|
INSERT INTO fruit (name, appearance) VALUES (?,?)
|
|
|
|
|
"]
|
|
|
|
|
{:return-keys true})]
|
|
|
|
|
(let [result (prep/execute-batch! ps [["fruit1" "one"]
|
|
|
|
|
["fruit2" "two"]
|
|
|
|
|
["fruit3" "three"]
|
|
|
|
|
["fruit4" "four"]
|
|
|
|
|
["fruit5" "five"]
|
|
|
|
|
["fruit6" "six"]
|
|
|
|
|
["fruit7" "seven"]
|
|
|
|
|
["fruit8" "eight"]
|
|
|
|
|
["fruit9" "nine"]]
|
|
|
|
|
{:batch-size 4
|
|
|
|
|
:return-generated-keys true})]
|
|
|
|
|
(conj result (count (jdbc/execute! t ["select * from fruit"]))))))]
|
|
|
|
|
(is (= 13 (last results)))
|
|
|
|
|
(is (every? map? (butlast results)))
|
|
|
|
|
;; Derby and SQLite only return one generated key per batch so there
|
|
|
|
|
;; are only three keys, plus the overall count here:
|
|
|
|
|
(is (< 3 (count results))))
|
2019-07-09 23:00:10 +00:00
|
|
|
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))))
|