File: plan_test.clj

package info (click to toggle)
next-jdbc-clojure 1.3.955-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 876 kB
  • sloc: xml: 62; sh: 39; makefile: 13
file content (72 lines) | stat: -rw-r--r-- 2,917 bytes parent folder | download
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
;; copyright (c) 2020-2021 Sean Corfield, all rights reserved

(ns next.jdbc.plan-test
  "Tests for the plan helpers."
  (:require [clojure.test :refer [deftest is use-fixtures]]
            [next.jdbc.plan :as plan]
            [next.jdbc.specs :as specs]
            [next.jdbc.test-fixtures
             :refer [with-test-db ds]]
            [clojure.string :as str]))

(set! *warn-on-reflection* true)

;; around each test because of the folding tests using 1,000 rows
(use-fixtures :each with-test-db)

(specs/instrument)

(deftest select-one!-tests
  (is (= {:id 1}
         (plan/select-one! (ds) [:id] ["select * from fruit order by id"])))
  (is (= 1
         (plan/select-one! (ds) :id ["select * from fruit order by id"])))
  (is (= "Banana"
         (plan/select-one! (ds) :name ["select * from fruit where id = ?" 2])))
  (is (= [1 "Apple"]
         (plan/select-one! (ds) (juxt :id :name)
                           ["select * from fruit order by id"])))
  (is (= {:id 1 :name "Apple"}
         (plan/select-one! (ds) #(select-keys % [:id :name])
                           ["select * from fruit order by id"]))))

(deftest select-vector-tests
  (is (= [{:id 1} {:id 2} {:id 3} {:id 4}]
         (plan/select! (ds) [:id] ["select * from fruit order by id"])))
  (is (= [1 2 3 4]
         (plan/select! (ds) :id ["select * from fruit order by id"])))
  (is (= ["Banana"]
         (plan/select! (ds) :name ["select * from fruit where id = ?" 2])))
  (is (= [[2 "Banana"]]
         (plan/select! (ds) (juxt :id :name)
                       ["select * from fruit where id = ?" 2])))
  (is (= [{:id 2 :name "Banana"}]
         (plan/select! (ds) [:id :name]
                       ["select * from fruit where id = ?" 2]))))

(deftest select-set-tests
  (is (= #{{:id 1} {:id 2} {:id 3} {:id 4}}
         (plan/select! (ds) [:id] ["select * from fruit order by id"]
                       {:into #{}})))
  (is (= #{1 2 3 4}
         (plan/select! (ds) :id ["select * from fruit order by id"]
                       {:into #{}}))))

(deftest select-map-tests
  (is (= {1 "Apple", 2 "Banana", 3 "Peach", 4 "Orange"}
         (plan/select! (ds) (juxt :id :name) ["select * from fruit order by id"]
                       {:into {}}))))

(deftest select-issue-227
  (is (= ["Apple"]
         (plan/select! (ds) :name ["select * from fruit where id = ?" 1]
                       {:column-fn #(str/replace % "-" "_")})))
  (is (= ["Apple"]
         (plan/select! (ds) :foo/name ["select * from fruit where id = ?" 1]
                       {:column-fn #(str/replace % "-" "_")})))
  (is (= ["Apple"]
         (plan/select! (ds) #(get % "name") ["select * from fruit where id = ?" 1]
                       {:column-fn #(str/replace % "-" "_")})))
  (is (= [["Apple"]]
         (plan/select! (ds) (juxt :name) ["select * from fruit where id = ?" 1]
                       {:column-fn #(str/replace % "-" "_")}))))