database_cleaner 1.6.2 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile.lock +3 -3
  3. data/History.rdoc +23 -0
  4. data/README.markdown +24 -1
  5. data/VERSION.yml +2 -2
  6. data/lib/database_cleaner.rb +6 -2
  7. data/lib/database_cleaner/active_record/base.rb +1 -1
  8. data/lib/database_cleaner/active_record/deletion.rb +9 -7
  9. data/lib/database_cleaner/base.rb +2 -0
  10. data/lib/database_cleaner/safeguard.rb +72 -0
  11. metadata +6 -86
  12. data/examples/Gemfile +0 -1
  13. data/examples/Gemfile.lock +0 -1
  14. data/examples/config/database.yml.example +0 -8
  15. data/examples/config/redis.yml +0 -8
  16. data/examples/db/sqlite_databases_go_here +0 -0
  17. data/examples/features/example.feature +0 -11
  18. data/examples/features/example_multiple_db.feature +0 -23
  19. data/examples/features/example_multiple_orm.feature +0 -22
  20. data/examples/features/step_definitions/activerecord_steps.rb +0 -31
  21. data/examples/features/step_definitions/couchpotato_steps.rb +0 -31
  22. data/examples/features/step_definitions/datamapper_steps.rb +0 -37
  23. data/examples/features/step_definitions/mongoid_steps.rb +0 -23
  24. data/examples/features/step_definitions/mongomapper_steps.rb +0 -31
  25. data/examples/features/step_definitions/neo4j_steps.rb +0 -23
  26. data/examples/features/step_definitions/ohm_steps.rb +0 -31
  27. data/examples/features/step_definitions/redis_steps.rb +0 -31
  28. data/examples/features/step_definitions/translation_steps.rb +0 -55
  29. data/examples/features/support/env.rb +0 -61
  30. data/examples/lib/activerecord_models.rb +0 -41
  31. data/examples/lib/couchpotato_models.rb +0 -61
  32. data/examples/lib/datamapper_models.rb +0 -50
  33. data/examples/lib/mongoid_models.rb +0 -44
  34. data/examples/lib/mongomapper_models.rb +0 -51
  35. data/examples/lib/neo4j_models.rb +0 -17
  36. data/examples/lib/ohm_models.rb +0 -43
  37. data/examples/lib/redis_models.rb +0 -65
  38. data/examples/lib/sequel_models.rb +0 -9
  39. data/features/cleaning.feature +0 -30
  40. data/features/cleaning_default_strategy.feature +0 -23
  41. data/features/cleaning_multiple_dbs.feature +0 -22
  42. data/features/cleaning_multiple_orms.feature +0 -67
  43. data/features/step_definitions/database_cleaner_steps.rb +0 -33
  44. data/features/support/env.rb +0 -4
  45. data/features/support/feature_runner.rb +0 -39
  46. data/spec/database_cleaner/active_record/base_spec.rb +0 -188
  47. data/spec/database_cleaner/active_record/transaction_spec.rb +0 -176
  48. data/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +0 -38
  49. data/spec/database_cleaner/active_record/truncation/mysql_spec.rb +0 -38
  50. data/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +0 -75
  51. data/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb +0 -40
  52. data/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +0 -40
  53. data/spec/database_cleaner/active_record/truncation_spec.rb +0 -180
  54. data/spec/database_cleaner/base_spec.rb +0 -617
  55. data/spec/database_cleaner/configuration_spec.rb +0 -345
  56. data/spec/database_cleaner/couch_potato/truncation_spec.rb +0 -41
  57. data/spec/database_cleaner/data_mapper/base_spec.rb +0 -30
  58. data/spec/database_cleaner/data_mapper/transaction_spec.rb +0 -23
  59. data/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +0 -41
  60. data/spec/database_cleaner/data_mapper/truncation_spec.rb +0 -11
  61. data/spec/database_cleaner/generic/base_spec.rb +0 -61
  62. data/spec/database_cleaner/generic/truncation_spec.rb +0 -118
  63. data/spec/database_cleaner/mongo/mongo_examples.rb +0 -26
  64. data/spec/database_cleaner/mongo/truncation_spec.rb +0 -72
  65. data/spec/database_cleaner/mongo_mapper/base_spec.rb +0 -33
  66. data/spec/database_cleaner/mongo_mapper/mongo_examples.rb +0 -8
  67. data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +0 -74
  68. data/spec/database_cleaner/moped/moped_examples.rb +0 -32
  69. data/spec/database_cleaner/moped/truncation_spec.rb +0 -80
  70. data/spec/database_cleaner/neo4j/base_spec.rb +0 -43
  71. data/spec/database_cleaner/neo4j/transaction_spec.rb +0 -25
  72. data/spec/database_cleaner/null_strategy_spec.rb +0 -28
  73. data/spec/database_cleaner/ohm/truncation_spec.rb +0 -70
  74. data/spec/database_cleaner/redis/base_spec.rb +0 -43
  75. data/spec/database_cleaner/redis/truncation_spec.rb +0 -63
  76. data/spec/database_cleaner/sequel/base_spec.rb +0 -32
  77. data/spec/database_cleaner/sequel/deletion_spec.rb +0 -58
  78. data/spec/database_cleaner/sequel/transaction_spec.rb +0 -21
  79. data/spec/database_cleaner/sequel/truncation/sqlite3_spec.rb +0 -0
  80. data/spec/database_cleaner/sequel/truncation_spec.rb +0 -182
  81. data/spec/database_cleaner/shared_strategy.rb +0 -15
  82. data/spec/rcov.opts +0 -1
  83. data/spec/spec_helper.rb +0 -21
  84. data/spec/support/active_record/database_setup.rb +0 -6
  85. data/spec/support/active_record/migrations/20150101010000_create_users.rb +0 -14
  86. data/spec/support/active_record/migrations/20150101020000_create_agents.rb +0 -14
  87. data/spec/support/active_record/mysql2_setup.rb +0 -39
  88. data/spec/support/active_record/mysql_setup.rb +0 -38
  89. data/spec/support/active_record/postgresql_setup.rb +0 -48
  90. data/spec/support/active_record/schema_setup.rb +0 -17
  91. data/spec/support/active_record/sqlite3_setup.rb +0 -40
  92. data/spec/support/data_mapper/schema_setup.rb +0 -15
  93. data/spec/support/data_mapper/sqlite3_setup.rb +0 -39
