/test/lobos/test/connectivity.clj

http://github.com/budu/lobos · Clojure · 97 lines · 80 code · 10 blank · 7 comment · 12 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. (ns lobos.test.connectivity
  9. (:use clojure.test
  10. lobos.connectivity))
  11. (def ^{:dynamic true} *cnx*)
  12. (defn cnx-fixture [f]
  13. (binding [*cnx* (reify java.sql.Connection (close [this] nil))
  14. *get-cnx* (fn [& _] *cnx*)]
  15. (f)))
  16. (use-fixtures :each cnx-fixture)
  17. (deftest test-open-and-close-global
  18. (open-global {})
  19. (is (= @global-connections
  20. {:default-connection {:connection *cnx* :db-spec {}}})
  21. "Opening a default global connection")
  22. (close-global)
  23. (is (= @global-connections {})
  24. "Closing a default global connection")
  25. (open-global :foo {})
  26. (is (= @global-connections
  27. {:foo {:connection *cnx* :db-spec {}}})
  28. "Opening a named global connection")
  29. (close-global :foo)
  30. (is (= @global-connections {})
  31. "Closing a named global connection")
  32. (open-global :foo {:unsafe true})
  33. (open-global :foo {})
  34. (is (= @global-connections
  35. {:foo {:connection *cnx* :db-spec {}}})
  36. "Re-opening a named global connection")
  37. (is (thrown? Exception
  38. (open-global :foo nil))
  39. "Opening a global connection with a existing name")
  40. (close-global :foo)
  41. (is (thrown? Exception
  42. (close-global :foo))
  43. "Closing an inexistant global connection"))
  44. (deftest test-with-named-connection
  45. (is (thrown? Exception
  46. (with-named-connection :test
  47. #(identity)))
  48. "With inexistant named connection")
  49. (open-global :test {})
  50. (is (= (with-named-connection :test
  51. #(connection))
  52. *cnx*)
  53. "With existant named connection")
  54. (close-global :test))
  55. (deftest test-with-spec-connection
  56. (is (= (with-spec-connection {}
  57. #(connection))
  58. *cnx*)
  59. "With connection specification"))
  60. (deftest test-with-connection
  61. (is (= (with-connection {}
  62. (connection))
  63. *cnx*)
  64. "With connection specification")
  65. (is (thrown? Exception
  66. (with-connection :test
  67. nil))
  68. "With inexistant named connection")
  69. (open-global :test {})
  70. (is (= (with-connection :test
  71. (connection))
  72. *cnx*)
  73. "With existant named connection")
  74. (close-global :test))
  75. (deftest test-default-connection
  76. (open-global {})
  77. (is (= (default-connection)
  78. *cnx*)
  79. "Default connection")
  80. (close-global))
  81. (deftest test-get-db-spec
  82. (is (= (get-db-spec {}) {})
  83. "Get db-spec from db-spec")
  84. (open-global {})
  85. (is (= (get-db-spec) {})
  86. "Get db-spec from global connection")
  87. (close-global))