diff --git a/test/next/jdbc/result_set_test.clj b/test/next/jdbc/result_set_test.clj index bb267cb..8a070f7 100644 --- a/test/next/jdbc/result_set_test.clj +++ b/test/next/jdbc/result_set_test.clj @@ -19,9 +19,9 @@ (deftest test-datafy-nav (testing "default schema" (let [connectable (ds) - test-row (rs/datafiable-row {:table/fruit_id 1} connectable {}) + test-row (rs/datafiable-row {:TABLE/FRUIT_ID 1} connectable {}) data (d/datafy test-row) - v (get data :table/fruit_id)] + v (get data :TABLE/FRUIT_ID)] ;; check datafication is sane (is (= 1 v)) (let [object (d/nav data :table/fruit_id v)] diff --git a/test/next/jdbc/sql_test.clj b/test/next/jdbc/sql_test.clj index b8ce083..9f8c534 100644 --- a/test/next/jdbc/sql_test.clj +++ b/test/next/jdbc/sql_test.clj @@ -7,7 +7,8 @@ (:require [clojure.test :refer [deftest is testing use-fixtures]] [next.jdbc.quoted :refer [mysql sql-server]] [next.jdbc.sql :as sql] - [next.jdbc.test-fixtures :refer [with-test-db ds]])) + [next.jdbc.test-fixtures + :refer [with-test-db ds derby? sqlite?]])) (use-fixtures :once with-test-db) @@ -117,7 +118,12 @@ (deftest test-insert-delete (testing "single insert/delete" - (is (= {:FRUIT/ID 5} + (is (= (cond (derby?) + {:1 5M} + (sqlite?) + {(keyword "last_insert_rowid()") 5} + :else + {:FRUIT/ID 5}) (sql/insert! (ds) :fruit {:name "Kiwi" :appearance "green & fuzzy" :cost 100 :grade 99.9}))) @@ -126,7 +132,12 @@ (sql/delete! (ds) :fruit {:id 5}))) (is (= 4 (count (sql/query (ds) ["select * from fruit"]))))) (testing "multiple insert/delete" - (is (= [{:FRUIT/ID 6} {:FRUIT/ID 7} {:FRUIT/ID 8}] + (is (= (cond (derby?) + [{:1 nil}] ; WTF Apache Derby? + (sqlite?) + [{(keyword "last_insert_rowid()") 8}] + :else + [{:FRUIT/ID 6} {:FRUIT/ID 7} {:FRUIT/ID 8}]) (sql/insert-multi! (ds) :fruit [:name :appearance :cost :grade] [["Kiwi" "green & fuzzy" 100 99.9] diff --git a/test/next/jdbc/test_fixtures.clj b/test/next/jdbc/test_fixtures.clj index 891e5cb..bcd0c98 100644 --- a/test/next/jdbc/test_fixtures.clj +++ b/test/next/jdbc/test_fixtures.clj @@ -4,7 +4,23 @@ (:require [next.jdbc :as jdbc] [next.jdbc.sql :as sql])) -(def ^:private test-db-spec {:dbtype "h2:mem" :dbname "clojure_test_fixture"}) +(def ^:private test-derby {:dbtype "derby" :dbname "clojure_test_derby" :create true}) + +(def ^:private test-h2-mem {:dbtype "h2:mem" :dbname "clojure_test_h2_mem"}) + +(def ^:private test-h2 {:dbtype "h2" :dbname "clojure_test_h2"}) + +(def ^:private test-hsql {:dbtype "hsqldb" :dbname "clojure_test_hsqldb"}) + +(def ^:private test-sqlite {:dbtype "sqlite" :dbname "clojure_test_sqlite"}) + +(def ^:private test-db-specs [test-derby test-h2-mem test-h2 test-hsql test-sqlite]) + +(def ^:private test-db-spec (atom nil)) + +(defn derby? [] (= "derby" (:dbtype @test-db-spec))) + +(defn sqlite? [] (= "sqlite" (:dbtype @test-db-spec))) (def ^:private test-datasource (atom nil)) @@ -20,24 +36,35 @@ Tests can reach into here and call ds (above) to get a DataSource for use in test functions (that operate inside this fixture)." [t] - (reset! test-datasource (jdbc/get-datasource test-db-spec)) - (with-open [con (jdbc/get-connection (ds))] - (try - (jdbc/execute-one! con ["DROP TABLE fruit"]) - (catch Exception _)) - (jdbc/execute-one! con [" -CREATE TABLE fruit ( - id int auto_increment primary key, - name varchar(32), - appearance varchar(32), - cost int, - grade real -)"]) - (sql/insert-multi! con :fruit - [:id :name :appearance :cost :grade] - [[1 "Apple" "red" 59 87] - [2,"Banana","yellow",29,92.2] - [3,"Peach","fuzzy",139,90.0] - [4,"Orange","juicy",89,88.6]] - {:return-keys false}) - (t))) + (doseq [db test-db-specs] + (reset! test-db-spec db) + (reset! test-datasource (jdbc/get-datasource db)) + (let [auto-inc-pk + (cond (or (derby?) (= "hsqldb" (:dbtype db))) + (str "GENERATED ALWAYS AS IDENTITY" + " (START WITH 1, INCREMENT BY 1)" + " PRIMARY KEY") + (sqlite?) + "PRIMARY KEY AUTOINCREMENT" + :else + "AUTO_INCREMENT PRIMARY KEY")] + (with-open [con (jdbc/get-connection (ds))] + (try + (jdbc/execute-one! con ["DROP TABLE FRUIT"]) + (catch Exception _)) + (jdbc/execute-one! con [(str " +CREATE TABLE FRUIT ( + ID INTEGER " auto-inc-pk ", + NAME VARCHAR(32), + APPEARANCE VARCHAR(32), + COST INT, + GRADE REAL +)")]) + (sql/insert-multi! con :fruit + [:name :appearance :cost :grade] + [["Apple" "red" 59 87] + ["Banana","yellow",29,92.2] + ["Peach","fuzzy",139,90.0] + ["Orange","juicy",89,88.6]] + {:return-keys false}) + (t))))) diff --git a/test/next/jdbc_test.clj b/test/next/jdbc_test.clj index 64dfbe8..b85d604 100644 --- a/test/next/jdbc_test.clj +++ b/test/next/jdbc_test.clj @@ -98,8 +98,8 @@ (is (= [{:next.jdbc/update-count 1}] (jdbc/transact (ds) (fn [t] (jdbc/execute! t [" -INSERT INTO fruit (id, name, appearance, cost, grade) -VALUES (5, 'Pear', 'green', 49, 47) +INSERT INTO fruit (name, appearance, cost, grade) +VALUES ('Pear', 'green', 49, 47) "])) {:rollback-only true}))) (is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))) @@ -107,7 +107,7 @@ VALUES (5, 'Pear', 'green', 49, 47) (is (= [{:next.jdbc/update-count 1}] (jdbc/with-transaction [t (ds) {:rollback-only true}] (jdbc/execute! t [" -INSERT INTO fruit (id, name, appearance, cost, grade) -VALUES (5, 'Pear', 'green', 49, 47) +INSERT INTO fruit (name, appearance, cost, grade) +VALUES ('Pear', 'green', 49, 47) "])))) (is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))))