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.
Files changed (74) hide show
  1. data/Gemfile.lock +145 -0
  2. data/History.txt +43 -1
  3. data/README.textile +49 -1
  4. data/Rakefile +4 -0
  5. data/TODO +3 -0
  6. data/VERSION.yml +3 -2
  7. data/examples/Gemfile +46 -0
  8. data/examples/Gemfile.lock +145 -0
  9. data/examples/config/database.yml +7 -0
  10. data/examples/config/database.yml.example +8 -0
  11. data/examples/db/activerecord_one.db +0 -0
  12. data/examples/db/activerecord_two.db +0 -0
  13. data/examples/db/datamapper_default.db +0 -0
  14. data/examples/db/datamapper_one.db +0 -0
  15. data/examples/db/datamapper_two.db +0 -0
  16. data/examples/db/sqlite_databases_go_here +0 -0
  17. data/examples/features/example_multiple_db.feature +23 -0
  18. data/examples/features/example_multiple_orm.feature +22 -0
  19. data/examples/features/step_definitions/activerecord_steps.rb +31 -0
  20. data/examples/features/step_definitions/couchpotato_steps.rb +31 -0
  21. data/examples/features/step_definitions/datamapper_steps.rb +37 -0
  22. data/examples/features/step_definitions/mongoid_steps.rb +23 -0
  23. data/examples/features/step_definitions/mongomapper_steps.rb +31 -0
  24. data/examples/features/step_definitions/translation_steps.rb +55 -0
  25. data/examples/features/support/env.rb +49 -10
  26. data/examples/lib/activerecord_models.rb +34 -5
  27. data/examples/lib/couchpotato_models.rb +46 -6
  28. data/examples/lib/datamapper_models.rb +37 -3
  29. data/examples/lib/mongoid_models.rb +49 -0
  30. data/examples/lib/mongomapper_models.rb +36 -2
  31. data/features/cleaning.feature +1 -0
  32. data/features/cleaning_multiple_dbs.feature +20 -0
  33. data/features/cleaning_multiple_orms.feature +29 -0
  34. data/features/step_definitions/database_cleaner_steps.rb +21 -14
  35. data/features/support/feature_runner.rb +39 -0
  36. data/lib/database_cleaner/active_record/base.rb +46 -0
  37. data/lib/database_cleaner/active_record/transaction.rb +10 -10
  38. data/lib/database_cleaner/active_record/truncation.rb +42 -11
  39. data/lib/database_cleaner/base.rb +129 -0
  40. data/lib/database_cleaner/configuration.rb +45 -82
  41. data/lib/database_cleaner/couch_potato/base.rb +7 -0
  42. data/lib/database_cleaner/couch_potato/truncation.rb +4 -2
  43. data/lib/database_cleaner/cucumber.rb +0 -1
  44. data/lib/database_cleaner/data_mapper/base.rb +21 -0
  45. data/lib/database_cleaner/data_mapper/transaction.rb +10 -5
  46. data/lib/database_cleaner/data_mapper/truncation.rb +52 -19
  47. data/lib/database_cleaner/generic/base.rb +23 -0
  48. data/lib/database_cleaner/generic/truncation.rb +43 -0
  49. data/lib/database_cleaner/mongo_mapper/base.rb +20 -0
  50. data/lib/database_cleaner/mongo_mapper/truncation.rb +9 -3
  51. data/lib/database_cleaner/mongoid/base.rb +20 -0
  52. data/lib/database_cleaner/mongoid/truncation.rb +27 -0
  53. data/spec/database_cleaner/active_record/base_spec.rb +130 -0
  54. data/spec/database_cleaner/active_record/transaction_spec.rb +65 -0
  55. data/spec/database_cleaner/active_record/truncation_spec.rb +19 -18
  56. data/spec/database_cleaner/base_spec.rb +441 -0
  57. data/spec/database_cleaner/configuration_spec.rb +255 -65
  58. data/spec/database_cleaner/couch_potato/truncation_spec.rb +4 -3
  59. data/spec/database_cleaner/data_mapper/base_spec.rb +30 -0
  60. data/spec/database_cleaner/data_mapper/transaction_spec.rb +23 -0
  61. data/spec/database_cleaner/data_mapper/truncation_spec.rb +11 -0
  62. data/spec/database_cleaner/generic/base_spec.rb +22 -0
  63. data/spec/database_cleaner/generic/truncation_spec.rb +68 -0
  64. data/spec/database_cleaner/mongo_mapper/base_spec.rb +33 -0
  65. data/spec/database_cleaner/mongo_mapper/mongo_examples.rb +8 -0
  66. data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +12 -19
  67. data/spec/database_cleaner/mongoid/truncation_spec.rb +84 -0
  68. data/spec/database_cleaner/shared_strategy_spec.rb +13 -0
  69. data/spec/rcov.opts +1 -0
  70. data/spec/spec.opts +1 -0
  71. data/spec/spec_helper.rb +10 -3
  72. metadata +83 -8
  73. data/examples/features/step_definitions/example_steps.rb +0 -8
  74. data/lib/database_cleaner/truncation_base.rb +0 -41
