;; copyright (c) 2019 Sean Corfield, all rights reserved (ns next.jdbc-test "Not exactly a test suite -- more a series of examples." (:require [clojure.test :refer [deftest is testing]] [next.jdbc :as jdbc] [next.jdbc.test-fixtures :refer [with-test-db ds]] [next.jdbc.prepare :as prep] [next.jdbc.result-set :as rs]) (:import (java.sql ResultSet ResultSetMetaData))) (deftest basic-tests (testing "reducible!" (is (= "Apple" (reduce (fn [_ row] (reduced (:name row))) nil (jdbc/reducible! (ds) ["select * from fruit where appearance = ?" "red"]))))) (testing "execute-one!" (is (= "Apple" (:FRUIT/NAME (jdbc/execute-one! (ds) ["select * from fruit where appearance = ?" "red"]))))) (testing "execute!" (let [rs (jdbc/execute! (ds) ["select * from fruit where appearance = ?" "red"])] (is (= 1 (count rs))) (is (= 1 (:FRUIT/ID (first rs))))) (let [rs (jdbc/execute! (ds) ["select * from fruit order by id"] {:gen-fn rs/as-maps})] (is (every? map? rs)) (is (every? meta rs)) (is (= 4 (count rs))) (is (= 1 (:FRUIT/ID (first rs)))) (is (= 4 (:FRUIT/ID (last rs))))) (let [rs (jdbc/execute! (ds) ["select * from fruit order by id"] {:gen-fn rs/as-arrays})] (is (every? vector? rs)) (is (= 5 (count rs))) (is (every? #(= 5 (count %)) rs)) ;; columns come first (is (every? qualified-keyword? (first rs))) ;; :FRUIT/ID should be first column (is (= :FRUIT/ID (ffirst rs))) ;; and all its corresponding values should be ints (is (every? int? (map first (rest rs)))) (is (every? string? (map second (rest rs))))) (let [rs (jdbc/execute! (ds) ["select * from fruit order by id"] {:gen-fn rs/as-unqualified-maps})] (is (every? map? rs)) (is (every? meta rs)) (is (= 4 (count rs))) (is (= 1 (:ID (first rs)))) (is (= 4 (:ID (last rs))))) (let [rs (jdbc/execute! (ds) ["select * from fruit order by id"] {:gen-fn rs/as-unqualified-arrays})] (is (every? vector? rs)) (is (= 5 (count rs))) (is (every? #(= 5 (count %)) rs)) ;; columns come first (is (every? simple-keyword? (first rs))) ;; :ID should be first column (is (= :ID (ffirst rs))) ;; and all its corresponding values should be ints (is (every? int? (map first (rest rs)))) (is (every? string? (map second (rest rs)))))) (testing "prepare" (let [rs (with-open [con (jdbc/get-connection (ds))] (with-open [ps (jdbc/prepare con ["select * from fruit order by id"])] (jdbc/execute! ps)))] (is (every? map? rs)) (is (every? meta rs)) (is (= 4 (count rs))) (is (= 1 (:FRUIT/ID (first rs)))) (is (= 4 (:FRUIT/ID (last rs))))) (let [rs (with-open [con (jdbc/get-connection (ds))] (with-open [ps (jdbc/prepare con ["select * from fruit where id = ?"])] (jdbc/execute! (prep/set-parameters ps [4]))))] (is (every? map? rs)) (is (every? meta rs)) (is (= 1 (count rs))) (is (= 4 (:FRUIT/ID (first rs)))))) (testing "transact" (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) "])) {:rollback-only true}))) (is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))) (testing "with-transaction" (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) "])))) (is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))))