From b210cbead73b5ac21bbbda52bee46e3182134450 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Wed, 22 Jul 2020 15:32:54 -0700 Subject: [PATCH] Address #134 by adding tests around bool/bit types --- test/next/jdbc/test_fixtures.clj | 22 ++++++++++++++++++++++ test/next/jdbc_test.clj | 31 +++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/test/next/jdbc/test_fixtures.clj b/test/next/jdbc/test_fixtures.clj index 37df885..8cea90f 100644 --- a/test/next/jdbc/test_fixtures.clj +++ b/test/next/jdbc/test_fixtures.clj @@ -124,6 +124,7 @@ (.getPostgresDatabase ^EmbeddedPostgres @embedded-pg)) (reset! test-datasource (jdbc/get-datasource db))) (let [fruit (if (mysql?) "fruit" "FRUIT") ; MySQL is case sensitive! + btest (if (mysql?) "btest" "BTEST") auto-inc-pk (cond (or (derby?) (hsqldb?)) (str "GENERATED ALWAYS AS IDENTITY" @@ -146,6 +147,9 @@ (try (do-commands con [(str "DROP TABLE " fruit)]) (catch Exception _)) + (try + (do-commands con [(str "DROP TABLE " btest)]) + (catch Exception _)) (when (postgres?) (try (do-commands con ["DROP TABLE LANG_TEST"]) @@ -166,6 +170,24 @@ CREATE TABLE " fruit " ( COST INT DEFAULT NULL, GRADE REAL DEFAULT NULL )")]) + (let [created (atom false)] + ;; MS SQL Server does not support bool/boolean: + (doseq [btype ["BOOL" "BOOLEAN" "BIT"]] + ;; Derby does not support bit: + (doseq [bitty ["BIT" "SMALLINT"]] + (try + (when-not @created + (do-commands con [(str " +CREATE TABLE " btest " ( + NAME VARCHAR(32), + IS_IT " btype ", + TWIDDLE " bitty " +)")]) + (reset! created true)) + (catch Throwable _)))) + (when-not @created + (println (:dbtype db) "failed btest creation") + #_(throw (ex-info (str (:dbtype db) " has no boolean type?") {})))) (when (stored-proc?) (let [[begin end] (if (postgres?) ["$$" "$$"] ["BEGIN" "END"])] (try diff --git a/test/next/jdbc_test.clj b/test/next/jdbc_test.clj index cb5d505..eb1e951 100644 --- a/test/next/jdbc_test.clj +++ b/test/next/jdbc_test.clj @@ -10,10 +10,11 @@ [next.jdbc.test-fixtures :refer [with-test-db db ds column default-options stored-proc? - derby? hsqldb? jtds? mssql? mysql? postgres?]] + derby? hsqldb? jtds? mssql? mysql? postgres? sqlite?]] [next.jdbc.prepare :as prep] [next.jdbc.result-set :as rs] - [next.jdbc.specs :as specs]) + [next.jdbc.specs :as specs] + [next.jdbc.types :as types]) (:import (java.sql ResultSet))) (set! *warn-on-reflection* true) @@ -312,6 +313,32 @@ VALUES ('Pear', 'green', 49, 47) (is (= 4 (count (jdbc/execute! con ["select * from fruit"])))) (is (= ac (.getAutoCommit con))))))) +(deftest bool-tests + (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 (?,?,?)" + n + (if (postgres?) + (types/as-boolean b) + b) ; 0, 1, false, true are all acceptable + (cond (hsqldb?) + v-bool ; hsqldb requires a boolean here + (postgres?) + (types/as-other v-bit) ; really postgres?? + :else + v-bit)])) + (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?)) + (is (every? number? (map (column :BTEST/TWIDDLE) data))) + (is (every? boolean? (map (column :BTEST/TWIDDLE) data)))))) + (deftest folding-test (jdbc/execute-one! (ds) ["delete from fruit"]) (with-open [con (jdbc/get-connection (ds))