@@ -1,104 +1,294 @@
1
1
  require File.dirname(__FILE__) + '/../spec_helper'
2
- require 'database_cleaner/active_record/transaction'
3
- require 'database_cleaner/data_mapper/transaction'
4
2
 
3
+ module DatabaseCleaner
4
+ class << self
5
+ def reset
6
+ @connections = nil
7
+ end
8
+
9
+ def connections_stub!(array)
10
+ @connections = array
11
+ end
12
+ end
13
+ end
14
+
15
+ describe ::DatabaseCleaner do
16
+ before(:each) { ::DatabaseCleaner.reset }
17
+
18
+ context "orm specification" do
19
+ it "should not accept unrecognised orms" do
20
+ expect { ::DatabaseCleaner[nil] }.should raise_error(::DatabaseCleaner::NoORMDetected)
21
+ end
5
22
 
6
- describe DatabaseCleaner do
23
+ it "should accept :active_record" do
24
+ cleaner = ::DatabaseCleaner[:active_record]
25
+ cleaner.should be_a(::DatabaseCleaner::Base)
26
+ cleaner.orm.should == :active_record
27
+ ::DatabaseCleaner.connections.size.should == 1
28
+ end
29
+
30
+ it "should accept :data_mapper" do
31
+ cleaner = ::DatabaseCleaner[:data_mapper]
32
+ cleaner.should be_a(::DatabaseCleaner::Base)
33
+ cleaner.orm.should == :data_mapper
34
+ ::DatabaseCleaner.connections.size.should == 1
35
+ end
36
+
37
+ it "should accept :mongo_mapper" do
38
+ cleaner = ::DatabaseCleaner[:mongo_mapper]
39
+ cleaner.should be_a(::DatabaseCleaner::Base)
40
+ cleaner.orm.should == :mongo_mapper
41
+ ::DatabaseCleaner.connections.size.should == 1
42
+ end
7
43
 
8
- # These examples muck around with the constants for autodetection so we need to clean up....
9
- before(:all) do
10
- TempAR = ActiveRecord unless defined?(TempAR)
11
- TempMM = MongoMapper unless defined?(TempMM)
12
- Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper)
13
- # need to add one for each ORM that we load in the spec helper...
44
+ it "should accept :couch_potato" do
45
+ cleaner = ::DatabaseCleaner[:couch_potato]
46
+ cleaner.should be_a(::DatabaseCleaner::Base)
47
+ cleaner.orm.should == :couch_potato
48
+ ::DatabaseCleaner.connections.size.should == 1
49
+ end
14
50
  end
