Clj-Liquibase / src / test / clj / org / bituf / clj_liquibase / test_change.clj

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
(ns org.bituf.clj-liquibase.test-change
  (:import
    (java.util Date)
    (liquibase.change.core
      ;; Structural Refactorings
      AddColumnChange           RenameColumnChange         ModifyDataTypeChange
      DropColumnChange          AlterSequenceChange        CreateTableChange
      RenameTableChange         DropTableChange            CreateViewChange
      RenameViewChange          DropViewChange             MergeColumnChange
      CreateProcedureChange
      ;; Data Quality Refactorings
      AddLookupTableChange      AddNotNullConstraintChange DropNotNullConstraintChange
      AddUniqueConstraintChange DropUniqueConstraintChange CreateSequenceChange
      DropSequenceChange        AddAutoIncrementChange     AddDefaultValueChange
      DropDefaultValueChange
      ;; Referential Integrity Refactorings
      AddForeignKeyConstraintChange DropForeignKeyConstraintChange
      AddPrimaryKeyChange           DropPrimaryKeyChange
      ;; Non-Refactoring Transformations
      InsertDataChange LoadDataChange    LoadUpdateDataChange UpdateDataChange
      DeleteDataChange TagDatabaseChange StopChange
      ;; Architectural Refactorings
      CreateIndexChange DropIndexChange)
    (liquibase.statement DatabaseFunction)
    (liquibase.util      ISODateFormat))
  (:require
    [org.bituf.clj-liquibase.change :as change])
  (:use org.bituf.test-util)
  (:use clojure.test))


