Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added missing tests for the migration namespace and fixed the others.

  • Loading branch information...
commit 636ba7a43f88f9bfba618147f25a85831117be8a 1 parent 0c274e6
Nicolas Buduroi budu authored

Showing 1 changed file with 91 additions and 28 deletions. Show diff stats Hide diff stats

  1. +91 28 test/lobos/test/migration.clj
119 test/lobos/test/migration.clj
@@ -7,7 +7,7 @@
7 7 ;; You must not remove this notice, or any other, from this software.
8 8
9 9 (ns lobos.test.migration
10   - (:refer-clojure :exclude [complement])
  10 + (:refer-clojure :exclude [complement create alter drop])
11 11 (:require (lobos [connectivity :as conn]
12 12 [schema :as schema]))
13 13 (:use (clojure.java [io :only [delete-file]])
@@ -18,9 +18,16 @@
18 18
19 19 ;;;; Fixtures
20 20
  21 +(defn with-config-file-global-connection-fixture [f]
  22 + (spit (ns-file *config-namespace*)
  23 + (str "(ns lobos.config (:use lobos.connectivity))"
  24 + "(open-global " h2-spec ")"))
  25 + (f)
  26 + (conn/close-global nil true))
  27 +
21 28 (use-fixtures :once
22 29 remove-tmp-files-fixture
23   - #(do (%) (conn/close-global nil true)))
  30 + with-config-file-global-connection-fixture)
24 31
25 32 ;;;; Tests
26 33
@@ -41,7 +48,7 @@
41 48
42 49 (deftest test-complement-create
43 50 (is (= (complement '(create (table :users (integer :id))))
44   - '(drop (table :users (integer :id))))
  51 + '(drop (table :users)))
45 52 "Should return a drop table action without cascade clause")
46 53 (is (= (complement '(create db (schema :users)))
47 54 '(drop db (schema :users) :cascade))
@@ -95,17 +102,17 @@
95 102 (deftest test-append-to-mfile
96 103 (delete-file (migrations-file) true)
97 104 (create-mfile)
98   - (append-to-mfile 'foo "this is a test" '(println "up"))
  105 + (append-to-mfile 'foo "this is a test" '[(println "up")])
99 106 (is (.endsWith
100 107 (slurp (migrations-file))
101   - "(defmigration foo \"this is a test\" (up [] println \"up\"))\n")
  108 + "(defmigration foo \"this is a test\" (up [] (println \"up\")))\n")
102 109 (str "Should create a migrations file ending with "
103 110 "a migration with a docstring and an up clause"))
104   - (append-to-mfile 'bar nil '(println "up") '(println "down"))
  111 + (append-to-mfile 'bar nil '(println "up") '[(println "down")])
105 112 (is (.endsWith
106 113 (slurp (migrations-file))
107 114 (str "(defmigration bar (up [] println \"up\") "
108   - "(down [] println \"down\"))\n"))
  115 + "(down [] (println \"down\")))\n"))
109 116 (str "Should append a migration without a docstring and "
110 117 " an up and down clauses")))
111 118
@@ -114,17 +121,17 @@
114 121 (is (nil? (list-migrations))
115 122 "Should return nil")
116 123 (doseq [name '(foo bar baz)]
117   - (append-to-mfile name nil '(println "up")))
  124 + (append-to-mfile name nil '[(println "up")]))
118 125 (is (= (count (list-migrations)) 3)
119 126 "Should return a collection of three elements")
120 127 (is (every? #(satisfies? Migration %) (list-migrations))
121 128 "All items returned should satisfy the Migration protocol"))
122 129
123 130 (defmacro with-migrations-table [& body]
124   - `(conn/with-connection h2-spec
125   - (binding [*db* h2-spec]
  131 + `(binding [*db* h2-spec]
  132 + (conn/with-connection *db*
126 133 (with-schema [~'lobos :lobos]
127   - (create-migrations-table h2-spec :lobos)
  134 + (create-migrations-table *db* :lobos)
128 135 ~@body))))
129 136
130 137 (deftest test-create-migrations-table
@@ -136,38 +143,94 @@
136 143
137 144 (deftest test-insert-and-delete-migrations
138 145 (with-migrations-table
139   - (insert-migrations h2-spec :lobos 'foo)
140   - (is (= (query h2-spec :lobos :lobos_migrations)
  146 + (insert-migrations *db* :lobos 'foo)
  147 + (is (= (query *db* :lobos :lobos_migrations)
141 148 (list {:name "foo"}))
142 149 "Should insert a migration entry named 'foo'")
143   - (insert-migrations h2-spec :lobos 'bar 'baz)
144   - (is (= (query h2-spec :lobos :lobos_migrations)
  150 + (insert-migrations *db* :lobos 'bar 'baz)
  151 + (is (= (query *db* :lobos :lobos_migrations)
145 152 (list {:name "foo"}
146 153 {:name "bar"}
147 154 {:name "baz"}))
148 155 "Should insert two migration entries named 'bar' and 'baz'")
149   - (delete-migrations h2-spec :lobos 'foo)
150   - (is (= (query h2-spec :lobos :lobos_migrations)
  156 + (delete-migrations *db* :lobos 'foo)
  157 + (is (= (query *db* :lobos :lobos_migrations)
151 158 (list {:name "bar"} {:name "baz"}))
152 159 "Should delete a migration entry named 'foo'")
153   - (delete-migrations h2-spec :lobos 'bar 'baz)
154   - (is (empty? (query h2-spec :lobos :lobos_migrations))
  160 + (delete-migrations *db* :lobos 'bar 'baz)
  161 + (is (empty? (query *db* :lobos :lobos_migrations))
155 162 "Should delete all migration entries")))
156 163
157   -(deftest test-query-migrations-table
158   - (with-migrations-table
159   - (is (empty? (query-migrations-table h2-spec :lobos))
160   - "Should return an empty list")
161   - (insert-migrations h2-spec :lobos 'foo 'bar)
162   - (is (= (query-migrations-table h2-spec :lobos)
163   - (list "foo" "bar"))
164   - "Should return a list containing 'foo' and 'bar'")))
  164 +(deftest test-record
  165 + (delete-file *stash-file* true)
  166 + (record 'foo)
  167 + (is (= (slurp *stash-file*) "\nfoo\n")
  168 + "Should record the given action to the stash file")
  169 + (clear-stash-file)
  170 + (binding [*record* nil] (record 'foo))
  171 + (is (= (slurp *stash-file*) "")
  172 + "Should not record the given action to the stash file"))
165 173
166 174 (deftest test-list-migrations-names
167 175 (delete-file (migrations-file) true)
168 176 (is (empty? (list-migrations-names))
169 177 "Should return an empty list")
170 178 (doseq [name '(foo bar baz)]
171   - (append-to-mfile name nil '(println "up")))
  179 + (append-to-mfile name nil '[(println "up")]))
172 180 (is (= (list-migrations-names) (list "foo" "bar" "baz"))
173 181 "Should return a list containing 'foo' 'bar' and 'baz'"))
  182 +
  183 +(deftest test-query-migrations-table
  184 + (with-migrations-table
  185 + (is (empty? (query-migrations-table *db* :lobos))
  186 + "Should return an empty list")
  187 + (insert-migrations *db* :lobos 'foo 'bar)
  188 + (is (= (query-migrations-table *db* :lobos)
  189 + (list "foo" "bar"))
  190 + "Should return a list containing 'foo' and 'bar'")))
  191 +
  192 +(deftest test-pending-migrations
  193 + (with-migrations-table
  194 + (delete-file (migrations-file) true)
  195 + (is (empty? (pending-migrations *db* :lobos))
  196 + "Should return an empty list")
  197 + (doseq [name '(foo bar baz)]
  198 + (append-to-mfile name nil '[(println "up")]))
  199 + (is (= (pending-migrations *db* :lobos) (list "foo" "bar" "baz"))
  200 + "Should return a list containing 'foo' 'bar' and 'baz'")
  201 + (insert-migrations *db* :lobos 'foo 'bar)
  202 + (is (= (pending-migrations *db* :lobos) (list "baz"))
  203 + "Should return a list containing 'baz'")))
  204 +
  205 +(deftest test-do-migrations
  206 + (with-migrations-table
  207 + (delete-file (migrations-file) true)
  208 + (doseq [n [:foo :bar :baz]]
  209 + (generate-migration* *db* :lobos (symbol (name n)) nil
  210 + `[(~'create (~'table ~n (~'integer ~n)))]))
  211 + (delete-migrations *db* :lobos 'foo 'bar 'baz)
  212 + (is (= (pending-migrations *db* :lobos) (list "foo" "bar" "baz"))
  213 + "Should return a list containing 'foo' 'bar' and 'baz'")
  214 + (do-migrations *db* :lobos :up '[foo] true)
  215 + (is (= (pending-migrations *db* :lobos) (list "bar" "baz"))
  216 + "Should return a list containing 'bar' and 'baz'")
  217 + (do-migrations *db* :lobos :up '[bar baz] true)
  218 + (is (empty? (pending-migrations *db* :lobos))
  219 + "Should return an empty list")
  220 + (do-migrations *db* :lobos :down '[bar] true)
  221 + (is (= (pending-migrations *db* :lobos) (list "bar"))
  222 + "Should return a list containing 'bar'")
  223 + (do-migrations *db* :lobos :down '[foo baz] true)
  224 + (is (= (pending-migrations *db* :lobos) (list "foo" "bar" "baz"))
  225 + "Should return a list containing 'foo' 'bar' and 'baz'")))
  226 +
  227 +(deftest test-generate-migration*
  228 + (with-migrations-table
  229 + (delete-file (migrations-file) true)
  230 + (generate-migration* *db* :lobos 'foo nil [])
  231 + (is (empty? (query-migrations-table *db* :lobos))
  232 + "Should return an empty list")
  233 + (generate-migration* *db* :lobos 'bar nil '[(println "up")])
  234 + (is (= (query-migrations-table *db* :lobos)
  235 + (list "bar"))
  236 + "Should return a list containing 'bar'")))

0 comments on commit 636ba7a

Please sign in to comment.
Something went wrong with that request. Please try again.