database_cleaner 0.5.0 → 0.5.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/History.txt +14 -0
- data/VERSION.yml +1 -1
- data/examples/lib/mongoid_models.rb +23 -0
- data/examples/lib/mongomapper_models.rb +1 -1
- data/features/cleaning.feature +1 -0
- data/features/step_definitions/database_cleaner_steps.rb +1 -1
- data/lib/database_cleaner/active_record/truncation.rb +28 -7
- data/lib/database_cleaner/configuration.rb +18 -2
- data/lib/database_cleaner/mongoid/truncation.rb +23 -0
- data/spec/database_cleaner/active_record/transaction_spec.rb +65 -0
- data/spec/database_cleaner/configuration_spec.rb +3 -0
- data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +1 -1
- data/spec/database_cleaner/mongoid/truncation_spec.rb +84 -0
- data/spec/spec_helper.rb +1 -1
- metadata +9 -2
data/History.txt
CHANGED
@@ -1,5 +1,19 @@
|
|
1
1
|
0.5.x (In Git)
|
2
2
|
|
3
|
+
== 0.5.1 - The Mongoid Release
|
4
|
+
|
5
|
+
This release also attempts to fix AR for Rails 3 support. I have seen mixed reviews on this. Some people
|
6
|
+
claim the fixes allow for use in Rails3 while others have not had good luck with it. I plan on reworking
|
7
|
+
the way AR support is added so that it is more friendly with how Rails 3 uses autoload.
|
8
|
+
|
9
|
+
=== New features
|
10
|
+
* Clean and clean_with methods are now aliased to clean! and clean_with!. (Ben Mabey)
|
11
|
+
* Mongoid Support! (Sidney Burks)
|
12
|
+
|
13
|
+
=== Bugfixes
|
14
|
+
* Check PostgreSQL version >= 8.2 before using TRUNCATE CASCADE (James B. Byrne)
|
15
|
+
* Correct superclass is used in ActiveRecord connection adapters. (johnathan, Aslak Hellesoy, Ben Mabey)
|
16
|
+
|
3
17
|
== 0.5.0 2010-02-22 - The CouchPotato Release
|
4
18
|
|
5
19
|
=== New features
|
data/VERSION.yml
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
Mongoid.configure do |config|
|
4
|
+
name = 'database_cleaner_test'
|
5
|
+
config.master = Mongo::Connection.new.db(name)
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
#::MongoMapper.connection = Mongo::Connection.new('127.0.0.1')
|
10
|
+
#::MongoMapper.database = 'database_cleaner_test'
|
11
|
+
|
12
|
+
class Widget
|
13
|
+
include Mongoid::Document
|
14
|
+
field :id, :type => Integer
|
15
|
+
field :name
|
16
|
+
|
17
|
+
class << self
|
18
|
+
#mongomapper doesn't seem to provide this...
|
19
|
+
def create!(*args)
|
20
|
+
new(*args).save!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/features/cleaning.feature
CHANGED
@@ -1,21 +1,29 @@
|
|
1
|
+
require 'active_record/base'
|
2
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
1
3
|
require "database_cleaner/truncation_base"
|
2
4
|
|
3
5
|
module ActiveRecord
|
4
6
|
module ConnectionAdapters
|
5
7
|
|
6
|
-
class
|
8
|
+
class AbstractAdapter
|
9
|
+
end
|
10
|
+
|
11
|
+
class SQLiteAdapter < AbstractAdapter
|
12
|
+
end
|
13
|
+
|
14
|
+
class MysqlAdapter < AbstractAdapter
|
7
15
|
def truncate_table(table_name)
|
8
16
|
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
|
9
17
|
end
|
10
18
|
end
|
11
19
|
|
12
|
-
class SQLite3Adapter
|
20
|
+
class SQLite3Adapter < SQLiteAdapter
|
13
21
|
def truncate_table(table_name)
|
14
22
|
execute("DELETE FROM #{quote_table_name(table_name)};")
|
15
23
|
end
|
16
24
|
end
|
17
25
|
|
18
|
-
class JdbcAdapter
|
26
|
+
class JdbcAdapter < AbstractAdapter
|
19
27
|
def truncate_table(table_name)
|
20
28
|
begin
|
21
29
|
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
|
@@ -25,19 +33,32 @@ module ActiveRecord
|
|
25
33
|
end
|
26
34
|
end
|
27
35
|
|
28
|
-
class PostgreSQLAdapter
|
36
|
+
class PostgreSQLAdapter < AbstractAdapter
|
37
|
+
|
38
|
+
def self.db_version
|
39
|
+
@db_version ||= ActiveRecord::Base.connection.select_values(
|
40
|
+
"SELECT CHARACTER_VALUE
|
41
|
+
FROM INFORMATION_SCHEMA.SQL_IMPLEMENTATION_INFO
|
42
|
+
WHERE IMPLEMENTATION_INFO_NAME = 'DBMS VERSION' ").to_s
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.cascade
|
46
|
+
@cascade ||= db_version >= "08.02" ? "CASCADE" : ""
|
47
|
+
end
|
48
|
+
|
29
49
|
def truncate_table(table_name)
|
30
|
-
execute("TRUNCATE TABLE #{quote_table_name(table_name)}
|
50
|
+
execute("TRUNCATE TABLE #{quote_table_name(table_name)} #{self.class.cascade};")
|
31
51
|
end
|
52
|
+
|
32
53
|
end
|
33
54
|
|
34
|
-
class SQLServerAdapter
|
55
|
+
class SQLServerAdapter < AbstractAdapter
|
35
56
|
def truncate_table(table_name)
|
36
57
|
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
|
37
58
|
end
|
38
59
|
end
|
39
60
|
|
40
|
-
class OracleEnhancedAdapter
|
61
|
+
class OracleEnhancedAdapter < AbstractAdapter
|
41
62
|
def truncate_table(table_name)
|
42
63
|
execute("TRUNCATE TABLE #{quote_table_name(table_name)}")
|
43
64
|
end
|
@@ -16,12 +16,19 @@ module DatabaseCleaner
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
+
|
19
20
|
module MongoMapper
|
20
21
|
def self.available_strategies
|
21
22
|
%w[truncation]
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
26
|
+
module Mongoid
|
27
|
+
def self.available_strategies
|
28
|
+
%w[truncation]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
25
32
|
module CouchPotato
|
26
33
|
def self.available_strategies
|
27
34
|
%w[truncation]
|
@@ -41,6 +48,8 @@ module DatabaseCleaner
|
|
41
48
|
strategy
|
42
49
|
end
|
43
50
|
|
51
|
+
alias clean_with! clean_with
|
52
|
+
|
44
53
|
def strategy=(args)
|
45
54
|
strategy, *strategy_args = args
|
46
55
|
if strategy.is_a?(Symbol)
|
@@ -64,6 +73,8 @@ module DatabaseCleaner
|
|
64
73
|
strategy.clean
|
65
74
|
end
|
66
75
|
|
76
|
+
alias clean! clean
|
77
|
+
|
67
78
|
private
|
68
79
|
|
69
80
|
def strategy
|
@@ -72,7 +83,8 @@ module DatabaseCleaner
|
|
72
83
|
end
|
73
84
|
|
74
85
|
def orm_strategy(strategy)
|
75
|
-
|
86
|
+
puts "Strategy is : #{strategy} and ORM is #{orm}"
|
87
|
+
require "database_cleaner/#{orm}/#{strategy}"
|
76
88
|
orm_module.const_get(strategy.to_s.capitalize)
|
77
89
|
rescue LoadError => e
|
78
90
|
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
|
@@ -87,10 +99,12 @@ module DatabaseCleaner
|
|
87
99
|
'data_mapper'
|
88
100
|
elsif defined? ::MongoMapper
|
89
101
|
'mongo_mapper'
|
102
|
+
elsif defined? ::Mongoid
|
103
|
+
'mongoid'
|
90
104
|
elsif defined? ::CouchPotato
|
91
105
|
'couch_potato'
|
92
106
|
else
|
93
|
-
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper or CouchPotato loaded?"
|
107
|
+
raise NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, MongoMapper, Mongoid, or CouchPotato loaded?"
|
94
108
|
end
|
95
109
|
end
|
96
110
|
end
|
@@ -104,6 +118,8 @@ module DatabaseCleaner
|
|
104
118
|
DatabaseCleaner::DataMapper
|
105
119
|
when 'mongo_mapper'
|
106
120
|
DatabaseCleaner::MongoMapper
|
121
|
+
when 'mongoid'
|
122
|
+
DatabaseCleaner::Mongoid
|
107
123
|
when 'couch_potato'
|
108
124
|
DatabaseCleaner::CouchPotato
|
109
125
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'database_cleaner/truncation_base'
|
2
|
+
|
3
|
+
module DatabaseCleaner
|
4
|
+
module Mongoid
|
5
|
+
class Truncation < DatabaseCleaner::TruncationBase
|
6
|
+
def clean
|
7
|
+
if @only
|
8
|
+
collections.each { |c| c.remove if @only.include?(c.name) }
|
9
|
+
else
|
10
|
+
collections.each { |c| c.remove unless @tables_to_exclude.include?(c.name) }
|
11
|
+
end
|
12
|
+
true
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def collections
|
18
|
+
::Mongoid.database.collections
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
23
|
+
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
|
@@ -9,13 +9,16 @@ describe DatabaseCleaner do
|
|
9
9
|
before(:all) do
|
10
10
|
TempAR = ActiveRecord unless defined?(TempAR)
|
11
11
|
TempMM = MongoMapper unless defined?(TempMM)
|
12
|
+
TempMI = Mongoid unless defined?(TempMI)
|
12
13
|
Object.send(:remove_const, 'MongoMapper') if defined?(::MongoMapper)
|
14
|
+
Object.send(:remove_const, 'Mongoid') if defined?(::Mongoid)
|
13
15
|
# need to add one for each ORM that we load in the spec helper...
|
14
16
|
end
|
15
17
|
after(:all) do
|
16
18
|
Object.send(:remove_const, 'ActiveRecord') if defined?(::ActiveRecord) #want to make sure we have the real one...
|
17
19
|
ActiveRecord = TempAR
|
18
20
|
MongoMapper = TempMM
|
21
|
+
Mongoid = TempMI
|
19
22
|
end
|
20
23
|
|
21
24
|
before(:each) do
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
require 'mongoid'
|
3
|
+
require 'database_cleaner/mongoid/truncation'
|
4
|
+
|
5
|
+
TEST_DATABASE = 'database_cleaner_specs'
|
6
|
+
Mongoid.configure do |config|
|
7
|
+
name = TEST_DATABASE
|
8
|
+
config.master = Mongo::Connection.new.db(name)
|
9
|
+
end
|
10
|
+
|
11
|
+
class TestClassA
|
12
|
+
include Mongoid::Document
|
13
|
+
field :name
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestClassB
|
17
|
+
include Mongoid::Document
|
18
|
+
field :name
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
module DatabaseCleaner
|
23
|
+
module Mongoid
|
24
|
+
|
25
|
+
describe Truncation do
|
26
|
+
before(:each) do
|
27
|
+
::Mongoid.database.connection.drop_database(TEST_DATABASE)
|
28
|
+
#::MongoMapper.connection.db(TEST_DATABASE).collections.each {|c| c.remove }
|
29
|
+
#::MongoMapper.database = TEST_DATABASE
|
30
|
+
end
|
31
|
+
|
32
|
+
def ensure_counts(expected_counts)
|
33
|
+
# I had to add this sanity_check garbage because I was getting non-determinisc results from mongomapper at times..
|
34
|
+
# very odd and disconcerting...
|
35
|
+
sanity_check = expected_counts.delete(:sanity_check)
|
36
|
+
begin
|
37
|
+
expected_counts.each do |model_class, expected_count|
|
38
|
+
model_class.count.should equal(expected_count), "#{model_class} expected to have a count of #{expected_count} but was #{model_class.count}"
|
39
|
+
end
|
40
|
+
rescue Spec::Expectations::ExpectationNotMetError => e
|
41
|
+
raise !sanity_check ? e : Spec::ExpectationNotMetError::ExpectationNotMetError.new("SANITY CHECK FAILURE! This should never happen here: #{e.message}")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def create_testclassa(attrs={})
|
46
|
+
TestClassA.new({:name => 'some testclassa'}.merge(attrs)).save!
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_testclassb(attrs={})
|
50
|
+
TestClassB.new({:name => 'some testclassb'}.merge(attrs)).save!
|
51
|
+
end
|
52
|
+
|
53
|
+
it "truncates all collections by default" do
|
54
|
+
create_testclassa
|
55
|
+
create_testclassb
|
56
|
+
ensure_counts(TestClassA => 1, TestClassB => 1, :sanity_check => true)
|
57
|
+
Truncation.new.clean
|
58
|
+
ensure_counts(TestClassA => 0, TestClassB => 0)
|
59
|
+
end
|
60
|
+
|
61
|
+
context "when collections are provided to the :only option" do
|
62
|
+
it "only truncates the specified collections" do
|
63
|
+
create_testclassa
|
64
|
+
create_testclassb
|
65
|
+
ensure_counts(TestClassA => 1, TestClassB => 1, :sanity_check => true)
|
66
|
+
Truncation.new(:only => ['test_class_as']).clean
|
67
|
+
ensure_counts(TestClassA => 0, TestClassB => 1)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "when collections are provided to the :except option" do
|
72
|
+
it "truncates all but the specified collections" do
|
73
|
+
create_testclassa
|
74
|
+
create_testclassb
|
75
|
+
ensure_counts(TestClassA => 1, TestClassB => 1, :sanity_check => true)
|
76
|
+
Truncation.new(:except => ['test_class_as']).clean
|
77
|
+
ensure_counts(TestClassA => 1, TestClassB => 0)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_cleaner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Mabey
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-04-13 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- examples/lib/activerecord_models.rb
|
36
36
|
- examples/lib/couchpotato_models.rb
|
37
37
|
- examples/lib/datamapper_models.rb
|
38
|
+
- examples/lib/mongoid_models.rb
|
38
39
|
- examples/lib/mongomapper_models.rb
|
39
40
|
- features/cleaning.feature
|
40
41
|
- features/step_definitions/database_cleaner_steps.rb
|
@@ -48,11 +49,14 @@ files:
|
|
48
49
|
- lib/database_cleaner/data_mapper/transaction.rb
|
49
50
|
- lib/database_cleaner/data_mapper/truncation.rb
|
50
51
|
- lib/database_cleaner/mongo_mapper/truncation.rb
|
52
|
+
- lib/database_cleaner/mongoid/truncation.rb
|
51
53
|
- lib/database_cleaner/truncation_base.rb
|
54
|
+
- spec/database_cleaner/active_record/transaction_spec.rb
|
52
55
|
- spec/database_cleaner/active_record/truncation_spec.rb
|
53
56
|
- spec/database_cleaner/configuration_spec.rb
|
54
57
|
- spec/database_cleaner/couch_potato/truncation_spec.rb
|
55
58
|
- spec/database_cleaner/mongo_mapper/truncation_spec.rb
|
59
|
+
- spec/database_cleaner/mongoid/truncation_spec.rb
|
56
60
|
- spec/spec.opts
|
57
61
|
- spec/spec_helper.rb
|
58
62
|
- LICENSE
|
@@ -86,14 +90,17 @@ signing_key:
|
|
86
90
|
specification_version: 3
|
87
91
|
summary: Strategies for cleaning databases. Can be used to ensure a clean state for testing.
|
88
92
|
test_files:
|
93
|
+
- spec/database_cleaner/active_record/transaction_spec.rb
|
89
94
|
- spec/database_cleaner/active_record/truncation_spec.rb
|
90
95
|
- spec/database_cleaner/configuration_spec.rb
|
91
96
|
- spec/database_cleaner/couch_potato/truncation_spec.rb
|
92
97
|
- spec/database_cleaner/mongo_mapper/truncation_spec.rb
|
98
|
+
- spec/database_cleaner/mongoid/truncation_spec.rb
|
93
99
|
- spec/spec_helper.rb
|
94
100
|
- examples/features/step_definitions/example_steps.rb
|
95
101
|
- examples/features/support/env.rb
|
96
102
|
- examples/lib/activerecord_models.rb
|
97
103
|
- examples/lib/couchpotato_models.rb
|
98
104
|
- examples/lib/datamapper_models.rb
|
105
|
+
- examples/lib/mongoid_models.rb
|
99
106
|
- examples/lib/mongomapper_models.rb
|