15
- after(:all) do
16
- Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord) #want to make sure we have the real one...
17
- ActiveRecord = TempAR
18
- MongoMapper = TempMM
51
+
52
+ it "should accept multiple orm's" do
53
+ ::DatabaseCleaner[:couch_potato]
54
+ ::DatabaseCleaner[:data_mapper]
55
+ ::DatabaseCleaner.connections.size.should == 2
56
+ ::DatabaseCleaner.connections[0].orm.should == :couch_potato
57
+ ::DatabaseCleaner.connections[1].orm.should == :data_mapper
19
58
  end
20
59
 
21
- before(:each) do
22
- DatabaseCleaner::ActiveRecord::Transaction.stub!(:new).and_return(@strategy = mock('strategy'))
23
- Object.const_set('ActiveRecord', "just mocking out the constant here...") unless defined?(::ActiveRecord)
24
- DatabaseCleaner.strategy = nil
25
- DatabaseCleaner.orm = nil
26
- end
60
+ context "connection/db specification" do
61
+ it "should accept a connection parameter and store it" do
62
+ cleaner = ::DatabaseCleaner[:active_record, {:connection => :first_connection}]
63
+ cleaner.should be_a(::DatabaseCleaner::Base)
64
+ cleaner.orm.should == :active_record
65
+ cleaner.db.should == :first_connection
66
+ end
67
+
68
+ it "should accept multiple connections for a single orm" do
69
+ ::DatabaseCleaner[:data_mapper,{:connection => :first_db}]
70
+ ::DatabaseCleaner[:data_mapper,{:connection => :second_db}]
71
+ ::DatabaseCleaner.connections.size.should == 2
72
+ ::DatabaseCleaner.connections[0].orm.should == :data_mapper
73
+ ::DatabaseCleaner.connections[0].db.should == :first_db
74
+ ::DatabaseCleaner.connections[1].orm.should == :data_mapper
75
+ ::DatabaseCleaner.connections[1].db.should == :second_db
76
+ end
77
+
78
+ it "should accept multiple connections and multiple orms" do
79
+ ::DatabaseCleaner[:data_mapper, {:connection => :first_db} ]
80
+ ::DatabaseCleaner[:active_record,{:connection => :second_db}]
81
+ ::DatabaseCleaner[:active_record,{:connection => :first_db} ]
82
+ ::DatabaseCleaner[:data_mapper, {:connection => :second_db}]
83
+
84
+ ::DatabaseCleaner.connections.size.should == 4
85
+
86
+ ::DatabaseCleaner.connections[0].orm.should == :data_mapper
87
+ ::DatabaseCleaner.connections[0].db.should == :first_db
88
+
89
+ ::DatabaseCleaner.connections[1].orm.should == :active_record
90
+ ::DatabaseCleaner.connections[1].db.should == :second_db
27
91
 
28
- describe ".create_strategy" do
29
- it "should initialize and return the appropirate strategy" do
30
- DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
31
- result = DatabaseCleaner.create_strategy(:transaction, {'options' => 'hash'})
32
- result.should == @strategy
92
+ ::DatabaseCleaner.connections[2].orm.should == :active_record
93
+ ::DatabaseCleaner.connections[2].db.should == :first_db
94
+
95
+ ::DatabaseCleaner.connections[3].orm.should == :data_mapper
96
+ ::DatabaseCleaner.connections[3].db.should == :second_db
33
97
  end
34
98
  end
35
99
 
36
- describe ".clean_with" do
37
- it "should initialize the appropirate strategy and clean with it" do
38
- DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
39
- @strategy.should_receive(:clean)
40
- DatabaseCleaner.clean_with(:transaction, 'options' => 'hash')
100
+ context "connection/db retrieval" do
101
+ it "should retrieve a db rather than create a new one" do
102
+ pending
103
+ connection = ::DatabaseCleaner[:active_record].strategy = :truncation
104
+ ::DatabaseCleaner[:active_record].should == connection
41
105
  end
