-
Notifications
You must be signed in to change notification settings - Fork 21
/
test_helper.clj
95 lines (83 loc) · 2.86 KB
/
test_helper.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
(ns clj-record.test-helper
(:require [clj-record.core :as core]
[clojure.java.jdbc :as sql])
(:use clj-record.test-model.config
clojure.test))
(defmacro defdbtest [name & body]
`(deftest ~name
(rolling-back ~@body)))
(defmacro rolling-back [& body]
`(core/transaction clj-record.test-model.config/db
(try
~@body
(finally
(clojure.java.jdbc/set-rollback-only)))))
(defmacro restoring-ref [ref & body]
`(let [old-value# (deref ~ref)]
(try
~@body
(finally
(dosync (ref-set ~ref old-value#))))))
(def valid-manufacturer {:name "Valid Name" :founded "1999" :grade 99})
(defn valid-manufacturer-with [attributes] (merge valid-manufacturer attributes))
(defmulti get-id-key-spec :subprotocol)
(defmethod get-id-key-spec "derby" [db-spec name]
[:id :int (str "GENERATED ALWAYS AS IDENTITY CONSTRAINT " name " PRIMARY KEY")])
(defmethod get-id-key-spec "sqlserver" [db-spec name]
[:id :int (str "PRIMARY KEY IDENTITY")])
(defmethod get-id-key-spec "db2" [db-spec name]
[:id :int (str "GENERATED ALWAYS AS IDENTITY CONSTRAINT " name " PRIMARY KEY")])
(defmethod get-id-key-spec :default [db-spec name]
[:id "SERIAL UNIQUE PRIMARY KEY"])
(defmulti get-table-spec :subprotocol)
(defmethod get-table-spec "mysql" [db-spec]
"Engine=InnoDB")
(defmethod get-table-spec :default [db-spec]
nil)
(def table-specs
{ :manufacturers
[ (get-id-key-spec db "manufacturer_pk")
[:name "VARCHAR(32)" "NOT NULL"]
[:founded "VARCHAR(4)"]
[:grade :int]
:table-spec (get-table-spec db)]
:productos
[ (get-id-key-spec db "product_pk")
[:name "VARCHAR(32)" "NOT NULL"]
[:price :int]
[:manufacturer_id :int "NOT NULL"]
:table-spec (get-table-spec db)]
:person
[ (get-id-key-spec db "person_pk")
[:name "VARCHAR(32) NOT NULL"]
[:mother_id :int]
[:father_person_id :int]
:table-spec (get-table-spec db)]
:thing_one
[ (get-id-key-spec db "thing_one_pk")
[:name "VARCHAR(32)" "NOT NULL"]
[:owner_person_id :int]
:table-spec (get-table-spec db)]
:thing_two
[ (get-id-key-spec db "thing_two_pk")
[:thing_one_id :int "NOT NULL"]
:table-spec (get-table-spec db)] })
(defn drop-tables
([] (drop-tables (keys table-specs)))
([tables]
(try
(doseq [table tables]
(sql/drop-table table))
(catch Exception e))))
(defn create-tables
([] (create-tables (keys table-specs)))
([tables]
(doseq [table-pair (select-keys table-specs tables)]
(apply sql/create-table (first table-pair) (second table-pair)))))
(defn reset-db []
(println "resetting" (db :subprotocol))
(sql/with-connection db
(sql/transaction
(drop-tables)
(create-tables)))
(println "database reset"))