beef up boolean tests; add xtdb into them

Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2024-12-14 12:16:19 -08:00
parent b981357d47
commit 2dc72d9254
No known key found for this signature in database
2 changed files with 95 additions and 58 deletions

View file

@ -80,6 +80,8 @@
(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 jtds? [] (= "jtds" (:dbtype @test-db-spec)))
@ -180,7 +182,10 @@
(if (xtdb?) ; no DDL for creation
(do
(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 _))
(sql/insert-multi! con :fruit
[:_id :name :appearance :cost]

View file

@ -12,8 +12,8 @@
[next.jdbc.result-set :as rs]
[next.jdbc.specs :as specs]
[next.jdbc.test-fixtures
:refer [col-kw column db default-options derby? ds hsqldb? index
jtds? mssql? mysql? postgres? sqlite? stored-proc?
:refer [col-kw column db default-options derby? ds h2? hsqldb?
index jtds? mssql? mysql? postgres? sqlite? stored-proc?
with-test-db xtdb?]]
[next.jdbc.types :as types])
(:import
@ -501,15 +501,41 @@ VALUES ('Pear', 'green', 49, 47)
"\n\t" (ex-message t)))))
(deftest bool-tests
(when-not (xtdb?)
(testing (str "bool-tests for " (:dbtype (db)))
(let [lit-t (cond (hsqldb?) "(1=1)" (mssql?) "1" :else "TRUE")
lit-f (cond (hsqldb?) "(1=0)" (mssql?) "0" :else "FALSE")]
(when-not (or (hsqldb?) (derby?))
(testing "literal TRUE select"
(is (= {(column :V) (if (or (sqlite?) (mysql?) (mssql?)) 1 true)}
(jdbc/execute-one! (ds) [(str "SELECT " lit-t " AS V")]))))
(testing "literal FALSE select"
(is (= {(column :V) (if (or (sqlite?) (mysql?) (mssql?)) 0 false)}
(jdbc/execute-one! (ds) [(str "SELECT " lit-f " AS V")])))))
(testing "literal TRUE insert"
(jdbc/execute-one!
(ds)
[(str "insert into btest (" (if (xtdb?) "_id" "name") ",is_it,twiddle)"
" values ('lit_t'," lit-t ","
(if (or (derby?) (sqlite?) (h2?) (mssql?) (xtdb?)) "1" "b'1'")
")")]))
(testing "literal FALSE insert"
(jdbc/execute-one!
(ds)
[(str "insert into btest (" (if (xtdb?) "_id" "name") ",is_it,twiddle)"
" values ('lit_f'," lit-f ","
(if (or (derby?) (sqlite?) (h2?) (mssql?) (xtdb?)) "0" "b'0'")
")")]))
(testing "BIT/BOOLEAN value insertion"
(doseq [[n b] [["zero" 0] ["one" 1] ["false" false] ["true" true]]
:let [v-bit (if (number? b) b (if b 1 0))
v-bool (if (number? b) (pos? b) b)]]
(jdbc/execute-one!
(ds)
["insert into btest (name,is_it,twiddle) values (?,?,?)"
[(str "insert into btest ("
(if (xtdb?) "_id" "name")
",is_it,twiddle) values (?,?,?)")
n
(if (postgres?)
(if (or (postgres?) (xtdb?))
(types/as-boolean b)
b) ; 0, 1, false, true are all acceptable
(cond (hsqldb?)
@ -517,18 +543,20 @@ VALUES ('Pear', 'green', 49, 47)
(postgres?)
(types/as-other v-bit) ; really postgres??
:else
v-bit)]))
v-bit)])))
(testing "BOOLEAN results from SELECT"
(let [data (jdbc/execute! (ds) ["select * from btest"]
(default-options))]
(if (sqlite?)
(is (every? number? (map (column :BTEST/IS_IT) data)))
(is (every? boolean? (map (column :BTEST/IS_IT) data))))
(if (or (sqlite?) (derby?))
(if (or (sqlite?) (derby?) (xtdb?))
(is (every? number? (map (column :BTEST/TWIDDLE) data)))
(is (every? boolean? (map (column :BTEST/TWIDDLE) data)))))
(is (every? boolean? (map (column :BTEST/TWIDDLE) data))))))
(testing "BOOLEAN read column by index"
(let [data (jdbc/execute! (ds) ["select * from btest"]
(cond-> (default-options)
(sqlite?)
(or (sqlite?) (xtdb?))
(assoc :builder-fn
(rs/builder-adapter
rs/as-maps
@ -536,8 +564,11 @@ VALUES ('Pear', 'green', 49, 47)
(let [rsm ^ResultSetMetaData (:rsmeta builder)]
(rs/read-column-by-index
;; we only use bit and bool for
;; sqlite (not boolean)
(if (#{"BIT" "BOOL"} (.getColumnTypeName rsm i))
;; sqlite (not boolean), and
;; int8 and bool for xtdb:
(if (#{"BIT" "BOOL"
"int8" "bool"}
(.getColumnTypeName rsm i))
(.getBoolean rs i)
(.getObject rs i))
rsm
@ -545,17 +576,18 @@ VALUES ('Pear', 'green', 49, 47)
(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)))))
(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?))
(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))))))
(is (every? boolean? (map :twiddle data))))))))
(deftest execute-batch-tests
(when-not (xtdb?)