@@ -1,38 +0,0 @@
1
- require 'spec_helper'
2
- require 'active_record'
3
- require 'support/active_record/mysql_setup'
4
- require 'database_cleaner/active_record/truncation'
5
- require 'database_cleaner/active_record/truncation/shared_fast_truncation'
6
-
7
- module ActiveRecord
8
- module ConnectionAdapters
9
- describe do
10
- before(:all) { active_record_mysql_setup }
11
-
12
- let(:connection) { active_record_mysql_connection }
13
-
14
- describe "#truncate_table" do
15
- it "should truncate the table" do
16
- 2.times { User.create }
17
-
18
- connection.truncate_table('users')
19
- User.count.should eq 0
20
- end
21
-
22
- it "should reset AUTO_INCREMENT index of table" do
23
- 2.times { User.create }
24
- User.delete_all
25
-
26
- connection.truncate_table('users')
27
-
28
- User.create.id.should eq 1
29
- end
30
- end
31
-
32
- it_behaves_like "an adapter with pre-count truncation" do
33
- let(:connection) { active_record_mysql_connection }
34
- end
35
- end
36
- end
37
- end
38
-
@@ -1,75 +0,0 @@
1
- require 'spec_helper'
2
- require 'active_record'
3
- require 'support/active_record/postgresql_setup'
4
- require 'database_cleaner/active_record/truncation'
5
- require 'database_cleaner/active_record/truncation/shared_fast_truncation'
6
-
7
- module ActiveRecord
8
- module ConnectionAdapters
9
- describe "schema_migrations table" do
10
- it "is not truncated" do
11
- active_record_pg_migrate
12
- DatabaseCleaner::ActiveRecord::Truncation.new.clean
13
- result = active_record_pg_connection.execute("select count(*) from schema_migrations;")
14
- result.values.first.should eq ["2"]
15
- end
16
- end
17
-
18
- describe do
19
- before(:all) { active_record_pg_setup }
20
-
21
- let(:connection) do
22
- active_record_pg_connection
23
- end
24
-
25
- before(:each) do
26
- connection.truncate_tables connection.tables
27
- end
28
-
29
- describe "#truncate_table" do
30
- it "truncates the table" do
31
- 2.times { User.create }
32
-
33
- connection.truncate_table('users')
34
- User.count.should eq 0
35
- end
36
-
37
- it "truncates the table without id sequence" do
38
- 2.times { Agent.create }
39
-
40
- connection.truncate_table('agents')
41
- Agent.count.should eq 0
42
- end
43
-
44
- it "resets AUTO_INCREMENT index of table" do
45
- 2.times { User.create }
46
- User.delete_all
47
-
48
- connection.truncate_table('users')
49
-
50
- User.create.id.should eq 1
51
- end
52
- end
53
-
54
- describe ":except option cleanup" do
55
- it "should not truncate the tables specified in the :except option" do
56
- 2.times { User.create }
57
-
58
- ::DatabaseCleaner::ActiveRecord::Truncation.new(:except => ['users']).clean
59
- expect( User.count ).to eq 2
60
- end
61
- end
62
-
63
- describe '#database_cleaner_table_cache' do
64
- it 'should default to the list of tables with their schema' do
65
- connection.database_cleaner_table_cache.first.should match(/^public\./)
66
- end
67
- end
68
-
69
- it_behaves_like "an adapter with pre-count truncation" do
70
- let(:connection) { active_record_pg_connection }
71
- end
72
-
73
- end
74
- end
75
- end
@@ -1,40 +0,0 @@
1
- shared_examples_for "an adapter with pre-count truncation" do
2
- describe "#pre_count_truncate_tables" do
3
-
4
- context "with :reset_ids set true" do
5
- it "truncates the table" do
6
- 2.times { User.create }
7
-
8
- connection.pre_count_truncate_tables(%w[users], :reset_ids => true)
9
- User.count.should be_zero
10
- end
11
-
12
- it "resets AUTO_INCREMENT index of table" do
13
- 2.times { User.create }
14
- User.delete_all
15
-
16
- connection.pre_count_truncate_tables(%w[users]) # true is also the default
17
- User.create.id.should eq 1
18
- end
19
- end
20
-
21
-
22
- context "with :reset_ids set false" do
23
- it "truncates the table" do
24
- 2.times { User.create }
25
-
26
- connection.pre_count_truncate_tables(%w[users], :reset_ids => false)
27
- User.count.should be_zero
28
- end
29
-
30
- it "does not reset AUTO_INCREMENT index of table" do
31
- 2.times { User.create }
32
- User.delete_all
33
-
34
- connection.pre_count_truncate_tables(%w[users], :reset_ids => false)
35
-
36
- User.create.id.should eq 3
37
- end
38
- end
39
- end
40
- end
@@ -1,40 +0,0 @@
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(:connection) do
12
- active_record_sqlite3_connection
13
- end
14
-
15
- before(:each) do
16
- connection.truncate_tables connection.tables
17
- end
18
-
19
- describe "#truncate_table" do
20
- it "truncates the table" do
21
- 2.times { User.create }
22
-
23
- connection.truncate_table('users')
24
- User.count.should eq 0
25
- end
26
-
27
- it "resets AUTO_INCREMENT index of table" do
28
- 2.times { User.create }
29
- User.delete_all
30
-
31
- connection.truncate_table('users')
32
-
33
- User.create.id.should eq 1
34
- end
35
- end
36
-
37
- end
38
- end
39
- end
40
-
@@ -1,180 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
- require 'active_record'
3
- require 'active_record/connection_adapters/mysql_adapter'
4
- require 'active_record/connection_adapters/mysql2_adapter'
5
- require 'active_record/connection_adapters/sqlite3_adapter'
6
- require 'active_record/connection_adapters/postgresql_adapter'
7
-
8
- require 'database_cleaner/active_record/truncation'
9
-
10
- module ActiveRecord
11
- module ConnectionAdapters
12
- #JdbcAdapter IBM_DBAdapter
13
- [ MysqlAdapter, Mysql2Adapter, SQLite3Adapter, PostgreSQLAdapter ].each do |adapter|
14
- describe adapter, "#truncate_table" do
15
- it "responds" do
16
- adapter.instance_methods.should include(:truncate_table)
17
- end
18
- end
19
- end
20
- end
21
- end
22
-
23
- module DatabaseCleaner
24
- module ActiveRecord
25
-
26
- describe Truncation do
27
- let(:connection) { double('connection') }
28
-
29
- before(:each) do
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)
33
- end
34
-
35
- describe '#clean' do
36
- it "should truncate all tables except for schema_migrations" do
37
- connection.stub(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs])
38
-
39
- connection.should_receive(:truncate_tables).with(['widgets', 'dogs'])
40
- Truncation.new.clean
41
- end
42
-
43
- it "should use ActiveRecord's SchemaMigration.table_name" do
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')
47
-
48
- connection.should_receive(:truncate_tables).with(['widgets', 'dogs'])
49
-
50
- Truncation.new.clean
51
- end
52
-
53
- it "should only truncate the tables specified in the :only option when provided" do
54
- connection.stub(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs])
55
-
56
- connection.should_receive(:truncate_tables).with(['widgets'])
57
-
58
- Truncation.new(:only => ['widgets']).clean
59
- end
60
-
61
- it "should not truncate the tables specified in the :except option" do
62
- connection.stub(:database_cleaner_table_cache).and_return(%w[schema_migrations widgets dogs])
63
-
64
- connection.should_receive(:truncate_tables).with(['dogs'])
65
-
66
- Truncation.new(:except => ['widgets']).clean
67
- end
68
-
69
- it "should raise an error when :only and :except options are used" do
70
- running {
71
- Truncation.new(:except => ['widgets'], :only => ['widgets'])
72
- }.should raise_error(ArgumentError)
73
- end
74
-
75
- it "should raise an error when invalid options are provided" do
76
- running { Truncation.new(:foo => 'bar') }.should raise_error(ArgumentError)
77
- end
78
-
79
- it "should not truncate views" do
80
- connection.stub(:database_cleaner_table_cache).and_return(%w[widgets dogs])
81
- connection.stub(:database_cleaner_view_cache).and_return(["widgets"])
82
-
83
- connection.should_receive(:truncate_tables).with(['dogs'])
84
-
85
- Truncation.new.clean
86
- end
87
-
88
- describe "relying on #pre_count_truncate_tables if connection allows it" do
89
- subject { Truncation.new }
90
-
91
- it "should rely on #pre_count_truncate_tables if #pre_count? returns true" do
92
- connection.stub(:database_cleaner_table_cache).and_return(%w[widgets dogs])
93
- connection.stub(:database_cleaner_view_cache).and_return(["widgets"])
94
-
95
- subject.instance_variable_set(:"@pre_count", true)
96
-
97
- connection.should_not_receive(:truncate_tables).with(['dogs'])
98
- connection.should_receive(:pre_count_truncate_tables).with(['dogs'], :reset_ids => true)
99
-
100
- subject.clean
101
- end
102
-
103
- it "should not rely on #pre_count_truncate_tables if #pre_count? return false" do
104
- connection.stub(:database_cleaner_table_cache).and_return(%w[widgets dogs])
105
- connection.stub(:database_cleaner_view_cache).and_return(["widgets"])
106
-
107
- subject.instance_variable_set(:"@pre_count", false)
108
-
109
- connection.should_not_receive(:pre_count_truncate_tables).with(['dogs'], :reset_ids => true)
110
- connection.should_receive(:truncate_tables).with(['dogs'])
111
-
112
- subject.clean
113
- end
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
135
- end
136
-
137
- describe '#pre_count?' do
138
- before(:each) do
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)
142
- end
143
-
144
- subject { Truncation.new }
145
- its(:pre_count?) { should eq false }
146
-
147
- it 'should return true if @reset_id is set and non false or nil' do
148
- subject.instance_variable_set(:"@pre_count", true)
149
- subject.send(:pre_count?).should eq true
150
- end
151
-
152
- it 'should return false if @reset_id is set to false' do
153
- subject.instance_variable_set(:"@pre_count", false)
154
- subject.send(:pre_count?).should eq false
155
- end
156
- end
157
-
158
- describe '#reset_ids?' do
159
- before(:each) do
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)
163
- end
164
-
165
- subject { Truncation.new }
166
- its(:reset_ids?) { should eq true }
167
-
168
- it 'should return true if @reset_id is set and non false or nil' do
169
- subject.instance_variable_set(:"@reset_ids", 'Something')
170
- subject.send(:reset_ids?).should eq true
171
- end
172
-
173
- it 'should return false if @reset_id is set to false' do
174
- subject.instance_variable_set(:"@reset_ids", false)
175
- subject.send(:reset_ids?).should eq false
176
- end
177
- end
178
- end
179
- end
180
- end
@@ -1,617 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
- require 'database_cleaner/active_record/transaction'
3
- require 'database_cleaner/data_mapper/transaction'
4
- require 'database_cleaner/mongo_mapper/truncation'
5
- require 'database_cleaner/mongoid/truncation'
6
- require 'database_cleaner/couch_potato/truncation'
7
- require 'database_cleaner/neo4j/transaction'
8
-
9
- module DatabaseCleaner
10
- describe Base do
11
-
12
- let(:mock_strategy) {
13
- double("strategy").tap{|strategy|
14
- strategy.stub(:to_ary => [strategy])
15
- }
16
- }
17
-
18
- describe "autodetect" do
19
-
20
- #Cache all ORMs, we'll need them later but not now.
21
- before(:all) do
22
- Temp_AR = ::ActiveRecord if defined?(::ActiveRecord) and not defined?(Temp_AR)
23
- Temp_DM = ::DataMapper if defined?(::DataMapper) and not defined?(Temp_DM)
24
- Temp_MM = ::MongoMapper if defined?(::MongoMapper) and not defined?(Temp_MM)
25
- Temp_MO = ::Mongoid if defined?(::Mongoid) and not defined?(Temp_MO)
26
- Temp_CP = ::CouchPotato if defined?(::CouchPotato) and not defined?(Temp_CP)
27
- Temp_SQ = ::Sequel if defined?(::Sequel) and not defined?(Temp_SQ)
28
- Temp_MP = ::Moped if defined?(::Moped) and not defined?(Temp_MP)
29
- Temp_RS = ::Redis if defined?(::Redis) and not defined?(Temp_RS)
30
- Temp_OH = ::Ohm if defined?(::Ohm) and not defined?(Temp_OH)
31
- Temp_NJ = ::Neo4j if defined?(::Neo4j) and not defined?(Temp_NJ)
32
- end
33
-
34
- #Remove all ORM mocks and restore from cache
35
- after(:all) do
36
- Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
37
- Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper)
38
- Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper)
39
- Object.send(:remove_const, 'Mongoid') if defined?(::Mongoid)
40
- Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
41
- Object.send(:remove_const, 'Sequel') if defined?(::Sequel)
42
- Object.send(:remove_const, 'Moped') if defined?(::Moped)
43
- Object.send(:remove_const, 'Ohm') if defined?(::Ohm)
44
- Object.send(:remove_const, 'Redis') if defined?(::Redis)
45
- Object.send(:remove_const, 'Neo4j') if defined?(::Neo4j)
46
-
47
-
48
- # Restore ORMs
49
- ::ActiveRecord = Temp_AR if defined? Temp_AR
50
- ::DataMapper = Temp_DM if defined? Temp_DM
51
- ::MongoMapper = Temp_MM if defined? Temp_MM
52
- ::Mongoid = Temp_MO if defined? Temp_MO
53
- ::CouchPotato = Temp_CP if defined? Temp_CP
54
- ::Sequel = Temp_SQ if defined? Temp_SQ
55
- ::Moped = Temp_MP if defined? Temp_MP
56
- ::Ohm = Temp_OH if defined? Temp_OH
57
- ::Redis = Temp_RS if defined? Temp_RS
58
- ::Neo4j = Temp_NJ if defined? Temp_NJ
59
- end
60
-
61
- #reset the orm mocks
62
- before(:each) do
63
- Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
64
- Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper)
65
- Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper)
66
- Object.send(:remove_const, 'Mongoid') if defined?(::Mongoid)
67
- Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
68
- Object.send(:remove_const, 'Sequel') if defined?(::Sequel)
69
- Object.send(:remove_const, 'Moped') if defined?(::Moped)
70
- Object.send(:remove_const, 'Ohm') if defined?(::Ohm)
71
- Object.send(:remove_const, 'Redis') if defined?(::Redis)
72
- Object.send(:remove_const, 'Neo4j') if defined?(::Neo4j)
73
- end
74
-
75
- let(:cleaner) { DatabaseCleaner::Base.new :autodetect }
76
-
77
- it "should raise an error when no ORM is detected" do
78
- running { cleaner }.should raise_error(DatabaseCleaner::NoORMDetected)
79
- end
80
-
81
- it "should detect ActiveRecord first" do
82
- Object.const_set('ActiveRecord','Actively mocking records.')
83
- Object.const_set('DataMapper', 'Mapping data mocks')
84
- Object.const_set('MongoMapper', 'Mapping mock mongos')
85
- Object.const_set('Mongoid', 'Mongoid mock')
86
- Object.const_set('CouchPotato', 'Couching mock potatos')
87
- Object.const_set('Sequel', 'Sequel mock')
88
- Object.const_set('Moped', 'Moped mock')
89
- Object.const_set('Ohm', 'Ohm mock')
90
- Object.const_set('Redis', 'Redis mock')
91
- Object.const_set('Neo4j', 'Neo4j mock')
92
-
93
- cleaner.orm.should eq :active_record
94
- cleaner.should be_auto_detected
95
- end
96
-
97
- it "should detect DataMapper second" do
98
- Object.const_set('DataMapper', 'Mapping data mocks')
99
- Object.const_set('MongoMapper', 'Mapping mock mongos')
100
- Object.const_set('Mongoid', 'Mongoid mock')
101
- Object.const_set('CouchPotato', 'Couching mock potatos')
102
- Object.const_set('Sequel', 'Sequel mock')
103
- Object.const_set('Moped', 'Moped mock')
104
- Object.const_set('Ohm', 'Ohm mock')
105
- Object.const_set('Redis', 'Redis mock')
106
- Object.const_set('Neo4j', 'Neo4j mock')
107
-
108
- cleaner.orm.should eq :data_mapper
109
- cleaner.should be_auto_detected
110
- end
111
-
112
- it "should detect MongoMapper third" do
113
- Object.const_set('MongoMapper', 'Mapping mock mongos')
114
- Object.const_set('Mongoid', 'Mongoid mock')
115
- Object.const_set('CouchPotato', 'Couching mock potatos')
116
- Object.const_set('Sequel', 'Sequel mock')
117
- Object.const_set('Moped', 'Moped mock')
118
- Object.const_set('Ohm', 'Ohm mock')
119
- Object.const_set('Redis', 'Redis mock')
120
- Object.const_set('Neo4j', 'Neo4j mock')
121
-
122
- cleaner.orm.should eq :mongo_mapper
123
- cleaner.should be_auto_detected
124
- end
125
-
126
- it "should detect Mongoid fourth" do
127
- Object.const_set('Mongoid', 'Mongoid mock')
128
- Object.const_set('CouchPotato', 'Couching mock potatos')
129
- Object.const_set('Sequel', 'Sequel mock')
130
- Object.const_set('Moped', 'Moped mock')
131
- Object.const_set('Ohm', 'Ohm mock')
132
- Object.const_set('Redis', 'Redis mock')
133
- Object.const_set('Neo4j', 'Neo4j mock')
134
-
135
- cleaner.orm.should eq :mongoid
136
- cleaner.should be_auto_detected
137
- end
138
-
139
- it "should detect CouchPotato fifth" do
140
- Object.const_set('CouchPotato', 'Couching mock potatos')
141
- Object.const_set('Sequel', 'Sequel mock')
142
- Object.const_set('Moped', 'Moped mock')
143
- Object.const_set('Ohm', 'Ohm mock')
144
- Object.const_set('Redis', 'Redis mock')
145
- Object.const_set('Neo4j', 'Neo4j mock')
146
-
147
- cleaner.orm.should eq :couch_potato
148
- cleaner.should be_auto_detected
149
- end
150
-
151
- it "should detect Sequel sixth" do
152
- Object.const_set('Sequel', 'Sequel mock')
153
- Object.const_set('Moped', 'Moped mock')
154
- Object.const_set('Ohm', 'Ohm mock')
155
- Object.const_set('Redis', 'Redis mock')
156
- Object.const_set('Neo4j', 'Neo4j mock')
157
-
158
- cleaner.orm.should eq :sequel
159
- cleaner.should be_auto_detected
160
- end
161
-
162
- it 'detects Moped seventh' do
163
- Object.const_set('Moped', 'Moped mock')
164
-
165
- cleaner.orm.should eq :moped
166
- cleaner.should be_auto_detected
167
- end
168
-
169
- it 'detects Ohm eighth' do
170
- Object.const_set('Ohm', 'Ohm mock')
171
- Object.const_set('Redis', 'Redis mock')
172
- Object.const_set('Neo4j', 'Neo4j mock')
173
-
174
- cleaner.orm.should eq :ohm
175
- cleaner.should be_auto_detected
176
- end
177
-
178
- it 'detects Redis ninth' do
179
- Object.const_set('Redis', 'Redis mock')
180
- Object.const_set('Neo4j', 'Neo4j mock')
181
-
182
- cleaner.orm.should eq :redis
183
- cleaner.should be_auto_detected
184
- end
185
-
186
- it 'detects Neo4j tenth' do
187
- Object.const_set('Neo4j', 'Neo4j mock')
188
-
189
- cleaner.orm.should eq :neo4j
190
- cleaner.should be_auto_detected
191
- end
192
- end
193
-
194
- describe "orm_module" do
195
- it "should ask ::DatabaseCleaner what the module is for its orm" do
196
- orm = double("orm")
197
- mockule = double("module")
198
-
199
- cleaner = ::DatabaseCleaner::Base.new
200
- cleaner.should_receive(:orm).and_return(orm)
201
-
202
- ::DatabaseCleaner.should_receive(:orm_module).with(orm).and_return(mockule)
203
-
204
- cleaner.send(:orm_module).should eq mockule
205
- end
206
- end
207
-
208
- describe "comparison" do
209
- it "should be equal if orm, connection and strategy are the same" do
210
- one = DatabaseCleaner::Base.new(:active_record,:connection => :default)
211
- one.strategy = mock_strategy
212
-
213
- two = DatabaseCleaner::Base.new(:active_record,:connection => :default)
214
- two.strategy = mock_strategy
215
-
216
- one.should eq two
217
- two.should eq one
218
- end
219
-
220
- it "should not be equal if orm are not the same" do
221
- one = DatabaseCleaner::Base.new(:mongo_id, :connection => :default)
222
- one.strategy = mock_strategy
223
-
224
- two = DatabaseCleaner::Base.new(:active_record, :connection => :default)
225
- two.strategy = mock_strategy
226
-
227
- one.should_not eq two
228
- two.should_not eq one
229
- end
230
-
231
- it "should not be equal if connection are not the same" do
232
-
233
- one = DatabaseCleaner::Base.new(:active_record, :connection => :default)
234
- one.strategy = :truncation
235
-
236
- two = DatabaseCleaner::Base.new(:active_record, :connection => :other)
237
- two.strategy = :truncation
238
-
239
- one.should_not eq two
240
- two.should_not eq one
241
- end
242
- end
243
-
244
- describe "initialization" do
245
- context "db specified" do
246
- subject { ::DatabaseCleaner::Base.new(:active_record,:connection => :my_db) }
247
-
248
- it "should store db from :connection in params hash" do
249
- subject.db.should eq :my_db
250
- end
251
- end
252
-
253
- describe "orm" do
254
- it "should store orm" do
255
- cleaner = ::DatabaseCleaner::Base.new :a_orm
256
- cleaner.orm.should eq :a_orm
257
- end
258
-
259
- it "converts string to symbols" do
260
- cleaner = ::DatabaseCleaner::Base.new "mongoid"
261
- cleaner.orm.should eq :mongoid
262
- end
263
-
264
- it "is autodetected if orm is not provided" do
265
- cleaner = ::DatabaseCleaner::Base.new
266
- cleaner.should be_auto_detected
267
- end
268
-
269
- it "is autodetected if you specify :autodetect" do
270
- cleaner = ::DatabaseCleaner::Base.new "autodetect"
271
- cleaner.should be_auto_detected
272
- end
273
-
274
- it "should default to autodetect upon initalisation" do
275
- subject.should be_auto_detected
276
- end
277
- end
278
- end
279
-
280
- describe "db" do
281
- it "should default to :default" do
282
- subject.db.should eq :default
283
- end
284
-
285
- it "should return any stored db value" do
286
- subject.stub(:strategy_db=)
287
- subject.db = :test_db
288
- subject.db.should eq :test_db
289
- end
290
-
291
- it "should pass db to any specified strategy" do
292
- subject.should_receive(:strategy_db=).with(:a_new_db)
293
- subject.db = :a_new_db
294
- end
295
- end
296
-
297
- describe "strategy_db=" do
298
- let(:strategy) { mock_strategy }
299
-
300
- before(:each) do
301
- subject.strategy = strategy
302
- end
303
-
304
- it "should check that strategy supports db specification" do
305
- strategy.should_receive(:respond_to?).with(:db=).and_return(true)
306
- strategy.stub(:db=)
307
- subject.strategy_db = :a_db
308
- end
309
-
310
- context "when strategy supports db specification" do
311
- before(:each) { strategy.stub(:respond_to?).with(:db=).and_return true }
312
-
313
- it "should pass db to the strategy" do
314
- strategy.should_receive(:db=).with(:a_db)
315
- subject.strategy_db = :a_db
316
- end
317
- end
318
-
319
- context "when strategy doesn't supports db specification" do
320
- before(:each) { strategy.stub(:respond_to?).with(:db=).and_return false }
321
-
322
- it "should check to see if db is :default" do
323
- db = double("default")
324
- db.should_receive(:==).with(:default).and_return(true)
325
-
326
- subject.strategy_db = db
327
- end
328
-
329
- it "should raise an argument error when db isn't default" do
330
- db = double("a db")
331
- expect{ subject.strategy_db = db }.to raise_error ArgumentError
332
- end
333
- end
334
- end
335
-
336
- describe "clean_with" do
337
- let (:strategy) { double("strategy",:clean => true) }
338
-
339
- before(:each) { subject.stub(:create_strategy).with(anything).and_return(strategy) }
340
-
341
- it "should pass all arguments to create_strategy" do
342
- subject.should_receive(:create_strategy).with(:lorum, :dollar, :amet, :ipsum => "random").and_return(strategy)
343
- subject.clean_with :lorum, :dollar, :amet, { :ipsum => "random" }
344
- end
345
-
346
- it "should invoke clean on the created strategy" do
347
- strategy.should_receive(:clean)
348
- subject.clean_with :strategy
349
- end
350
-
351
- it "should return the strategy" do
352
- subject.clean_with( :strategy ).should eq strategy
353
- end
354
- end
355
-
356
- describe "clean_with!" do
357
- let (:strategy) { double("strategy",:clean => true) }
358
-
359
- before(:each) { subject.stub(:create_strategy).with(anything).and_return(strategy) }
360
-
361
- it "should pass all arguments to create_strategy" do
362
- subject.should_receive(:create_strategy).with(:lorum, :dollar, :amet, :ipsum => "random").and_return(strategy)
363
- subject.clean_with! :lorum, :dollar, :amet, { :ipsum => "random" }
364
- end
365
-
366
- it "should invoke clean on the created strategy" do
367
- strategy.should_receive(:clean)
368
- subject.clean_with! :strategy
369
- end
370
-
371
- it "should return the strategy" do
372
- subject.clean_with!( :strategy ).should eq strategy
373
- end
374
- end
375
-
376
- describe "create_strategy" do
377
- let(:strategy_class) { double("strategy_class",:new => double("instance")) }
378
-
379
- before :each do
380
- subject.stub(:orm_strategy).and_return(strategy_class)
381
- end
382
-
383
- it "should pass the first argument to orm_strategy" do
384
- subject.should_receive(:orm_strategy).with(:strategy).and_return(Object)
385
- subject.create_strategy :strategy
386
- end
387
- it "should pass the remainding argument to orm_strategy.new" do
388
- strategy_class.should_receive(:new).with(:params => {:lorum => "ipsum"})
389
-
390
- subject.create_strategy :strategy, {:params => {:lorum => "ipsum"}}
391
- end
392
- it "should return the resulting strategy" do
393
- subject.create_strategy( :strategy ).should eq strategy_class.new
394
- end
395
- end
396
-
397
- describe "strategy=" do
398
- it "should proxy symbolised strategies to create_strategy" do
399
- subject.should_receive(:create_strategy).with(:symbol)
400
- subject.strategy = :symbol
401
- end
402
-
403
- it "should proxy params with symbolised strategies" do
404
- subject.should_receive(:create_strategy).with(:symbol,:param => "one")
405
- subject.strategy= :symbol, {:param => "one"}
406
- end
407
-
408
- it "should accept strategy objects" do
409
- expect{ subject.strategy = mock_strategy }.to_not raise_error
410
- end
411
-
412
- it "should raise argument error when params given with strategy Object" do
413
- expect{ subject.strategy = double("object"), {:param => "one"} }.to raise_error ArgumentError
414
- end
415
-
416
- it "should attempt to set strategy db" do
417
- subject.stub(:db).and_return(:my_db)
418
- subject.should_receive(:set_strategy_db).with(mock_strategy, :my_db)
419
- subject.strategy = mock_strategy
420
- end
421
-
422
- it "should return the stored strategy" do
423
- result = subject.strategy = mock_strategy
424
- result.should eq mock_strategy
425
- end
426
- end
427
-
428
- describe "strategy" do
429
- subject { ::DatabaseCleaner::Base.new :a_orm }
430
-
431
- it "returns a null strategy when strategy is not set and undetectable" do
432
- subject.strategy.should eq DatabaseCleaner::NullStrategy
433
- end
434
-
435
- it "returns the set strategy" do
436
- subject.strategy = mock_strategy
437
- subject.strategy.should eq mock_strategy
438
- end
439
- end
440
-
441
- describe "orm=" do
442
- it "should stored the desired orm" do
443
- subject.orm.should_not eq :desired_orm
444
- subject.orm = :desired_orm
445
- subject.orm.should eq :desired_orm
446
- end
447
- end
448
-
449
- describe "orm" do
450
- let(:mock_orm) { double("orm") }
451
-
452
- it "should return orm if orm set" do
453
- subject.instance_variable_set "@orm", mock_orm
454
- subject.orm.should eq mock_orm
455
- end
456
-
457
- context "orm isn't set" do
458
- before(:each) { subject.instance_variable_set "@orm", nil }
459
-
460
- it "should run autodetect if orm isn't set" do
461
- subject.should_receive(:autodetect)
462
- subject.orm
463
- end
464
-
465
- it "should return the result of autodetect if orm isn't set" do
466
- subject.stub(:autodetect).and_return(mock_orm)
467
- subject.orm.should eq mock_orm
468
- end
469
- end
470
- end
471
-
472
- describe "proxy methods" do
473
- let (:strategy) { double("strategy") }
474
-
475
- before(:each) do
476
- subject.stub(:strategy).and_return(strategy)
477
- end
478
-
479
- describe "start" do
480
- it "should proxy start to the strategy" do
481
- strategy.should_receive(:start)
482
- subject.start
483
- end
484
- end
485
-
486
- describe "clean" do
487
- it "should proxy clean to the strategy" do
488
- strategy.should_receive(:clean)
489
- subject.clean
490
- end
491
- end
492
-
493
- describe "clean!" do
494
- it "should proxy clean! to the strategy clean" do
495
- strategy.should_receive(:clean)
496
- subject.clean!
497
- end
498
- end
499
-
500
- describe "cleaning" do
501
- it "should proxy cleaning to the strategy" do
502
- strategy.should_receive(:cleaning)
503
- subject.cleaning { }
504
- end
505
- end
506
- end
507
-
508
- describe "auto_detected?" do
509
- it "should return true unless @autodetected is nil" do
510
- subject.instance_variable_set("@autodetected","not nil")
511
- subject.auto_detected?.should be_true
512
- end
513
-
514
- it "should return false if @autodetect is nil" do
515
- subject.instance_variable_set("@autodetected",nil)
516
- subject.auto_detected?.should be_false
517
- end
518
- end
519
-
520
- describe "orm_strategy" do
521
- let (:strategy_class) { double("strategy_class") }
522
-
523
- before(:each) do
524
- subject.stub(:orm_module).and_return(strategy_class)
525
- end
526
-
527
- context "in response to a LoadError" do
528
- before(:each) { subject.should_receive(:require).with(anything).and_raise(LoadError) }
529
-
530
- it "should raise UnknownStrategySpecified" do
531
- expect { subject.send(:orm_strategy,:a_strategy) }.to raise_error UnknownStrategySpecified
532
- end
533
-
534
- it "should ask orm_module if it will list available_strategies" do
535
- strategy_class.should_receive(:respond_to?).with(:available_strategies)
536
-
537
- subject.stub(:orm_module).and_return(strategy_class)
538
-
539
- expect { subject.send(:orm_strategy,:a_strategy) }.to raise_error UnknownStrategySpecified
540
- end
541
-
542
- it "should use available_strategies (for the error message) if its available" do
543
- strategy_class.stub(:respond_to?).with(:available_strategies).and_return(true)
544
- strategy_class.should_receive(:available_strategies).and_return([])
545
-
546
- subject.stub(:orm_module).and_return(strategy_class)
547
-
548
- expect { subject.send(:orm_strategy,:a_strategy) }.to raise_error UnknownStrategySpecified
549
- end
550
- end
551
-
552
- it "should return the constant of the Strategy class requested" do
553
- strategy_strategy_class = double("strategy strategy_class")
554
-
555
- subject.stub(:require).with(anything).and_return(true)
556
-
557
- strategy_class.should_receive(:const_get).with("Cunningplan").and_return(strategy_strategy_class)
558
-
559
- subject.send(:orm_strategy, :cunningplan).should eq strategy_strategy_class
560
- end
561
-
562
- end
563
-
564
- describe 'set_default_orm_strategy' do
565
- it 'sets strategy to :transaction for ActiveRecord' do
566
- cleaner = DatabaseCleaner::Base.new(:active_record)
567
- cleaner.strategy.should be_instance_of DatabaseCleaner::ActiveRecord::Transaction
568
- end
569
-
570
- it 'sets strategy to :transaction for DataMapper' do
571
- cleaner = DatabaseCleaner::Base.new(:data_mapper)
572
- cleaner.strategy.should be_instance_of DatabaseCleaner::DataMapper::Transaction
573
- end
574
-
575
- it 'sets strategy to :truncation for MongoMapper' do
576
- cleaner = DatabaseCleaner::Base.new(:mongo_mapper)
577
- cleaner.strategy.should be_instance_of DatabaseCleaner::MongoMapper::Truncation
578
- end
579
-
580
- it 'sets strategy to :truncation for Mongoid' do
581
- cleaner = DatabaseCleaner::Base.new(:mongoid)
582
- cleaner.strategy.should be_instance_of DatabaseCleaner::Mongoid::Truncation
583
- end
584
-
585
- it 'sets strategy to :truncation for CouchPotato' do
586
- cleaner = DatabaseCleaner::Base.new(:couch_potato)
587
- cleaner.strategy.should be_instance_of DatabaseCleaner::CouchPotato::Truncation
588
- end
589
-
590
- it 'sets strategy to :transaction for Sequel' do
591
- cleaner = DatabaseCleaner::Base.new(:sequel)
592
- cleaner.strategy.should be_instance_of DatabaseCleaner::Sequel::Transaction
593
- end
594
-
595
- it 'sets strategy to :truncation for Moped' do
596
- cleaner = DatabaseCleaner::Base.new(:moped)
597
- cleaner.strategy.should be_instance_of DatabaseCleaner::Moped::Truncation
598
- end
599
-
600
- it 'sets strategy to :truncation for Ohm' do
601
- cleaner = DatabaseCleaner::Base.new(:ohm)
602
- cleaner.strategy.should be_instance_of DatabaseCleaner::Ohm::Truncation
603
- end
604
-
605
- it 'sets strategy to :truncation for Redis' do
606
- cleaner = DatabaseCleaner::Base.new(:redis)
607
- cleaner.strategy.should be_instance_of DatabaseCleaner::Redis::Truncation
608
- end
609
-
610
- it 'sets strategy to :transaction for Neo4j' do
611
- cleaner = DatabaseCleaner::Base.new(:neo4j)
612
- cleaner.strategy.should be_instance_of DatabaseCleaner::Neo4j::Transaction
613
- end
614
- end
615
-
616
- end
617
- end