PageRenderTime 26ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/test/lobos/test/compiler.clj

http://github.com/budu/lobos
Clojure | 201 lines | 170 code | 22 blank | 9 comment | 0 complexity | 010543d2220e69d3502f1e176ba52550 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.compiler
 10  (:refer-clojure :exclude [compile])
 11  (:use clojure.test
 12        (lobos [schema :only [expression build-definition]])
 13        lobos.compiler
 14        lobos.ast))
 15
 16(import-all)
 17
 18;;;; Helpers
 19
 20(deftest test-unsupported
 21  (is (thrown-with-msg? java.lang.UnsupportedOperationException
 22        #"foo"
 23        (unsupported true "foo"))))
 24
 25;;;; Default compiler
 26
 27(deftest test-compile-scalar-expression
 28  (is (= (compile (ScalarExpression. nil :foo))
 29         "FOO")
 30      "Compiling a SQL keyword")
 31  (is (= (compile (ScalarExpression. nil "foo"))
 32         "'foo'")
 33      "Compiling a string"))
 34
 35(deftest test-compile-auto-inc-clause
 36  (is (= (compile (AutoIncClause. nil))
 37         "GENERATED ALWAYS AS IDENTITY")
 38      "Compiling an auto-incrementing clause"))
 39
 40(def column-definition-stub
 41  (let [data-type (DataTypeClause. nil :integer nil nil)]
 42    (ColumnDefinition. nil :foo data-type nil false false [])))
 43
 44(deftest test-compile-column-definition
 45  (is (= (compile column-definition-stub)
 46         "\"foo\" INTEGER NULL")
 47      "Compiling a simple column definition")
 48  (is (= (compile (assoc column-definition-stub
 49                    :default (ScalarExpression. nil 0)))
 50         "\"foo\" INTEGER DEFAULT 0 NULL")
 51      "Compiling a column definition with default value")
 52  (is (= (compile (assoc column-definition-stub
 53                    :auto-inc (AutoIncClause. nil)))
 54         "\"foo\" INTEGER GENERATED ALWAYS AS IDENTITY NULL")
 55      "Compiling a column definition with auto-incrementing clause")
 56  (is (= (compile (assoc column-definition-stub
 57                    :not-null true))
 58         "\"foo\" INTEGER NOT NULL")
 59      "Compiling a column definition with not null option")
 60  (is (= (compile (assoc column-definition-stub
 61                    :others ["BAR" "BAZ"]))
 62         "\"foo\" INTEGER NULL BAR BAZ")
 63      "Compiling a column definition with custom options"))
 64
 65(def unique-constraint-definition-stub
 66  (UniqueConstraintDefinition. nil :foo_a_b_c :unique [:a :b :c]))
 67
 68(deftest test-compile-unique-constraint-definition
 69  (is (= (compile unique-constraint-definition-stub)
 70         "CONSTRAINT \"foo_a_b_c\" UNIQUE (\"a\", \"b\", \"c\")")
 71      "Compiling a unique constraint definition")
 72  (is (= (compile (assoc unique-constraint-definition-stub
 73                    :ctype :primary-key))
 74         "CONSTRAINT \"foo_a_b_c\" PRIMARY KEY (\"a\", \"b\", \"c\")")
 75      "Compiling a primary key constraint definition"))
 76
 77(def foreign-key-constraint-definition-stub
 78  (ForeignKeyConstraintDefinition.
 79   nil
 80   :foo_fkey_a_b_c
 81   [:a :b :c]
 82   :bar
 83   [:a :b :c]
 84   nil
 85   {}))
 86
 87(deftest test-compile-foreign-key-constraint-definition
 88  (is (= (compile foreign-key-constraint-definition-stub)
 89         (str "CONSTRAINT \"foo_fkey_a_b_c\" "
 90              "FOREIGN KEY (\"a\", \"b\", \"c\") "
 91              "REFERENCES \"bar\" (\"a\", \"b\", \"c\")"))
 92      "Compiling a foreign key constraint definition")
 93  (is (= (compile (assoc foreign-key-constraint-definition-stub
 94                    :parent-columns [:d :e :f]))
 95         (str "CONSTRAINT \"foo_fkey_a_b_c\" "
 96              "FOREIGN KEY (\"a\", \"b\", \"c\") "
 97              "REFERENCES \"bar\" (\"d\", \"e\", \"f\")"))
 98      (str "Compiling a foreign key constraint definition"
 99           " with different parent columns"))
100  (is (= (compile (assoc foreign-key-constraint-definition-stub
101                    :match :full))
102         (str "CONSTRAINT \"foo_fkey_a_b_c\" "
103              "FOREIGN KEY (\"a\", \"b\", \"c\") "
104              "REFERENCES \"bar\" (\"a\", \"b\", \"c\") MATCH FULL"))
105      "Compiling a foreign key constraint definition with match type")
106  (is (= (compile (assoc foreign-key-constraint-definition-stub
107                    :triggered-actions {:on-delete :cascade
108                                        :on-update :restrict}))
109         (str "CONSTRAINT \"foo_fkey_a_b_c\" "
110              "FOREIGN KEY (\"a\", \"b\", \"c\") "
111              "REFERENCES \"bar\" (\"a\", \"b\", \"c\") "
112              "ON DELETE CASCADE ON UPDATE RESTRICT"))
113      (str "Compiling a foreign key constraint definition"
114           " with triggered actions")))
115
116(deftest test-compile-check-constraint-definition
117  (is (= (compile (CheckConstraintDefinition.
118                   nil
119                   :bar
120                   (build-definition (expression (> :a 1)) nil)))
121         "CONSTRAINT \"bar\" CHECK (\"a\" > 1)")
122      "Compiling a simple check constraint definition")
123  (is (= (compile (CheckConstraintDefinition.
124                   nil
125                   :bar
126                   (build-definition
127                    (expression (and (> :a 1) (< :a 10)
128                                     (or (= :b "foo")
129                                         (= :b "bar"))
130                                     (in :ab [1 2 3])))
131                    nil)))
132         (str "CONSTRAINT \"bar\" CHECK "
133              "((\"a\" > 1) AND (\"a\" < 10) AND "
134              "((\"b\" = 'foo') OR (\"b\" = 'bar')) AND "
135              "(\"ab\" IN (1, 2, 3)))"))
136      "Compiling a complex check constraint definition"))
137
138(def create-schema-statement-stub
139  (CreateSchemaStatement. nil :foo []))
140
141(def create-table-statement-stub
142  (CreateTableStatement. nil :foo []))
143
144(deftest test-compile-create-schema-statement
145  (is (= (compile create-schema-statement-stub)
146         (list "CREATE SCHEMA \"foo\""))
147      "Compiling an empty create schema statement")
148  (is (= (compile (assoc create-schema-statement-stub
149                    :elements [create-table-statement-stub]))
150         (list "CREATE SCHEMA \"foo\"\nCREATE TABLE \"foo\" ()"))
151      "Compiling a create schema statement containing a table"))
152
153(deftest test-compile-create-table-statement
154  (is (= (compile create-table-statement-stub)
155         "CREATE TABLE \"foo\" ()")
156      "Compiling a create table statement"))
157
158(def drop-statement-stub
159  (DropStatement. nil :schema :foo nil nil))
160
161(deftest test-compile-drop-statement
162  (is (= (compile drop-statement-stub)
163         "DROP SCHEMA \"foo\"")
164      "Compiling a drop statement")
165  (is (= (compile (assoc drop-statement-stub
166                    :behavior :cascade))
167         "DROP SCHEMA \"foo\" CASCADE")
168      "Compiling a drop statement with cascade behavior"))
169
170(def alter-statement-stub
171  (AlterTableStatement. nil :foo :add column-definition-stub))
172
173(deftest test-compile-alter-statement
174  (is (= (compile alter-statement-stub)
175         "ALTER TABLE \"foo\" ADD \"foo\" INTEGER NULL")
176      "Compiling an alter table statement to add a column")
177  (is (= (compile (assoc alter-statement-stub :action :drop))
178         "ALTER TABLE \"foo\" DROP COLUMN \"foo\"")
179      "Compiling an alter table statement to drop a column")
180  (is (= (compile (assoc alter-statement-stub
181                    :element unique-constraint-definition-stub))
182         (str "ALTER TABLE \"foo\" ADD CONSTRAINT \"foo_a_b_c\" "
183              "UNIQUE (\"a\", \"b\", \"c\")"))
184      "Compiling an alter table statement to add a constraint")
185  (is (= (compile (assoc alter-statement-stub
186                    :action :drop
187                    :element unique-constraint-definition-stub))
188         "ALTER TABLE \"foo\" DROP CONSTRAINT \"foo_a_b_c\"")
189      "Compiling an alter table statement to drop a constraint")
190  (is (= (compile (assoc alter-statement-stub
191                    :action :modify
192                    :element (assoc (dissoc column-definition-stub :data-type)
193                               :default (ScalarExpression. nil 1))))
194         "ALTER TABLE \"foo\" ALTER COLUMN \"foo\" SET DEFAULT 1")
195      "Compiling an alter table statement to set default clause")
196  (is (= (compile (assoc alter-statement-stub
197                    :action :modify
198                    :element (assoc (dissoc column-definition-stub :data-type)
199                               :default :drop)))
200         "ALTER TABLE \"foo\" ALTER COLUMN \"foo\" DROP DEFAULT")
201      "Compiling an alter table statement to drop default clause"))