Skip to content
This repository
Browse code

Refactor testcase codes. It's repeated.

  • Loading branch information...
commit 4d6853946d036d47cae6741fab10a4b56494a279 1 parent 323ee20
Toshinori Kajihara authored June 21, 2012
170  activerecord/test/cases/database_tasks_test.rb
@@ -10,31 +10,21 @@ def setup
10 10
     end
11 11
   end
12 12
 
  13
+  ADAPTERS_TASKS = {
  14
+     :mysql      => :mysql_tasks,
  15
+     :mysql2     => :mysql_tasks,
  16
+     :postgresql => :postgresql_tasks,
  17
+     :sqlite3    => :sqlite_tasks
  18
+  }
  19
+ 
13 20
   class DatabaseTasksCreateTest < ActiveRecord::TestCase
14 21
     include DatabaseTasksSetupper
15 22
 
16  
-    def test_mysql_create
17  
-      @mysql_tasks.expects(:create)
18  
-
19  
-      ActiveRecord::Tasks::DatabaseTasks.create 'adapter' => 'mysql'
20  
-    end
21  
-
22  
-    def test_mysql2_create
23  
-      @mysql_tasks.expects(:create)
24  
-
25  
-      ActiveRecord::Tasks::DatabaseTasks.create 'adapter' => 'mysql2'
26  
-    end
27  
-
28  
-    def test_postgresql_create
29  
-      @postgresql_tasks.expects(:create)
30  
-
31  
-      ActiveRecord::Tasks::DatabaseTasks.create 'adapter' => 'postgresql'
32  
-    end
33  
-
34  
-    def test_sqlite_create
35  
-      @sqlite_tasks.expects(:create)
36  
-
37  
-      ActiveRecord::Tasks::DatabaseTasks.create 'adapter' => 'sqlite3'
  23
+    ADAPTERS_TASKS.each do |k, v|
  24
+      define_method("test_#{k}_create") do
  25
+        eval("@#{v}").expects(:create)
  26
+        ActiveRecord::Tasks::DatabaseTasks.create 'adapter' => k
  27
+      end
38 28
     end
39 29
   end
40 30
 
@@ -141,28 +131,11 @@ def test_establishes_connection_for_the_given_environment
141 131
   class DatabaseTasksDropTest < ActiveRecord::TestCase
142 132
     include DatabaseTasksSetupper
143 133
 
144  
-    def test_mysql_create
145  
-      @mysql_tasks.expects(:drop)
146  
-
147  
-      ActiveRecord::Tasks::DatabaseTasks.drop 'adapter' => 'mysql'
148  
-    end
149  
-
150  
-    def test_mysql2_create
151  
-      @mysql_tasks.expects(:drop)
152  
-
153  
-      ActiveRecord::Tasks::DatabaseTasks.drop 'adapter' => 'mysql2'
154  
-    end
155  
-
156  
-    def test_postgresql_create
157  
-      @postgresql_tasks.expects(:drop)
158  
-
159  
-      ActiveRecord::Tasks::DatabaseTasks.drop 'adapter' => 'postgresql'
160  
-    end
161  
-
162  
-    def test_sqlite_create
163  
-      @sqlite_tasks.expects(:drop)
164  
-
165  
-      ActiveRecord::Tasks::DatabaseTasks.drop 'adapter' => 'sqlite3'
  134
+    ADAPTERS_TASKS.each do |k, v|
  135
+      define_method("test_#{k}_drop") do
  136
+        eval("@#{v}").expects(:drop)
  137
+        ActiveRecord::Tasks::DatabaseTasks.drop 'adapter' => k
  138
+      end
166 139
     end
167 140
   end
168 141
 
@@ -255,115 +228,48 @@ def test_creates_test_database_when_environment_is_database
255 228
     end
256 229
   end
257 230
 
  231
+
258 232
   class DatabaseTasksPurgeTest < ActiveRecord::TestCase
259 233
     include DatabaseTasksSetupper
260 234
 
261  
-    def test_mysql_create
262  
-      @mysql_tasks.expects(:purge)
263  
-
264  
-      ActiveRecord::Tasks::DatabaseTasks.purge 'adapter' => 'mysql'
265  
-    end
266  
-
267  
-    def test_mysql2_create
268  
-      @mysql_tasks.expects(:purge)
269  
-
270  
-      ActiveRecord::Tasks::DatabaseTasks.purge 'adapter' => 'mysql2'
271  
-    end
272  
-
273  
-    def test_postgresql_create
274  
-      @postgresql_tasks.expects(:purge)
275  
-
276  
-      ActiveRecord::Tasks::DatabaseTasks.purge 'adapter' => 'postgresql'
277  
-    end
278  
-
279  
-    def test_sqlite_create
280  
-      @sqlite_tasks.expects(:purge)
281  
-
282  
-      ActiveRecord::Tasks::DatabaseTasks.purge 'adapter' => 'sqlite3'
  235
+    ADAPTERS_TASKS.each do |k, v|
  236
+      define_method("test_#{k}_purge") do
  237
+        eval("@#{v}").expects(:purge)
  238
+        ActiveRecord::Tasks::DatabaseTasks.purge 'adapter' => k
  239
+      end
283 240
     end