42
106
  end
43
107
 
44
- describe ".strategy=" do
45
- it "should initialize the appropirate strategy based on the ORM adapter detected" do
46
- DatabaseCleaner::ActiveRecord::Transaction.should_receive(:new).with('options' => 'hash')
47
- DatabaseCleaner.strategy = :transaction, {'options' => 'hash'}
108
+ context "class methods" do
109
+ subject { ::DatabaseCleaner }
110
+
111
+ its(:connections) { should respond_to(:each) }
48
112
 
49
- Object.send(:remove_const, 'ActiveRecord')
50
- Object.const_set('DataMapper', "just mocking out the constant here...")
51
- DatabaseCleaner.orm = nil
113
+ it "should give me a default (autodetection) databasecleaner by default" do
114
+ cleaner = mock("cleaner").as_null_object
115
+ ::DatabaseCleaner::Base.should_receive(:new).with().and_return(cleaner)
52
116
 
53
- DatabaseCleaner::DataMapper::Transaction.should_receive(:new).with(no_args)
54
- DatabaseCleaner.strategy = :transaction
117
+ ::DatabaseCleaner.connections.should have(1).items
118
+ ::DatabaseCleaner.connections.first.should == cleaner
55
119
  end
120
+ end
56
121
 
57
- it "should raise an error when no ORM is detected" do
58
- Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord)
59
- Object.send(:remove_const, 'DataMapper') if defined?(::DataMapper)
60
- Object.send(:remove_const, 'CouchPotato') if defined?(::CouchPotato)
122
+ context "single orm single connection" do
123
+ let(:connection) { ::DatabaseCleaner.connections.first }
61
124
 
62
- running { DatabaseCleaner.strategy = :transaction }.should raise_error(DatabaseCleaner::NoORMDetected)
125
+ it "should proxy strategy=" do
126
+ stratagum = mock("stratagum")
127
+ connection.should_receive(:strategy=).with(stratagum)
128
+ ::DatabaseCleaner.strategy = stratagum
63
129
  end
64
130
 
65
- it "should use the strategy version of the ORM specified with #orm=" do
66
- DatabaseCleaner.orm = 'data_mapper'
67
- DatabaseCleaner::DataMapper::Transaction.should_receive(:new)
131
+ it "should proxy orm=" do
132
+ orm = mock("orm")
133
+ connection.should_receive(:orm=).with(orm)
134
+ ::DatabaseCleaner.orm = orm
135
+ end
68
136
 
69
- DatabaseCleaner.strategy = :transaction
137
+ it "should proxy start" do
138
+ connection.should_receive(:start)
139
+ ::DatabaseCleaner.start
70
140
  end
71
141
 
72
- it "should raise an error when multiple args is passed in and the first is not a symbol" do
73
- running { DatabaseCleaner.strategy=Object.new, {:foo => 'bar'} }.should raise_error(ArgumentError)
142
+ it "should proxy clean" do
143
+ connection.should_receive(:clean)
144
+ ::DatabaseCleaner.clean
74
145
  end
75
146
 
76
- it "should raise an error when the specified strategy is not found" do
77
- running { DatabaseCleaner.strategy = :foo }.should raise_error(DatabaseCleaner::UnknownStrategySpecified)
147
+ it "should proxy clean_with" do
148
+ stratagem = mock("stratgem")
149
+ connection.should_receive(:clean_with).with(stratagem)
150
+ ::DatabaseCleaner.clean_with stratagem
78
151
  end
