database_cleaner 1.6.2 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/database_cleaner.rb +1 -8
- metadata +25 -499
- data/CONTRIBUTE.markdown +0 -28
- data/Gemfile.lock +0 -286
- data/History.rdoc +0 -430
- data/LICENSE +0 -20
- data/README.markdown +0 -518
- data/Rakefile +0 -40
- data/TODO +0 -3
- data/VERSION.yml +0 -4
- data/cucumber.yml +0 -1
- data/examples/Gemfile +0 -1
- data/examples/Gemfile.lock +0 -1
- data/examples/config/database.yml.example +0 -8
- data/examples/config/redis.yml +0 -8
- data/examples/db/sqlite_databases_go_here +0 -0
- data/examples/features/example.feature +0 -11
- data/examples/features/example_multiple_db.feature +0 -23
- data/examples/features/example_multiple_orm.feature +0 -22
- data/examples/features/step_definitions/activerecord_steps.rb +0 -31
- data/examples/features/step_definitions/couchpotato_steps.rb +0 -31
- data/examples/features/step_definitions/datamapper_steps.rb +0 -37
- data/examples/features/step_definitions/mongoid_steps.rb +0 -23
- data/examples/features/step_definitions/mongomapper_steps.rb +0 -31
- data/examples/features/step_definitions/neo4j_steps.rb +0 -23
- data/examples/features/step_definitions/ohm_steps.rb +0 -31
- data/examples/features/step_definitions/redis_steps.rb +0 -31
- data/examples/features/step_definitions/translation_steps.rb +0 -55
- data/examples/features/support/env.rb +0 -61
- data/examples/lib/activerecord_models.rb +0 -41
- data/examples/lib/couchpotato_models.rb +0 -61
- data/examples/lib/datamapper_models.rb +0 -50
- data/examples/lib/mongoid_models.rb +0 -44
- data/examples/lib/mongomapper_models.rb +0 -51
- data/examples/lib/neo4j_models.rb +0 -17
- data/examples/lib/ohm_models.rb +0 -43
- data/examples/lib/redis_models.rb +0 -65
- data/examples/lib/sequel_models.rb +0 -9
- data/features/cleaning.feature +0 -30
- data/features/cleaning_default_strategy.feature +0 -23
- data/features/cleaning_multiple_dbs.feature +0 -22
- data/features/cleaning_multiple_orms.feature +0 -67
- data/features/step_definitions/database_cleaner_steps.rb +0 -33
- data/features/support/env.rb +0 -4
- data/features/support/feature_runner.rb +0 -39
- data/lib/database_cleaner/active_record/base.rb +0 -92
- data/lib/database_cleaner/active_record/deletion.rb +0 -106
- data/lib/database_cleaner/active_record/transaction.rb +0 -59
- data/lib/database_cleaner/active_record/truncation.rb +0 -275
- data/lib/database_cleaner/base.rb +0 -164
- data/lib/database_cleaner/configuration.rb +0 -131
- data/lib/database_cleaner/couch_potato/base.rb +0 -7
- data/lib/database_cleaner/couch_potato/truncation.rb +0 -28
- data/lib/database_cleaner/cucumber.rb +0 -3
- data/lib/database_cleaner/data_mapper/base.rb +0 -21
- data/lib/database_cleaner/data_mapper/transaction.rb +0 -28
- data/lib/database_cleaner/data_mapper/truncation.rb +0 -172
- data/lib/database_cleaner/generic/base.rb +0 -29
- data/lib/database_cleaner/generic/transaction.rb +0 -11
- data/lib/database_cleaner/generic/truncation.rb +0 -40
- data/lib/database_cleaner/mongo/base.rb +0 -16
- data/lib/database_cleaner/mongo/truncation.rb +0 -62
- data/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
- data/lib/database_cleaner/mongo2/base.rb +0 -16
- data/lib/database_cleaner/mongo2/truncation_mixin.rb +0 -39
- data/lib/database_cleaner/mongo_mapper/base.rb +0 -20
- data/lib/database_cleaner/mongo_mapper/truncation.rb +0 -19
- data/lib/database_cleaner/mongoid/base.rb +0 -20
- data/lib/database_cleaner/mongoid/truncation.rb +0 -49
- data/lib/database_cleaner/moped/base.rb +0 -39
- data/lib/database_cleaner/moped/truncation.rb +0 -9
- data/lib/database_cleaner/moped/truncation_base.rb +0 -40
- data/lib/database_cleaner/neo4j/base.rb +0 -62
- data/lib/database_cleaner/neo4j/deletion.rb +0 -16
- data/lib/database_cleaner/neo4j/transaction.rb +0 -35
- data/lib/database_cleaner/neo4j/truncation.rb +0 -9
- data/lib/database_cleaner/null_strategy.rb +0 -20
- data/lib/database_cleaner/ohm/truncation.rb +0 -15
- data/lib/database_cleaner/redis/base.rb +0 -37
- data/lib/database_cleaner/redis/truncation.rb +0 -26
- data/lib/database_cleaner/sequel/base.rb +0 -22
- data/lib/database_cleaner/sequel/deletion.rb +0 -47
- data/lib/database_cleaner/sequel/transaction.rb +0 -40
- data/lib/database_cleaner/sequel/truncation.rb +0 -78
- data/spec/database_cleaner/active_record/base_spec.rb +0 -188
- data/spec/database_cleaner/active_record/transaction_spec.rb +0 -176
- data/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +0 -38
- data/spec/database_cleaner/active_record/truncation/mysql_spec.rb +0 -38
- data/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +0 -75
- data/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb +0 -40
- data/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +0 -40
- data/spec/database_cleaner/active_record/truncation_spec.rb +0 -180
- data/spec/database_cleaner/base_spec.rb +0 -617
- data/spec/database_cleaner/configuration_spec.rb +0 -345
- data/spec/database_cleaner/couch_potato/truncation_spec.rb +0 -41
- data/spec/database_cleaner/data_mapper/base_spec.rb +0 -30
- data/spec/database_cleaner/data_mapper/transaction_spec.rb +0 -23
- data/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +0 -41
- data/spec/database_cleaner/data_mapper/truncation_spec.rb +0 -11
- data/spec/database_cleaner/generic/base_spec.rb +0 -61
- data/spec/database_cleaner/generic/truncation_spec.rb +0 -118
- data/spec/database_cleaner/mongo/mongo_examples.rb +0 -26
- data/spec/database_cleaner/mongo/truncation_spec.rb +0 -72
- data/spec/database_cleaner/mongo_mapper/base_spec.rb +0 -33
- data/spec/database_cleaner/mongo_mapper/mongo_examples.rb +0 -8
- data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +0 -74
- data/spec/database_cleaner/moped/moped_examples.rb +0 -32
- data/spec/database_cleaner/moped/truncation_spec.rb +0 -80
- data/spec/database_cleaner/neo4j/base_spec.rb +0 -43
- data/spec/database_cleaner/neo4j/transaction_spec.rb +0 -25
- data/spec/database_cleaner/null_strategy_spec.rb +0 -28
- data/spec/database_cleaner/ohm/truncation_spec.rb +0 -70
- data/spec/database_cleaner/redis/base_spec.rb +0 -43
- data/spec/database_cleaner/redis/truncation_spec.rb +0 -63
- data/spec/database_cleaner/sequel/base_spec.rb +0 -32
- data/spec/database_cleaner/sequel/deletion_spec.rb +0 -58
- data/spec/database_cleaner/sequel/transaction_spec.rb +0 -21
- data/spec/database_cleaner/sequel/truncation/sqlite3_spec.rb +0 -0
- data/spec/database_cleaner/sequel/truncation_spec.rb +0 -182
- data/spec/database_cleaner/shared_strategy.rb +0 -15
- data/spec/rcov.opts +0 -1
- data/spec/spec_helper.rb +0 -21
- data/spec/support/active_record/database_setup.rb +0 -6
- data/spec/support/active_record/migrations/20150101010000_create_users.rb +0 -14
- data/spec/support/active_record/migrations/20150101020000_create_agents.rb +0 -14
- data/spec/support/active_record/mysql2_setup.rb +0 -39
- data/spec/support/active_record/mysql_setup.rb +0 -38
- data/spec/support/active_record/postgresql_setup.rb +0 -48
- data/spec/support/active_record/schema_setup.rb +0 -17
- data/spec/support/active_record/sqlite3_setup.rb +0 -40
- data/spec/support/data_mapper/schema_setup.rb +0 -15
- data/spec/support/data_mapper/sqlite3_setup.rb +0 -39
data/LICENSE
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
Copyright (c) 2009 Ben Mabey
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
DELETED
@@ -1,518 +0,0 @@
|
|
1
|
-
# Database Cleaner
|
2
|
-
|
3
|
-
[![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner)
|
4
|
-
[![Code Climate](https://codeclimate.com/github/DatabaseCleaner/database_cleaner/badges/gpa.svg)](https://codeclimate.com/github/DatabaseCleaner/database_cleaner)
|
5
|
-
|
6
|
-
Database Cleaner is a set of strategies for cleaning your database in Ruby.
|
7
|
-
|
8
|
-
The original use case was to ensure a clean state during tests.
|
9
|
-
Each strategy is a small amount of code but is code that is usually needed in any ruby app that is testing with a database.
|
10
|
-
|
11
|
-
## Gem Setup
|
12
|
-
|
13
|
-
```ruby
|
14
|
-
# Gemfile
|
15
|
-
group :test do
|
16
|
-
gem 'database_cleaner'
|
17
|
-
end
|
18
|
-
```
|
19
|
-
|
20
|
-
## Supported Databases, Libraries and Strategies
|
21
|
-
|
22
|
-
ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, CouchPotato, Ohm and Redis are supported.
|
23
|
-
|
24
|
-
Here is an overview of the strategies supported for each library:
|
25
|
-
|
26
|
-
<table>
|
27
|
-
<tbody>
|
28
|
-
<tr>
|
29
|
-
<th>ORM</th>
|
30
|
-
<th>Truncation</th>
|
31
|
-
<th>Transaction</th>
|
32
|
-
<th>Deletion</th>
|
33
|
-
</tr>
|
34
|
-
<tr>
|
35
|
-
<td> ActiveRecord </td>
|
36
|
-
<td> Yes</td>
|
37
|
-
<td> <b>Yes</b></td>
|
38
|
-
<td> Yes</td>
|
39
|
-
</tr>
|
40
|
-
<tr>
|
41
|
-
<td> DataMapper</td>
|
42
|
-
<td> Yes</td>
|
43
|
-
<td> <b>Yes</b></td>
|
44
|
-
<td> No</td>
|
45
|
-
</tr>
|
46
|
-
<tr>
|
47
|
-
<td> CouchPotato</td>
|
48
|
-
<td> <b>Yes</b></td>
|
49
|
-
<td> No</td>
|
50
|
-
<td> No</td>
|
51
|
-
</tr>
|
52
|
-
<tr>
|
53
|
-
<td> MongoMapper</td>
|
54
|
-
<td> <b>Yes</b></td>
|
55
|
-
<td> No</td>
|
56
|
-
<td> No</td>
|
57
|
-
</tr>
|
58
|
-
<tr>
|
59
|
-
<td> Mongoid</td>
|
60
|
-
<td> <b>Yes</b></td>
|
61
|
-
<td> No</td>
|
62
|
-
<td> No</td>
|
63
|
-
</tr>
|
64
|
-
<tr>
|
65
|
-
<td> Sequel</td>
|
66
|
-
<td> <b>Yes</b></td>
|
67
|
-
<td> Yes</td>
|
68
|
-
<td> Yes</td>
|
69
|
-
</tr>
|
70
|
-
<tr>
|
71
|
-
<td>Redis</td>
|
72
|
-
<td><b>Yes</b></td>
|
73
|
-
<td>No</td>
|
74
|
-
<td>No</td>
|
75
|
-
</tr>
|
76
|
-
<tr>
|
77
|
-
<td>Ohm</td>
|
78
|
-
<td><b>Yes</b></td>
|
79
|
-
<td>No</td>
|
80
|
-
<td>No</td>
|
81
|
-
</tr>
|
82
|
-
<tr>
|
83
|
-
<td>Neo4j</td>
|
84
|
-
<td>Yes</td>
|
85
|
-
<td>Yes*</td>
|
86
|
-
<td>Yes*</td>
|
87
|
-
</tr>
|
88
|
-
</tbody>
|
89
|
-
</table>
|
90
|
-
|
91
|
-
\* Truncation and Deletion strategies for Neo4j will just delete all nodes and relationships from the database.
|
92
|
-
|
93
|
-
<table>
|
94
|
-
<tbody>
|
95
|
-
<tr>
|
96
|
-
<th>Driver</th>
|
97
|
-
<th>Truncation</th>
|
98
|
-
<th>Transaction</th>
|
99
|
-
<th>Deletion</th>
|
100
|
-
</tr>
|
101
|
-
<tr>
|
102
|
-
<td> Mongo</td>
|
103
|
-
<td> Yes</td>
|
104
|
-
<td> No</td>
|
105
|
-
<td> No</td>
|
106
|
-
</tr>
|
107
|
-
<tr>
|
108
|
-
<td> Moped</td>
|
109
|
-
<td> Yes</td>
|
110
|
-
<td> No</td>
|
111
|
-
<td> No</td>
|
112
|
-
</tr>
|
113
|
-
</tbody>
|
114
|
-
</table>
|
115
|
-
|
116
|
-
(Default strategy for each library is denoted in bold)
|
117
|
-
|
118
|
-
Database Cleaner also includes a `null` strategy (that does no cleaning at all) which can be used with any ORM library.
|
119
|
-
You can also explicitly use it by setting your strategy to `nil`.
|
120
|
-
|
121
|
-
For support or to discuss development please use the [Google Group](http://groups.google.com/group/database_cleaner).
|
122
|
-
|
123
|
-
## What strategy is fastest?
|
124
|
-
|
125
|
-
For the SQL libraries the fastest option will be to use `:transaction` as transactions are simply rolled back. If you can use this strategy you should. However, if you wind up needing to use multiple database connections in your tests (i.e. your tests run in a different process than your application) then using this strategy becomes a bit more difficult. You can get around the problem a number of ways.
|
126
|
-
|
127
|
-
One common approach is to force all processes to use the same database 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 non-deterministic failures.
|
128
|
-
|
129
|
-
Another approach is to have the transactions rolled back in the application's process and relax the isolation level of the database (so the tests can read the uncommitted transactions).
|
130
|
-
|
131
|
-
An easier, but slower, solution is to use the `:truncation` or `:deletion` strategy.
|
132
|
-
|
133
|
-
So what is fastest out of `:deletion` and `:truncation`? Well, it depends on your table structure and what percentage of tables you populate in an average test. The reasoning is out of the 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).
|
134
|
-
|
135
|
-
Some people report much faster speeds with `:deletion` while others say `:truncation` is faster for them. The best approach therefore is it try all options on your test suite and see what is faster.
|
136
|
-
|
137
|
-
If you are using ActiveRecord then take a look at the [additional options](#additional-activerecord-options-for-truncation) available for `:truncation`.
|
138
|
-
|
139
|
-
## Dependencies
|
140
|
-
|
141
|
-
Because database_cleaner supports multiple ORMs, it doesn't make sense to include all the dependencies for each one in the gemspec. However, the DataMapper adapter does depend on dm-transactions. Therefore, if you use DataMapper, you must include dm-transactions in your Gemfile/bundle/gemset manually.
|
142
|
-
|
143
|
-
## How to use
|
144
|
-
|
145
|
-
```ruby
|
146
|
-
require 'database_cleaner'
|
147
|
-
|
148
|
-
DatabaseCleaner.strategy = :truncation
|
149
|
-
|
150
|
-
# then, whenever you need to clean the DB
|
151
|
-
DatabaseCleaner.clean
|
152
|
-
```
|
153
|
-
|
154
|
-
With the `:truncation` strategy you can also pass in options, for example:
|
155
|
-
|
156
|
-
```ruby
|
157
|
-
DatabaseCleaner.strategy = :truncation, {:only => %w[widgets dogs some_other_table]}
|
158
|
-
```
|
159
|
-
|
160
|
-
```ruby
|
161
|
-
DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
|
162
|
-
```
|
163
|
-
|
164
|
-
With Ohm and Redis, `:only` and `:except` take a list of strings to be
|
165
|
-
passed to [`keys`](http://redis.io/commands/keys)).
|
166
|
-
|
167
|
-
(I should point out the truncation strategy will never truncate your schema_migrations table.)
|
168
|
-
|
169
|
-
Some strategies need to be started before tests are run (for example the `:transaction` strategy needs to know to open up a transaction). This can be accomplished by calling `DatabaseCleaner.start` at the beginning of the run, or by running the tests inside a block to `DatabaseCleaner.cleaning`. So you would have:
|
170
|
-
|
171
|
-
```ruby
|
172
|
-
require 'database_cleaner'
|
173
|
-
|
174
|
-
DatabaseCleaner.strategy = :transaction
|
175
|
-
|
176
|
-
DatabaseCleaner.start # usually this is called in setup of a test
|
177
|
-
|
178
|
-
dirty_the_db
|
179
|
-
|
180
|
-
DatabaseCleaner.clean # cleanup of the test
|
181
|
-
|
182
|
-
# OR
|
183
|
-
|
184
|
-
DatabaseCleaner.cleaning do
|
185
|
-
dirty_the_db
|
186
|
-
end
|
187
|
-
```
|
188
|
-
|
189
|
-
At times you may want to do a single clean with one strategy.
|
190
|
-
|
191
|
-
For example, you may want to start the process by truncating all the tables, but then use the faster transaction strategy the remaining time. To accomplish this you can say:
|
192
|
-
|
193
|
-
```ruby
|
194
|
-
require 'database_cleaner'
|
195
|
-
|
196
|
-
DatabaseCleaner.clean_with :truncation
|
197
|
-
|
198
|
-
DatabaseCleaner.strategy = :transaction
|
199
|
-
|
200
|
-
# then make the DatabaseCleaner.start and DatabaseCleaner.clean calls appropriately
|
201
|
-
```
|
202
|
-
|
203
|
-
### Additional ActiveRecord options for Truncation
|
204
|
-
|
205
|
-
The following options are available for ActiveRecord's `:truncation` strategy _only_ for MySQL and Postgres.
|
206
|
-
|
207
|
-
* `:pre_count` - When set to `true` this will check each table for existing rows before truncating it. This can speed up test suites when many of the tables to be truncated are never populated. Defaults to `:false`. (Also, see the section on [What strategy is fastest?](#what-strategy-is-fastest))
|
208
|
-
* `:reset_ids` - This only matters when `:pre_count` is used, and it will make sure that a tables auto-incrementing id is reset even if there are no rows in the table (e.g. records were created in the test but also removed before DatabaseCleaner gets to it). Defaults to `true`.
|
209
|
-
|
210
|
-
The following option is available for ActiveRecord's `:truncation` and `:deletion` strategy for any DB.
|
211
|
-
|
212
|
-
* `:cache_tables` - When set to `true` the list of tables to truncate or delete from will only be read from the DB once, otherwise it will be read before each cleanup run. Set this to `false` if (1) you create and drop tables in your tests, or (2) you change Postgres schemas (`ActiveRecord::Base.connection.schema_search_path`) in your tests (for example, in a multitenancy setup with each tenant in a different Postgres schema). Defaults to `true`.
|
213
|
-
|
214
|
-
|
215
|
-
### RSpec Example
|
216
|
-
|
217
|
-
```ruby
|
218
|
-
RSpec.configure do |config|
|
219
|
-
|
220
|
-
config.before(:suite) do
|
221
|
-
DatabaseCleaner.strategy = :transaction
|
222
|
-
DatabaseCleaner.clean_with(:truncation)
|
223
|
-
end
|
224
|
-
|
225
|
-
config.around(:each) do |example|
|
226
|
-
DatabaseCleaner.cleaning do
|
227
|
-
example.run
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
end
|
232
|
-
```
|
233
|
-
|
234
|
-
### RSpec with Capybara Example
|
235
|
-
|
236
|
-
You'll typically discover a feature spec is incorrectly using transaction
|
237
|
-
instead of truncation strategy when the data created in the spec is not
|
238
|
-
visible in the app-under-test.
|
239
|
-
|
240
|
-
A frequently occurring example of this is when, after creating a user in a
|
241
|
-
spec, the spec mysteriously fails to login with the user. This happens because
|
242
|
-
the user is created inside of an uncommitted transaction on one database
|
243
|
-
connection, while the login attempt is made using a separate database
|
244
|
-
connection. This separate database connection cannot access the
|
245
|
-
uncommitted user data created over the first database connection due to
|
246
|
-
transaction isolation.
|
247
|
-
|
248
|
-
For feature specs using a Capybara driver for an external
|
249
|
-
JavaScript-capable browser (in practice this is all drivers except
|
250
|
-
`:rack_test`), the Rack app under test and the specs do not share a
|
251
|
-
database connection.
|
252
|
-
|
253
|
-
When a spec and app-under-test do not share a database connection,
|
254
|
-
you'll likely need to use the truncation strategy instead of the
|
255
|
-
transaction strategy.
|
256
|
-
|
257
|
-
See the suggested config below to temporarily enable truncation strategy
|
258
|
-
for affected feature specs only. This config continues to use transaction
|
259
|
-
strategy for all other specs.
|
260
|
-
|
261
|
-
It's also recommended to use `append_after` to ensure `DatabaseCleaner.clean`
|
262
|
-
runs *after* the after-test cleanup `capybara/rspec` installs.
|
263
|
-
|
264
|
-
```ruby
|
265
|
-
require 'capybara/rspec'
|
266
|
-
|
267
|
-
#...
|
268
|
-
|
269
|
-
RSpec.configure do |config|
|
270
|
-
|
271
|
-
config.use_transactional_fixtures = false
|
272
|
-
|
273
|
-
config.before(:suite) do
|
274
|
-
if config.use_transactional_fixtures?
|
275
|
-
raise(<<-MSG)
|
276
|
-
Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
|
277
|
-
(or set it to false) to prevent uncommitted transactions being used in
|
278
|
-
JavaScript-dependent specs.
|
279
|
-
|
280
|
-
During testing, the app-under-test that the browser driver connects to
|
281
|
-
uses a different database connection to the database connection used by
|
282
|
-
the spec. The app's database connection would not be able to access
|
283
|
-
uncommitted transaction data setup over the spec's database connection.
|
284
|
-
MSG
|
285
|
-
end
|
286
|
-
DatabaseCleaner.clean_with(:truncation)
|
287
|
-
end
|
288
|
-
|
289
|
-
config.before(:each) do
|
290
|
-
DatabaseCleaner.strategy = :transaction
|
291
|
-
end
|
292
|
-
|
293
|
-
config.before(:each, type: :feature) do
|
294
|
-
# :rack_test driver's Rack app under test shares database connection
|
295
|
-
# with the specs, so continue to use transaction strategy for speed.
|
296
|
-
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
|
297
|
-
|
298
|
-
if !driver_shares_db_connection_with_specs
|
299
|
-
# Driver is probably for an external browser with an app
|
300
|
-
# under test that does *not* share a database connection with the
|
301
|
-
# specs, so use truncation strategy.
|
302
|
-
DatabaseCleaner.strategy = :truncation
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
config.before(:each) do
|
307
|
-
DatabaseCleaner.start
|
308
|
-
end
|
309
|
-
|
310
|
-
config.append_after(:each) do
|
311
|
-
DatabaseCleaner.clean
|
312
|
-
end
|
313
|
-
|
314
|
-
end
|
315
|
-
```
|
316
|
-
|
317
|
-
|
318
|
-
### Minitest Example
|
319
|
-
|
320
|
-
```ruby
|
321
|
-
DatabaseCleaner.strategy = :transaction
|
322
|
-
|
323
|
-
class Minitest::Spec
|
324
|
-
before :each do
|
325
|
-
DatabaseCleaner.start
|
326
|
-
end
|
327
|
-
|
328
|
-
after :each do
|
329
|
-
DatabaseCleaner.clean
|
330
|
-
end
|
331
|
-
end
|
332
|
-
|
333
|
-
# with the minitest-around gem, this may be used instead:
|
334
|
-
class Minitest::Spec
|
335
|
-
around do |tests|
|
336
|
-
DatabaseCleaner.cleaning(&tests)
|
337
|
-
end
|
338
|
-
end
|
339
|
-
```
|
340
|
-
|
341
|
-
### Cucumber Example
|
342
|
-
|
343
|
-
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.
|
344
|
-
|
345
|
-
Otherwise, to add DatabaseCleaner to your project by hand, create a file `features/support/database_cleaner.rb` that looks like this:
|
346
|
-
|
347
|
-
```ruby
|
348
|
-
begin
|
349
|
-
require 'database_cleaner'
|
350
|
-
require 'database_cleaner/cucumber'
|
351
|
-
|
352
|
-
DatabaseCleaner.strategy = :truncation
|
353
|
-
rescue NameError
|
354
|
-
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
|
355
|
-
end
|
356
|
-
|
357
|
-
Around do |scenario, block|
|
358
|
-
DatabaseCleaner.cleaning(&block)
|
359
|
-
end
|
360
|
-
```
|
361
|
-
|
362
|
-
This should cover the basics of tear down between scenarios and keeping your database clean.
|
363
|
-
|
364
|
-
For more examples see the section ["Why?"](#why).
|
365
|
-
|
366
|
-
## How to use with multiple ORMs
|
367
|
-
|
368
|
-
Sometimes you need to use multiple ORMs in your application.
|
369
|
-
|
370
|
-
You can use DatabaseCleaner to clean multiple ORMs, and multiple connections for those ORMs.
|
371
|
-
|
372
|
-
```ruby
|
373
|
-
#How to specify particular orms
|
374
|
-
DatabaseCleaner[:active_record].strategy = :transaction
|
375
|
-
DatabaseCleaner[:mongo_mapper].strategy = :truncation
|
376
|
-
|
377
|
-
#How to specify particular connections
|
378
|
-
DatabaseCleaner[:active_record, { :connection => :two }]
|
379
|
-
|
380
|
-
# You may also pass in the model directly:
|
381
|
-
DatabaseCleaner[:active_record, { :model => ModelWithDifferentConnection }]
|
382
|
-
```
|
383
|
-
|
384
|
-
Usage beyond that remains the same with `DatabaseCleaner.start` calling any setup on the different configured connections, and `DatabaseCleaner.clean` executing afterwards.
|
385
|
-
|
386
|
-
### Configuration options
|
387
|
-
|
388
|
-
<table>
|
389
|
-
<tbody>
|
390
|
-
<tr>
|
391
|
-
<th>ORM</th>
|
392
|
-
<th>How to access</th>
|
393
|
-
<th>Notes</th>
|
394
|
-
</tr>
|
395
|
-
<tr>
|
396
|
-
<td> Active Record </td>
|
397
|
-
<td> <code>DatabaseCleaner[:active_record]</code></td>
|
398
|
-
<td> Connection specified as <code>:symbol</code> keys, loaded from <code>config/database.yml</code>. You may also pass in the ActiveRecord model under the <code>:model</code> key.</td>
|
399
|
-
</tr>
|
400
|
-
<tr>
|
401
|
-
<td> Data Mapper</td>
|
402
|
-
<td> <code>DatabaseCleaner[:data_mapper]</code></td>
|
403
|
-
<td> Connection specified as <code>:symbol</code> keys, loaded via Datamapper repositories </td>
|
404
|
-
</tr>
|
405
|
-
<tr>
|
406
|
-
<td> Mongo Mapper</td>
|
407
|
-
<td> <code>DatabaseCleaner[:mongo_mapper]</code></td>
|
408
|
-
<td> Multiple connections not yet supported</td>
|
409
|
-
</tr>
|
410
|
-
<tr>
|
411
|
-
<td> Mongoid</td>
|
412
|
-
<td> <code>DatabaseCleaner[:mongoid]</code></td>
|
413
|
-
<td> Multiple databases supported for Mongoid 3. Specify <code>DatabaseCleaner[:mongoid, {:connection => :db_name}]</code> </td>
|
414
|
-
</tr>
|
415
|
-
<tr>
|
416
|
-
<td> Moped</td>
|
417
|
-
<td> <code>DatabaseCleaner[:moped]</code></td>
|
418
|
-
<td> It is necessary to configure database name with <code>DatabaseCleaner[:moped].db = db_name</code> otherwise name `default` will be used.</td>
|
419
|
-
</tr>
|
420
|
-
<tr>
|
421
|
-
<td> Couch Potato</td>
|
422
|
-
<td> <code>DatabaseCleaner[:couch_potato]</code></td>
|
423
|
-
<td> Multiple connections not yet supported</td>
|
424
|
-
</tr>
|
425
|
-
<tr>
|
426
|
-
<td> Sequel</td>
|
427
|
-
<td> <code>DatabaseCleaner[:sequel]</code></td>
|
428
|
-
<td> Multiple databases supported; specify <code>DatabaseCleaner[:sequel, {:connection => Sequel.connect(uri)}]</code></td>
|
429
|
-
</tr>
|
430
|
-
<tr>
|
431
|
-
<td>Redis</td>
|
432
|
-
<td><code>DatabaseCleaner[:redis]</code></td>
|
433
|
-
<td>Connection specified as Redis URI</td>
|
434
|
-
</tr>
|
435
|
-
<tr>
|
436
|
-
<td>Ohm</td>
|
437
|
-
<td><code>DatabaseCleaner[:ohm]</code></td>
|
438
|
-
<td>Connection specified as Redis URI</td>
|
439
|
-
</tr>
|
440
|
-
<tr>
|
441
|
-
<td>Neo4j</td>
|
442
|
-
<td><code>DatabaseCleaner[:neo4j]</code></td>
|
443
|
-
<td>Database type and path(URI) <code>DatabaseCleaner[:neo4j, connection: {type: :server_db, path: 'http://localhost:7475'}].</code></td>
|
444
|
-
</tr>
|
445
|
-
</tbody>
|
446
|
-
</table>
|
447
|
-
|
448
|
-
## Why?
|
449
|
-
|
450
|
-
One of my motivations for writing this library was to have an easy way to turn on what Rails calls "transactional_fixtures" in my non-rails ActiveRecord projects.
|
451
|
-
|
452
|
-
After copying and pasting code to do this several times I decided to package it up as a gem and save everyone a bit of time.
|
453
|
-
|
454
|
-
## Common Errors
|
455
|
-
|
456
|
-
#### DatabaseCleaner is trying to use the wrong ORM
|
457
|
-
|
458
|
-
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.
|
459
|
-
|
460
|
-
Since ActiveRecord is the most common ORM used that is the first one checked for.
|
461
|
-
|
462
|
-
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:
|
463
|
-
|
464
|
-
```ruby
|
465
|
-
# How to setup your ORM explicitly
|
466
|
-
DatabaseCleaner[:mongoid].strategy = :truncation
|
467
|
-
```
|
468
|
-
|
469
|
-
### STDERR is being flooded when using Postgres
|
470
|
-
|
471
|
-
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 the form of "NOTICE truncate cascades" messages).
|
472
|
-
|
473
|
-
To silence these warnings set the following log level in your `postgresql.conf` file:
|
474
|
-
|
475
|
-
```ruby
|
476
|
-
client_min_messages = warning
|
477
|
-
```
|
478
|
-
|
479
|
-
For ActiveRecord, you add the following parameter in your database.yml file:
|
480
|
-
|
481
|
-
<pre>
|
482
|
-
test:
|
483
|
-
adapter: postgresql
|
484
|
-
# ...
|
485
|
-
min_messages: WARNING
|
486
|
-
</pre>
|
487
|
-
|
488
|
-
### Nothing happens in JRuby with Sequel using transactions
|
489
|
-
|
490
|
-
Due to an inconsistency in JRuby's implementation of Fibers, Sequel gives a different connection to `DatabaseCleaner.start` than is used for tests run between `.start` and `.clean`. This can be worked around by running your tests in a block like `DatabaseCleaner.cleaning { run_my_tests }` instead, which does not use Fibers.
|
491
|
-
|
492
|
-
### Model fails to load with Neo4j using transactions
|
493
|
-
|
494
|
-
When you are using [neo4j](https://github.com/neo4jrb/neo4j) gem it creates schema and reads indexes upon loading models. These operations can't be done during a transaction. You have to preload your models before DatabaseCleaner starts a transaction.
|
495
|
-
|
496
|
-
Add to your rails_helper or spec_helper after requiring database_cleaner:
|
497
|
-
|
498
|
-
```ruby
|
499
|
-
require 'database_cleaner'
|
500
|
-
Dir["#{Rails.root}/app/models/**/*.rb"].each do |model|
|
501
|
-
load model
|
502
|
-
end
|
503
|
-
```
|
504
|
-
|
505
|
-
## Debugging
|
506
|
-
|
507
|
-
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.
|
508
|
-
|
509
|
-
Here's an example of using the `Rails.logger` in `env.rb`:
|
510
|
-
|
511
|
-
```ruby
|
512
|
-
DatabaseCleaner.logger = Rails.logger
|
513
|
-
```
|
514
|
-
|
515
|
-
|
516
|
-
## COPYRIGHT
|
517
|
-
|
518
|
-
Copyright (c) 2014 Ben Mabey. See LICENSE for details.
|
data/Rakefile
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
require "bundler"
|
3
|
-
Bundler.setup
|
4
|
-
|
5
|
-
require 'rake'
|
6
|
-
require 'rspec/core'
|
7
|
-
require 'rspec/core/rake_task'
|
8
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
9
|
-
|
10
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
11
|
-
end
|
12
|
-
|
13
|
-
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
14
|
-
spec.pattern = 'spec/**/*_spec.rb'
|
15
|
-
spec.rcov = true
|
16
|
-
end
|
17
|
-
|
18
|
-
begin
|
19
|
-
require 'cucumber/rake/task'
|
20
|
-
Cucumber::Rake::Task.new(:features)
|
21
|
-
rescue LoadError
|
22
|
-
puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
|
23
|
-
end
|
24
|
-
|
25
|
-
task :default => [:spec, :features]
|
26
|
-
|
27
|
-
|
28
|
-
desc "Cleans the project of any tmp file that should not be included in the gemspec."
|
29
|
-
task :clean do
|
30
|
-
["examples/config/database.yml", "examples/db/activerecord_one.db", "examples/db/activerecord_two.db", "examples/db/datamapper_default.db",
|
31
|
-
"examples/db/datamapper_one.db", "examples/db/datamapper_two.db"].each do |f|
|
32
|
-
FileUtils.rm_f(f)
|
33
|
-
end
|
34
|
-
%w[*.sqlite3 *.log #* *.swp *.swo].each do |pattern|
|
35
|
-
`find . -name "#{pattern}" -delete`
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
desc "Cleans the dir and builds the gem"
|
40
|
-
task :prep => [:clean, :gemspec, :build]
|
data/TODO
DELETED
data/VERSION.yml
DELETED
data/cucumber.yml
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
default: features
|
data/examples/Gemfile
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
examples/../Gemfile
|
data/examples/Gemfile.lock
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
examples/../Gemfile.lock
|
@@ -1,8 +0,0 @@
|
|
1
|
-
#This is an example of what database.yml *should* look like (when I wrote it)
|
2
|
-
#The real database.yml is generated automatically by the active record model lib (so it can be correct)
|
3
|
-
two:
|
4
|
-
adapter: sqlite3
|
5
|
-
database: /path/to/examples/features/support/../../db/activerecord_two.db
|
6
|
-
one:
|
7
|
-
adapter: sqlite3
|
8
|
-
database: /path/to/examples/features/support/../../db/activerecord_one.db
|
data/examples/config/redis.yml
DELETED
File without changes
|
@@ -1,11 +0,0 @@
|
|
1
|
-
Feature: example
|
2
|
-
In order to test DataBase Cleaner
|
3
|
-
Here are some scenarios that rely on the DB being clean!
|
4
|
-
|
5
|
-
Scenario: dirty the db
|
6
|
-
When I create a widget
|
7
|
-
Then I should see 1 widget
|
8
|
-
|
9
|
-
Scenario: assume a clean db
|
10
|
-
When I create a widget
|
11
|
-
Then I should see 1 widget
|
@@ -1,23 +0,0 @@
|
|
1
|
-
Feature: example
|
2
|
-
In order to test DataBase Cleaner
|
3
|
-
Here are some scenarios that rely on the DB being clean!
|
4
|
-
|
5
|
-
# Background:
|
6
|
-
# Given I have setup DatabaseCleaner to clean multiple databases
|
7
|
-
#
|
8
|
-
Scenario: dirty the db
|
9
|
-
When I create a widget in one db
|
10
|
-
And I create a widget in another db
|
11
|
-
Then I should see 1 widget in one db
|
12
|
-
And I should see 1 widget in another db
|
13
|
-
|
14
|
-
Scenario: assume a clean db
|
15
|
-
When I create a widget in one db
|
16
|
-
Then I should see 1 widget in one db
|
17
|
-
And I should see 0 widget in another db
|
18
|
-
|
19
|
-
Scenario: assume a clean db
|
20
|
-
When I create a widget in another db
|
21
|
-
Then I should see 0 widget in one db
|
22
|
-
And I should see 1 widget in another db
|
23
|
-
|
@@ -1,22 +0,0 @@
|
|
1
|
-
Feature: example
|
2
|
-
In order to test DataBase Cleaner
|
3
|
-
Here are some scenarios that rely on the DB being clean!
|
4
|
-
|
5
|
-
# Background:
|
6
|
-
# Given I have setup DatabaseCleaner to clean multiple orms
|
7
|
-
|
8
|
-
Scenario: dirty the db
|
9
|
-
When I create a widget in one orm
|
10
|
-
And I create a widget in another orm
|
11
|
-
Then I should see 1 widget in one orm
|
12
|
-
And I should see 1 widget in another orm
|
13
|
-
|
14
|
-
Scenario: assume a clean db
|
15
|
-
When I create a widget in one orm
|
16
|
-
Then I should see 1 widget in one orm
|
17
|
-
And I should see 0 widget in another orm
|
18
|
-
|
19
|
-
Scenario: assume a clean db
|
20
|
-
When I create a widget in another orm
|
21
|
-
Then I should see 0 widget in one orm
|
22
|
-
And I should see 1 widget in another orm
|