/test/lobos/test/schema.clj

http://github.com/budu/lobos · Clojure · 267 lines · 233 code · 21 blank · 13 comment · 52 complexity · bde040c6c5802d4d8f7ca6fc57eb76cc 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.schema
  9. (:refer-clojure :exclude [bigint boolean char double float time])
  10. (:use clojure.test
  11. lobos.schema)
  12. (:import (lobos.schema CheckConstraint
  13. Column
  14. ForeignKeyConstraint
  15. Index
  16. UniqueConstraint
  17. DataType
  18. Table
  19. Schema)))
  20. ;;;; Constraint definition tests
  21. (deftest test-primary-key
  22. (is (= (table :foo (primary-key [:a :b :c]))
  23. (table* :foo {} {:foo_primary_key_a_b_c
  24. (UniqueConstraint.
  25. :foo_primary_key_a_b_c
  26. :primary-key
  27. [:a :b :c])} {}))
  28. "Unnamed primary key constraint definition")
  29. (is (= (table :foo (primary-key :bar [:a :b :c]))
  30. (table* :foo {} {:bar
  31. (UniqueConstraint. :bar :primary-key [:a :b :c])}))
  32. "Named primary key constraint definition"))
  33. (deftest test-unique
  34. (is (= (table :foo (unique [:a :b :c]))
  35. (table* :foo {} {:foo_unique_a_b_c
  36. (UniqueConstraint.
  37. :foo_unique_a_b_c
  38. :unique
  39. [:a :b :c])} {}))
  40. "Unnamed unique constraint definition")
  41. (is (= (table :foo (unique :bar [:a :b :c]))
  42. (table* :foo {} {:bar (UniqueConstraint. :bar :unique [:a :b :c])}))
  43. "Named unique constraint definition"))
  44. (def foreign-key-stub
  45. (ForeignKeyConstraint. :foo_fkey_a_b_c [:a :b :c] :bar [:a :b :c] nil {}))
  46. (deftest test-foreign-key
  47. (is (= (table :foo (foreign-key [:a :b :c] :bar))
  48. (table* :foo {} {:foo_fkey_a_b_c foreign-key-stub}))
  49. "Foreign key constraint definition")
  50. (is (= (table :foo (foreign-key :foobar [:a :b :c] :bar))
  51. (table* :foo {} {:foobar (assoc foreign-key-stub
  52. :cname :foobar)}))
  53. "Foreign key constraint definition with name")
  54. (is (= (table :foo (foreign-key [:a :b :c] :bar [:d :e :f]))
  55. (table* :foo {} {:foo_fkey_a_b_c (assoc foreign-key-stub
  56. :parent-columns [:d :e :f])}))
  57. "Foreign key constraint definition with foreign columns")
  58. (is (= (table :foo (foreign-key [:a :b :c] :bar :full))
  59. (table* :foo {} {:foo_fkey_a_b_c (assoc foreign-key-stub
  60. :match :full)}))
  61. "Foreign key constraint definition with match")
  62. (is (= (table :foo (foreign-key [:a :b :c] :bar :on-delete :cascade))
  63. (table* :foo {} {:foo_fkey_a_b_c (assoc foreign-key-stub
  64. :triggered-actions
  65. {:on-delete :cascade})}))
  66. "Foreign key constraint definition with one triggered action")
  67. (is (= (table :foo (foreign-key [:a :b :c] :bar :on-delete :cascade
  68. :on-update :restrict))
  69. (table* :foo {} {:foo_fkey_a_b_c (assoc foreign-key-stub
  70. :triggered-actions
  71. {:on-delete :cascade
  72. :on-update :restrict})}))
  73. "Foreign key constraint definition with two triggered actions")
  74. (is (= (table :foo (foreign-key :foobar [:a :b :c]
  75. :bar [:d :e :f]
  76. :full
  77. :on-delete :cascade
  78. :on-update :restrict))
  79. (table* :foo {} {:foobar (assoc foreign-key-stub
  80. :cname :foobar
  81. :parent-columns [:d :e :f]
  82. :match :full
  83. :triggered-actions
  84. {:on-delete :cascade
  85. :on-update :restrict})}))
  86. "Foreign key constraint definition with everything"))
  87. (deftest test-check
  88. (is (= (-> (table :foo (check :bar (> :a 1))) :constraints :bar)
  89. (CheckConstraint. :bar (expression (> :a 1))))
  90. "Simple check constraint definition")
  91. (is (= (-> (table :foo (check :bar (and (> :a 1) (< :a 10)
  92. (or (= :b "foo")
  93. (= :b "bar"))
  94. (in :ab [1 2 3])))) :constraints :bar)
  95. (CheckConstraint. :bar
  96. (expression (and (> :a 1) (< :a 10)
  97. (or (= :b "foo")
  98. (= :b "bar"))
  99. (in :ab [1 2 3])))))
  100. "Complex check constraint definition"))
  101. ;;;; Index definition tests
  102. (deftest test-index
  103. (is (= (index :foo [:bar :baz])
  104. (Index. :foo_index_bar_baz :foo [:bar :baz] nil))
  105. "Simple index definition")
  106. (is (= (index :foo [:bar :baz] :unique)
  107. (Index. :foo_unique_bar_baz :foo [:bar :baz] [:unique]))
  108. "Simple index definition with options")
  109. (is (= (index :foo :foo_index [:bar :baz])
  110. (Index. :foo_index :foo [:bar :baz] nil))
  111. "Named index definition")
  112. (is (= (index :foo :foo_index [:bar :baz] :unique)
  113. (Index. :foo_index :foo [:bar :baz] [:unique]))
  114. "Named index definition with options"))
  115. ;;;; Column and data-type definition tests
  116. (def column-definition-stub
  117. (Column. :foo nil nil nil nil []))
  118. (deftest test-column
  119. (testing "Column definition"
  120. (is (= (table :foo (column :foo nil nil))
  121. (table* :foo {:foo column-definition-stub} {} {}))
  122. "Column with name and data-type")
  123. (is (= (table :foo (column :foo nil :auto-inc))
  124. (table* :foo {:foo (assoc column-definition-stub
  125. :auto-inc true)} {} {}))
  126. "Setting column as auto incremented")
  127. (is (= (table :foo (column :foo nil :not-null))
  128. (table* :foo {:foo (assoc column-definition-stub
  129. :not-null true)} {} {}))
  130. "Setting column as not null")
  131. (is (= (table :foo (column :foo nil [:default 0]))
  132. (table* :foo {:foo (assoc column-definition-stub
  133. :default 0)} {} {}))
  134. "Setting default value")
  135. (is (= (table :foo (column :foo nil "BAR"))
  136. (table* :foo {:foo (assoc column-definition-stub
  137. :others ["BAR"])} {} {}))
  138. "Setting custom options")
  139. (is (= (table :foo (column :foo :to :bar))
  140. (table* :foo {:foo (assoc column-definition-stub
  141. :others :bar)}))
  142. "Should set :others value to the new column name")
  143. (is (= (table :foo (column :foo :drop-default))
  144. (table* :foo {:foo (assoc column-definition-stub
  145. :default :drop)}))
  146. "Should set :default value to :drop")))
  147. (deftest test-typed-column
  148. (testing "Typed column definition"
  149. (is (= (table :foo (integer :foo))
  150. (table* :foo {:foo (assoc column-definition-stub
  151. :data-type (data-type :integer))} {} {}))
  152. "Integer")
  153. (is (= (table :foo (numeric :foo))
  154. (table* :foo {:foo (assoc column-definition-stub
  155. :data-type (data-type :numeric))} {} {}))
  156. "Numeric")
  157. (is (= (table :foo (numeric :foo 2))
  158. (table* :foo {:foo (assoc column-definition-stub
  159. :data-type (data-type :numeric [2]))} {} {}))
  160. "Numeric with precision")
  161. (is (= (table :foo (numeric :foo 2 4))
  162. (table* :foo {:foo (assoc column-definition-stub
  163. :data-type (data-type :numeric [2 4]))} {} {}))
  164. "Numeric with precision and scale")
  165. (is (= (table :foo (numeric :foo "BAR"))
  166. (table* :foo {:foo (assoc column-definition-stub
  167. :data-type (data-type :numeric)
  168. :others ["BAR"])} {} {}))
  169. "Numeric with other option")
  170. (is (= (table :foo (float :foo))
  171. (table* :foo {:foo (assoc column-definition-stub
  172. :data-type (data-type :float))} {} {}))
  173. "Float")
  174. (is (= (table :foo (float :foo 1))
  175. (table* :foo {:foo (assoc column-definition-stub
  176. :data-type (data-type :float [1]))} {} {}))
  177. "Float with precision")
  178. (is (= (table :foo (varchar :foo 1))
  179. (table* :foo {:foo (assoc column-definition-stub
  180. :data-type (data-type :varchar [1]))} {} {}))
  181. "Varchar with limit")
  182. (is (= (table :foo (varchar :foo 1 "BAR"))
  183. (table* :foo {:foo (assoc column-definition-stub
  184. :data-type (data-type :varchar [1])
  185. :others ["BAR"])} {} {}))
  186. "Varchar with other option")
  187. (is (= (table :foo (timestamp :foo))
  188. (table* :foo {:foo (assoc column-definition-stub
  189. :data-type (data-type :timestamp))} {} {}))
  190. "Timestamp")))
  191. ;;;; Table definition tests
  192. (deftest test-table*
  193. (is (= (table* :foo nil nil nil)
  194. (Table. :foo {} {} {}))
  195. "Table definition"))
  196. (deftest test-table
  197. (is (= (table :foo)
  198. (Table. :foo {} {} {}))
  199. "Empty table")
  200. (is (= (table :foo (column :foo nil nil))
  201. (Table. :foo {:foo column-definition-stub} {} {}))
  202. "Table with column")
  203. (is (= (table :foo (unique :bar [:a]))
  204. (Table. :foo {} {:bar (UniqueConstraint. :bar :unique [:a])} {}))
  205. "Table with constraint"))
  206. ;;;; Schema definition tests
  207. (deftest test-schema
  208. (is (= (schema :foo)
  209. (Schema. :foo {} {} {}))
  210. "Empty schema")
  211. (is (= (schema :foo {:bar nil :baz nil})
  212. (Schema. :foo {} {} {:bar nil
  213. :baz nil}))
  214. "Empty schema with option.")
  215. (is (= (schema :foo (table :bar) (table :baz))
  216. (Schema. :foo {:bar (table :bar)
  217. :baz (table :baz)} {} {}))
  218. "Schema with tables")
  219. (is (= (schema :foo {:bar nil :baz nil}
  220. (table :bar)
  221. (table :baz))
  222. (Schema. :foo
  223. {:bar (table :bar)
  224. :baz (table :baz)}
  225. {}
  226. {:bar nil
  227. :baz nil}))
  228. "Schema with tables and options."))
  229. ;;;; Invalid definition tests
  230. (deftest test-name-required
  231. (doseq [definition [#(column* nil nil nil)
  232. #(table* nil nil nil nil)
  233. #(schema nil)]]
  234. (is (thrown-with-msg? IllegalArgumentException
  235. #"A .*? definition needs at least a name."
  236. (definition))
  237. "Should throw an exception when no name is provided.")))
  238. (deftest test-check-valid-options
  239. (doseq [definition [#(column* :foo nil [:uto-inc])
  240. #(column* :foo nil [[:ncoding ""]])
  241. #(column* :foo nil [[:refr :foo :bar]])
  242. #(data-type :foo nil [:ncoding])]]
  243. (is (thrown-with-msg? IllegalArgumentException
  244. #".*? are invalid, only .*? options are valid."
  245. (definition))
  246. "Should throw an exception when options are mistyped.")))