PageRenderTime 44ms CodeModel.GetById 13ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/test/lobos/test/schema.clj

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