152
+ end
153
+
154
+ context "multiple connections" do
155
+
156
+ #these are relativly simple, all we need to do is make sure all connections are cleaned/started/cleaned_with appropriatly.
157
+ context "simple proxy methods" do
158
+
159
+ let(:active_record) { mock("active_mock") }
160
+ let(:data_mapper) { mock("data_mock") }
161
+
162
+ before(:each) do
163
+ ::DatabaseCleaner.stub!(:connections).and_return([active_record,data_mapper])
164
+ end
165
+
166
+ it "should proxy orm to all connections" do
167
+ active_record.should_receive(:orm=)
168
+ data_mapper.should_receive(:orm=)
169
+
170
+ ::DatabaseCleaner.orm = mock("orm")
171
+ end
172
+
173
+ it "should proxy start to all connections" do
174
+ active_record.should_receive(:start)
175
+ data_mapper.should_receive(:start)
176
+
177
+ ::DatabaseCleaner.start
178
+ end
179
+
180
+ it "should proxy clean to all connections" do
181
+ active_record.should_receive(:clean)
182
+ data_mapper.should_receive(:clean)
183
+
184
+ ::DatabaseCleaner.clean
185
+ end
186
+
187
+ it "should proxy clean_with to all connections" do
188
+ stratagem = mock("stratgem")
189
+ active_record.should_receive(:clean_with).with(stratagem)
190
+ data_mapper.should_receive(:clean_with).with(stratagem)
79
191
 
80
- it "should allow any object to be set as the strategy" do
81
- mock_strategy = mock('strategy')
82
- running { DatabaseCleaner.strategy = mock_strategy }.should_not raise_error
192
+ ::DatabaseCleaner.clean_with stratagem
193
+ end
83
194
  end
84
195
 
85
- end
196
+ # ah now we have some difficulty, we mustn't allow duplicate connections to exist, but they could
197
+ # plausably want to force orm/strategy change on two sets of orm that differ only on db
198
+ context "multiple orm proxy methods" do
199
+
200
+ it "should proxy orm to all connections and remove duplicate connections" do
201
+ active_record_1 = mock("active_mock_on_db_one").as_null_object
202
+ active_record_2 = mock("active_mock_on_db_two").as_null_object
203
+ data_mapper_1 = mock("data_mock_on_db_one").as_null_object
204
+
205
+ ::DatabaseCleaner.connections_stub! [active_record_1,active_record_2,data_mapper_1]
206
+
207
+ active_record_1.should_receive(:orm=).with(:data_mapper)
208
+ active_record_2.should_receive(:orm=).with(:data_mapper)
209
+ data_mapper_1.should_receive(:orm=).with(:data_mapper)
210
+
211
+ active_record_1.should_receive(:==).with(data_mapper_1).and_return(true)
212
+
213
+ ::DatabaseCleaner.connections.size.should == 3
214
+ ::DatabaseCleaner.orm = :data_mapper
215
+ ::DatabaseCleaner.connections.size.should == 2
216
+ end
86
217
 
218
+ it "should proxy strategy to all connections and remove duplicate connections" do
219
+ active_record_1 = mock("active_mock_strategy_one").as_null_object
220
+ active_record_2 = mock("active_mock_strategy_two").as_null_object
221
+ strategy = mock("strategy")
87
222
 
88
- %w[start clean].each do |strategy_method|
89
- describe ".#{strategy_method}" do
90
- it "should be delgated to the strategy set with strategy=" do
91
- DatabaseCleaner.strategy = :transaction
223
+ ::DatabaseCleaner.connections_stub! [active_record_1,active_record_2]
92
224
 
93
- @strategy.should_receive(strategy_method)
225
+ active_record_1.should_receive(:strategy=).with(strategy)
226
+ active_record_2.should_receive(:strategy=).with(strategy)
94
227
 
95
- DatabaseCleaner.send(strategy_method)
96
- end
228
+ active_record_1.should_receive(:==).with(active_record_2).and_return(true)
97
229
 
98
- it "should raise en error when no strategy has been set" do
99
- running { DatabaseCleaner.send(strategy_method) }.should raise_error(DatabaseCleaner::NoStrategySetError)
100
- end
230
+ ::DatabaseCleaner.connections.size.should == 2
231
+ ::DatabaseCleaner.strategy = strategy
232
+ ::DatabaseCleaner.connections.size.should == 1
101
233
  end
