bmabey-database_cleaner 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +100 -0
- data/Rakefile +46 -0
- data/VERSION.yml +4 -0
- data/cucumber.yml +1 -0
- data/examples/features/example.feature +11 -0
- data/examples/features/step_definitions/example_steps.rb +8 -0
- data/examples/features/support/env.rb +16 -0
- data/examples/lib/activerecord.rb +12 -0
- data/features/cleaning.feature +16 -0
- data/features/step_definitions/database_cleaner_steps.rb +25 -0
- data/features/support/env.rb +11 -0
- data/lib/database_cleaner.rb +3 -0
- data/lib/database_cleaner/active_record/transaction.rb +26 -0
- data/lib/database_cleaner/active_record/truncation.rb +63 -0
- data/lib/database_cleaner/configuration.rb +94 -0
- data/lib/database_cleaner/cucumber.rb +8 -0
- data/lib/database_cleaner/data_mapper/transaction.rb +6 -0
- data/spec/database_cleaner/active_record/truncation_spec.rb +66 -0
- data/spec/database_cleaner/configuration_spec.rb +101 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +12 -0
- metadata +85 -0
data/README.textile
ADDED
@@ -0,0 +1,100 @@
|
|
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
|
+
Right now the only ORM supported is ActiveRecord and it currently has two strategies:
|
9
|
+
truncation and transaction.
|
10
|
+
|
11
|
+
Support for DataMapper is built-in. All that needs to be written are the strategies. :)
|
12
|
+
|
13
|
+
h2. How to use
|
14
|
+
|
15
|
+
<pre>
|
16
|
+
require 'database_cleaner'
|
17
|
+
DatabaseCleaner.strategy = :truncation
|
18
|
+
|
19
|
+
# then, whenever you need to clean the DB
|
20
|
+
DatabaseCleaner.clean
|
21
|
+
</pre>
|
22
|
+
|
23
|
+
With the :truncation strategy you can also pass in options, for example:
|
24
|
+
<pre>
|
25
|
+
DatabaseCleaner.strategy = :truncation, {:only => %[widigets dogs some_other_table]}
|
26
|
+
</pre>
|
27
|
+
|
28
|
+
<pre>
|
29
|
+
DatabaseCleaner.strategy = :truncation, {:except => %[widigets]}
|
30
|
+
</pre>
|
31
|
+
|
32
|
+
(I should point out the truncation strategy will never truncate your schema_migrations table.)
|
33
|
+
|
34
|
+
|
35
|
+
Some strategies require that you call DatabaseCleaner.start before calling clean
|
36
|
+
(for example the :transaction one needs to know to open up a transaction). So
|
37
|
+
you would have:
|
38
|
+
|
39
|
+
<pre>
|
40
|
+
require 'database_cleaner'
|
41
|
+
DatabaseCleaner.strategy = :transaction
|
42
|
+
|
43
|
+
DatabaseCleaner.start # usually this is called in setup of a test
|
44
|
+
dirty_the_db
|
45
|
+
DatabaseCleaner.clean # cleanup of the test
|
46
|
+
</pre>
|
47
|
+
|
48
|
+
At times you may want to do a single clean with one strategy. For example, you may want
|
49
|
+
to start the process by truncating all the tables, but then use the faster transaction
|
50
|
+
strategy the remaining time. To accomplish this you can say:
|
51
|
+
|
52
|
+
<pre>
|
53
|
+
require 'database_cleaner'
|
54
|
+
DatabaseCleaner.clean_with :truncation
|
55
|
+
DatabaseCleaner.strategy = :transaction
|
56
|
+
# then make the DatabaseCleaner.start and DatabaseCleaner.clean calls appropriately
|
57
|
+
</pre>
|
58
|
+
|
59
|
+
For use in Cucumber please see the section below.
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
h2. Why?
|
64
|
+
|
65
|
+
One of my motivations for writing this library was to have an easy way to
|
66
|
+
turn on what Rails calls "transactional_fixtures" in my non-rails
|
67
|
+
ActiveRecord projects. For example, Cucumber ships with a Rails world that
|
68
|
+
will wrap each scenario in a transaction. This is great, but what if you are
|
69
|
+
using ActiveRecord in a non-rails project? You used to have to copy-and-paste
|
70
|
+
the needed code, but with DatabaseCleaner you can now say:
|
71
|
+
|
72
|
+
<pre>
|
73
|
+
#env.rb
|
74
|
+
require 'database_cleaner'
|
75
|
+
require 'database_cleaner/cucumber'
|
76
|
+
DatabaseCleaner.strategy = :transaction
|
77
|
+
</pre>
|
78
|
+
|
79
|
+
Now lets say you are running your features and it requires that another process be
|
80
|
+
involved (i.e. Selenium running against your app's server.) You can simply change
|
81
|
+
your strategy type:
|
82
|
+
|
83
|
+
<pre>
|
84
|
+
#env.rb
|
85
|
+
require 'database_cleaner'
|
86
|
+
require 'database_cleaner/cucumber'
|
87
|
+
DatabaseCleaner.strategy = :truncation
|
88
|
+
</pre>
|
89
|
+
|
90
|
+
You can have the best of both worlds and use the best one for the job:
|
91
|
+
<pre>
|
92
|
+
#env.rb
|
93
|
+
require 'database_cleaner'
|
94
|
+
require 'database_cleaner/cucumber'
|
95
|
+
DatabaseCleaner.strategy = (ENV['SELENIUM'] == 'true') ? :truncation : :transaction
|
96
|
+
</pre>
|
97
|
+
|
98
|
+
h2. COPYRIGHT
|
99
|
+
|
100
|
+
Copyright (c) 2009 Ben Mabey. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |s|
|
6
|
+
s.name = "database_cleaner"
|
7
|
+
s.summary = %Q{TODO}
|
8
|
+
s.email = "ben@benmabey.com"
|
9
|
+
s.homepage = "http://github.com/bmabey/database_cleaner"
|
10
|
+
s.description = "TODO"
|
11
|
+
s.files = FileList["[A-Z]*.*", "{examples,lib,features,spec}/**/*", "Rakefile", "cucumber.yml"]
|
12
|
+
s.authors = ["Ben Mabey"]
|
13
|
+
end
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'rake/rdoctask'
|
19
|
+
Rake::RDocTask.new do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'database_cleaner'
|
22
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
23
|
+
rdoc.rdoc_files.include('README*')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'spec/rake/spectask'
|
28
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
29
|
+
t.libs << 'lib' << 'spec'
|
30
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
31
|
+
end
|
32
|
+
|
33
|
+
Spec::Rake::SpecTask.new(:rcov) do |t|
|
34
|
+
t.libs << 'lib' << 'spec'
|
35
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
36
|
+
t.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
begin
|
40
|
+
require 'cucumber/rake/task'
|
41
|
+
Cucumber::Rake::Task.new(:features)
|
42
|
+
rescue LoadError
|
43
|
+
puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
|
44
|
+
end
|
45
|
+
|
46
|
+
task :default => :spec
|
data/VERSION.yml
ADDED
data/cucumber.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
default: features
|
@@ -0,0 +1,11 @@
|
|
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
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec/expectations'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require "#{File.dirname(__FILE__)}/../../lib/#{ENV['ORM']}"
|
6
|
+
rescue LoadError
|
7
|
+
raise "I don't have the setup for the '#{ENV['ORM']}' ORM!"
|
8
|
+
end
|
9
|
+
|
10
|
+
$:.unshift(File.dirname(__FILE__) + '/../../../lib')
|
11
|
+
require 'database_cleaner'
|
12
|
+
require 'database_cleaner/cucumber'
|
13
|
+
|
14
|
+
DatabaseCleaner.strategy = ENV['STRATEGY'].to_sym
|
15
|
+
|
16
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'activerecord'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
4
|
+
|
5
|
+
ActiveRecord::Schema.define(:version => 1) do
|
6
|
+
create_table :widgets do |t|
|
7
|
+
t.string :name
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Widget < ActiveRecord::Base
|
12
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
Feature: database cleaning
|
2
|
+
In order to ease example and feature writing
|
3
|
+
As a developer
|
4
|
+
I want to have my database in a clean state
|
5
|
+
|
6
|
+
Scenario Outline: ruby app
|
7
|
+
Given I am using <ORM>
|
8
|
+
And the <Strategy> cleaning strategy
|
9
|
+
|
10
|
+
When I run my scenarios that rely on a clean database
|
11
|
+
Then I should see all green
|
12
|
+
|
13
|
+
Examples:
|
14
|
+
| ORM | Strategy |
|
15
|
+
| ActiveRecord | transaction |
|
16
|
+
| ActiveRecord | truncation |
|
@@ -0,0 +1,25 @@
|
|
1
|
+
Given /^I am using (ActiveRecord|DataMapper)$/ do |orm|
|
2
|
+
@orm = orm
|
3
|
+
end
|
4
|
+
|
5
|
+
Given /^the (.+) cleaning strategy$/ do |strategy|
|
6
|
+
@strategy = strategy
|
7
|
+
end
|
8
|
+
|
9
|
+
When "I run my scenarios that rely on a clean database" do
|
10
|
+
full_dir ||= File.expand_path(File.dirname(__FILE__) + "/../../examples/")
|
11
|
+
Dir.chdir(full_dir) do
|
12
|
+
ENV['ORM'] = @orm.downcase
|
13
|
+
ENV['STRATEGY'] = @strategy
|
14
|
+
@out = `cucumber features`
|
15
|
+
@status = $?.exitstatus
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Then "I should see all green" do
|
20
|
+
unless @status == 0
|
21
|
+
raise "Expected to see all green but we saw FAIL! Output:\n#{@out}"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module DatabaseCleaner::ActiveRecord
|
2
|
+
class Transaction
|
3
|
+
|
4
|
+
def start
|
5
|
+
if ActiveRecord::Base.connection.respond_to?(:increment_open_transactions)
|
6
|
+
ActiveRecord::Base.connection.increment_open_transactions
|
7
|
+
else
|
8
|
+
ActiveRecord::Base.__send__(:increment_open_transactions)
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveRecord::Base.connection.begin_db_transaction
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def clean
|
16
|
+
ActiveRecord::Base.connection.rollback_db_transaction
|
17
|
+
|
18
|
+
if ActiveRecord::Base.connection.respond_to?(:decrement_open_transactions)
|
19
|
+
ActiveRecord::Base.connection.decrement_open_transactions
|
20
|
+
else
|
21
|
+
ActiveRecord::Base.__send__(:decrement_open_transactions)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
|
2
|
+
module ActiveRecord
|
3
|
+
module ConnectionAdapters
|
4
|
+
class MysqlAdapter
|
5
|
+
def truncate_table(table_name)
|
6
|
+
execute("TRUNCATE TABLE #{table_name};")
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class SQLite3Adapter
|
11
|
+
def truncate_table(table_name)
|
12
|
+
execute("DELETE FROM #{table_name};")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
module DatabaseCleaner::ActiveRecord
|
22
|
+
class Truncation
|
23
|
+
|
24
|
+
def initialize(options={})
|
25
|
+
if !options.empty? && !(options.keys - [:only, :except]).empty?
|
26
|
+
raise ArgumentError, "The only valid options are :only and :except. You specified #{options.keys.join(',')}."
|
27
|
+
end
|
28
|
+
if options.has_key?(:only) && options.has_key?(:except)
|
29
|
+
raise ArgumentError, "You may only specify either :only or :either. Doing both doesn't really make sense does it?"
|
30
|
+
end
|
31
|
+
|
32
|
+
@only = options[:only]
|
33
|
+
@tables_to_exclude = (options[:except] || []) << 'schema_migrations'
|
34
|
+
end
|
35
|
+
|
36
|
+
def start
|
37
|
+
# no-op
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
def clean
|
42
|
+
connection.disable_referential_integrity do
|
43
|
+
tables_to_truncate.each do |table_name|
|
44
|
+
connection.truncate_table table_name
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def tables_to_truncate
|
52
|
+
(@only || connection.tables) - @tables_to_exclude
|
53
|
+
end
|
54
|
+
|
55
|
+
def connection
|
56
|
+
::ActiveRecord::Base.connection
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module DatabaseCleaner
|
2
|
+
|
3
|
+
class NoStrategySetError < StandardError; end
|
4
|
+
class NoORMDetected < StandardError; end
|
5
|
+
class UnknownStrategySpecified < ArgumentError; end
|
6
|
+
|
7
|
+
module ActiveRecord
|
8
|
+
def self.available_strategies
|
9
|
+
%w[truncation transaction]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module DataMapper
|
14
|
+
def self.available_strategies
|
15
|
+
%w[]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class << self
|
20
|
+
|
21
|
+
def create_strategy(*args)
|
22
|
+
strategy, *strategy_args = args
|
23
|
+
orm_strategy(strategy).new(*strategy_args)
|
24
|
+
end
|
25
|
+
|
26
|
+
def clean_with(*args)
|
27
|
+
strategy = create_strategy(*args)
|
28
|
+
strategy.clean
|
29
|
+
strategy
|
30
|
+
end
|
31
|
+
|
32
|
+
def strategy=(args)
|
33
|
+
strategy, *strategy_args = args
|
34
|
+
if strategy.is_a?(Symbol)
|
35
|
+
@strategy = create_strategy(*args)
|
36
|
+
elsif strategy_args.empty?
|
37
|
+
@strategy = strategy
|
38
|
+
else
|
39
|
+
raise ArgumentError, "You must provide a strategy object, or a symbol for a know strategy along with initialization params."
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def orm=(orm_string)
|
44
|
+
@orm = orm_string
|
45
|
+
end
|
46
|
+
|
47
|
+
def start
|
48
|
+
strategy.start
|
49
|
+
end
|
50
|
+
|
51
|
+
def clean
|
52
|
+
strategy.clean
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def strategy
|
58
|
+
return @strategy if @strategy
|
59
|
+
raise NoStrategySetError, "Please set a strategy with DatabaseCleaner.strategy=."
|
60
|
+
end
|
61
|
+
|
62
|
+
def orm_strategy(strategy)
|
63
|
+
require "database_cleaner/#{orm}/#{strategy}"
|
64
|
+
orm_module.const_get(strategy.to_s.capitalize)
|
65
|
+
rescue LoadError => e
|
66
|
+
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
def orm
|
71
|
+
@orm ||=begin
|
72
|
+
if defined? ::ActiveRecord
|
73
|
+
'active_record'
|
74
|
+
elsif defined? ::DataMapper
|
75
|
+
'data_mapper'
|
76
|
+
else
|
77
|
+
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord or DataMapper loaded?"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
def orm_module
|
84
|
+
case orm
|
85
|
+
when 'active_record'
|
86
|
+
DatabaseCleaner::ActiveRecord
|
87
|
+
when 'data_mapper'
|
88
|
+
DatabaseCleaner::DataMapper
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
require 'database_cleaner/active_record/truncation'
|
3
|
+
require 'active_record'
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters
|
6
|
+
[MysqlAdapter, SQLite3Adapter].each do |adapter|
|
7
|
+
describe adapter, "#truncate_table" do
|
8
|
+
it "should truncate the table"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module DatabaseCleaner
|
15
|
+
module ActiveRecord
|
16
|
+
|
17
|
+
describe Truncation do
|
18
|
+
before(:each) do
|
19
|
+
@connection = mock('connection')
|
20
|
+
@connection.stub!(:disable_referential_integrity).and_yield
|
21
|
+
::ActiveRecord::Base.stub!(:connection).and_return(@connection)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should truncate all tables except for schema_migrations" do
|
25
|
+
@connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
|
26
|
+
|
27
|
+
@connection.should_receive(:truncate_table).with('widgets')
|
28
|
+
@connection.should_receive(:truncate_table).with('dogs')
|
29
|
+
@connection.should_not_receive(:truncate_table).with('schema_migrations')
|
30
|
+
|
31
|
+
Truncation.new.clean
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should only truncate the tables specified in the :only option when provided" do
|
35
|
+
@connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
|
36
|
+
|
37
|
+
@connection.should_receive(:truncate_table).with('widgets')
|
38
|
+
@connection.should_not_receive(:truncate_table).with('dogs')
|
39
|
+
|
40
|
+
Truncation.new(:only => ['widgets']).clean
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should not truncate the tables specified in the :except option" do
|
44
|
+
@connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
|
45
|
+
|
46
|
+
@connection.should_receive(:truncate_table).with('dogs')
|
47
|
+
@connection.should_not_receive(:truncate_table).with('widgets')
|
48
|
+
|
49
|
+
Truncation.new(:except => ['widgets']).clean
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should raise an error when :only and :except options are used" do
|
53
|
+
running {
|
54
|
+
Truncation.new(:except => ['widgets'], :only => ['widgets'])
|
55
|
+
}.should raise_error(ArgumentError)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should raise an error when invalid options are provided" do
|
59
|
+
running { Truncation.new(:foo => 'bar') }.should raise_error(ArgumentError)
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
require 'database_cleaner/active_record/transaction'
|
3
|
+
require 'database_cleaner/data_mapper/transaction'
|
4
|
+
|
5
|
+
describe DatabaseCleaner do
|
6
|
+
|
7
|
+
# These examples muck around with the constants for autodetection so we need to clean up....
|
8
|
+
before(:all) do
|
9
|
+
TempAR = ActiveRecord unless defined?(TempAR)
|
10
|
+
# need to add one for each ORM that we load in the spec helper...
|
11
|
+
end
|
12
|
+
after(:all) do
|
13
|
+
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord) #want to make sure we have the real one...
|
14
|
+
ActiveRecord = TempAR
|
15
|
+
end
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
DatabaseCleaner::ActiveRecord::Transaction.stub!(:new).and_return(@strategy = mock('strategy'))
|
19
|
+
Object.const_set('ActiveRecord', "just mocking out the constant here...") unless defined?(::ActiveRecord)
|
20
|
+
DatabaseCleaner.strategy = nil
|
21
|
+
DatabaseCleaner.orm = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
describe ".create_strategy" do
|
25
|
+
it "should initialize and return the appropirate strategy" do
|
26
|
+
DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
|
27
|
+
result = DatabaseCleaner.create_strategy(:transaction, {'options' => 'hash'})
|
28
|
+
|
29
|
+
result.should == @strategy
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe ".clean_with" do
|
34
|
+
it "should initialize the appropirate strategy and clean with it" do
|
35
|
+
DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
|
36
|
+
@strategy.should_receive(:clean)
|
37
|
+
|
38
|
+
DatabaseCleaner.clean_with(:transaction, 'options' => 'hash')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe ".strategy=" do
|
43
|
+
it "should initialize the appropirate strategy based on the ORM adapter detected" do
|
44
|
+
DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
|
45
|
+
DatabaseCleaner.strategy = :transaction, {'options' => 'hash'}
|
46
|
+
|
47
|
+
Object.send(:remove_const, 'ActiveRecord')
|
48
|
+
Object.const_set('DataMapper', "just mocking out the constant here...")
|
49
|
+
DatabaseCleaner.orm = nil
|
50
|
+
|
51
|
+
DatabaseCleaner::DataMapper::Transaction.should_receive(:new).with(no_args)
|
52
|
+
DatabaseCleaner.strategy = :transaction
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should raise an error when no ORM is detected" do
|
56
|
+
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
|
57
|
+
Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper)
|
58
|
+
|
59
|
+
running { DatabaseCleaner.strategy = :transaction }.should raise_error(DatabaseCleaner::NoORMDetected)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should use the strategy version of the ORM specified with #orm=" do
|
63
|
+
DatabaseCleaner.orm = 'data_mapper'
|
64
|
+
DatabaseCleaner::DataMapper::Transaction.should_receive(:new)
|
65
|
+
|
66
|
+
DatabaseCleaner.strategy = :transaction
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should raise an error when multiple args is passed in and the first is not a symbol" do
|
70
|
+
running { DatabaseCleaner.strategy=Object.new, {:foo => 'bar'} }.should raise_error(ArgumentError)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should raise an error when the specified strategy is not found" do
|
74
|
+
running { DatabaseCleaner.strategy = :foo }.should raise_error(DatabaseCleaner::UnknownStrategySpecified)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should allow any object to be set as the strategy" do
|
78
|
+
mock_strategy = mock('strategy')
|
79
|
+
running { DatabaseCleaner.strategy = mock_strategy }.should_not raise_error
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
%w[start clean].each do |strategy_method|
|
86
|
+
describe ".#{strategy_method}" do
|
87
|
+
it "should be delgated to the strategy set with strategy=" do
|
88
|
+
DatabaseCleaner.strategy = :transaction
|
89
|
+
|
90
|
+
@strategy.should_receive(strategy_method)
|
91
|
+
|
92
|
+
DatabaseCleaner.send(strategy_method)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should raise en error when no strategy has been set" do
|
96
|
+
running { DatabaseCleaner.send(strategy_method) }.should raise_error(DatabaseCleaner::NoStrategySetError)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bmabey-database_cleaner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Mabey
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-04 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: TODO
|
17
|
+
email: ben@benmabey.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.textile
|
26
|
+
- VERSION.yml
|
27
|
+
- examples/features
|
28
|
+
- examples/features/example.feature
|
29
|
+
- examples/features/step_definitions
|
30
|
+
- examples/features/step_definitions/example_steps.rb
|
31
|
+
- examples/features/support
|
32
|
+
- examples/features/support/env.rb
|
33
|
+
- examples/lib
|
34
|
+
- examples/lib/activerecord.rb
|
35
|
+
- lib/database_cleaner
|
36
|
+
- lib/database_cleaner/active_record
|
37
|
+
- lib/database_cleaner/active_record/transaction.rb
|
38
|
+
- lib/database_cleaner/active_record/truncation.rb
|
39
|
+
- lib/database_cleaner/configuration.rb
|
40
|
+
- lib/database_cleaner/cucumber.rb
|
41
|
+
- lib/database_cleaner/data_mapper
|
42
|
+
- lib/database_cleaner/data_mapper/transaction.rb
|
43
|
+
- lib/database_cleaner.rb
|
44
|
+
- features/cleaning.feature
|
45
|
+
- features/step_definitions
|
46
|
+
- features/step_definitions/database_cleaner_steps.rb
|
47
|
+
- features/support
|
48
|
+
- features/support/env.rb
|
49
|
+
- spec/database_cleaner
|
50
|
+
- spec/database_cleaner/active_record
|
51
|
+
- spec/database_cleaner/active_record/truncation_spec.rb
|
52
|
+
- spec/database_cleaner/configuration_spec.rb
|
53
|
+
- spec/spec.opts
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
- Rakefile
|
56
|
+
- cucumber.yml
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/bmabey/database_cleaner
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --inline-source
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.2.0
|
81
|
+
signing_key:
|
82
|
+
specification_version: 2
|
83
|
+
summary: TODO
|
84
|
+
test_files: []
|
85
|
+
|