beef up boolean tests; add xtdb into them
Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
parent
b981357d47
commit
2dc72d9254
2 changed files with 95 additions and 58 deletions
|
|
@ -80,6 +80,8 @@
|
||||||
|
|
||||||
(defn derby? [] (= "derby" (:dbtype @test-db-spec)))
|
(defn derby? [] (= "derby" (:dbtype @test-db-spec)))
|
||||||
|
|
||||||
|
(defn h2? [] (str/starts-with? (:dbtype @test-db-spec) "h2"))
|
||||||
|
|
||||||
(defn hsqldb? [] (= "hsqldb" (:dbtype @test-db-spec)))
|
(defn hsqldb? [] (= "hsqldb" (:dbtype @test-db-spec)))
|
||||||
|
|
||||||
(defn jtds? [] (= "jtds" (:dbtype @test-db-spec)))
|
(defn jtds? [] (= "jtds" (:dbtype @test-db-spec)))
|
||||||
|
|
@ -180,7 +182,10 @@
|
||||||
(if (xtdb?) ; no DDL for creation
|
(if (xtdb?) ; no DDL for creation
|
||||||
(do
|
(do
|
||||||
(try
|
(try
|
||||||
(do-commands con ["DELETE FROM fruit WHERE true"])
|
(do-commands con ["ERASE FROM fruit WHERE true"])
|
||||||
|
(catch Throwable _))
|
||||||
|
(try
|
||||||
|
(do-commands con ["ERASE FROM btest WHERE true"])
|
||||||
(catch Throwable _))
|
(catch Throwable _))
|
||||||
(sql/insert-multi! con :fruit
|
(sql/insert-multi! con :fruit
|
||||||
[:_id :name :appearance :cost]
|
[:_id :name :appearance :cost]
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,8 @@
|
||||||
[next.jdbc.result-set :as rs]
|
[next.jdbc.result-set :as rs]
|
||||||
[next.jdbc.specs :as specs]
|
[next.jdbc.specs :as specs]
|
||||||
[next.jdbc.test-fixtures
|
[next.jdbc.test-fixtures
|
||||||
:refer [col-kw column db default-options derby? ds hsqldb? index
|
:refer [col-kw column db default-options derby? ds h2? hsqldb?
|
||||||
jtds? mssql? mysql? postgres? sqlite? stored-proc?
|
index jtds? mssql? mysql? postgres? sqlite? stored-proc?
|
||||||
with-test-db xtdb?]]
|
with-test-db xtdb?]]
|
||||||
[next.jdbc.types :as types])
|
[next.jdbc.types :as types])
|
||||||
(:import
|
(:import
|
||||||
|
|
@ -501,61 +501,93 @@ VALUES ('Pear', 'green', 49, 47)
|
||||||
"\n\t" (ex-message t)))))
|
"\n\t" (ex-message t)))))
|
||||||
|
|
||||||
(deftest bool-tests
|
(deftest bool-tests
|
||||||
(when-not (xtdb?)
|
(testing (str "bool-tests for " (:dbtype (db)))
|
||||||
(doseq [[n b] [["zero" 0] ["one" 1] ["false" false] ["true" true]]
|
(let [lit-t (cond (hsqldb?) "(1=1)" (mssql?) "1" :else "TRUE")
|
||||||
:let [v-bit (if (number? b) b (if b 1 0))
|
lit-f (cond (hsqldb?) "(1=0)" (mssql?) "0" :else "FALSE")]
|
||||||
v-bool (if (number? b) (pos? b) b)]]
|
(when-not (or (hsqldb?) (derby?))
|
||||||
(jdbc/execute-one!
|
(testing "literal TRUE select"
|
||||||
(ds)
|
(is (= {(column :V) (if (or (sqlite?) (mysql?) (mssql?)) 1 true)}
|
||||||
["insert into btest (name,is_it,twiddle) values (?,?,?)"
|
(jdbc/execute-one! (ds) [(str "SELECT " lit-t " AS V")]))))
|
||||||
n
|
(testing "literal FALSE select"
|
||||||
(if (postgres?)
|
(is (= {(column :V) (if (or (sqlite?) (mysql?) (mssql?)) 0 false)}
|
||||||
(types/as-boolean b)
|
(jdbc/execute-one! (ds) [(str "SELECT " lit-f " AS V")])))))
|
||||||
b) ; 0, 1, false, true are all acceptable
|
(testing "literal TRUE insert"
|
||||||
(cond (hsqldb?)
|
(jdbc/execute-one!
|
||||||
v-bool ; hsqldb requires a boolean here
|
(ds)
|
||||||
(postgres?)
|
[(str "insert into btest (" (if (xtdb?) "_id" "name") ",is_it,twiddle)"
|
||||||
(types/as-other v-bit) ; really postgres??
|
" values ('lit_t'," lit-t ","
|
||||||
:else
|
(if (or (derby?) (sqlite?) (h2?) (mssql?) (xtdb?)) "1" "b'1'")
|
||||||
v-bit)]))
|
")")]))
|
||||||
(let [data (jdbc/execute! (ds) ["select * from btest"]
|
(testing "literal FALSE insert"
|
||||||
(default-options))]
|
(jdbc/execute-one!
|
||||||
(if (sqlite?)
|
(ds)
|
||||||
(is (every? number? (map (column :BTEST/IS_IT) data)))
|
[(str "insert into btest (" (if (xtdb?) "_id" "name") ",is_it,twiddle)"
|
||||||
(is (every? boolean? (map (column :BTEST/IS_IT) data))))
|
" values ('lit_f'," lit-f ","
|
||||||
(if (or (sqlite?) (derby?))
|
(if (or (derby?) (sqlite?) (h2?) (mssql?) (xtdb?)) "0" "b'0'")
|
||||||
(is (every? number? (map (column :BTEST/TWIDDLE) data)))
|
")")]))
|
||||||
(is (every? boolean? (map (column :BTEST/TWIDDLE) data)))))
|
(testing "BIT/BOOLEAN value insertion"
|
||||||
(let [data (jdbc/execute! (ds) ["select * from btest"]
|
(doseq [[n b] [["zero" 0] ["one" 1] ["false" false] ["true" true]]
|
||||||
(cond-> (default-options)
|
:let [v-bit (if (number? b) b (if b 1 0))
|
||||||
(sqlite?)
|
v-bool (if (number? b) (pos? b) b)]]
|
||||||
(assoc :builder-fn
|
(jdbc/execute-one!
|
||||||
(rs/builder-adapter
|
(ds)
|
||||||
rs/as-maps
|
[(str "insert into btest ("
|
||||||
(fn [builder ^ResultSet rs ^Integer i]
|
(if (xtdb?) "_id" "name")
|
||||||
(let [rsm ^ResultSetMetaData (:rsmeta builder)]
|
",is_it,twiddle) values (?,?,?)")
|
||||||
(rs/read-column-by-index
|
n
|
||||||
;; we only use bit and bool for
|
(if (or (postgres?) (xtdb?))
|
||||||
;; sqlite (not boolean)
|
(types/as-boolean b)
|
||||||
(if (#{"BIT" "BOOL"} (.getColumnTypeName rsm i))
|
b) ; 0, 1, false, true are all acceptable
|
||||||
(.getBoolean rs i)
|
(cond (hsqldb?)
|
||||||
(.getObject rs i))
|
v-bool ; hsqldb requires a boolean here
|
||||||
rsm
|
(postgres?)
|
||||||
i)))))))]
|
(types/as-other v-bit) ; really postgres??
|
||||||
(is (every? boolean? (map (column :BTEST/IS_IT) data)))
|
:else
|
||||||
(if (derby?)
|
v-bit)])))
|
||||||
(is (every? number? (map (column :BTEST/TWIDDLE) data)))
|
(testing "BOOLEAN results from SELECT"
|
||||||
(is (every? boolean? (map (column :BTEST/TWIDDLE) data)))))
|
(let [data (jdbc/execute! (ds) ["select * from btest"]
|
||||||
(let [data (reduce (fn [acc row]
|
(default-options))]
|
||||||
(conj acc (cond-> (select-keys row [:is_it :twiddle])
|
(if (sqlite?)
|
||||||
(sqlite?)
|
(is (every? number? (map (column :BTEST/IS_IT) data)))
|
||||||
(update :is_it pos?)
|
(is (every? boolean? (map (column :BTEST/IS_IT) data))))
|
||||||
(or (sqlite?) (derby?))
|
(if (or (sqlite?) (derby?) (xtdb?))
|
||||||
(update :twiddle pos?))))
|
(is (every? number? (map (column :BTEST/TWIDDLE) data)))
|
||||||
[]
|
(is (every? boolean? (map (column :BTEST/TWIDDLE) data))))))
|
||||||
(jdbc/plan (ds) ["select * from btest"]))]
|
(testing "BOOLEAN read column by index"
|
||||||
(is (every? boolean? (map :is_it data)))
|
(let [data (jdbc/execute! (ds) ["select * from btest"]
|
||||||
(is (every? boolean? (map :twiddle data))))))
|
(cond-> (default-options)
|
||||||
|
(or (sqlite?) (xtdb?))
|
||||||
|
(assoc :builder-fn
|
||||||
|
(rs/builder-adapter
|
||||||
|
rs/as-maps
|
||||||
|
(fn [builder ^ResultSet rs ^Integer i]
|
||||||
|
(let [rsm ^ResultSetMetaData (:rsmeta builder)]
|
||||||
|
(rs/read-column-by-index
|
||||||
|
;; we only use bit and bool for
|
||||||
|
;; sqlite (not boolean), and
|
||||||
|
;; int8 and bool for xtdb:
|
||||||
|
(if (#{"BIT" "BOOL"
|
||||||
|
"int8" "bool"}
|
||||||
|
(.getColumnTypeName rsm i))
|
||||||
|
(.getBoolean rs i)
|
||||||
|
(.getObject rs i))
|
||||||
|
rsm
|
||||||
|
i)))))))]
|
||||||
|
(is (every? boolean? (map (column :BTEST/IS_IT) data)))
|
||||||
|
(if (derby?)
|
||||||
|
(is (every? number? (map (column :BTEST/TWIDDLE) data)))
|
||||||
|
(is (every? boolean? (map (column :BTEST/TWIDDLE) data))))))
|
||||||
|
(testing "BOOLEAN via coercion"
|
||||||
|
(let [data (reduce (fn [acc row]
|
||||||
|
(conj acc (cond-> (select-keys row [:is_it :twiddle])
|
||||||
|
(sqlite?)
|
||||||
|
(update :is_it pos?)
|
||||||
|
(or (sqlite?) (derby?) (xtdb?))
|
||||||
|
(update :twiddle pos?))))
|
||||||
|
[]
|
||||||
|
(jdbc/plan (ds) ["select * from btest"]))]
|
||||||
|
(is (every? boolean? (map :is_it data)))
|
||||||
|
(is (every? boolean? (map :twiddle data))))))))
|
||||||
|
|
||||||
(deftest execute-batch-tests
|
(deftest execute-batch-tests
|
||||||
(when-not (xtdb?)
|
(when-not (xtdb?)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue