database_cleaner 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +12 -12
- data/History.rdoc +17 -0
- data/README.markdown +4 -0
- data/Rakefile +1 -0
- data/VERSION.yml +2 -2
- data/examples/Gemfile.lock +12 -12
- data/lib/database_cleaner/active_record/base.rb +3 -3
- data/lib/database_cleaner/active_record/deletion.rb +19 -36
- data/lib/database_cleaner/active_record/truncation.rb +40 -67
- data/lib/database_cleaner/base.rb +3 -3
- data/lib/database_cleaner/configuration.rb +1 -1
- data/lib/database_cleaner/data_mapper/base.rb +1 -1
- data/lib/database_cleaner/data_mapper/truncation.rb +22 -2
- data/lib/database_cleaner/generic/truncation.rb +4 -3
- data/lib/database_cleaner/mongo_mapper/base.rb +1 -1
- data/lib/database_cleaner/moped/base.rb +2 -2
- data/lib/database_cleaner/redis/base.rb +1 -1
- data/spec/database_cleaner/active_record/base_spec.rb +10 -10
- data/spec/database_cleaner/active_record/transaction_spec.rb +1 -1
- data/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +2 -2
- data/spec/database_cleaner/active_record/truncation/mysql_spec.rb +2 -2
- data/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +2 -2
- data/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb +2 -2
- data/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +42 -0
- data/spec/database_cleaner/active_record/truncation_spec.rb +50 -29
- data/spec/database_cleaner/base_spec.rb +44 -48
- data/spec/database_cleaner/configuration_spec.rb +74 -74
- data/spec/database_cleaner/couch_potato/truncation_spec.rb +2 -2
- data/spec/database_cleaner/data_mapper/base_spec.rb +2 -2
- data/spec/database_cleaner/generic/base_spec.rb +1 -1
- data/spec/database_cleaner/generic/truncation_spec.rb +26 -16
- data/spec/database_cleaner/mongo_mapper/base_spec.rb +2 -2
- data/spec/database_cleaner/moped/truncation_spec.rb +1 -1
- data/spec/database_cleaner/ohm/truncation_spec.rb +8 -8
- data/spec/database_cleaner/redis/base_spec.rb +2 -2
- data/spec/database_cleaner/redis/truncation_spec.rb +8 -8
- data/spec/database_cleaner/sequel/base_spec.rb +2 -2
- data/spec/support/active_record/sqlite3_setup.rb +40 -0
- metadata +4 -2
@@ -53,7 +53,7 @@ module DataMapper
|
|
53
53
|
|
54
54
|
def truncate_table(table_name)
|
55
55
|
execute("DELETE FROM #{quote_name(table_name)};")
|
56
|
-
if uses_sequence
|
56
|
+
if uses_sequence?
|
57
57
|
execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
|
58
58
|
end
|
59
59
|
end
|
@@ -65,6 +65,16 @@ module DataMapper
|
|
65
65
|
yield
|
66
66
|
end
|
67
67
|
|
68
|
+
private
|
69
|
+
|
70
|
+
def uses_sequence?
|
71
|
+
sql = <<-SQL
|
72
|
+
SELECT name FROM sqlite_master
|
73
|
+
WHERE type='table' AND name='sqlite_sequence'
|
74
|
+
SQL
|
75
|
+
select(sql).first
|
76
|
+
end
|
77
|
+
|
68
78
|
end
|
69
79
|
|
70
80
|
class SqliteAdapter < DataObjectsAdapter
|
@@ -82,7 +92,7 @@ module DataMapper
|
|
82
92
|
|
83
93
|
def truncate_table(table_name)
|
84
94
|
execute("DELETE FROM #{quote_name(table_name)};")
|
85
|
-
if uses_sequence
|
95
|
+
if uses_sequence?
|
86
96
|
execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
|
87
97
|
end
|
88
98
|
end
|
@@ -94,6 +104,16 @@ module DataMapper
|
|
94
104
|
yield
|
95
105
|
end
|
96
106
|
|
107
|
+
private
|
108
|
+
|
109
|
+
def uses_sequence?
|
110
|
+
sql = <<-SQL
|
111
|
+
SELECT name FROM sqlite_master
|
112
|
+
WHERE type='table' AND name='sqlite_sequence'
|
113
|
+
SQL
|
114
|
+
select(sql).first
|
115
|
+
end
|
116
|
+
|
97
117
|
end
|
98
118
|
|
99
119
|
# FIXME
|
@@ -2,18 +2,19 @@ module DatabaseCleaner
|
|
2
2
|
module Generic
|
3
3
|
module Truncation
|
4
4
|
def initialize(opts={})
|
5
|
-
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :reset_ids]).empty?
|
6
|
-
raise ArgumentError, "The only valid options are :only, :except, :pre_count or :
|
5
|
+
if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :reset_ids, :cache_tables]).empty?
|
6
|
+
raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids or :cache_tables. You specified #{opts.keys.join(',')}."
|
7
7
|
end
|
8
8
|
if opts.has_key?(:only) && opts.has_key?(:except)
|
9
9
|
raise ArgumentError, "You may only specify either :only or :except. Doing both doesn't really make sense does it?"
|
10
10
|
end
|
11
11
|
|
12
12
|
@only = opts[:only]
|
13
|
-
@tables_to_exclude = (opts[:except] || []).dup
|
13
|
+
@tables_to_exclude = Array( (opts[:except] || []).dup ).flatten
|
14
14
|
@tables_to_exclude += migration_storage_names
|
15
15
|
@pre_count = opts[:pre_count]
|
16
16
|
@reset_ids = opts[:reset_ids]
|
17
|
+
@cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
|
17
18
|
end
|
18
19
|
|
19
20
|
def start
|
@@ -14,7 +14,7 @@ module DatabaseCleaner
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def db
|
17
|
-
@db
|
17
|
+
@db ||= :default
|
18
18
|
end
|
19
19
|
|
20
20
|
def host_port=(desired_host)
|
@@ -22,7 +22,7 @@ module DatabaseCleaner
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def host
|
25
|
-
@host
|
25
|
+
@host ||= '127.0.0.1:27017'
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
@@ -19,7 +19,7 @@ module DatabaseCleaner
|
|
19
19
|
it "should default to DatabaseCleaner.root / config / database.yml" do
|
20
20
|
ActiveRecord.config_file_location=nil
|
21
21
|
DatabaseCleaner.should_receive(:app_root).and_return("/path/to")
|
22
|
-
subject.should
|
22
|
+
subject.should eq '/path/to/config/database.yml'
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -45,11 +45,11 @@ module DatabaseCleaner
|
|
45
45
|
subject.stub(:load_config)
|
46
46
|
|
47
47
|
subject.db = :my_db
|
48
|
-
subject.db.should
|
48
|
+
subject.db.should eq :my_db
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should default to :default" do
|
52
|
-
subject.db.should
|
52
|
+
subject.db.should eq :default
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should load_config when I set db" do
|
@@ -86,7 +86,7 @@ my_db:
|
|
86
86
|
|
87
87
|
it "should store the relevant config in connection_hash" do
|
88
88
|
subject.load_config
|
89
|
-
subject.connection_hash.should
|
89
|
+
subject.connection_hash.should eq( "database" => "one" )
|
90
90
|
end
|
91
91
|
|
92
92
|
it "should skip config if config file is not available" do
|
@@ -115,14 +115,14 @@ my_db:
|
|
115
115
|
describe "connection_hash" do
|
116
116
|
it "should store connection_hash" do
|
117
117
|
subject.connection_hash = { :key => "value" }
|
118
|
-
subject.connection_hash.should
|
118
|
+
subject.connection_hash.should eq( :key => "value" )
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|
122
122
|
describe "connection_class" do
|
123
123
|
it { expect { subject.connection_class }.to_not raise_error }
|
124
124
|
it "should default to ActiveRecord::Base" do
|
125
|
-
subject.connection_class.should
|
125
|
+
subject.connection_class.should eq ::ActiveRecord::Base
|
126
126
|
end
|
127
127
|
|
128
128
|
context "with database models" do
|
@@ -131,21 +131,21 @@ my_db:
|
|
131
131
|
subject.db = FakeModel
|
132
132
|
subject.connection_hash = { }
|
133
133
|
subject.load_config
|
134
|
-
subject.connection_class.should
|
134
|
+
subject.connection_class.should eq FakeModel
|
135
135
|
end
|
136
136
|
end
|
137
137
|
|
138
138
|
context "connection_hash is not set" do
|
139
139
|
it "allows for database models to be passed in" do
|
140
140
|
subject.db = FakeModel
|
141
|
-
subject.connection_class.should
|
141
|
+
subject.connection_class.should eq FakeModel
|
142
142
|
end
|
143
143
|
end
|
144
144
|
end
|
145
145
|
|
146
146
|
context "when connection_hash is set" do
|
147
|
-
let(:hash) {
|
148
|
-
before { ::ActiveRecord::Base.stub
|
147
|
+
let(:hash) { double("hash") }
|
148
|
+
before { ::ActiveRecord::Base.stub(:respond_to?).and_return(false) }
|
149
149
|
before { subject.stub(:connection_hash).and_return(hash) }
|
150
150
|
|
151
151
|
it "establish a connection using ActiveRecord::Base" do
|
@@ -17,7 +17,7 @@ module ActiveRecord
|
|
17
17
|
2.times { User.create }
|
18
18
|
|
19
19
|
connection.truncate_table('users')
|
20
|
-
User.count.should
|
20
|
+
User.count.should eq 0
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should reset AUTO_INCREMENT index of table" do
|
@@ -26,7 +26,7 @@ module ActiveRecord
|
|
26
26
|
|
27
27
|
connection.truncate_table('users')
|
28
28
|
|
29
|
-
User.create.id.should
|
29
|
+
User.create.id.should eq 1
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -17,7 +17,7 @@ module ActiveRecord
|
|
17
17
|
2.times { User.create }
|
18
18
|
|
19
19
|
connection.truncate_table('users')
|
20
|
-
User.count.should
|
20
|
+
User.count.should eq 0
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should reset AUTO_INCREMENT index of table" do
|
@@ -26,7 +26,7 @@ module ActiveRecord
|
|
26
26
|
|
27
27
|
connection.truncate_table('users')
|
28
28
|
|
29
|
-
User.create.id.should
|
29
|
+
User.create.id.should eq 1
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -24,7 +24,7 @@ module ActiveRecord
|
|
24
24
|
2.times { User.create }
|
25
25
|
|
26
26
|
connection.truncate_table('users')
|
27
|
-
User.count.should
|
27
|
+
User.count.should eq 0
|
28
28
|
end
|
29
29
|
|
30
30
|
it "resets AUTO_INCREMENT index of table" do
|
@@ -33,7 +33,7 @@ module ActiveRecord
|
|
33
33
|
|
34
34
|
connection.truncate_table('users')
|
35
35
|
|
36
|
-
User.create.id.should
|
36
|
+
User.create.id.should eq 1
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -14,7 +14,7 @@ shared_examples_for "an adapter with pre-count truncation" do
|
|
14
14
|
User.delete_all
|
15
15
|
|
16
16
|
connection.pre_count_truncate_tables(%w[users]) # true is also the default
|
17
|
-
User.create.id.should
|
17
|
+
User.create.id.should eq 1
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -33,7 +33,7 @@ shared_examples_for "an adapter with pre-count truncation" do
|
|
33
33
|
|
34
34
|
connection.pre_count_truncate_tables(%w[users], :reset_ids => false)
|
35
35
|
|
36
|
-
User.create.id.should
|
36
|
+
User.create.id.should eq 3
|
37
37
|
end
|
38
38
|
end
|
39
39
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'active_record'
|
3
|
+
require 'support/active_record/sqlite3_setup'
|
4
|
+
require 'database_cleaner/active_record/truncation'
|
5
|
+
|
6
|
+
module ActiveRecord
|
7
|
+
module ConnectionAdapters
|
8
|
+
describe do
|
9
|
+
before(:all) { active_record_sqlite3_setup }
|
10
|
+
|
11
|
+
let(:adapter) { SQLite3Adapter }
|
12
|
+
|
13
|
+
let(:connection) do
|
14
|
+
active_record_sqlite3_connection
|
15
|
+
end
|
16
|
+
|
17
|
+
before(:each) do
|
18
|
+
connection.truncate_tables connection.tables
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#truncate_table" do
|
22
|
+
it "truncates the table" do
|
23
|
+
2.times { User.create }
|
24
|
+
|
25
|
+
connection.truncate_table('users')
|
26
|
+
User.count.should eq 0
|
27
|
+
end
|
28
|
+
|
29
|
+
it "resets AUTO_INCREMENT index of table" do
|
30
|
+
2.times { User.create }
|
31
|
+
User.delete_all
|
32
|
+
|
33
|
+
connection.truncate_table('users')
|
34
|
+
|
35
|
+
User.create.id.should eq 1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -9,7 +9,8 @@ require 'database_cleaner/active_record/truncation'
|
|
9
9
|
|
10
10
|
module ActiveRecord
|
11
11
|
module ConnectionAdapters
|
12
|
-
|
12
|
+
#JdbcAdapter IBM_DBAdapter
|
13
|
+
[ MysqlAdapter, Mysql2Adapter, SQLite3Adapter, PostgreSQLAdapter ].each do |adapter|
|
13
14
|
describe adapter, "#truncate_table" do
|
14
15
|
it "responds" do
|
15
16
|
adapter.instance_methods.should include(:truncate_table)
|
@@ -23,26 +24,26 @@ module DatabaseCleaner
|
|
23
24
|
module ActiveRecord
|
24
25
|
|
25
26
|
describe Truncation do
|
26
|
-
let(:connection) {
|
27
|
+
let(:connection) { double('connection') }
|
27
28
|
|
28
29
|
before(:each) do
|
29
|
-
connection.stub
|
30
|
-
connection.stub
|
31
|
-
::ActiveRecord::Base.stub
|
30
|
+
connection.stub(:disable_referential_integrity).and_yield
|
31
|
+
connection.stub(:database_cleaner_view_cache).and_return([])
|
32
|
+
::ActiveRecord::Base.stub(:connection).and_return(connection)
|
32
33
|
end
|
33
34
|
|
34
35
|
describe '#clean' do
|
35
36
|
it "should truncate all tables except for schema_migrations" do
|
36
|
-
connection.stub
|
37
|
+
connection.stub(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs])
|
37
38
|
|
38
39
|
connection.should_receive(:truncate_tables).with(['widgets', 'dogs'])
|
39
40
|
Truncation.new.clean
|
40
41
|
end
|
41
42
|
|
42
43
|
it "should use ActiveRecord's schema_migrations_table_name" do
|
43
|
-
connection.stub
|
44
|
-
::ActiveRecord::Base.stub
|
45
|
-
::ActiveRecord::Base.stub
|
44
|
+
connection.stub(:database_cleaner_table_cache).and_return(%w[pre_schema_migrations_suf widgets dogs])
|
45
|
+
::ActiveRecord::Base.stub(:table_name_prefix).and_return('pre_')
|
46
|
+
::ActiveRecord::Base.stub(:table_name_suffix).and_return('_suf')
|
46
47
|
|
47
48
|
connection.should_receive(:truncate_tables).with(['widgets', 'dogs'])
|
48
49
|
|
@@ -50,7 +51,7 @@ module DatabaseCleaner
|
|
50
51
|
end
|
51
52
|
|
52
53
|
it "should only truncate the tables specified in the :only option when provided" do
|
53
|
-
connection.stub
|
54
|
+
connection.stub(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs])
|
54
55
|
|
55
56
|
connection.should_receive(:truncate_tables).with(['widgets'])
|
56
57
|
|
@@ -58,7 +59,7 @@ module DatabaseCleaner
|
|
58
59
|
end
|
59
60
|
|
60
61
|
it "should not truncate the tables specified in the :except option" do
|
61
|
-
connection.stub
|
62
|
+
connection.stub(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs])
|
62
63
|
|
63
64
|
connection.should_receive(:truncate_tables).with(['dogs'])
|
64
65
|
|
@@ -76,8 +77,8 @@ module DatabaseCleaner
|
|
76
77
|
end
|
77
78
|
|
78
79
|
it "should not truncate views" do
|
79
|
-
connection.stub
|
80
|
-
connection.stub
|
80
|
+
connection.stub(:database_cleaner_table_cache).and_return(%w[widgets dogs])
|
81
|
+
connection.stub(:database_cleaner_view_cache).and_return(["widgets"])
|
81
82
|
|
82
83
|
connection.should_receive(:truncate_tables).with(['dogs'])
|
83
84
|
|
@@ -88,8 +89,8 @@ module DatabaseCleaner
|
|
88
89
|
subject { Truncation.new }
|
89
90
|
|
90
91
|
it "should rely on #pre_count_truncate_tables if #pre_count? returns true" do
|
91
|
-
connection.stub
|
92
|
-
connection.stub
|
92
|
+
connection.stub(:database_cleaner_table_cache).and_return(%w[widgets dogs])
|
93
|
+
connection.stub(:database_cleaner_view_cache).and_return(["widgets"])
|
93
94
|
|
94
95
|
subject.instance_variable_set(:"@pre_count", true)
|
95
96
|
|
@@ -100,8 +101,8 @@ module DatabaseCleaner
|
|
100
101
|
end
|
101
102
|
|
102
103
|
it "should not rely on #pre_count_truncate_tables if #pre_count? return false" do
|
103
|
-
connection.stub
|
104
|
-
connection.stub
|
104
|
+
connection.stub(:database_cleaner_table_cache).and_return(%w[widgets dogs])
|
105
|
+
connection.stub(:database_cleaner_view_cache).and_return(["widgets"])
|
105
106
|
|
106
107
|
subject.instance_variable_set(:"@pre_count", false)
|
107
108
|
|
@@ -111,47 +112,67 @@ module DatabaseCleaner
|
|
111
112
|
subject.clean
|
112
113
|
end
|
113
114
|
end
|
115
|
+
|
116
|
+
context 'when :cache_tables is set to true' do
|
117
|
+
it 'caches the list of tables to be truncated' do
|
118
|
+
connection.should_receive(:database_cleaner_table_cache).and_return([])
|
119
|
+
connection.should_not_receive(:tables)
|
120
|
+
|
121
|
+
connection.stub!(:truncate_tables)
|
122
|
+
Truncation.new({ :cache_tables => true }).clean
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'when :cache_tables is set to false' do
|
127
|
+
it 'does not cache the list of tables to be truncated' do
|
128
|
+
connection.should_not_receive(:database_cleaner_table_cache)
|
129
|
+
connection.should_receive(:tables).and_return([])
|
130
|
+
|
131
|
+
connection.stub!(:truncate_tables)
|
132
|
+
Truncation.new({ :cache_tables => false }).clean
|
133
|
+
end
|
134
|
+
end
|
114
135
|
end
|
115
136
|
|
116
137
|
describe '#pre_count?' do
|
117
138
|
before(:each) do
|
118
|
-
connection.stub
|
119
|
-
connection.stub
|
120
|
-
::ActiveRecord::Base.stub
|
139
|
+
connection.stub(:disable_referential_integrity).and_yield
|
140
|
+
connection.stub(:database_cleaner_view_cache).and_return([])
|
141
|
+
::ActiveRecord::Base.stub(:connection).and_return(connection)
|
121
142
|
end
|
122
143
|
|
123
144
|
subject { Truncation.new }
|
124
|
-
its(:pre_count?) { should
|
145
|
+
its(:pre_count?) { should eq false }
|
125
146
|
|
126
147
|
it 'should return true if @reset_id is set and non false or nil' do
|
127
148
|
subject.instance_variable_set(:"@pre_count", true)
|
128
|
-
subject.send(:pre_count?).should
|
149
|
+
subject.send(:pre_count?).should eq true
|
129
150
|
end
|
130
151
|
|
131
152
|
it 'should return false if @reset_id is set to false' do
|
132
153
|
subject.instance_variable_set(:"@pre_count", false)
|
133
|
-
subject.send(:pre_count?).should
|
154
|
+
subject.send(:pre_count?).should eq false
|
134
155
|
end
|
135
156
|
end
|
136
157
|
|
137
158
|
describe '#reset_ids?' do
|
138
159
|
before(:each) do
|
139
|
-
connection.stub
|
140
|
-
connection.stub
|
141
|
-
::ActiveRecord::Base.stub
|
160
|
+
connection.stub(:disable_referential_integrity).and_yield
|
161
|
+
connection.stub(:database_cleaner_view_cache).and_return([])
|
162
|
+
::ActiveRecord::Base.stub(:connection).and_return(connection)
|
142
163
|
end
|
143
164
|
|
144
165
|
subject { Truncation.new }
|
145
|
-
its(:reset_ids?) { should
|
166
|
+
its(:reset_ids?) { should eq true }
|
146
167
|
|
147
168
|
it 'should return true if @reset_id is set and non false or nil' do
|
148
169
|
subject.instance_variable_set(:"@reset_ids", 'Something')
|
149
|
-
subject.send(:reset_ids?).should
|
170
|
+
subject.send(:reset_ids?).should eq true
|
150
171
|
end
|
151
172
|
|
152
173
|
it 'should return false if @reset_id is set to false' do
|
153
174
|
subject.instance_variable_set(:"@reset_ids", false)
|
154
|
-
subject.send(:reset_ids?).should
|
175
|
+
subject.send(:reset_ids?).should eq false
|
155
176
|
end
|
156
177
|
end
|
157
178
|
end
|