/test/lobos/test/compiler.clj

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