/
middleware_test.clj
79 lines (72 loc) · 3.2 KB
/
middleware_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
;; copyright (c) 2019-2020 Sean Corfield, all rights reserved
(ns next.jdbc.middleware-test
(:require [clojure.test :refer [deftest is testing use-fixtures]]
[next.jdbc :as jdbc]
[next.jdbc.connection :as c]
[next.jdbc.middleware :as mw]
[next.jdbc.test-fixtures :refer [with-test-db db ds
default-options
derby? postgres?]]
[next.jdbc.prepare :as prep]
[next.jdbc.result-set :as rs]
[next.jdbc.specs :as specs])
(:import (java.sql ResultSet ResultSetMetaData)))
(set! *warn-on-reflection* true)
(use-fixtures :once with-test-db)
(specs/instrument)
(deftest logging-test
(let [logging (atom [])
logger (fn [data _] (swap! logging conj data) data)
sql-p ["select * from fruit where id in (?,?) order by id desc" 1 4]]
(jdbc/execute! (mw/wrapper (ds))
sql-p
(assoc (default-options)
:builder-fn rs/as-lower-maps
:sql-params-fn logger
:row!-fn logger
:rs!-fn logger))
;; should log four things
(is (= 4 (-> @logging count)))
;; :next.jdbc/sql-params value
(is (= sql-p (-> @logging (nth 0))))
;; first row (with PK 4)
(is (= 4 (-> @logging (nth 1) :fruit/id)))
;; second row (with PK 1)
(is (= 1 (-> @logging (nth 2) :fruit/id)))
;; full result set with two rows
(is (= 2 (-> @logging (nth 3) count)))
(is (= [4 1] (-> @logging (nth 3) (->> (map :fruit/id)))))
;; now repeat without the row logging
(reset! logging [])
(jdbc/execute! (mw/wrapper (ds)
{:builder-fn rs/as-lower-maps
:sql-params-fn logger
:rs!-fn logger})
sql-p
(default-options))
;; should log two things
(is (= 2 (-> @logging count)))
;; :next.jdbc/sql-params value
(is (= sql-p (-> @logging (nth 0))))
;; full result set with two rows
(is (= 2 (-> @logging (nth 1) count)))
(is (= [4 1] (-> @logging (nth 1) (->> (map :fruit/id)))))))
(deftest timing-test
(let [timing (atom {:calls 0 :total 0.0})
start-fn (fn [sql-p opts]
(swap! (:timing opts) update :calls inc)
(assoc opts :start (System/nanoTime)))
exec-fn (fn [_ opts]
(let [end (System/nanoTime)]
(swap! (:timing opts) update :total + (- end (:start opts)))
opts))
sql-p ["select * from fruit where id in (?,?) order by id desc" 1 4]]
(jdbc/execute! (mw/wrapper (ds) {:timing timing
:sql-params-fn start-fn
:execute-fn exec-fn})
sql-p)
(jdbc/execute! (mw/wrapper (ds) {:timing timing
:sql-params-fn start-fn
:execute-fn exec-fn})
sql-p)
(println (db) (:calls @timing) "calls took" (long (:total @timing)) "nanoseconds")))