284 241
   end
285 242
 
286 243
   class DatabaseTasksCharsetTest < ActiveRecord::TestCase
287 244
     include DatabaseTasksSetupper
288 245
  
289  
-    def test_mysql_charset
290  
-      @mysql_tasks.expects(:charset)
291  
-
292  
-      ActiveRecord::Tasks::DatabaseTasks.charset 'adapter' => 'mysql'
293  
-    end
294  
-
295  
-    def test_mysql2_charset
296  
-      @mysql_tasks.expects(:charset)
297  
-
298  
-      ActiveRecord::Tasks::DatabaseTasks.charset 'adapter' => 'mysql2'
299  
-    end
300  
-
301  
-    def test_postgresql_charset
302  
-      @postgresql_tasks.expects(:charset)
303  
-
304  
-      ActiveRecord::Tasks::DatabaseTasks.charset 'adapter' => 'postgresql'
305  
-    end
306  
-
307  
-    def test_sqlite_charset
308  
-      @sqlite_tasks.expects(:charset)
309  
-
310  
-      ActiveRecord::Tasks::DatabaseTasks.charset 'adapter' => 'sqlite3'
  246
+    ADAPTERS_TASKS.each do |k, v|
  247
+      define_method("test_#{k}_charset") do
  248
+        eval("@#{v}").expects(:charset)
  249
+        ActiveRecord::Tasks::DatabaseTasks.charset 'adapter' => k
  250
+      end
311 251
     end
312 252
   end
313 253
 
314 254
   class DatabaseTasksStructureDumpTest < ActiveRecord::TestCase
315 255
     include DatabaseTasksSetupper
316 256
 
317  
-    def test_mysql_structure_dump
318  
-      @mysql_tasks.expects(:structure_dump).with("awesome-file.sql")
319  
-
320  
-      ActiveRecord::Tasks::DatabaseTasks.structure_dump({'adapter' => 'mysql'}, "awesome-file.sql")
321  
-    end
322  
-
323  
-    def test_mysql2_structure_dump
324  
-      @mysql_tasks.expects(:structure_dump).with("awesome-file.sql")
325  
-
326  
-      ActiveRecord::Tasks::DatabaseTasks.structure_dump({'adapter' => 'mysql2'}, "awesome-file.sql")
327  
-    end
328  
-
329  
-    def test_postgresql_structure_dump
330  
-      @postgresql_tasks.expects(:structure_dump).with("awesome-file.sql")
331  
-
332  
-      ActiveRecord::Tasks::DatabaseTasks.structure_dump({'adapter' => 'postgresql'}, "awesome-file.sql")
333  
-    end
334  
-
335  
-    def test_sqlite_structure_dump
336  
-      @sqlite_tasks.expects(:structure_dump).with("awesome-file.sql")
337  
-
338  
-      ActiveRecord::Tasks::DatabaseTasks.structure_dump({'adapter' => 'sqlite3'}, "awesome-file.sql")
  257
+    ADAPTERS_TASKS.each do |k, v|
  258
+      define_method("test_#{k}_structure_dump") do
  259
+        eval("@#{v}").expects(:structure_dump).with("awesome-file.sql")
  260
+        ActiveRecord::Tasks::DatabaseTasks.structure_dump({'adapter' => k}, "awesome-file.sql")
  261
+      end
339 262
     end
340 263
   end
341 264
 
342 265
   class DatabaseTasksStructureLoadTest < ActiveRecord::TestCase
343 266
     include DatabaseTasksSetupper
344 267
 
345  
-    def test_mysql_structure_load
346  
-      @mysql_tasks.expects(:structure_load).with("awesome-file.sql")
347  
-
348  
-      ActiveRecord::Tasks::DatabaseTasks.structure_load({'adapter' => 'mysql'}, "awesome-file.sql")
349  
-    end
350  
-
351  
-    def test_mysql2_structure_load
352  
-      @mysql_tasks.expects(:structure_load).with("awesome-file.sql")
353  
-
354  
-      ActiveRecord::Tasks::DatabaseTasks.structure_load({'adapter' => 'mysql2'}, "awesome-file.sql")
355  
-    end
356  
-
357  
-    def test_postgresql_structure_load
358  
-      @postgresql_tasks.expects(:structure_load).with("awesome-file.sql")
359  
-
360  
-      ActiveRecord::Tasks::DatabaseTasks.structure_load({'adapter' => 'postgresql'}, "awesome-file.sql")
361  
-    end
362  
-
363  
-    def test_sqlite_structure_load
364  
-      @sqlite_tasks.expects(:structure_load).with("awesome-file.sql")
365  
-
366  
-      ActiveRecord::Tasks::DatabaseTasks.structure_load({'adapter' => 'sqlite3'}, "awesome-file.sql")
  268
+    ADAPTERS_TASKS.each do |k, v|
  269
+      define_method("test_#{k}_structure_load") do
  270
+        eval("@#{v}").expects(:structure_load).with("awesome-file.sql")
  271
+        ActiveRecord::Tasks::DatabaseTasks.structure_load({'adapter' => k}, "awesome-file.sql")
  272
+      end
367 273
     end
368 274
   end
369 275
 end

0 notes on commit 4d68539

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