database_cleaner 0.5.0 → 0.6.0.rc.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/Gemfile.lock +145 -0
- data/History.txt +43 -1
- data/README.textile +49 -1
- data/Rakefile +4 -0
- data/TODO +3 -0
- data/VERSION.yml +3 -2
- data/examples/Gemfile +46 -0
- data/examples/Gemfile.lock +145 -0
- data/examples/config/database.yml +7 -0
- data/examples/config/database.yml.example +8 -0
- data/examples/db/activerecord_one.db +0 -0
- data/examples/db/activerecord_two.db +0 -0
- data/examples/db/datamapper_default.db +0 -0
- data/examples/db/datamapper_one.db +0 -0
- data/examples/db/datamapper_two.db +0 -0
- data/examples/db/sqlite_databases_go_here +0 -0
- data/examples/features/example_multiple_db.feature +23 -0
- data/examples/features/example_multiple_orm.feature +22 -0
- data/examples/features/step_definitions/activerecord_steps.rb +31 -0
- data/examples/features/step_definitions/couchpotato_steps.rb +31 -0
- data/examples/features/step_definitions/datamapper_steps.rb +37 -0
- data/examples/features/step_definitions/mongoid_steps.rb +23 -0
- data/examples/features/step_definitions/mongomapper_steps.rb +31 -0
- data/examples/features/step_definitions/translation_steps.rb +55 -0
- data/examples/features/support/env.rb +49 -10
- data/examples/lib/activerecord_models.rb +34 -5
- data/examples/lib/couchpotato_models.rb +46 -6
- data/examples/lib/datamapper_models.rb +37 -3
- data/examples/lib/mongoid_models.rb +49 -0
- data/examples/lib/mongomapper_models.rb +36 -2
- data/features/cleaning.feature +1 -0
- data/features/cleaning_multiple_dbs.feature +20 -0
- data/features/cleaning_multiple_orms.feature +29 -0
- data/features/step_definitions/database_cleaner_steps.rb +21 -14
- data/features/support/feature_runner.rb +39 -0
- data/lib/database_cleaner/active_record/base.rb +46 -0
- data/lib/database_cleaner/active_record/transaction.rb +10 -10
- data/lib/database_cleaner/active_record/truncation.rb +42 -11
- data/lib/database_cleaner/base.rb +129 -0
- data/lib/database_cleaner/configuration.rb +45 -82
- data/lib/database_cleaner/couch_potato/base.rb +7 -0
- data/lib/database_cleaner/couch_potato/truncation.rb +4 -2
- data/lib/database_cleaner/cucumber.rb +0 -1
- data/lib/database_cleaner/data_mapper/base.rb +21 -0
- data/lib/database_cleaner/data_mapper/transaction.rb +10 -5
- data/lib/database_cleaner/data_mapper/truncation.rb +52 -19
- data/lib/database_cleaner/generic/base.rb +23 -0
- data/lib/database_cleaner/generic/truncation.rb +43 -0
- data/lib/database_cleaner/mongo_mapper/base.rb +20 -0
- data/lib/database_cleaner/mongo_mapper/truncation.rb +9 -3
- data/lib/database_cleaner/mongoid/base.rb +20 -0
- data/lib/database_cleaner/mongoid/truncation.rb +27 -0
- data/spec/database_cleaner/active_record/base_spec.rb +130 -0
- data/spec/database_cleaner/active_record/transaction_spec.rb +65 -0
- data/spec/database_cleaner/active_record/truncation_spec.rb +19 -18
- data/spec/database_cleaner/base_spec.rb +441 -0
- data/spec/database_cleaner/configuration_spec.rb +255 -65
- data/spec/database_cleaner/couch_potato/truncation_spec.rb +4 -3
- data/spec/database_cleaner/data_mapper/base_spec.rb +30 -0
- data/spec/database_cleaner/data_mapper/transaction_spec.rb +23 -0
- data/spec/database_cleaner/data_mapper/truncation_spec.rb +11 -0
- data/spec/database_cleaner/generic/base_spec.rb +22 -0
- data/spec/database_cleaner/generic/truncation_spec.rb +68 -0
- data/spec/database_cleaner/mongo_mapper/base_spec.rb +33 -0
- data/spec/database_cleaner/mongo_mapper/mongo_examples.rb +8 -0
- data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +12 -19
- data/spec/database_cleaner/mongoid/truncation_spec.rb +84 -0
- data/spec/database_cleaner/shared_strategy_spec.rb +13 -0
- data/spec/rcov.opts +1 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -3
- metadata +83 -8
- data/examples/features/step_definitions/example_steps.rb +0 -8
- data/lib/database_cleaner/truncation_base.rb +0 -41
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
require 'database_cleaner/
|
|
1
|
+
require 'database_cleaner/mongo_mapper/base'
|
|
2
|
+
require 'database_cleaner/generic/truncation'
|
|
2
3
|
|
|
3
4
|
module DatabaseCleaner
|
|
4
5
|
module MongoMapper
|
|
5
|
-
class Truncation
|
|
6
|
+
class Truncation
|
|
7
|
+
include ::DatabaseCleaner::MongoMapper::Base
|
|
8
|
+
include ::DatabaseCleaner::Generic::Truncation
|
|
9
|
+
|
|
6
10
|
def clean
|
|
7
11
|
if @only
|
|
8
12
|
collections.each { |c| c.remove if @only.include?(c.name) }
|
|
9
|
-
|
|
13
|
+
elsif @tables_to_exclude
|
|
10
14
|
collections.each { |c| c.remove unless @tables_to_exclude.include?(c.name) }
|
|
15
|
+
else
|
|
16
|
+
collections.each { |c| c.remove }
|
|
11
17
|
end
|
|
12
18
|
true
|
|
13
19
|
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require 'database_cleaner/generic/base'
|
|
2
|
+
module DatabaseCleaner
|
|
3
|
+
module Mongoid
|
|
4
|
+
def self.available_strategies
|
|
5
|
+
%w[truncation]
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
module Base
|
|
9
|
+
include ::DatabaseCleaner::Generic::Base
|
|
10
|
+
|
|
11
|
+
def db=(desired_db)
|
|
12
|
+
@db = desired_db
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def db
|
|
16
|
+
@db || :default
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'database_cleaner/mongoid/base'
|
|
2
|
+
require 'database_cleaner/generic/truncation'
|
|
3
|
+
|
|
4
|
+
module DatabaseCleaner
|
|
5
|
+
module Mongoid
|
|
6
|
+
class Truncation
|
|
7
|
+
include ::DatabaseCleaner::Mongoid::Base
|
|
8
|
+
include ::DatabaseCleaner::Generic::Truncation
|
|
9
|
+
|
|
10
|
+
def clean
|
|
11
|
+
if @only
|
|
12
|
+
collections.each { |c| c.remove if @only.include?(c.name) }
|
|
13
|
+
else
|
|
14
|
+
collections.each { |c| c.remove unless @tables_to_exclude.include?(c.name) }
|
|
15
|
+
end
|
|
16
|
+
true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def collections
|
|
22
|
+
::Mongoid.database.collections
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'active_record'
|
|
3
|
+
require 'database_cleaner/active_record/base'
|
|
4
|
+
require 'database_cleaner/shared_strategy_spec'
|
|
5
|
+
|
|
6
|
+
module DatabaseCleaner
|
|
7
|
+
describe ActiveRecord do
|
|
8
|
+
it { should respond_to(:available_strategies) }
|
|
9
|
+
|
|
10
|
+
describe "config_file_location" do
|
|
11
|
+
subject { ActiveRecord.config_file_location }
|
|
12
|
+
|
|
13
|
+
it "should default to DatabaseCleaner.root / config / database.yml" do
|
|
14
|
+
DatabaseCleaner.should_receive(:app_root).and_return("/path/to")
|
|
15
|
+
subject.should == '/path/to/config/database.yml'
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
module ActiveRecord
|
|
22
|
+
class ExampleStrategy
|
|
23
|
+
include ::DatabaseCleaner::ActiveRecord::Base
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe ExampleStrategy do
|
|
27
|
+
|
|
28
|
+
before { ::DatabaseCleaner::ActiveRecord.stub(:config_file_location).and_return('/path/to/config/database.yml') }
|
|
29
|
+
|
|
30
|
+
it_should_behave_like "a generic strategy"
|
|
31
|
+
|
|
32
|
+
describe "db" do
|
|
33
|
+
it { should respond_to(:db=) }
|
|
34
|
+
|
|
35
|
+
it "should store my desired db" do
|
|
36
|
+
subject.stub(:load_config)
|
|
37
|
+
|
|
38
|
+
subject.db = :my_db
|
|
39
|
+
subject.db.should == :my_db
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "should default to :default" do
|
|
43
|
+
subject.db.should == :default
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "should load_config when I set db" do
|
|
47
|
+
subject.should_receive(:load_config)
|
|
48
|
+
subject.db = :my_db
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
describe "load_config" do
|
|
53
|
+
|
|
54
|
+
it { should respond_to(:load_config) }
|
|
55
|
+
|
|
56
|
+
before do
|
|
57
|
+
yaml = <<-Y
|
|
58
|
+
my_db:
|
|
59
|
+
database: <%= "ONE".downcase %>
|
|
60
|
+
Y
|
|
61
|
+
IO.stub(:read).with('/path/to/config/database.yml').and_return(yaml)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "should parse the config" do
|
|
65
|
+
YAML.should_receive(:load).and_return( {:nil => nil} )
|
|
66
|
+
subject.load_config
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "should process erb in the config" do
|
|
70
|
+
transformed = <<-Y
|
|
71
|
+
my_db:
|
|
72
|
+
database: one
|
|
73
|
+
Y
|
|
74
|
+
YAML.should_receive(:load).with(transformed).and_return({ "my_db" => {"database" => "one"} })
|
|
75
|
+
subject.load_config
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "should store the relevant config in connection_hash" do
|
|
79
|
+
subject.should_receive(:db).and_return(:my_db)
|
|
80
|
+
subject.load_config
|
|
81
|
+
subject.connection_hash.should == {"database" => "one"}
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
describe "connection_hash" do
|
|
86
|
+
it { should respond_to(:connection_hash) }
|
|
87
|
+
it { should respond_to(:connection_hash=) }
|
|
88
|
+
it "should store connection_hash" do
|
|
89
|
+
subject.connection_hash = { :key => "value" }
|
|
90
|
+
subject.connection_hash.should == { :key => "value" }
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
describe "create_connection_klass" do
|
|
95
|
+
it "should return a class" do
|
|
96
|
+
subject.create_connection_klass.should be_a(Class)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "should return a class extending ::ActiveRecord::Base" do
|
|
100
|
+
subject.create_connection_klass.ancestors.should include(::ActiveRecord::Base)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
describe "connection_klass" do
|
|
105
|
+
it { expect{ subject.connection_klass }.to_not raise_error }
|
|
106
|
+
it "should default to ActiveRecord::Base" do
|
|
107
|
+
subject.connection_klass.should == ::ActiveRecord::Base
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
context "when connection_hash is set" do
|
|
111
|
+
let(:hash) { mock("hash") }
|
|
112
|
+
before { subject.stub(:connection_hash).and_return(hash) }
|
|
113
|
+
|
|
114
|
+
it "should create connection_klass if it doesnt exist if connection_hash is set" do
|
|
115
|
+
subject.should_receive(:create_connection_klass).and_return(mock('class').as_null_object)
|
|
116
|
+
subject.connection_klass
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it "should configure the class from create_connection_klass if connection_hash is set" do
|
|
120
|
+
klass = mock('klass')
|
|
121
|
+
klass.should_receive(:establish_connection).with(hash)
|
|
122
|
+
|
|
123
|
+
subject.should_receive(:create_connection_klass).and_return(klass)
|
|
124
|
+
subject.connection_klass
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
|
2
|
+
require 'database_cleaner/active_record/transaction'
|
|
3
|
+
require 'active_record'
|
|
4
|
+
|
|
5
|
+
module DatabaseCleaner
|
|
6
|
+
module ActiveRecord
|
|
7
|
+
|
|
8
|
+
describe Transaction do
|
|
9
|
+
let (:connection) { mock("connection") }
|
|
10
|
+
before(:each) do
|
|
11
|
+
::ActiveRecord::Base.stub!(:connection).and_return(connection)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe "#start" do
|
|
15
|
+
it "should increment open transactions if possible" do
|
|
16
|
+
connection.stub!(:respond_to?).with(:increment_open_transactions).and_return(true)
|
|
17
|
+
connection.stub!(:begin_db_transaction)
|
|
18
|
+
|
|
19
|
+
connection.should_receive(:increment_open_transactions)
|
|
20
|
+
Transaction.new.start
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do
|
|
24
|
+
connection.stub!(:respond_to?).with(:increment_open_transactions).and_return(false)
|
|
25
|
+
connection.stub!(:begin_db_transaction)
|
|
26
|
+
|
|
27
|
+
::ActiveRecord::Base.should_receive(:increment_open_transactions)
|
|
28
|
+
Transaction.new.start
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "should start a transaction" do
|
|
32
|
+
connection.stub!(:increment_open_transactions)
|
|
33
|
+
|
|
34
|
+
connection.should_receive(:begin_db_transaction)
|
|
35
|
+
Transaction.new.start
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
describe "#clean" do
|
|
40
|
+
it "should start a transaction" do
|
|
41
|
+
connection.stub!(:decrement_open_transactions)
|
|
42
|
+
|
|
43
|
+
connection.should_receive(:rollback_db_transaction)
|
|
44
|
+
Transaction.new.clean
|
|
45
|
+
end
|
|
46
|
+
it "should decrement open transactions if possible" do
|
|
47
|
+
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
|
|
48
|
+
connection.stub!(:rollback_db_transaction)
|
|
49
|
+
|
|
50
|
+
connection.should_receive(:decrement_open_transactions)
|
|
51
|
+
Transaction.new.clean
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "should decrement connection via ActiveRecord::Base if connection won't" do
|
|
55
|
+
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(false)
|
|
56
|
+
connection.stub!(:rollback_db_transaction)
|
|
57
|
+
|
|
58
|
+
::ActiveRecord::Base.should_receive(:decrement_open_transactions)
|
|
59
|
+
Transaction.new.clean
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
require File.dirname(__FILE__) + '/../../spec_helper'
|
|
2
|
-
require 'database_cleaner/active_record/truncation'
|
|
3
2
|
require 'active_record'
|
|
3
|
+
require 'database_cleaner/active_record/truncation'
|
|
4
|
+
|
|
5
|
+
|
|
4
6
|
module ActiveRecord
|
|
5
7
|
module ConnectionAdapters
|
|
6
|
-
[MysqlAdapter, SQLite3Adapter, JdbcAdapter, PostgreSQLAdapter].each do |adapter|
|
|
8
|
+
[MysqlAdapter, Mysql2Adapter, SQLite3Adapter, JdbcAdapter, PostgreSQLAdapter].each do |adapter|
|
|
7
9
|
describe adapter, "#truncate_table" do
|
|
8
10
|
it "should truncate the table"
|
|
9
11
|
end
|
|
@@ -15,36 +17,38 @@ module DatabaseCleaner
|
|
|
15
17
|
module ActiveRecord
|
|
16
18
|
|
|
17
19
|
describe Truncation do
|
|
20
|
+
let(:connection) { mock('connection') }
|
|
21
|
+
|
|
22
|
+
|
|
18
23
|
before(:each) do
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
::ActiveRecord::Base.stub!(:connection).and_return(@connection)
|
|
24
|
+
connection.stub!(:disable_referential_integrity).and_yield
|
|
25
|
+
::ActiveRecord::Base.stub!(:connection).and_return(connection)
|
|
22
26
|
end
|
|
23
27
|
|
|
24
28
|
it "should truncate all tables except for schema_migrations" do
|
|
25
|
-
|
|
29
|
+
connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
connection.should_receive(:truncate_table).with('widgets')
|
|
32
|
+
connection.should_receive(:truncate_table).with('dogs')
|
|
33
|
+
connection.should_not_receive(:truncate_table).with('schema_migrations')
|
|
30
34
|
|
|
31
35
|
Truncation.new.clean
|
|
32
36
|
end
|
|
33
37
|
|
|
34
38
|
it "should only truncate the tables specified in the :only option when provided" do
|
|
35
|
-
|
|
39
|
+
connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
|
|
36
40
|
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
connection.should_receive(:truncate_table).with('widgets')
|
|
42
|
+
connection.should_not_receive(:truncate_table).with('dogs')
|
|
39
43
|
|
|
40
44
|
Truncation.new(:only => ['widgets']).clean
|
|
41
45
|
end
|
|
42
46
|
|
|
43
47
|
it "should not truncate the tables specified in the :except option" do
|
|
44
|
-
|
|
48
|
+
connection.stub!(:tables).and_return(%w[schema_migrations widgets dogs])
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
connection.should_receive(:truncate_table).with('dogs')
|
|
51
|
+
connection.should_not_receive(:truncate_table).with('widgets')
|
|
48
52
|
|
|
49
53
|
Truncation.new(:except => ['widgets']).clean
|
|
50
54
|
end
|
|
@@ -58,9 +62,6 @@ module DatabaseCleaner
|
|
|
58
62
|
it "should raise an error when invalid options are provided" do
|
|
59
63
|
running { Truncation.new(:foo => 'bar') }.should raise_error(ArgumentError)
|
|
60
64
|
end
|
|
61
|
-
|
|
62
|
-
|
|
63
65
|
end
|
|
64
|
-
|
|
65
66
|
end
|
|
66
67
|
end
|