next-jdbc/test/next/jdbc_test.clj

121 lines
4.3 KiB
Clojure
Raw Normal View History

2019-04-19 05:43:27 +00:00
;; copyright (c) 2019 Sean Corfield, all rights reserved
2019-01-08 04:38:58 +00:00
(ns next.jdbc-test
2019-04-01 06:17:12 +00:00
"Not exactly a test suite -- more a series of examples."
2019-05-01 23:21:23 +00:00
(:require [clojure.test :refer [deftest is testing use-fixtures]]
2019-04-20 05:51:48 +00:00
[next.jdbc :as jdbc]
[next.jdbc.test-fixtures :refer [with-test-db ds]]
[next.jdbc.prepare :as prep]
[next.jdbc.result-set :as rs]
[next.jdbc.specs :as specs])
2019-04-18 15:13:16 +00:00
(:import (java.sql ResultSet ResultSetMetaData)))
2019-01-08 04:38:58 +00:00
(set! *warn-on-reflection* true)
2019-05-01 23:21:23 +00:00
(use-fixtures :once with-test-db)
(specs/instrument)
2019-04-20 05:51:48 +00:00
(deftest basic-tests
(testing "plan"
2019-04-20 05:51:48 +00:00
(is (= "Apple"
(reduce (fn [_ row] (reduced (:name row)))
nil
(jdbc/plan
2019-04-20 05:51:48 +00:00
(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"]
2019-04-24 21:22:35 +00:00
{:builder-fn rs/as-maps})]
2019-04-20 05:51:48 +00:00
(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"]
2019-04-24 21:22:35 +00:00
{:builder-fn rs/as-arrays})]
2019-04-20 05:51:48 +00:00
(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"]
2019-04-24 21:22:35 +00:00
{:builder-fn rs/as-unqualified-maps})]
2019-04-20 05:51:48 +00:00
(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"]
2019-04-24 21:22:35 +00:00
{:builder-fn rs/as-unqualified-arrays})]
2019-04-20 05:51:48 +00:00
(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 (name, appearance, cost, grade)
VALUES ('Pear', 'green', 49, 47)
2019-04-20 05:51:48 +00:00
"]))
{: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 (name, appearance, cost, grade)
VALUES ('Pear', 'green', 49, 47)
2019-04-20 05:51:48 +00:00
"]))))
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))))