102
234
  end
235
+ end
236
+
237
+ describe "remove_duplicates" do
238
+ it "should remove duplicates if they are identical" do
239
+ orm = mock("orm")
240
+ connection = mock("a datamapper connection", :orm => orm )
241
+
242
+ ::DatabaseCleaner.connections_stub! [connection,connection,connection]
243
+
244
+ ::DatabaseCleaner.remove_duplicates
245
+ ::DatabaseCleaner.connections.size.should == 1
246
+ end
247
+ end
248
+
249
+ describe "app_root" do
250
+ it "should default to Dir.pwd" do
251
+ DatabaseCleaner.app_root.should == Dir.pwd
252
+ end
103
253
 
254
+ it "should store specific paths" do
255
+ DatabaseCleaner.app_root = '/path/to'
256
+ DatabaseCleaner.app_root.should == '/path/to'
257
+ end
258
+ end
259
+
260
+ describe "orm_module" do
261
+ subject { ::DatabaseCleaner }
262
+
263
+ it "should return DatabaseCleaner::ActiveRecord for :active_record" do
264
+ ::DatabaseCleaner::ActiveRecord = mock("ar module") unless defined? ::DatabaseCleaner::ActiveRecord
265
+ subject.orm_module(:active_record).should == DatabaseCleaner::ActiveRecord
266
+ end
267
+
268
+ it "should return DatabaseCleaner::DataMapper for :data_mapper" do
269
+ ::DatabaseCleaner::DataMapper = mock("dm module") unless defined? ::DatabaseCleaner::DataMapper
270
+ subject.orm_module(:data_mapper).should == DatabaseCleaner::DataMapper
271
+ end
272
+
273
+ it "should return DatabaseCleaner::MongoMapper for :mongo_mapper" do
274
+ ::DatabaseCleaner::MongoMapper = mock("mm module") unless defined? ::DatabaseCleaner::MongoMapper
275
+ subject.orm_module(:mongo_mapper).should == DatabaseCleaner::MongoMapper
276
+ end
277
+
278
+ it "should return DatabaseCleaner::Mongoid for :mongoid" do
279
+ ::DatabaseCleaner::Mongoid = mock("mongoid module") unless defined? ::DatabaseCleaner::Mongoid
280
+ subject.orm_module(:mongoid).should == DatabaseCleaner::Mongoid
281
+ end
282
+
283
+ it "should return DatabaseCleaner::Mongo for :mongo" do
284
+ ::DatabaseCleaner::Mongo = mock("mongo module") unless defined? ::DatabaseCleaner::Mongo
285
+ subject.orm_module(:mongo).should == DatabaseCleaner::Mongo
286
+ end
287
+
288
+ it "should return DatabaseCleaner::CouchPotato for :couch_potato" do
289
+ ::DatabaseCleaner::CouchPotato = mock("cp module") unless defined? ::DatabaseCleaner::CouchPotato
290
+ subject.orm_module(:couch_potato).should == DatabaseCleaner::CouchPotato
291
+ end
292
+
293
+ end
104
294
  end
@@ -6,13 +6,14 @@ module DatabaseCleaner
6
6
  module CouchPotato
7
7
 
8
8
  describe Truncation do
9
+ let(:database) { mock('database') }
10
+
9
11
  before(:each) do
10
- @database = mock('database')
11
- ::CouchPotato.stub!(:couchrest_database).and_return(@database)
12
+ ::CouchPotato.stub!(:couchrest_database).and_return(database)
12
13
  end
13
14
 
14
15
  it "should re-create the database" do
15
- @database.should_receive(:recreate!)
16
+ database.should_receive(:recreate!)
16
17
 
17
18
  Truncation.new.clean
18
19
  end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+ require 'database_cleaner/data_mapper/base'