(defn test-change
  "Assert Change instance by running 'pch' partial function through arg colls.
  Each arg coll is a list where the first element is test description (string)."
  [^Class ch-class pch argcoll & argcolls]
  (let [ch? #(instance? ch-class %)]
    (doseq [each (into [argcoll] argcolls)]
      (let [desc (first each)
            args (rest each)]
        (is (ch? (apply pch args)) desc)))))


(def min-args ["Minimum args"])
(def with-schema-name  ["With :schema-name argument" :schema-name :sample])
(def with-schema       ["With :schema argument"      :schema      :sample])
(def with-where-clause ["With :where-clause value" :where-clause "grade < 5"])
(def with-where        ["With :where value"        :where        "code NOT NULL"])

(def long-names  "Optional args with long-names")
(def short-names "Optional args with short-names")


;; ----- Structural Refactorings -----


(deftest test-add-columns
  (testing "add-columns with required args"
    (let [ch  change/add-columns
          pch (partial ch :table [[:emp :char]])]
      (is (thrown? IllegalArgumentException (ch :table []))        "Empty columndef-list")
      (is (thrown? IllegalArgumentException (ch :table [[:name]])) "Insufficient arguments")
      (test-change AddColumnChange (partial ch :table [[:name :type]])
        min-args)
      (test-change AddColumnChange
        (partial ch :table [[:name :type][:name2 :type2]]) ["Multiple columndefs"])
      (test-change AddColumnChange
        (partial ch :table [[:name :type :default true]]) ["With optional argument"])
      (test-change AddColumnChange pch with-schema-name with-schema))))


(deftest test-rename-column
  (testing "rename-column"
    (test-change RenameColumnChange
      (partial change/rename-column :table :oldname :newname)
      min-args
      with-schema-name
      with-schema
      ["With :column-data-type argument" :column-data-type :char]
      ["With :data-type argument"        :data-type        :char])))


(deftest test-modify-column
  (testing "modify-column"
    (test-change ModifyDataTypeChange
      (partial change/modify-column :table :colname [:float 10 7])
      min-args
      with-schema-name
      with-schema)))


(deftest test-drop-column
  (testing "drop-column"
    (test-change DropColumnChange
      (partial change/drop-column :table :colname)
      min-args
      with-schema-name
      with-schema)))


(deftest test-alter-sequence
  (testing "alter-sequence"
    (test-change AlterSequenceChange
      (partial change/alter-sequence :seq-name 500)
      min-args
      with-schema-name
      with-schema
      [long-names
       :max-value   6779    ; number or string
       :min-value   2000    ; number or string
       :ordered     true    ; Boolean
       ]
      [short-names
       :max    5697     ; number or string
       :min    "56"     ; number or string
       :ord    true     ; Boolean
       ])))


(deftest test-create-table
  (testing "create-table"
    (let [ct  change/create-table
          pch (partial ct :emp [[:c1 :int] [:c2 [:varchar 30]]])]
      (is (thrown? IllegalArgumentException
            (ct :tbl-name []))        "No column-defs")
      (test-change CreateTableChange
        (partial ct :tbl-name [[:c1 :int]]) min-args)
      (test-change CreateTableChange pch
        with-schema-name
        with-schema
        [long-names
         :table-space :hello  ; string/Keyword - s.t. clj-to-dbident
         :remarks     "Later" ; string
         ]
        [short-names
         :tspace  :go-air  ; string/Keyword - s.t. clj-to-dbident
         :remarks "Notnow" ; string
         ])))
  (testing "create-table-withid"
           (test-change CreateTableChange
                        (partial change/create-table-withid :emp
                                 [[:c1 :int] [:c2 [:varchar 30]]])
                        min-args)
           (test-change CreateTableChange
                        (partial change/create-table-withid :emp
                                 [[:c1 :int] [:c2 [:varchar 30]]])
                        ["With :idcol" :idcol "id"])))


(deftest test-rename-table
  (testing "rename-table"
    (test-change RenameTableChange
      (partial change/rename-table :old-name :new-name)
      min-args
      with-schema-name
      with-schema)))


(deftest test-drop-table
  (testing "drop-table"
    (test-change DropTableChange
      (partial change/drop-table :table-name)
      min-args
      with-schema-name
      with-schema
      [long-names  :cascade-constraints false]
      [short-names :cascade             true])))


(deftest test-create-view
  (testing "create-view"
    (test-change CreateViewChange
      (partial change/create-view :view-name "SELECT * FROM emp;")
      min-args
      with-schema-name
      with-schema
      [long-names  :replace-if-exists false]
      [short-names :replace           true])))


(deftest test-rename-view
  (testing "rename-view"
    (test-change RenameViewChange
      (partial change/rename-view :old-name :new-name)
      min-args
      with-schema-name
      with-schema)))


(deftest test-drop-view
  (testing "drop-view"
    (test-change DropViewChange
      (partial change/drop-view :view-name)
      min-args
      with-schema-name
      with-schema)))


(deftest test-merge-columns
  (testing "merge-columns"
    (test-change MergeColumnChange
      (partial change/merge-columns 
        :table-name :column1-name "|" :column2-name
        :final-column-name [:char 100])
      min-args
      with-schema-name
      with-schema)))


(deftest test-create-stored-procedure
  (testing "create-stored-procedure"
    (test-change CreateProcedureChange
      (partial change/create-stored-procedure
        "CREATE OR REPLACE PROCEDURE testHello
        IS
        BEGIN
          DBMS_OUTPUT.PUT_LINE('Hello From The Database!');
        END;")
      min-args
      ["With :comments argument" :comments "foobar"])))


;; ----- Data Quality Refactorings -----


(deftest test-add-lookup-table
  (testing "add-lookup-table"
    (test-change AddLookupTableChange
      (partial change/add-lookup-table
        :existing-table-name :existing-column-name
        :new-table-name      :new-column-name
        :constraint-name)
      min-args
      [long-names
       :existing-table-schema-name :sample  ; String/Keyword - s.t. clj-to-dbident
       :new-table-schema-name      :another ; String/Keyword - s.t. clj-to-dbident
       :new-column-data-type       :int     ; String/vector - s.t. as-coltype
       ]
      [short-names
       :existing-schema :sample    ; String/Keyword - s.t. clj-to-dbident
       :new-schema      :another   ; String/Keyword - s.t. clj-to-dbident
       :new-data-type   [:char 10] ; String/vector - s.t. as-coltype
       ])))


(deftest test-add-not-null-constraint
  (testing "add-not-null-constraint"
    (test-change AddNotNullConstraintChange
      (partial change/add-not-null-constraint
        :table-name :column-name [:char 100])
      min-args
      with-schema-name
      with-schema
      [long-names  :default-null-value "default"]
      [short-names :default            "default"])))


(deftest test-drop-not-null-constraint
  (testing "drop-not-null-constraint"
    (test-change DropNotNullConstraintChange
      (partial change/drop-not-null-constraint :table-name :column-name)
      min-args
      with-schema-name
      with-schema
      [long-names  :column-data-type [:char 100]]
      [short-names :data-type        [:char 100]])))


(deftest test-add-unique-constraint
  (testing "add-unique-constraint"
    (test-change AddUniqueConstraintChange
      (partial change/add-unique-constraint
        :table-name [:col1 :col2] :constraint-name)
      min-args
      with-schema-name
      with-schema
      [long-names
       :table-space        :tspace ; String/Keyword - s.t. clj-to-dbident
       :deferrable         false   ; Boolean
       :initially-deferred true    ; Boolean
       :disabled           false   ; Boolean
       ]
      [short-names
       :tspace   :tspace ; String/Keyword - s.t. clj-to-dbident
       :defer    false   ; Boolean
       :idefer   true    ; Boolean
       :disabled true    ; Boolean
       ])))


(deftest test-drop-unique-constraint
  (testing "drop-unique-constraint"
    (test-change DropUniqueConstraintChange
      (partial change/drop-unique-constraint :table-name :constraint-name)
      min-args
      with-schema-name
      with-schema)))


(deftest test-create-sequence
  (testing "create-sequence"
    (test-change CreateSequenceChange
      (partial change/create-sequence :sequence-name)
      min-args
      with-schema-name
      with-schema
      [long-names
       :start-value  1000 ; BigInteger
       :increment-by 1    ; BigInteger
       :max-value    9999 ; BigInteger
       :min-value    1000 ; BigInteger
       :ordered      true ; Boolean
       :cycle        true ; Boolean
       ]
      [short-names
       :start 1000 ; BigInteger
       :incby 1    ; BigInteger
       :max   9999 ; BigInteger
       :min   1000 ; BigInteger
       :ord   true ; Boolean
       :cyc   true ; Boolean
       ])))


(deftest test-drop-sequence
  (testing "drop-sequence"
    (test-change DropSequenceChange
      (partial change/drop-sequence :sequence-name)
      min-args
      with-schema-name
      with-schema)))


(deftest test-add-auto-increment
  (testing "add-auto-increment"
    (test-change AddAutoIncrementChange
      (partial change/add-auto-increment
        :table-name :column-name :column-data-type)
      min-args
      with-schema-name
      with-schema)))


(deftest test-add-default-value
  (testing "add-default-value"
    (doseq [each ["default-value" 100 (Date.) true (change/dbfn "NOW")
                  (java.sql.Date. (.getTime (Date.)))
                  (java.sql.Time. (.getTime (Date.)))
                  (java.sql.Timestamp. (.getTime (Date.)))]]
      (test-change AddDefaultValueChange
        (partial change/add-default-value :table-name :column-name each)
        min-args
        with-schema-name
        with-schema))))


(deftest test-drop-default-value
  (testing "drop-default-value"
    (test-change DropDefaultValueChange
      (partial change/drop-default-value :table-name :column-name)
      min-args
      with-schema-name
      with-schema
      ["With :column-data-type value" :column-data-type [:char 100]]
      ["With :data-type value" :data-type [:float 12 5]])))


;; ----- Referential Integrity Refactorings -----


(deftest test-add-foreign-key-constraint
  (testing "add-foreign-key-constraint"
    (test-change AddForeignKeyConstraintChange
      (partial change/add-foreign-key-constraint
        :constraint-name :base-table-name [:base-column1 :base-column2]
        :referenced-table-name [:referenced-column1 :referenced-column2])
      min-args
      [long-names
       :base-table-schema-name       :base  ; String
       :referenced-table-schema-name :ref   ; String
       :deferrable                   false  ; Boolean
       :initially-deferred           true   ; Boolean
       :on-delete                    "none" ; String
       :on-update                    "none" ; String
       ]
      [short-names
       :base-schema :base  ; String
       :ref-schema  :ref   ; String
       :defer       false  ; Boolean
       :idefer      true   ; Boolean
       :ondel       "none" ; String
       :onupd       "none" ; String
       ])))


(deftest test-drop-foreign-key-constraint
  (testing "drop-foreign-key-constraint"
    (test-change DropForeignKeyConstraintChange
      (partial change/drop-foreign-key-constraint :constraint-name :base-table)
      min-args
      with-schema-name
      with-schema)))


(deftest test-add-primary-key
  (testing "add-primary-key"
    (test-change AddPrimaryKeyChange
      (partial change/add-primary-key :table-name [:col1 :col2] :constraint-name)
      min-args
      with-schema-name
      with-schema
      ["With :table-space value" :table-space :space]
      ["With :tspace value"      :tspace      :space])))


(deftest test-drop-primary-key
  (testing "drop-primary-key"
    (test-change DropPrimaryKeyChange
      (partial change/drop-primary-key :table-name)
      min-args
      with-schema-name
      with-schema
      ["With :constraint-name value" :constraint-name :constraint]
      ["With :constr value"          :constr          :constraint])))


;; ----- Non-Refactoring Transformations -----


(deftest test-insert-data
  (testing "insert-data"
    (test-change InsertDataChange
      (partial change/insert-data :table-name
        {:name "Abraham" :age 30 :male true :joined (Date.) :now (change/dbfn "NOW")})
      min-args
      with-schema-name
      with-schema)))


(deftest test-load-data
  (testing "load-data"
    (test-change LoadDataChange
      (partial change/load-data :table-name "filename.csv"
        (array-map :col1 :string :col2 :numeric :col3 :date :col4 :boolean))
      min-args
      with-schema-name
      with-schema
      ["With :encoding value" :encoding "UTF-16"]
      ["With :enc value"      :enc      "UTF-8"])))


(deftest test-load-update-data
  (testing "load-update-data"
    (test-change LoadUpdateDataChange
      (partial change/load-update-data :table-name "filename.csv"
        [:pk-col1 :pk-col2]
        (array-map :col1 :string :col2 :numeric :col3 :date :col4 :boolean))
      min-args
      with-schema-name
      with-schema
      ["With :encoding value" :encoding "UTF-16"]
      ["With :enc value"      :enc      "UTF-8"])))


(deftest test-update-data
  (testing "update-data"
    (test-change UpdateDataChange
      (partial change/update-data :table-name
        {:name "Abraham" :age 30 :male true :joined (Date.) :now (change/dbfn "NOW")})
      min-args
      with-schema-name
      with-schema
      with-where-clause
      with-where)))


(deftest test-delete-data
  (testing "delete-data"
    (test-change DeleteDataChange
      (partial change/delete-data :table-name)
      min-args
      with-schema-name
      with-schema
      with-where-clause
      with-where)))


(deftest test-tag-database
  (testing "tag-database"
    (test-change TagDatabaseChange
      (partial change/tag-database :tag)
      min-args)))


(deftest test-stop
  (testing "stop"
    (test-change StopChange change/stop min-args)
    (test-change StopChange (partial change/stop "Some message") min-args)))


;; ----- Architectural Refactorings -----


(deftest test-create-index
  (testing "create-index"
    (test-change CreateIndexChange
      (partial change/create-index :table-name [:col1 :col2])
      min-args
      with-schema-name
      with-schema
      [long-names
       :index-name   "fooindex" ; String
       :unique       true       ; Boolean
       :table-space  "space"    ; String
       ]
      [short-names
       :index  "fooindex" ; String
       :uniq   false      ; Boolean
       :tspace "space"    ; String
       ])))


(deftest test-drop-index
  (testing "drop-index"
    (test-change DropIndexChange
      (partial change/drop-index :index-name :table-name)
      min-args
      with-schema-name
      with-schema)))


(defn test-ns-hook []
  ;; ----- Structural Refactorings -----
  (test-add-columns)
  (test-rename-column)
  (test-modify-column)
  (test-drop-column)
  (test-alter-sequence)
  (test-create-table)
  (test-rename-table)
  (test-drop-table)
  (test-create-view)
  (test-rename-view)
  (test-drop-view)
  (test-merge-columns)
  (test-create-stored-procedure)
  ;; ----- Data Quality Refactorings -----
  (test-add-lookup-table)
  (test-add-not-null-constraint)
  (test-drop-not-null-constraint)
  (test-add-unique-constraint)
  (test-drop-unique-constraint)
  (test-create-sequence)
  (test-drop-sequence)
  (test-add-auto-increment)
  (test-add-default-value)
  (test-drop-default-value)
  ;; ----- Referential Integrity Refactorings -----
  (test-add-foreign-key-constraint)
  (test-drop-foreign-key-constraint)
  (test-add-primary-key)
  (test-drop-primary-key)
  ;; ----- Non-Refactoring Transformations -----
  (test-insert-data)
  (test-load-data)
  (test-load-update-data)
  (test-update-data)
  (test-delete-data)
  (test-tag-database)
  (test-stop)
  ;; ----- Architectural Refactorings -----
  (test-create-index)
  (test-drop-index)
  (test-drop-default-value)
  (test-drop-default-value)
  (test-drop-default-value)
  (test-drop-default-value))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.