Turn all the examples into tests

This commit is contained in:
Sean Corfield 2019-04-19 22:51:48 -07:00
parent f691d59be7
commit 228335d2f3

View file

@ -2,190 +2,112 @@
(ns next.jdbc-test (ns next.jdbc-test
"Not exactly a test suite -- more a series of examples." "Not exactly a test suite -- more a series of examples."
(:require [clojure.string :as str] (:require [clojure.test :refer [deftest is testing]]
[clojure.test :refer [deftest is testing]] [next.jdbc :as jdbc]
[next.jdbc :refer :all] [next.jdbc.test-fixtures :refer [with-test-db ds]]
[next.jdbc.result-set :as rs] [next.jdbc.prepare :as prep]
[next.jdbc.sql :refer :all]) [next.jdbc.result-set :as rs])
(:import (java.sql ResultSet ResultSetMetaData))) (:import (java.sql ResultSet ResultSetMetaData)))
(comment (deftest basic-tests
(def db-spec {:dbtype "h2:mem" :dbname "clojure_test_perf"}) (testing "reducible!"
;; these should be equivalent (is (= "Apple"
(def con (get-connection (get-datasource db-spec) {})) (reduce (fn [_ row] (reduced (:name row)))
(def con (get-connection db-spec {})) nil
(execute-one! con ["DROP TABLE fruit"]) (jdbc/reducible!
;; h2 (ds)
(execute-one! con ["CREATE TABLE fruit (id int default 0, name varchar(32) primary key, appearance varchar(32), cost int, grade real)"]) ["select * from fruit where appearance = ?" "red"])))))
;; either this... (testing "execute-one!"
(execute-one! con ["INSERT INTO fruit (id,name,appearance,cost,grade) VALUES (1,'Apple','red',59,87), (2,'Banana','yellow',29,92.2), (3,'Peach','fuzzy',139,90.0), (4,'Orange','juicy',89,88.6)"]) (is (= "Apple" (:FRUIT/NAME
;; ...or this (H2 can't return generated keys for this) (jdbc/execute-one!
(insert-multi! con :fruit [:id :name :appearance :cost :grade] (ds)
[[1 "Apple" "red" 59 87] ["select * from fruit where appearance = ?" "red"])))))
[2,"Banana","yellow",29,92.2] (testing "execute!"
[3,"Peach","fuzzy",139,90.0] (let [rs (jdbc/execute!
[4,"Orange","juicy",89,88.6]] (ds)
{:return-keys false}) ["select * from fruit where appearance = ?" "red"])]
;; mysql (is (= 1 (count rs)))
(execute! con ["CREATE TABLE fruit (id int auto_increment, name varchar(32), appearance varchar(32), cost int, grade real, primary key (id))"]) (is (= 1 (:FRUIT/ID (first rs)))))
(execute! con ["INSERT INTO fruit (id,name,appearance,cost,grade) VALUES (1,'Apple','red',59,87), (2,'Banana','yellow',29,92.2), (3,'Peach','fuzzy',139,90.0), (4,'Orange','juicy',89,88.6)"] (let [rs (jdbc/execute!
{:return-keys true}) (ds)
;; when you're done ["select * from fruit order by id"]
(.close con) {:gen-fn rs/as-maps})]
(is (every? map? rs))
(require '[criterium.core :refer [bench quick-bench]]) (is (every? meta rs))
(is (= 4 (count rs)))
(require '[clojure.java.jdbc :as jdbc]) (is (= 1 (:FRUIT/ID (first rs))))
(is (= 4 (:FRUIT/ID (last rs)))))
;; calibrate (let [rs (jdbc/execute!
(quick-bench (reduce + (take 10e6 (range)))) (ds)
["select * from fruit order by id"]
;; raw java {:gen-fn rs/as-arrays})]
(defn select* [^java.sql.Connection con] (is (every? vector? rs))
(let [ps (doto (.prepareStatement con "SELECT * FROM fruit WHERE appearance = ?") (is (= 5 (count rs)))
(.setObject 1 "red")) (is (every? #(= 5 (count %)) rs))
rs (.executeQuery ps) ;; columns come first
_ (.next rs) (is (every? qualified-keyword? (first rs)))
value (.getObject rs "name")] ;; :FRUIT/ID should be first column
(.close ps) (is (= :FRUIT/ID (ffirst rs)))
value)) ;; and all its corresponding values should be ints
(quick-bench (select* con)) ; 1.14 micros (is (every? int? (map first (rest rs))))
(is (every? string? (map second (rest rs)))))
;; almost same as the Java example above -- 1.57 micros -- 1.4x Java (let [rs (jdbc/execute!
(quick-bench (ds)
(reduce (fn [rs m] (reduced (:name m))) ["select * from fruit order by id"]
nil {:gen-fn rs/as-unqualified-maps})]
(reducible! con ["select * from fruit where appearance = ?" "red"]))) (is (every? map? rs))
;; run through convenience function -- 2.4 micros (is (every? meta rs))
(quick-bench (is (= 4 (count rs)))
(:FRUIT/NAME (execute-one! con (is (= 1 (:ID (first rs))))
["select * from fruit where appearance = ?" "red"] (is (= 4 (:ID (last rs)))))
{}))) (let [rs (jdbc/execute!
(ds)
;; 6.8 micros -- 3x ["select * from fruit order by id"]
(quick-bench {:gen-fn rs/as-unqualified-arrays})]
(jdbc/query {:connection con} (is (every? vector? rs))
["select * from fruit where appearance = ?" "red"] (is (= 5 (count rs)))
{:row-fn :name :result-set-fn first})) (is (every? #(= 5 (count %)) rs))
;; columns come first
;; simple query -- 2.6 micros (is (every? simple-keyword? (first rs)))
(quick-bench ;; :ID should be first column
(execute! con ["select * from fruit where appearance = ?" "red"])) (is (= :ID (ffirst rs)))
;; and all its corresponding values should be ints
;; 6.9 -- ~2.6x (is (every? int? (map first (rest rs))))
(quick-bench (is (every? string? (map second (rest rs))))))
(jdbc/query {:connection con} ["select * from fruit where appearance = ?" "red"])) (testing "prepare"
(let [rs (with-open [con (jdbc/get-connection (ds))]
(quick-bench ; default -- 4.55-4.57 (with-open [ps (jdbc/prepare
(execute! con ["select * from fruit"] {:gen-fn rs/as-maps})) con
(quick-bench ; arrays -- 4.34-4.4 ["select * from fruit order by id"])]
(execute! con ["select * from fruit"] {:gen-fn rs/as-arrays})) (jdbc/execute! ps)))]
(quick-bench ; simple keys -- 4.55-4.57 (is (every? map? rs))
(execute! con ["select * from fruit"] {:gen-fn rs/as-unqualified-maps})) (is (every? meta rs))
(quick-bench ; simple keys, arrays -- 4.34-4.4 (is (= 4 (count rs)))
(execute! con ["select * from fruit"] {:gen-fn rs/as-unqualified-arrays})) (is (= 1 (:FRUIT/ID (first rs))))
(is (= 4 (:FRUIT/ID (last rs)))))
(quick-bench ; 9.5 -- 2x (let [rs (with-open [con (jdbc/get-connection (ds))]
(jdbc/query {:connection con} ["select * from fruit"])) (with-open [ps (jdbc/prepare
con
(quick-bench ; 8.2 ["select * from fruit where id = ?"])]
(with-transaction [t con {:rollback-only true}] (jdbc/execute! (prep/set-parameters ps [4]))))]
(execute! t ["INSERT INTO fruit (id,name,appearance,cost,grade) VALUES (5,'Pear','green',49,47)"]))) (is (every? map? rs))
(is (every? meta rs))
(quick-bench ; 15.7 (is (= 1 (count rs)))
(with-transaction [t con {:rollback-only true}] (is (= 4 (:FRUIT/ID (first rs))))))
(insert! t :fruit {:id 5, :name "Pear", :appearance "green", :cost 49, :grade 47}))) (testing "transact"
(is (= [{:next.jdbc/update-count 1}]
(quick-bench ; 13.6 -- 1.6x (jdbc/transact (ds)
(jdbc/with-db-transaction [t {:connection con}] (fn [t] (jdbc/execute! t ["
(jdbc/db-set-rollback-only! t) INSERT INTO fruit (id, name, appearance, cost, grade)
(jdbc/execute! t ["INSERT INTO fruit (id,name,appearance,cost,grade) VALUES (5,'Pear','green',49,47)"]))) VALUES (5, 'Pear', 'green', 49, 47)
"]))
(quick-bench ; 27.9-28.8 -- 1.8x {:rollback-only true})))
(jdbc/with-db-transaction [t {:connection con}] (is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"])))))
(jdbc/db-set-rollback-only! t) (testing "with-transaction"
(jdbc/insert! t :fruit {:id 5, :name "Pear", :appearance "green", :cost 49, :grade 47}))) (is (= [{:next.jdbc/update-count 1}]
(jdbc/with-transaction [t (ds) {:rollback-only true}]
(delete! con :fruit {:id 5}) (jdbc/execute! t ["
;; with a prepopulated prepared statement - 450ns INSERT INTO fruit (id, name, appearance, cost, grade)
(with-open [ps (prepare con ["select * from fruit where appearance = ?" "red"] {})] VALUES (5, 'Pear', 'green', 49, 47)
(quick-bench "]))))
[(reduce (fn [_ row] (reduced (:name row))) (is (= 4 (count (jdbc/execute! (ds) ["select * from fruit"]))))))
nil
(reducible! ps))]))
(require '[next.jdbc.prepare :as prepare])
;; same as above but setting parameters inside the benchmark
(with-open [ps (prepare con ["select * from fruit where appearance = ?"] {})]
(quick-bench
[(reduce (fn [_ row] (reduced (:name row)))
nil
(reducible! (prepare/set-parameters ps ["red"])))]))
;; this takes more than twice the time of the one above which seems strange
(with-open [ps (prepare con ["select * from fruit where appearance = ?"] {})]
(quick-bench
[(reduce (fn [_ row] (reduced (:name row)))
nil
(reducible! (prepare/set-parameters ps ["red"])))
(reduce (fn [_ row] (reduced (:name row)))
nil
(reducible! (prepare/set-parameters ps ["fuzzy"])))]))
;; full first row
(quick-bench
(execute-one! con ["select * from fruit where appearance = ?" "red"]))
(with-transaction [t con {:rollback-only true}]
(insert! t :fruit {:id 5, :name "Pear", :appearance "green", :cost 49, :grade 47})
(query t ["select * from fruit where name = ?" "Pear"]))
(query con ["select * from fruit where name = ?" "Pear"])
(delete! con :fruit {:id 1})
(update! con :fruit {:appearance "Brown"} {:name "Banana"})
(reduce (fn [rs m] (reduced (assoc m :test 42)))
nil
(reducible! con ["select * from fruit where appearance = ?" "red"]))
(reduce (fn [rs m] (reduced (rs/datafiable-row (assoc m :test 42) con {})))
nil
(reducible! con ["select * from fruit where appearance = ?" "red"]))
(reduce (fn [rs m] (reduced (rs/datafiable-row m con {})))
nil
(reducible! con ["select * from fruit where appearance = ?" "red"]))
(defrecord Fruit [id name appearance cost grade])
(defn fruit-builder [^java.sql.ResultSet rs opts]
(reify
rs/RowBuilder
(->row [_] (->Fruit (.getObject rs "id")
(.getObject rs "name")
(.getObject rs "appearance")
(.getObject rs "cost")
(.getObject rs "grade")))
(with-column [_ row i] row)
(column-count [_] 0) ; no need to iterate over columns
(row! [_ row] row)
rs/ResultSetBuilder
(->rs [_] (transient []))
(with-row [_ rs row] (conj! rs row))
(rs! [_ rs] (persistent! rs))))
(quick-bench ; 2.2 micros
(execute-one! con ["select * from fruit where appearance = ?" "red"]
{:gen-fn fruit-builder}))
(quick-bench ; 2.47 micros
(execute! con ["select * from fruit where appearance = ?" "red"]
{:gen-fn fruit-builder}))
(quick-bench ; 3 micros
(execute! con ["select * from fruit"]
{:gen-fn fruit-builder}))
;; with a prepopulated prepared statement - 1.7-1.8 micros
(with-open [ps (prepare con ["select * from fruit"] {})]
(quick-bench
(execute! ps [] {:gen-fn fruit-builder}))))