PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/test/lobos/test/connectivity.clj

http://github.com/budu/lobos
Clojure | 97 lines | 80 code | 10 blank | 7 comment | 0 complexity | 04e5272596861cf42296d0b6a0f52444 MD5 | raw file
 1;; Copyright (c) Nicolas Buduroi. All rights reserved.
 2;; The use and distribution terms for this software are covered by the
 3;; Eclipse Public License 1.0 which can be found in the file
 4;; epl-v10.html at the root of this distribution. By using this software
 5;; in any fashion, you are agreeing to be bound by the terms of this
 6;; license.
 7;; You must not remove this notice, or any other, from this software.
 8
 9(ns lobos.test.connectivity
10  (:use clojure.test
11        lobos.connectivity))
12
13(def ^{:dynamic true} *cnx*)
14
15(defn cnx-fixture [f]
16  (binding [*cnx* (reify java.sql.Connection (close [this] nil))
17            *get-cnx* (fn [& _] *cnx*)]
18    (f)))
19
20(use-fixtures :each cnx-fixture)
21
22(deftest test-open-and-close-global
23  (open-global {})
24  (is (= @global-connections
25         {:default-connection {:connection *cnx* :db-spec {}}})
26      "Opening a default global connection")
27  (close-global)
28  (is (= @global-connections {})
29      "Closing a default global connection")
30  (open-global :foo {})
31  (is (= @global-connections
32         {:foo {:connection *cnx* :db-spec {}}})
33      "Opening a named global connection")
34  (close-global :foo)
35  (is (= @global-connections {})
36      "Closing a named global connection")
37  (open-global :foo {:unsafe true})
38  (open-global :foo {})
39  (is (= @global-connections
40         {:foo {:connection *cnx* :db-spec {}}})
41      "Re-opening a named global connection")
42  (is (thrown? Exception
43               (open-global :foo nil))
44      "Opening a global connection with a existing name")
45  (close-global :foo)
46  (is (thrown? Exception
47               (close-global :foo))
48      "Closing an inexistant global connection"))
49
50(deftest test-with-named-connection
51  (is (thrown? Exception
52               (with-named-connection :test
53                 #(identity)))
54      "With inexistant named connection")
55  (open-global :test {})
56  (is (= (with-named-connection :test
57           #(connection))
58         *cnx*)
59      "With existant named connection")
60  (close-global :test))
61
62(deftest test-with-spec-connection
63  (is (= (with-spec-connection {}
64           #(connection))
65         *cnx*)
66      "With connection specification"))
67
68(deftest test-with-connection
69  (is (= (with-connection {}
70           (connection))
71         *cnx*)
72      "With connection specification")
73  (is (thrown? Exception
74               (with-connection :test
75                 nil))
76      "With inexistant named connection")
77  (open-global :test {})
78  (is (= (with-connection :test
79           (connection))
80         *cnx*)
81      "With existant named connection")
82  (close-global :test))
83
84(deftest test-default-connection
85  (open-global {})
86  (is (= (default-connection)
87         *cnx*)
88      "Default connection")
89  (close-global))
90
91(deftest test-get-db-spec
92  (is (= (get-db-spec {}) {})
93      "Get db-spec from db-spec")
94  (open-global {})
95  (is (= (get-db-spec) {})
96      "Get db-spec from global connection")
97  (close-global))