Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

119 lines (114 sloc) 4.32 KB
;; 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 use-fixtures]]
[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])
(:import (java.sql ResultSet ResultSetMetaData)))
(use-fixtures :once with-test-db)
(specs/instrument)
(deftest basic-tests
(testing "plan"
(is (= "Apple"
(reduce (fn [_ row] (reduced (:name row)))
nil
(jdbc/plan
(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"]
{:builder-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"]
{:builder-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"]
{:builder-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"]
{:builder-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 (name, appearance, cost, grade)
VALUES ('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 (name, appearance, cost, grade)
VALUES ('Pear', 'green', 49, 47)
"]))))
(is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))))
You can’t perform that action at this time.