mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
commit
b254ce57bd
9 changed files with 92 additions and 23 deletions
|
@ -146,7 +146,7 @@ passed to [`keys`](http://redis.io/commands/keys)).
|
||||||
|
|
||||||
(I should point out the truncation strategy will never truncate your schema_migrations table.)
|
(I should point out the truncation strategy will never truncate your schema_migrations table.)
|
||||||
|
|
||||||
Some strategies require that you call `DatabaseCleaner.start` before calling `clean` (for example the `:transaction` one needs to know to open up a transaction). So you would have:
|
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 `Database.cleaning`. So you would have:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
require 'database_cleaner'
|
require 'database_cleaner'
|
||||||
|
@ -158,6 +158,12 @@ DatabaseCleaner.start # usually this is called in setup of a test
|
||||||
dirty_the_db
|
dirty_the_db
|
||||||
|
|
||||||
DatabaseCleaner.clean # cleanup of the test
|
DatabaseCleaner.clean # cleanup of the test
|
||||||
|
|
||||||
|
# OR
|
||||||
|
|
||||||
|
DatabaseCleaner.cleaning do
|
||||||
|
dirty_the_db
|
||||||
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
At times you may want to do a single clean with one strategy.
|
At times you may want to do a single clean with one strategy.
|
||||||
|
@ -196,12 +202,10 @@ RSpec.configure do |config|
|
||||||
DatabaseCleaner.clean_with(:truncation)
|
DatabaseCleaner.clean_with(:truncation)
|
||||||
end
|
end
|
||||||
|
|
||||||
config.before(:each) do
|
config.around(:each) do |example|
|
||||||
DatabaseCleaner.start
|
DatabaseCleaner.cleaning do
|
||||||
end
|
example.run
|
||||||
|
end
|
||||||
config.after(:each) do
|
|
||||||
DatabaseCleaner.clean
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -221,6 +225,13 @@ class MiniTest::Spec
|
||||||
DatabaseCleaner.clean
|
DatabaseCleaner.clean
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# with the minitest-around gem, this may be used instead:
|
||||||
|
class Minitest::Spec
|
||||||
|
around do |tests|
|
||||||
|
DatabaseCleaner.cleaning(&tests)
|
||||||
|
end
|
||||||
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
### Cucumber Example
|
### Cucumber Example
|
||||||
|
@ -239,12 +250,8 @@ rescue NameError
|
||||||
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
|
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
|
||||||
end
|
end
|
||||||
|
|
||||||
Before do
|
Around do |scenario, block|
|
||||||
DatabaseCleaner.start
|
DatabaseCleaner.cleaning(&block)
|
||||||
end
|
|
||||||
|
|
||||||
After do |scenario|
|
|
||||||
DatabaseCleaner.clean
|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -358,6 +365,9 @@ If you are using Postgres and have foreign key constraints, the truncation strat
|
||||||
client_min_messages = warning
|
client_min_messages = warning
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Nothing happens in JRuby with Sequel using transactions
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
## Debugging
|
## Debugging
|
||||||
|
|
||||||
|
|
|
@ -88,6 +88,10 @@ module DatabaseCleaner
|
||||||
|
|
||||||
alias clean! clean
|
alias clean! clean
|
||||||
|
|
||||||
|
def cleaning(&block)
|
||||||
|
strategy.cleaning(&block)
|
||||||
|
end
|
||||||
|
|
||||||
def auto_detected?
|
def auto_detected?
|
||||||
!!@autodetected
|
!!@autodetected
|
||||||
end
|
end
|
||||||
|
|
|
@ -81,6 +81,12 @@ module DatabaseCleaner
|
||||||
|
|
||||||
alias clean! clean
|
alias clean! clean
|
||||||
|
|
||||||
|
def cleaning(&inner_block)
|
||||||
|
connections.inject(inner_block) do |curr_block, connection|
|
||||||
|
proc { connection.cleaning(&curr_block) }
|
||||||
|
end.call
|
||||||
|
end
|
||||||
|
|
||||||
def clean_with(*args)
|
def clean_with(*args)
|
||||||
connections.each { |connection| connection.clean_with(*args) }
|
connections.each { |connection| connection.clean_with(*args) }
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,3 @@
|
||||||
Before do
|
Around do |scenario, block|
|
||||||
DatabaseCleaner.start
|
DatabaseCleaner.cleaning(&block)
|
||||||
end
|
|
||||||
|
|
||||||
After do
|
|
||||||
begin
|
|
||||||
DatabaseCleaner.clean
|
|
||||||
rescue Exception => e
|
|
||||||
DatabaseCleaner.logger.error "Exception encountered by DatabaseCleaner in Cucumber After block: #{e}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,6 +10,12 @@ module ::DatabaseCleaner
|
||||||
:default
|
:default
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cleaning(&block)
|
||||||
|
start
|
||||||
|
yield
|
||||||
|
clean
|
||||||
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
def available_strategies
|
def available_strategies
|
||||||
%W[]
|
%W[]
|
||||||
|
|
|
@ -4,7 +4,18 @@ module DatabaseCleaner
|
||||||
class Transaction
|
class Transaction
|
||||||
include ::DatabaseCleaner::Sequel::Base
|
include ::DatabaseCleaner::Sequel::Base
|
||||||
|
|
||||||
|
def self.check_fiber_brokenness
|
||||||
|
if !@checked_fiber_brokenness && Fiber.new { Thread.current }.resume != Thread.current
|
||||||
|
raise RuntimeError, "This ruby engine's Fibers are not compatible with Sequel's connection pool. " +
|
||||||
|
"To work around this, please use DatabaseCleaner.cleaning with a block instead of " +
|
||||||
|
"DatabaseCleaner.start and DatabaseCleaner.clean"
|
||||||
|
end
|
||||||
|
@checked_fiber_brokenness = true
|
||||||
|
end
|
||||||
|
|
||||||
def start
|
def start
|
||||||
|
self.class.check_fiber_brokenness
|
||||||
|
|
||||||
@fibers||= []
|
@fibers||= []
|
||||||
db= self.db
|
db= self.db
|
||||||
f= Fiber.new do
|
f= Fiber.new do
|
||||||
|
@ -20,6 +31,10 @@ module DatabaseCleaner
|
||||||
f= @fibers.pop
|
f= @fibers.pop
|
||||||
f.resume
|
f.resume
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cleaning
|
||||||
|
self.db.transaction(:rollback => :always, :savepoint => true) { yield }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -451,6 +451,13 @@ module DatabaseCleaner
|
||||||
subject.clean!
|
subject.clean!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "cleaning" do
|
||||||
|
it "should proxy cleaning to the strategy" do
|
||||||
|
strategy.should_receive(:cleaning)
|
||||||
|
subject.cleaning { }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "auto_detected?" do
|
describe "auto_detected?" do
|
||||||
|
|
|
@ -163,6 +163,11 @@ describe ::DatabaseCleaner do
|
||||||
::DatabaseCleaner.clean
|
::DatabaseCleaner.clean
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should proxy cleaning' do
|
||||||
|
connection.should_receive(:cleaning)
|
||||||
|
::DatabaseCleaner.cleaning { }
|
||||||
|
end
|
||||||
|
|
||||||
it "should proxy clean_with" do
|
it "should proxy clean_with" do
|
||||||
stratagem = double("stratgem")
|
stratagem = double("stratgem")
|
||||||
connection.should_receive(:clean_with).with(stratagem, {})
|
connection.should_receive(:clean_with).with(stratagem, {})
|
||||||
|
@ -203,6 +208,28 @@ describe ::DatabaseCleaner do
|
||||||
::DatabaseCleaner.clean
|
::DatabaseCleaner.clean
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should initiate cleaning on each connection, yield, and finish cleaning each connection" do
|
||||||
|
[active_record, data_mapper].each do |connection|
|
||||||
|
mc = class << connection; self; end
|
||||||
|
mc.send(:attr_reader, :started, :cleaned)
|
||||||
|
mc.send(:define_method, 'cleaning') do |&block|
|
||||||
|
@started = true
|
||||||
|
block.call
|
||||||
|
@cleaned = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
::DatabaseCleaner.cleaning do
|
||||||
|
active_record.started.should == true
|
||||||
|
data_mapper.started.should == true
|
||||||
|
active_record.cleaned.should == nil
|
||||||
|
data_mapper.cleaned.should == nil
|
||||||
|
@yielded = true
|
||||||
|
end
|
||||||
|
active_record.cleaned.should == true
|
||||||
|
data_mapper.cleaned.should == true
|
||||||
|
end
|
||||||
|
|
||||||
it "should proxy clean_with to all connections" do
|
it "should proxy clean_with to all connections" do
|
||||||
stratagem = double("stratgem")
|
stratagem = double("stratgem")
|
||||||
active_record.should_receive(:clean_with).with(stratagem)
|
active_record.should_receive(:clean_with).with(stratagem)
|
||||||
|
|
|
@ -5,9 +5,11 @@ end
|
||||||
shared_examples_for "a generic truncation strategy" do
|
shared_examples_for "a generic truncation strategy" do
|
||||||
it { should respond_to(:start) }
|
it { should respond_to(:start) }
|
||||||
it { should respond_to(:clean) }
|
it { should respond_to(:clean) }
|
||||||
|
it { should respond_to(:cleaning) }
|
||||||
end
|
end
|
||||||
|
|
||||||
shared_examples_for "a generic transaction strategy" do
|
shared_examples_for "a generic transaction strategy" do
|
||||||
it { should respond_to(:start) }
|
it { should respond_to(:start) }
|
||||||
it { should respond_to(:clean) }
|
it { should respond_to(:clean) }
|
||||||
|
it { should respond_to(:cleaning) }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue