database_cleaner-core 2.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
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 ADDED
@@ -0,0 +1,355 @@
1
+ **If you're viewing this at https://github.com/DatabaseCleaner/database_cleaner,
2
+ you're reading the documentation for the `master` branch.
3
+ [View documentation for the latest release
4
+ (1.8.2).](https://github.com/DatabaseCleaner/database_cleaner/blob/v1.8.2/README.markdown)**
5
+
6
+ # Database Cleaner
7
+
8
+ [![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner)
9
+ [![Code Climate](https://codeclimate.com/github/DatabaseCleaner/database_cleaner/badges/gpa.svg)](https://codeclimate.com/github/DatabaseCleaner/database_cleaner)
10
+ ![Gem Version](https://badge.fury.io/rb/database_cleaner.svg)
11
+ [![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=database_cleaner&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=database_cleaner&package-manager=bundler&version-scheme=semver)
12
+
13
+ Database Cleaner is a set of gems containing strategies for cleaning your database in Ruby.
14
+
15
+ The original use case was to ensure a clean state during tests.
16
+ 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.
17
+
18
+ ## Gem Setup
19
+
20
+ Instead of using the `database_cleaner` gem directly, each ORM has its own gem. Most projects will only need the `database_cleaner-active_record` gem:
21
+
22
+ ```ruby
23
+ # Gemfile
24
+ group :test do
25
+ gem 'database_cleaner-active_record'
26
+ end
27
+ ```
28
+
29
+ If you are using multiple ORMs, just load multiple gems:
30
+
31
+
32
+ ```ruby
33
+ # Gemfile
34
+ group :test do
35
+ gem 'database_cleaner-active_record'
36
+ gem 'database_cleaner-redis'
37
+ end
38
+ ```
39
+ ## List of adapters
40
+
41
+ Here is an overview of the databases and ORMs supported by each adapter:
42
+
43
+ MySQL, PostgreSQL, SQLite, etc
44
+ * [database_cleaner-active_record](https://github.com/DatabaseCleaner/database_cleaner-active_record)
45
+ * [database_cleaner-sequel](https://github.com/DatabaseCleaner/database_cleaner-sequel)
46
+
47
+ MongoDB
48
+ * [database_cleaner-mongo](https://github.com/DatabaseCleaner/database_cleaner-mongo)
49
+ * [database_cleaner-mongoid](https://github.com/DatabaseCleaner/database_cleaner-mongoid)
50
+
51
+ Redis
52
+ * [database_cleaner-redis](https://github.com/DatabaseCleaner/database_cleaner-redis)
53
+
54
+ Neo4j
55
+ * [database_cleaner-neo4j](https://github.com/DatabaseCleaner/database_cleaner-neo4j)
56
+
57
+ More details on available configuration options can be found in the README for the specific adapter gem that you're using.
58
+
59
+ For support or to discuss development please use the [Google Group](https://groups.google.com/group/database_cleaner).
60
+
61
+ ### Discontinued adapters
62
+
63
+ The following adapters have been discontinued, as their libraries appear to be unmaintained. Please let us know on the [Google Group](https://groups.google.com/group/database_cleaner) if you think one of these should be resurrected!
64
+
65
+ * [database_cleaner-data_mapper](https://github.com/DatabaseCleaner/database_cleaner-data_mapper)
66
+ * [database_cleaner-couch_potato](https://github.com/DatabaseCleaner/database_cleaner-couch_potato)
67
+ * [database_cleaner-mongo_mapper](https://github.com/DatabaseCleaner/database_cleaner-mongo_mapper)
68
+ * [database_cleaner-moped](https://github.com/DatabaseCleaner/database_cleaner-moped)
69
+
70
+ ## How to use
71
+
72
+ ```ruby
73
+ require 'database_cleaner/active_record'
74
+
75
+ DatabaseCleaner.strategy = :truncation
76
+
77
+ # then, whenever you need to clean the DB
78
+ DatabaseCleaner.clean
79
+ ```
80
+
81
+ With the `:truncation` strategy you can also pass in options, for example:
82
+
83
+ ```ruby
84
+ DatabaseCleaner.strategy = :truncation, {:only => %w[widgets dogs some_other_table]}
85
+ ```
86
+
87
+ ```ruby
88
+ DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
89
+ ```
90
+
91
+ (I should point out the truncation strategy will never truncate your schema_migrations table.)
92
+
93
+ 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:
94
+
95
+ ```ruby
96
+ require 'database_cleaner/active_record'
97
+
98
+ DatabaseCleaner.strategy = :transaction
99
+
100
+ DatabaseCleaner.start # usually this is called in setup of a test
101
+
102
+ dirty_the_db
103
+
104
+ DatabaseCleaner.clean # cleanup of the test
105
+
106
+ # OR
107
+
108
+ DatabaseCleaner.cleaning do
109
+ dirty_the_db
110
+ end
111
+ ```
112
+
113
+ At times you may want to do a single clean with one strategy.
114
+
115
+ 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:
116
+
117
+ ```ruby
118
+ require 'database_cleaner/active_record'
119
+
120
+ DatabaseCleaner.clean_with :truncation
121
+
122
+ DatabaseCleaner.strategy = :transaction
123
+
124
+ # then make the DatabaseCleaner.start and DatabaseCleaner.clean calls appropriately
125
+ ```
126
+
127
+ ## What strategy is fastest?
128
+
129
+ 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.
130
+
131
+ 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.
132
+
133
+ 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).
134
+
135
+ An easier, but slower, solution is to use the `:truncation` or `:deletion` strategy.
136
+
137
+ 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](https://stackoverflow.com/questions/11419536/postgresql-truncation-speed/11423886#11423886).
138
+
139
+ 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.
140
+
141
+ If you are using ActiveRecord then take a look at the [additional options](#additional-activerecord-options-for-truncation) available for `:truncation`.
142
+
143
+ Database Cleaner also includes a `null` strategy (that does no cleaning at all) which can be used with any ORM library.
144
+ You can also explicitly use it by setting your strategy to `nil`.
145
+
146
+ ## Test Framework Examples
147
+
148
+ ### RSpec Example
149
+
150
+ ```ruby
151
+ RSpec.configure do |config|
152
+
153
+ config.before(:suite) do
154
+ DatabaseCleaner.strategy = :transaction
155
+ DatabaseCleaner.clean_with(:truncation)
156
+ end
157
+
158
+ config.around(:each) do |example|
159
+ DatabaseCleaner.cleaning do
160
+ example.run
161
+ end
162
+ end
163
+
164
+ end
165
+ ```
166
+
167
+ ### RSpec with Capybara Example
168
+
169
+ You'll typically discover a feature spec is incorrectly using transaction
170
+ instead of truncation strategy when the data created in the spec is not
171
+ visible in the app-under-test.
172
+
173
+ A frequently occurring example of this is when, after creating a user in a
174
+ spec, the spec mysteriously fails to login with the user. This happens because
175
+ the user is created inside of an uncommitted transaction on one database
176
+ connection, while the login attempt is made using a separate database
177
+ connection. This separate database connection cannot access the
178
+ uncommitted user data created over the first database connection due to
179
+ transaction isolation.
180
+
181
+ For feature specs using a Capybara driver for an external
182
+ JavaScript-capable browser (in practice this is all drivers except
183
+ `:rack_test`), the Rack app under test and the specs do not share a
184
+ database connection.
185
+
186
+ When a spec and app-under-test do not share a database connection,
187
+ you'll likely need to use the truncation strategy instead of the
188
+ transaction strategy.
189
+
190
+ See the suggested config below to temporarily enable truncation strategy
191
+ for affected feature specs only. This config continues to use transaction
192
+ strategy for all other specs.
193
+
194
+ It's also recommended to use `append_after` to ensure `DatabaseCleaner.clean`
195
+ runs *after* the after-test cleanup `capybara/rspec` installs.
196
+
197
+ ```ruby
198
+ require 'capybara/rspec'
199
+
200
+ #...
201
+
202
+ RSpec.configure do |config|
203
+
204
+ config.use_transactional_fixtures = false
205
+
206
+ config.before(:suite) do
207
+ if config.use_transactional_fixtures?
208
+ raise(<<-MSG)
209
+ Delete line `config.use_transactional_fixtures = true` from rails_helper.rb
210
+ (or set it to false) to prevent uncommitted transactions being used in
211
+ JavaScript-dependent specs.
212
+
213
+ During testing, the app-under-test that the browser driver connects to
214
+ uses a different database connection to the database connection used by
215
+ the spec. The app's database connection would not be able to access
216
+ uncommitted transaction data setup over the spec's database connection.
217
+ MSG
218
+ end
219
+ DatabaseCleaner.clean_with(:truncation)
220
+ end
221
+
222
+ config.before(:each) do
223
+ DatabaseCleaner.strategy = :transaction
224
+ end
225
+
226
+ config.before(:each, type: :feature) do
227
+ # :rack_test driver's Rack app under test shares database connection
228
+ # with the specs, so continue to use transaction strategy for speed.
229
+ driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
230
+
231
+ unless driver_shares_db_connection_with_specs
232
+ # Driver is probably for an external browser with an app
233
+ # under test that does *not* share a database connection with the
234
+ # specs, so use truncation strategy.
235
+ DatabaseCleaner.strategy = :truncation
236
+ end
237
+ end
238
+
239
+ config.before(:each) do
240
+ DatabaseCleaner.start
241
+ end
242
+
243
+ config.append_after(:each) do
244
+ DatabaseCleaner.clean
245
+ end
246
+
247
+ end
248
+ ```
249
+
250
+
251
+ ### Minitest Example
252
+
253
+ ```ruby
254
+ DatabaseCleaner.strategy = :transaction
255
+
256
+ class Minitest::Spec
257
+ before :each do
258
+ DatabaseCleaner.start
259
+ end
260
+
261
+ after :each do
262
+ DatabaseCleaner.clean
263
+ end
264
+ end
265
+
266
+ # with the minitest-around gem, this may be used instead:
267
+ class Minitest::Spec
268
+ around do |tests|
269
+ DatabaseCleaner.cleaning(&tests)
270
+ end
271
+ end
272
+ ```
273
+
274
+ ### Cucumber Example
275
+
276
+ 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.
277
+
278
+ Otherwise, to add DatabaseCleaner to your project by hand, create a file `features/support/database_cleaner.rb` that looks like this:
279
+
280
+ ```ruby
281
+ require 'database_cleaner/active_record'
282
+
283
+ DatabaseCleaner.strategy = :truncation
284
+
285
+ Around do |scenario, block|
286
+ DatabaseCleaner.cleaning(&block)
287
+ end
288
+ ```
289
+
290
+ This should cover the basics of tear down between scenarios and keeping your database clean.
291
+
292
+ For more examples see the section ["Why?"](#why).
293
+
294
+ ## How to use with multiple ORMs
295
+
296
+ Sometimes you need to use multiple ORMs in your application.
297
+
298
+ You can use DatabaseCleaner to clean multiple ORMs, and multiple connections for those ORMs.
299
+
300
+ ```ruby
301
+ require 'database_cleaner/active_record'
302
+ require 'database_cleaner/mongo_mapper'
303
+
304
+ # How to specify particular orms
305
+ DatabaseCleaner[:active_record].strategy = :transaction
306
+ DatabaseCleaner[:mongo_mapper].strategy = :truncation
307
+
308
+ # How to specify particular connections
309
+ DatabaseCleaner[:active_record, { :connection => :two }]
310
+
311
+ # You may also pass in the model directly:
312
+ DatabaseCleaner[:active_record, { :model => ModelWithDifferentConnection }]
313
+ ```
314
+
315
+ Usage beyond that remains the same with `DatabaseCleaner.start` calling any setup on the different configured connections, and `DatabaseCleaner.clean` executing afterwards.
316
+
317
+ ## Why?
318
+
319
+ 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.
320
+
321
+ 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.
322
+
323
+ ## Safeguards
324
+
325
+ DatabaseCleaner comes with safeguards against:
326
+
327
+ * Running in production (checking for `ENV`, `RACK_ENV`, and `RAILS_ENV`)
328
+ * Running against a remote database (checking for a `DATABASE_URL` that does not include `localhost`, `.local` or `127.0.0.1`)
329
+
330
+ Both safeguards can be disabled separately as follows.
331
+
332
+ Using environment variables:
333
+
334
+ ```
335
+ export DATABASE_CLEANER_ALLOW_PRODUCTION=true
336
+ export DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true
337
+ ```
338
+
339
+ In Ruby:
340
+
341
+ ```ruby
342
+ DatabaseCleaner.allow_production = true
343
+ DatabaseCleaner.allow_remote_database_url = true
344
+ ```
345
+
346
+ In Ruby, a URL whitelist can be specified. When specified, DatabaseCleaner will only allow `DATABASE_URL` to be equal
347
+ to one of the values specified in the url whitelist like so:
348
+
349
+ ```ruby
350
+ DatabaseCleaner.url_whitelist = ['postgres://postgres@localhost', 'postgres://foo@bar']
351
+ ```
352
+
353
+ ## COPYRIGHT
354
+
355
+ See [LICENSE](LICENSE) for details.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # testing
2
+ require "rspec/core/rake_task"
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ task :default => :spec
5
+
6
+ # releasing
7
+ require "rake/clean"
8
+ CLOBBER.include "pkg"
9
+ require "bundler/gem_helper"
10
+ Bundler::GemHelper.install_tasks name: :database_cleaner
11
+ Bundler::GemHelper.install_tasks name: :"database_cleaner-core"
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle check || bundle install
7
+ cp spec/support/sample.config.yml spec/support/config.yml
data/cucumber.yml ADDED
@@ -0,0 +1 @@
1
+ default: features
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "database_cleaner/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "database_cleaner-core"
7
+ spec.version = DatabaseCleaner::VERSION
8
+ spec.authors = ["Ben Mabey", "Ernesto Tagwerker"]
9
+ spec.email = ["ernesto@ombulabs.com"]
10
+
11
+ spec.summary = "Strategies for cleaning databases. Can be used to ensure a clean slate for testing."
12
+ spec.description = "Strategies for cleaning databases. Can be used to ensure a clean slate for testing."
13
+ spec.homepage = "https://github.com/DatabaseCleaner/database_cleaner"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features|examples)/})
18
+ end - ["lib/database_cleaner.rb"] # should only exist in database_cleaner gem
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency 'guard-rspec'
26
+ spec.add_development_dependency "listen"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "cucumber"
29
+ end
@@ -0,0 +1,18 @@
1
+ require_relative "./lib/database_cleaner/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "database_cleaner"
5
+ spec.version = DatabaseCleaner::VERSION
6
+ spec.authors = ["Ben Mabey", "Ernesto Tagwerker", "Micah Geisel"]
7
+ spec.email = ["ernesto@ombulabs.com"]
8
+
9
+ spec.summary = "Strategies for cleaning databases. Can be used to ensure a clean slate for testing."
10
+ spec.description = "Strategies for cleaning databases. Can be used to ensure a clean slate for testing."
11
+ spec.homepage = "https://github.com/DatabaseCleaner/database_cleaner"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = ["lib/database_cleaner.rb"]
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_dependency "database_cleaner-active_record"
18
+ end