3
+ require 'database_cleaner/shared_strategy_spec'
4
+
5
+ module DatabaseCleaner
6
+ describe DataMapper do
7
+ it { should respond_to(:available_strategies) }
8
+ end
9
+
10
+ module DataMapper
11
+ class ExampleStrategy
12
+ include ::DatabaseCleaner::DataMapper::Base
13
+ end
14
+
15
+ describe ExampleStrategy do
16
+ it_should_behave_like "a generic strategy"
17
+ it { should respond_to(:db) }
18
+ it { should respond_to(:db=) }
19
+
20
+ it "should store my desired db" do
21
+ subject.db = :my_db
22
+ subject.db.should == :my_db
23
+ end
24
+
25
+ it "should default to :default" do
26
+ subject.db.should == :default
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+ require 'database_cleaner/data_mapper/transaction'
3
+ require 'database_cleaner/shared_strategy_spec'
4
+ #require 'data_mapper'
5
+
6
+ module DatabaseCleaner
7
+ module DataMapper
8
+
9
+ describe Transaction do
10
+ it_should_behave_like "a generic strategy"
11
+ it_should_behave_like "a generic transaction strategy"
12
+
13
+ describe "start" do
14
+ it "should start a transaction"
15
+ end
16
+
17
+ describe "clean" do
18
+ it "should finish a transaction"
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ require 'database_cleaner/data_mapper/truncation'
2
+ require 'database_cleaner/shared_strategy_spec'
3
+
4
+ module DatabaseCleaner
5
+ module DataMapper
6
+ describe Truncation do
7
+ it_should_behave_like "a generic strategy"
8
+ it_should_behave_like "a generic truncation strategy"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require 'database_cleaner/shared_strategy_spec'
3
+ require 'database_cleaner/generic/base'
4
+
5
+ module ::DatabaseCleaner
6
+ module Generic
7
+ class ExampleStrategy
8
+ include ::DatabaseCleaner::Generic::Base
9
+ end
10
+
11
+ describe ExampleStrategy do
12
+ context "class methods" do
13
+ subject { ExampleStrategy }
14
+ its(:available_strategies) { should be_empty }
15
+ end
16
+
17
+ it_should_behave_like "a generic strategy"
18
+
19
+ its(:db) { should == :default }
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ require 'database_cleaner/generic/truncation'
3
+
4
+ module ::DatabaseCleaner
5
+ module Generic
6
+ class TruncationExample
7
+ include ::DatabaseCleaner::Generic::Truncation
8
+
9
+ def only
10
+ @only
11
+ end
12
+
13
+ def except
14
+ @tables_to_exclude
15
+ end
16
+ end
17
+
18
+ class MigrationExample < TruncationExample
19
+ def migration_storage_name
20
+ "migration_storage_name"
21
+ end
22
+ end
23
+
24
+ describe TruncationExample do
25
+ its(:start) { expect{ subject }.to_not raise_error }
26
+ its(:clean) { expect{ subject }.to raise_error(NotImplementedError) }
27
+
28
+ context "private methods" do
29
+ it { should_not respond_to(:tables_to_truncate) }
30
+ its(:tables_to_truncate) { expect{ subject }.to raise_error(NotImplementedError) }
31
+
32
+ it { should_not respond_to(:migration_storage_name) }
33
+ its(:migration_storage_name) { should be_nil }
34
+ end
35
+
36
+ describe "initialize" do
37
+ it { expect{ subject }.to_not raise_error }
38
+
39
+ it "should accept a hash of options" do
40
+ expect{ TruncationExample.new {} }.to_not raise_error
41
+ end
42
+
43
+ it { expect{ TruncationExample.new( { :a_random_param => "should raise ArgumentError" } ) }.to raise_error(ArgumentError) }
44
+ it { expect{ TruncationExample.new( { :except => "something",:only => "something else" } ) }.to raise_error(ArgumentError) }
45
+ it { expect{ TruncationExample.new( { :only => "something" } ) }.to_not raise_error(ArgumentError) }
46
+ it { expect{ TruncationExample.new( { :except => "something" } ) }.to_not raise_error(ArgumentError) }
47
+
48
+ context "" do
49
+ subject { TruncationExample.new( { :only => ["something"] } ) }
50
+ its(:only) { should == ["something"] }
51
+ its(:except) { should == [] }
52
+ end
53
+
54
+ context "" do
55
+ subject { TruncationExample.new( { :except => ["something"] } ) }
56
+ its(:only) { should == nil }
57
+ its(:except) { should include("something") }
58
+ end
59
+
60
+ context "" do
61
+ subject { MigrationExample.new }
62
+ its(:only) { should == nil }
63
+ its(:except) { should == ["migration_storage_name"] }
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ require 'database_cleaner/mongo_mapper/base'
3
+ require 'database_cleaner/shared_strategy_spec'
4
+
5
+ module DatabaseCleaner
6
+ describe MongoMapper do
7
+ it { should respond_to(:available_strategies) }
8
+ end
9
+
10
+ module MongoMapper
11
+ class ExampleStrategy
12
+ include ::DatabaseCleaner::MongoMapper::Base
13
+ end
14
+
15
+ describe ExampleStrategy do
16
+
17
+ it_should_behave_like "a generic strategy"
18
+
19
+ describe "db" do
20
+ it { should respond_to(:db=) }
21
+
22
+ it "should store my desired db" do
23
+ subject.db = :my_db
24
+ subject.db.should == :my_db
25
+ end
26
+
27
+ it "should default to :default" do
28
+ subject.db.should == :default
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ class Widget
2
+ include ::MongoMapper::Document
3
+ key :name, String
4
+ end
5
+ class Gadget
6
+ include ::MongoMapper::Document
7
+ key :name, String
8
+ end
@@ -1,33 +1,26 @@
1
1
  require File.dirname(__FILE__) + '/../../spec_helper'
