database_cleaner 0.9.1 → 1.0.0.RC1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +127 -136
- data/History.txt +19 -1
- data/README.markdown +335 -0
- data/VERSION.yml +5 -5
- data/examples/Gemfile +18 -9
- data/examples/Gemfile.lock +127 -136
- data/examples/features/support/env.rb +3 -12
- data/lib/database_cleaner/active_record/base.rb +9 -9
- data/lib/database_cleaner/active_record/deletion.rb +5 -48
- data/lib/database_cleaner/active_record/transaction.rb +31 -9
- data/lib/database_cleaner/active_record/truncation.rb +23 -6
- data/lib/database_cleaner/data_mapper/truncation.rb +6 -2
- data/lib/database_cleaner/sequel/truncation.rb +1 -1
- data/spec/database_cleaner/active_record/base_spec.rb +32 -11
- data/spec/database_cleaner/active_record/transaction_spec.rb +101 -42
- data/spec/database_cleaner/active_record/truncation_spec.rb +1 -1
- metadata +28 -331
- data/README.textile +0 -247
@@ -8,7 +8,7 @@ module ActiveRecord
|
|
8
8
|
[MysqlAdapter, Mysql2Adapter, SQLite3Adapter, JdbcAdapter, PostgreSQLAdapter, IBM_DBAdapter].each do |adapter|
|
9
9
|
describe adapter, "#truncate_table" do
|
10
10
|
it "responds" do
|
11
|
-
adapter.instance_methods.should include(
|
11
|
+
adapter.instance_methods.should include(:truncate_table)
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
metadata
CHANGED
@@ -1,324 +1,29 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_cleaner
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 9
|
9
|
-
- 1
|
10
|
-
version: 0.9.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.RC1
|
5
|
+
prerelease: 6
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Ben Mabey
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
name: rake
|
22
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
-
none: false
|
24
|
-
requirements:
|
25
|
-
- - ">="
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
hash: 3
|
28
|
-
segments:
|
29
|
-
- 0
|
30
|
-
version: "0"
|
31
|
-
type: :development
|
32
|
-
requirement: *id001
|
33
|
-
prerelease: false
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: ruby-debug
|
36
|
-
version_requirements: &id002 !ruby/object:Gem::Requirement
|
37
|
-
none: false
|
38
|
-
requirements:
|
39
|
-
- - ">="
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
hash: 3
|
42
|
-
segments:
|
43
|
-
- 0
|
44
|
-
version: "0"
|
45
|
-
type: :development
|
46
|
-
requirement: *id002
|
47
|
-
prerelease: false
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: bundler
|
50
|
-
version_requirements: &id003 !ruby/object:Gem::Requirement
|
51
|
-
none: false
|
52
|
-
requirements:
|
53
|
-
- - ">="
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
hash: 3
|
56
|
-
segments:
|
57
|
-
- 0
|
58
|
-
version: "0"
|
59
|
-
type: :development
|
60
|
-
requirement: *id003
|
61
|
-
prerelease: false
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: jeweler
|
64
|
-
version_requirements: &id004 !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
|
-
requirements:
|
67
|
-
- - ">="
|
68
|
-
- !ruby/object:Gem::Version
|
69
|
-
hash: 3
|
70
|
-
segments:
|
71
|
-
- 0
|
72
|
-
version: "0"
|
73
|
-
type: :development
|
74
|
-
requirement: *id004
|
75
|
-
prerelease: false
|
76
|
-
- !ruby/object:Gem::Dependency
|
77
|
-
name: json_pure
|
78
|
-
version_requirements: &id005 !ruby/object:Gem::Requirement
|
79
|
-
none: false
|
80
|
-
requirements:
|
81
|
-
- - ">="
|
82
|
-
- !ruby/object:Gem::Version
|
83
|
-
hash: 3
|
84
|
-
segments:
|
85
|
-
- 0
|
86
|
-
version: "0"
|
87
|
-
type: :development
|
88
|
-
requirement: *id005
|
89
|
-
prerelease: false
|
90
|
-
- !ruby/object:Gem::Dependency
|
91
|
-
name: activerecord
|
92
|
-
version_requirements: &id006 !ruby/object:Gem::Requirement
|
93
|
-
none: false
|
94
|
-
requirements:
|
95
|
-
- - ">="
|
96
|
-
- !ruby/object:Gem::Version
|
97
|
-
hash: 3
|
98
|
-
segments:
|
99
|
-
- 0
|
100
|
-
version: "0"
|
101
|
-
type: :development
|
102
|
-
requirement: *id006
|
103
|
-
prerelease: false
|
104
|
-
- !ruby/object:Gem::Dependency
|
105
|
-
name: datamapper
|
106
|
-
version_requirements: &id007 !ruby/object:Gem::Requirement
|
107
|
-
none: false
|
108
|
-
requirements:
|
109
|
-
- - "="
|
110
|
-
- !ruby/object:Gem::Version
|
111
|
-
hash: 23
|
112
|
-
segments:
|
113
|
-
- 1
|
114
|
-
- 0
|
115
|
-
- 0
|
116
|
-
version: 1.0.0
|
117
|
-
type: :development
|
118
|
-
requirement: *id007
|
119
|
-
prerelease: false
|
120
|
-
- !ruby/object:Gem::Dependency
|
121
|
-
name: dm-migrations
|
122
|
-
version_requirements: &id008 !ruby/object:Gem::Requirement
|
123
|
-
none: false
|
124
|
-
requirements:
|
125
|
-
- - "="
|
126
|
-
- !ruby/object:Gem::Version
|
127
|
-
hash: 23
|
128
|
-
segments:
|
129
|
-
- 1
|
130
|
-
- 0
|
131
|
-
- 0
|
132
|
-
version: 1.0.0
|
133
|
-
type: :development
|
134
|
-
requirement: *id008
|
135
|
-
prerelease: false
|
136
|
-
- !ruby/object:Gem::Dependency
|
137
|
-
name: dm-sqlite-adapter
|
138
|
-
version_requirements: &id009 !ruby/object:Gem::Requirement
|
139
|
-
none: false
|
140
|
-
requirements:
|
141
|
-
- - "="
|
142
|
-
- !ruby/object:Gem::Version
|
143
|
-
hash: 23
|
144
|
-
segments:
|
145
|
-
- 1
|
146
|
-
- 0
|
147
|
-
- 0
|
148
|
-
version: 1.0.0
|
149
|
-
type: :development
|
150
|
-
requirement: *id009
|
151
|
-
prerelease: false
|
152
|
-
- !ruby/object:Gem::Dependency
|
153
|
-
name: mongoid
|
154
|
-
version_requirements: &id010 !ruby/object:Gem::Requirement
|
155
|
-
none: false
|
156
|
-
requirements:
|
157
|
-
- - ">="
|
158
|
-
- !ruby/object:Gem::Version
|
159
|
-
hash: 3
|
160
|
-
segments:
|
161
|
-
- 0
|
162
|
-
version: "0"
|
163
|
-
type: :development
|
164
|
-
requirement: *id010
|
165
|
-
prerelease: false
|
166
|
-
- !ruby/object:Gem::Dependency
|
167
|
-
name: tzinfo
|
168
|
-
version_requirements: &id011 !ruby/object:Gem::Requirement
|
169
|
-
none: false
|
170
|
-
requirements:
|
171
|
-
- - ">="
|
172
|
-
- !ruby/object:Gem::Version
|
173
|
-
hash: 3
|
174
|
-
segments:
|
175
|
-
- 0
|
176
|
-
version: "0"
|
177
|
-
type: :development
|
178
|
-
requirement: *id011
|
179
|
-
prerelease: false
|
180
|
-
- !ruby/object:Gem::Dependency
|
181
|
-
name: mongo_ext
|
182
|
-
version_requirements: &id012 !ruby/object:Gem::Requirement
|
183
|
-
none: false
|
184
|
-
requirements:
|
185
|
-
- - ">="
|
186
|
-
- !ruby/object:Gem::Version
|
187
|
-
hash: 3
|
188
|
-
segments:
|
189
|
-
- 0
|
190
|
-
version: "0"
|
191
|
-
type: :development
|
192
|
-
requirement: *id012
|
193
|
-
prerelease: false
|
194
|
-
- !ruby/object:Gem::Dependency
|
195
|
-
name: bson_ext
|
196
|
-
version_requirements: &id013 !ruby/object:Gem::Requirement
|
197
|
-
none: false
|
198
|
-
requirements:
|
199
|
-
- - ">="
|
200
|
-
- !ruby/object:Gem::Version
|
201
|
-
hash: 3
|
202
|
-
segments:
|
203
|
-
- 0
|
204
|
-
version: "0"
|
205
|
-
type: :development
|
206
|
-
requirement: *id013
|
207
|
-
prerelease: false
|
208
|
-
- !ruby/object:Gem::Dependency
|
209
|
-
name: mongo_mapper
|
210
|
-
version_requirements: &id014 !ruby/object:Gem::Requirement
|
211
|
-
none: false
|
212
|
-
requirements:
|
213
|
-
- - ">="
|
214
|
-
- !ruby/object:Gem::Version
|
215
|
-
hash: 3
|
216
|
-
segments:
|
217
|
-
- 0
|
218
|
-
version: "0"
|
219
|
-
type: :development
|
220
|
-
requirement: *id014
|
221
|
-
prerelease: false
|
222
|
-
- !ruby/object:Gem::Dependency
|
223
|
-
name: couch_potato
|
224
|
-
version_requirements: &id015 !ruby/object:Gem::Requirement
|
225
|
-
none: false
|
226
|
-
requirements:
|
227
|
-
- - ">="
|
228
|
-
- !ruby/object:Gem::Version
|
229
|
-
hash: 3
|
230
|
-
segments:
|
231
|
-
- 0
|
232
|
-
version: "0"
|
233
|
-
type: :development
|
234
|
-
requirement: *id015
|
235
|
-
prerelease: false
|
236
|
-
- !ruby/object:Gem::Dependency
|
237
|
-
name: sequel
|
238
|
-
version_requirements: &id016 !ruby/object:Gem::Requirement
|
239
|
-
none: false
|
240
|
-
requirements:
|
241
|
-
- - ~>
|
242
|
-
- !ruby/object:Gem::Version
|
243
|
-
hash: 83
|
244
|
-
segments:
|
245
|
-
- 3
|
246
|
-
- 21
|
247
|
-
- 0
|
248
|
-
version: 3.21.0
|
249
|
-
type: :development
|
250
|
-
requirement: *id016
|
251
|
-
prerelease: false
|
252
|
-
- !ruby/object:Gem::Dependency
|
253
|
-
name: mysql
|
254
|
-
version_requirements: &id017 !ruby/object:Gem::Requirement
|
255
|
-
none: false
|
256
|
-
requirements:
|
257
|
-
- - ">="
|
258
|
-
- !ruby/object:Gem::Version
|
259
|
-
hash: 3
|
260
|
-
segments:
|
261
|
-
- 0
|
262
|
-
version: "0"
|
263
|
-
type: :development
|
264
|
-
requirement: *id017
|
265
|
-
prerelease: false
|
266
|
-
- !ruby/object:Gem::Dependency
|
267
|
-
name: mysql2
|
268
|
-
version_requirements: &id018 !ruby/object:Gem::Requirement
|
269
|
-
none: false
|
270
|
-
requirements:
|
271
|
-
- - ">="
|
272
|
-
- !ruby/object:Gem::Version
|
273
|
-
hash: 3
|
274
|
-
segments:
|
275
|
-
- 0
|
276
|
-
version: "0"
|
277
|
-
type: :development
|
278
|
-
requirement: *id018
|
279
|
-
prerelease: false
|
280
|
-
- !ruby/object:Gem::Dependency
|
281
|
-
name: pg
|
282
|
-
version_requirements: &id019 !ruby/object:Gem::Requirement
|
283
|
-
none: false
|
284
|
-
requirements:
|
285
|
-
- - ">="
|
286
|
-
- !ruby/object:Gem::Version
|
287
|
-
hash: 3
|
288
|
-
segments:
|
289
|
-
- 0
|
290
|
-
version: "0"
|
291
|
-
type: :development
|
292
|
-
requirement: *id019
|
293
|
-
prerelease: false
|
294
|
-
- !ruby/object:Gem::Dependency
|
295
|
-
name: guard-rspec
|
296
|
-
version_requirements: &id020 !ruby/object:Gem::Requirement
|
297
|
-
none: false
|
298
|
-
requirements:
|
299
|
-
- - ">="
|
300
|
-
- !ruby/object:Gem::Version
|
301
|
-
hash: 3
|
302
|
-
segments:
|
303
|
-
- 0
|
304
|
-
version: "0"
|
305
|
-
type: :development
|
306
|
-
requirement: *id020
|
307
|
-
prerelease: false
|
308
|
-
description: Strategies for cleaning databases. Can be used to ensure a clean state for testing.
|
12
|
+
date: 2013-03-04 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Strategies for cleaning databases. Can be used to ensure a clean state
|
15
|
+
for testing.
|
309
16
|
email: ben@benmabey.com
|
310
17
|
executables: []
|
311
|
-
|
312
18
|
extensions: []
|
313
|
-
|
314
|
-
extra_rdoc_files:
|
19
|
+
extra_rdoc_files:
|
315
20
|
- LICENSE
|
316
|
-
- README.
|
21
|
+
- README.markdown
|
317
22
|
- TODO
|
318
|
-
files:
|
23
|
+
files:
|
319
24
|
- Gemfile.lock
|
320
25
|
- History.txt
|
321
|
-
- README.
|
26
|
+
- README.markdown
|
322
27
|
- Rakefile
|
323
28
|
- VERSION.yml
|
324
29
|
- cucumber.yml
|
@@ -411,36 +116,28 @@ files:
|
|
411
116
|
- TODO
|
412
117
|
homepage: http://github.com/bmabey/database_cleaner
|
413
118
|
licenses: []
|
414
|
-
|
415
119
|
post_install_message:
|
416
120
|
rdoc_options: []
|
417
|
-
|
418
|
-
require_paths:
|
121
|
+
require_paths:
|
419
122
|
- lib
|
420
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
421
124
|
none: false
|
422
|
-
requirements:
|
423
|
-
- -
|
424
|
-
- !ruby/object:Gem::Version
|
425
|
-
|
426
|
-
|
427
|
-
- 0
|
428
|
-
version: "0"
|
429
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ! '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
430
130
|
none: false
|
431
|
-
requirements:
|
432
|
-
- -
|
433
|
-
- !ruby/object:Gem::Version
|
434
|
-
|
435
|
-
segments:
|
436
|
-
- 0
|
437
|
-
version: "0"
|
131
|
+
requirements:
|
132
|
+
- - ! '>'
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: 1.3.1
|
438
135
|
requirements: []
|
439
|
-
|
440
136
|
rubyforge_project:
|
441
|
-
rubygems_version: 1.8.
|
137
|
+
rubygems_version: 1.8.23
|
442
138
|
signing_key:
|
443
139
|
specification_version: 3
|
444
|
-
summary: Strategies for cleaning databases. Can be used to ensure a clean state for
|
140
|
+
summary: Strategies for cleaning databases. Can be used to ensure a clean state for
|
141
|
+
testing.
|
445
142
|
test_files: []
|
446
|
-
|
143
|
+
has_rdoc:
|
data/README.textile
DELETED
@@ -1,247 +0,0 @@
|
|
1
|
-
h1. Database Cleaner
|
2
|
-
|
3
|
-
Database Cleaner is a set of strategies for cleaning your database in Ruby.
|
4
|
-
The original use case was to ensure a clean state during tests. Each strategy
|
5
|
-
is a small amount of code but is code that is usually needed in any ruby app
|
6
|
-
that is testing with a database.
|
7
|
-
|
8
|
-
ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, and CouchPotato are supported.
|
9
|
-
|
10
|
-
!https://secure.travis-ci.org/bmabey/database_cleaner.png(Build Status)!:http://travis-ci.org/bmabey/database_cleaner
|
11
|
-
|
12
|
-
Here is an overview of the strategies supported for each library:
|
13
|
-
|
14
|
-
|_. ORM |_. Truncation |_. Transaction |_. Deletion |
|
15
|
-
| ActiveRecord | Yes | **Yes** | Yes |
|
16
|
-
| DataMapper | Yes | **Yes** | No |
|
17
|
-
| CouchPotato | **Yes** | No | No |
|
18
|
-
| MongoMapper | **Yes** | No | No |
|
19
|
-
| Mongoid | **Yes** | No | No |
|
20
|
-
| Sequel | **Yes** | Yes | No |
|
21
|
-
|
22
|
-
|_. Driver |_. Truncation |_. Transaction |_. Deletion |
|
23
|
-
| Mongo | Yes | No | No |
|
24
|
-
|
25
|
-
(Default strategy for each library is denoted in bold)
|
26
|
-
|
27
|
-
Database Cleaner also includes a @null@ strategy (that does no cleaning at all) which can be used
|
28
|
-
with any ORM library. You can also explicitly use it by setting your strategy to @nil@.
|
29
|
-
|
30
|
-
For support or to discuss development please use the "Google Group":http://groups.google.com/group/database_cleaner.
|
31
|
-
|
32
|
-
h2(fastest). What strategy is fastest?
|
33
|
-
|
34
|
-
For the SQL libraries the fastest option will be to use @:transaction@ as transactions are
|
35
|
-
simply rolled back. If you can use this strategy you should. However, if you wind up needing
|
36
|
-
to use multiple database connections in your tests (i.e. your tests run in a different proceess
|
37
|
-
than your application) then using this strategy becomes a bit more difficult. You can get around the
|
38
|
-
problem a number of ways. One common approach is to force all processes to use the same database
|
39
|
-
connection ("common ActiveRecord hack":http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/) however this approach has been reported to result in
|
40
|
-
non-deterministic failures. Another approach is to have the transactions rolled back in the
|
41
|
-
application's process and relax the isolation level of the database (so the tests can read the
|
42
|
-
uncommited transactions). An easier, but slower, solution is to use the @:truncation@ or
|
43
|
-
@:deletion@ strategy.
|
44
|
-
|
45
|
-
So what is fastest out of @:deletion@ and @:truncation@? Well, it depends on your table structure
|
46
|
-
and what percentage of tables you populate in an average test. The reasoning is out the the
|
47
|
-
scope of this README but here is a "good SO answer on this topic for Postgres":http://stackoverflow.com/questions/11419536/postgresql-truncation-speed/11423886#11423886. Some people report
|
48
|
-
much faster speeds with @:deletion@ while others say @:truncation@ is faster for them. The best approach therefore
|
49
|
-
is it try all options on your test suite and see what is faster. If you are using ActiveRecord then take a look
|
50
|
-
at the "additional options":#ar_truncation available for @:truncation@.
|
51
|
-
|
52
|
-
h2. Dependencies
|
53
|
-
|
54
|
-
Because database_cleaner supports multiple ORMs, it doesn't make sense to include all the dependencies
|
55
|
-
for each one in the gemspec. However, the DataMapper adapter does depend on dm-transactions. Therefore,
|
56
|
-
if you use DataMapper, you must include dm-transactions in your Gemfile/bundle/gemset manually.
|
57
|
-
|
58
|
-
h2. How to use
|
59
|
-
|
60
|
-
<pre>
|
61
|
-
require 'database_cleaner'
|
62
|
-
DatabaseCleaner.strategy = :truncation
|
63
|
-
|
64
|
-
# then, whenever you need to clean the DB
|
65
|
-
DatabaseCleaner.clean
|
66
|
-
</pre>
|
67
|
-
|
68
|
-
With the :truncation strategy you can also pass in options, for example:
|
69
|
-
<pre>
|
70
|
-
DatabaseCleaner.strategy = :truncation, {:only => %w[widgets dogs some_other_table]}
|
71
|
-
</pre>
|
72
|
-
|
73
|
-
<pre>
|
74
|
-
DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
|
75
|
-
</pre>
|
76
|
-
|
77
|
-
(I should point out the truncation strategy will never truncate your schema_migrations table.)
|
78
|
-
|
79
|
-
Some strategies require that you call DatabaseCleaner.start before calling clean
|
80
|
-
(for example the :transaction one needs to know to open up a transaction). So
|
81
|
-
you would have:
|
82
|
-
|
83
|
-
<pre>
|
84
|
-
require 'database_cleaner'
|
85
|
-
DatabaseCleaner.strategy = :transaction
|
86
|
-
|
87
|
-
DatabaseCleaner.start # usually this is called in setup of a test
|
88
|
-
dirty_the_db
|
89
|
-
DatabaseCleaner.clean # cleanup of the test
|
90
|
-
</pre>
|
91
|
-
|
92
|
-
At times you may want to do a single clean with one strategy. For example, you may want
|
93
|
-
to start the process by truncating all the tables, but then use the faster transaction
|
94
|
-
strategy the remaining time. To accomplish this you can say:
|
95
|
-
|
96
|
-
<pre>
|
97
|
-
require 'database_cleaner'
|
98
|
-
DatabaseCleaner.clean_with :truncation
|
99
|
-
DatabaseCleaner.strategy = :transaction
|
100
|
-
# then make the DatabaseCleaner.start and DatabaseCleaner.clean calls appropriately
|
101
|
-
</pre>
|
102
|
-
|
103
|
-
h3(#ar_truncation). Additional ActiveRecord options for Truncation
|
104
|
-
|
105
|
-
The following options are available for ActiveRecord's @:truncation@ strategy _only_ for
|
106
|
-
MySQL and Postgres.
|
107
|
-
|
108
|
-
* @:pre_count@ - When set to @true@ this will check each table for existing rows before
|
109
|
-
truncating it. This can speed up test suites when many of the tables to be truncated
|
110
|
-
are never populated. Defaults to @:false@. (Also, see the section on "What strategy is fastest?":#fastest)
|
111
|
-
* @:reset_ids@ - This only matters when @:pre_count@ is used, and it will make sure that a
|
112
|
-
tables auto-incrementing id is reset even if there are no rows in the table (e.g. records
|
113
|
-
were created in the test but also removed before DatabaseCleaner gets to it). Defaults to @true@.
|
114
|
-
|
115
|
-
|
116
|
-
h3. RSpec Example
|
117
|
-
|
118
|
-
<pre>
|
119
|
-
RSpec.configure do |config|
|
120
|
-
|
121
|
-
config.before(:suite) do
|
122
|
-
DatabaseCleaner.strategy = :transaction
|
123
|
-
DatabaseCleaner.clean_with(:truncation)
|
124
|
-
end
|
125
|
-
|
126
|
-
config.before(:each) do
|
127
|
-
DatabaseCleaner.start
|
128
|
-
end
|
129
|
-
|
130
|
-
config.after(:each) do
|
131
|
-
DatabaseCleaner.clean
|
132
|
-
end
|
133
|
-
|
134
|
-
end
|
135
|
-
</pre>
|
136
|
-
|
137
|
-
h3. Minitest Example
|
138
|
-
|
139
|
-
<pre>
|
140
|
-
DatabaseCleaner.strategy = :transaction
|
141
|
-
|
142
|
-
class MiniTest::Spec
|
143
|
-
before :each do
|
144
|
-
DatabaseCleaner.start
|
145
|
-
end
|
146
|
-
|
147
|
-
after :each do
|
148
|
-
DatabaseCleaner.clean
|
149
|
-
end
|
150
|
-
end
|
151
|
-
</pre>
|
152
|
-
|
153
|
-
h3. Cucumber Example
|
154
|
-
|
155
|
-
If you're using Cucumber with Rails, just use the generator that ships with cucumber-rails, and that will create all the code you need to integrate DatabaseCleaner into your Rails project.
|
156
|
-
|
157
|
-
Otherwise, to add DatabaseCleaner to your project by hand, create a file features/support/database_cleaner.rb that looks like this:
|
158
|
-
|
159
|
-
<pre>
|
160
|
-
begin
|
161
|
-
require 'database_cleaner'
|
162
|
-
require 'database_cleaner/cucumber'
|
163
|
-
DatabaseCleaner.strategy = :truncation
|
164
|
-
rescue NameError
|
165
|
-
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
|
166
|
-
end
|
167
|
-
|
168
|
-
Before do
|
169
|
-
DatabaseCleaner.start
|
170
|
-
end
|
171
|
-
|
172
|
-
After do |scenario|
|
173
|
-
DatabaseCleaner.clean
|
174
|
-
end
|
175
|
-
</pre>
|
176
|
-
|
177
|
-
This should cover the basics of tear down between scenarios and keeping your database clean.
|
178
|
-
For more examples see the section "Why?"
|
179
|
-
|
180
|
-
h2. How to use with multiple ORM's
|
181
|
-
|
182
|
-
Sometimes you need to use multiple ORMs in your application. You can use DatabaseCleaner to clean multiple ORMs, and multiple connections for those ORMs.
|
183
|
-
|
184
|
-
<pre>
|
185
|
-
#How to specify particular orms
|
186
|
-
DatabaseCleaner[:active_record].strategy = :transaction
|
187
|
-
DatabaseCleaner[:mongo_mapper].strategy = :truncation
|
188
|
-
|
189
|
-
#How to specify particular connections
|
190
|
-
DatabaseCleaner[:active_record,{:connection => :two}]
|
191
|
-
|
192
|
-
# You may also pass in the model directly:
|
193
|
-
DatabaseCleaner[:active_record,{:model => ModelWithDifferentConnection}]
|
194
|
-
</pre>
|
195
|
-
|
196
|
-
Usage beyond that remains the same with DatabaseCleaner.start calling any setup on the different configured connections, and DatabaseCleaner.clean executing afterwards.
|
197
|
-
|
198
|
-
Configuration options
|
199
|
-
|
200
|
-
|
201
|
-
|_. ORM |_. How to access |_. Notes |
|
202
|
-
| Active Record | DatabaseCleaner[:active_record] | Connection specified as :symbol keys, loaded from config/database.yml. You may also pass in the ActiveRecord model under the @:model@ key. |
|
203
|
-
| Data Mapper | DatabaseCleaner[:data_mapper] | Connection specified as :symbol keys, loaded via Datamapper repositories |
|
204
|
-
| Mongo Mapper | DatabaseCleaner[:mongo_mapper] | Multiple connections not yet supported |
|
205
|
-
| Mongoid | DatabaseCleaner[:mongoid] | Multiple databases supported for Mongoid 3. Specify DatabaseCleaner[:mongoid, {:connection => :db_name}] |
|
206
|
-
| Couch Potato | DatabaseCleaner[:couch_potato] | Multiple connections not yet supported |
|
207
|
-
| Sequel | DatabaseCleaner[:sequel] | ? |
|
208
|
-
|
209
|
-
h2. Why?
|
210
|
-
|
211
|
-
One of my motivations for writing this library was to have an easy way to turn on what Rails calls "transactional_fixtures"
|
212
|
-
in my non-rails ActiveRecord projects. After copying and pasting code to do this several times I decided to package it up
|
213
|
-
as a gem and same everyone a bit of time.
|
214
|
-
|
215
|
-
h2. Common Errors
|
216
|
-
|
217
|
-
h4. DatabaseCleaner is trying to use the wrong ORM
|
218
|
-
|
219
|
-
DatabaseCleaner has an autodetect mechanism where if you do not explicitly define your ORM it will use the first ORM it can detect that is loaded. Since ActiveRecord is the most common ORM used that is the first one checked for. Sometimes other libraries (e.g. ActiveAdmin) will load other ORMs (e.g. ActiveRecord) even though you are using a different ORM. This will result in DatabaseCleaner trying to use the wrong ORM (e.g. ActiveRecord) unless you explicitly define your ORM like so:
|
220
|
-
|
221
|
-
<pre>
|
222
|
-
# How to setup your ORM explicitly
|
223
|
-
DatabaseCleaner[:mongoid].strategy = :truncation
|
224
|
-
</pre>
|
225
|
-
|
226
|
-
h4. STDERR is being flooded when using Postgres
|
227
|
-
|
228
|
-
If you are using Postgres and have foreign key constraints, the truncation strategy will cause a lot of extra noise to appear on STDERR (in
|
229
|
-
the form of "NOTICE truncate cascades" messages). To silence these warnings set the following log level in your postgresql.conf file:
|
230
|
-
|
231
|
-
<pre>
|
232
|
-
client_min_messages = warning
|
233
|
-
</pre>
|
234
|
-
|
235
|
-
|
236
|
-
h2. Debugging
|
237
|
-
|
238
|
-
In rare cases DatabaseCleaner will encounter errors that it will log. By default it uses STDOUT set to the ERROR level but you can configure this to use whatever Logger you desire. Here's an example of using the Rails.logger in env.rb:
|
239
|
-
|
240
|
-
<pre>
|
241
|
-
DatabaseCleaner.logger = Rails.logger
|
242
|
-
</pre>
|
243
|
-
|
244
|
-
|
245
|
-
h2. COPYRIGHT
|
246
|
-
|
247
|
-
Copyright (c) 2009 Ben Mabey. See LICENSE for details.
|