2
- require 'mongomapper'
2
+ require 'mongo_mapper'
3
3
  require 'database_cleaner/mongo_mapper/truncation'
4
-
5
- MongoMapper.connection = Mongo::Connection.new('127.0.0.1')
6
- TEST_DATABASE = 'database_cleaner_specs'
7
- MongoMapper.database = TEST_DATABASE
8
-
9
- class Widget
10
- include MongoMapper::Document
11
- key :name, String
12
- end
13
- class Gadget
14
- include MongoMapper::Document
15
- key :name, String
16
- end
17
-
4
+ require File.dirname(__FILE__) + '/mongo_examples'
18
5
 
19
6
  module DatabaseCleaner
20
7
  module MongoMapper
21
8
 
22
9
  describe Truncation do
10
+
11
+ #doing this in the file root breaks autospec, doing it before(:all) just fails the specs
12
+ before(:all) do
13
+ ::MongoMapper.connection = ::Mongo::Connection.new('127.0.0.1')
14
+ @test_db = 'database_cleaner_specs'
15
+ ::MongoMapper.database = @test_db
16
+ end
17
+
23
18
  before(:each) do
24
- ::MongoMapper.connection.drop_database(TEST_DATABASE)
25
- #::MongoMapper.connection.db(TEST_DATABASE).collections.each {|c| c.remove }
26
- #::MongoMapper.database = TEST_DATABASE
19
+ ::MongoMapper.connection.drop_database(@test_db)
27
20
  end
28
21
 
29
22
  def ensure_counts(expected_counts)
30
- # I had to add this sanity_check garbage because I was getting non-determinisc results from mongomapper at times..
23
+ # I had to add this sanity_check garbage because I was getting non-determinisc results from mongomapper at times..
31
24
  # very odd and disconcerting...
32
25
  sanity_check = expected_counts.delete(:sanity_check)
33
26
  begin