database_cleaner 1.6.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +5 -5
  2. data/lib/database_cleaner.rb +1 -8
  3. metadata +18 -498
  4. data/CONTRIBUTE.markdown +0 -28
  5. data/Gemfile.lock +0 -286
  6. data/History.rdoc +0 -430
  7. data/LICENSE +0 -20
  8. data/README.markdown +0 -518
  9. data/Rakefile +0 -40
  10. data/TODO +0 -3
  11. data/VERSION.yml +0 -4
  12. data/cucumber.yml +0 -1
  13. data/examples/Gemfile +0 -1
  14. data/examples/Gemfile.lock +0 -1
  15. data/examples/config/database.yml.example +0 -8
  16. data/examples/config/redis.yml +0 -8
  17. data/examples/db/sqlite_databases_go_here +0 -0
  18. data/examples/features/example.feature +0 -11
  19. data/examples/features/example_multiple_db.feature +0 -23
  20. data/examples/features/example_multiple_orm.feature +0 -22
  21. data/examples/features/step_definitions/activerecord_steps.rb +0 -31
  22. data/examples/features/step_definitions/couchpotato_steps.rb +0 -31
  23. data/examples/features/step_definitions/datamapper_steps.rb +0 -37
  24. data/examples/features/step_definitions/mongoid_steps.rb +0 -23
  25. data/examples/features/step_definitions/mongomapper_steps.rb +0 -31
  26. data/examples/features/step_definitions/neo4j_steps.rb +0 -23
  27. data/examples/features/step_definitions/ohm_steps.rb +0 -31
  28. data/examples/features/step_definitions/redis_steps.rb +0 -31
  29. data/examples/features/step_definitions/translation_steps.rb +0 -55
  30. data/examples/features/support/env.rb +0 -61
  31. data/examples/lib/activerecord_models.rb +0 -41
  32. data/examples/lib/couchpotato_models.rb +0 -61
  33. data/examples/lib/datamapper_models.rb +0 -50
  34. data/examples/lib/mongoid_models.rb +0 -44
  35. data/examples/lib/mongomapper_models.rb +0 -51
  36. data/examples/lib/neo4j_models.rb +0 -17
  37. data/examples/lib/ohm_models.rb +0 -43
  38. data/examples/lib/redis_models.rb +0 -65
  39. data/examples/lib/sequel_models.rb +0 -9
  40. data/features/cleaning.feature +0 -30
  41. data/features/cleaning_default_strategy.feature +0 -23
  42. data/features/cleaning_multiple_dbs.feature +0 -22
  43. data/features/cleaning_multiple_orms.feature +0 -67
  44. data/features/step_definitions/database_cleaner_steps.rb +0 -33
  45. data/features/support/env.rb +0 -4
  46. data/features/support/feature_runner.rb +0 -39
  47. data/lib/database_cleaner/active_record/base.rb +0 -92
  48. data/lib/database_cleaner/active_record/deletion.rb +0 -106
  49. data/lib/database_cleaner/active_record/transaction.rb +0 -59
  50. data/lib/database_cleaner/active_record/truncation.rb +0 -275
  51. data/lib/database_cleaner/base.rb +0 -164
  52. data/lib/database_cleaner/configuration.rb +0 -131
  53. data/lib/database_cleaner/couch_potato/base.rb +0 -7
  54. data/lib/database_cleaner/couch_potato/truncation.rb +0 -28
  55. data/lib/database_cleaner/cucumber.rb +0 -3
  56. data/lib/database_cleaner/data_mapper/base.rb +0 -21
  57. data/lib/database_cleaner/data_mapper/transaction.rb +0 -28
  58. data/lib/database_cleaner/data_mapper/truncation.rb +0 -172
  59. data/lib/database_cleaner/generic/base.rb +0 -29
  60. data/lib/database_cleaner/generic/transaction.rb +0 -11
  61. data/lib/database_cleaner/generic/truncation.rb +0 -40
  62. data/lib/database_cleaner/mongo/base.rb +0 -16
  63. data/lib/database_cleaner/mongo/truncation.rb +0 -62
  64. data/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
  65. data/lib/database_cleaner/mongo2/base.rb +0 -16
  66. data/lib/database_cleaner/mongo2/truncation_mixin.rb +0 -39
  67. data/lib/database_cleaner/mongo_mapper/base.rb +0 -20
  68. data/lib/database_cleaner/mongo_mapper/truncation.rb +0 -19
  69. data/lib/database_cleaner/mongoid/base.rb +0 -20
  70. data/lib/database_cleaner/mongoid/truncation.rb +0 -49
  71. data/lib/database_cleaner/moped/base.rb +0 -39
  72. data/lib/database_cleaner/moped/truncation.rb +0 -9
  73. data/lib/database_cleaner/moped/truncation_base.rb +0 -40
  74. data/lib/database_cleaner/neo4j/base.rb +0 -62
  75. data/lib/database_cleaner/neo4j/deletion.rb +0 -16
  76. data/lib/database_cleaner/neo4j/transaction.rb +0 -35
  77. data/lib/database_cleaner/neo4j/truncation.rb +0 -9
  78. data/lib/database_cleaner/null_strategy.rb +0 -20
  79. data/lib/database_cleaner/ohm/truncation.rb +0 -15
  80. data/lib/database_cleaner/redis/base.rb +0 -37
  81. data/lib/database_cleaner/redis/truncation.rb +0 -26
  82. data/lib/database_cleaner/sequel/base.rb +0 -22
  83. data/lib/database_cleaner/sequel/deletion.rb +0 -47
  84. data/lib/database_cleaner/sequel/transaction.rb +0 -40
  85. data/lib/database_cleaner/sequel/truncation.rb +0 -78
  86. data/spec/database_cleaner/active_record/base_spec.rb +0 -188
  87. data/spec/database_cleaner/active_record/transaction_spec.rb +0 -176
  88. data/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +0 -38
  89. data/spec/database_cleaner/active_record/truncation/mysql_spec.rb +0 -38
  90. data/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +0 -75
  91. data/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb +0 -40
  92. data/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +0 -40
  93. data/spec/database_cleaner/active_record/truncation_spec.rb +0 -180
  94. data/spec/database_cleaner/base_spec.rb +0 -617
  95. data/spec/database_cleaner/configuration_spec.rb +0 -345
  96. data/spec/database_cleaner/couch_potato/truncation_spec.rb +0 -41
  97. data/spec/database_cleaner/data_mapper/base_spec.rb +0 -30
  98. data/spec/database_cleaner/data_mapper/transaction_spec.rb +0 -23
  99. data/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +0 -41
  100. data/spec/database_cleaner/data_mapper/truncation_spec.rb +0 -11
  101. data/spec/database_cleaner/generic/base_spec.rb +0 -61
  102. data/spec/database_cleaner/generic/truncation_spec.rb +0 -118
  103. data/spec/database_cleaner/mongo/mongo_examples.rb +0 -26
  104. data/spec/database_cleaner/mongo/truncation_spec.rb +0 -72
  105. data/spec/database_cleaner/mongo_mapper/base_spec.rb +0 -33
  106. data/spec/database_cleaner/mongo_mapper/mongo_examples.rb +0 -8
  107. data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +0 -74
  108. data/spec/database_cleaner/moped/moped_examples.rb +0 -32
  109. data/spec/database_cleaner/moped/truncation_spec.rb +0 -80
  110. data/spec/database_cleaner/neo4j/base_spec.rb +0 -43
  111. data/spec/database_cleaner/neo4j/transaction_spec.rb +0 -25
  112. data/spec/database_cleaner/null_strategy_spec.rb +0 -28
  113. data/spec/database_cleaner/ohm/truncation_spec.rb +0 -70
  114. data/spec/database_cleaner/redis/base_spec.rb +0 -43
  115. data/spec/database_cleaner/redis/truncation_spec.rb +0 -63
  116. data/spec/database_cleaner/sequel/base_spec.rb +0 -32
  117. data/spec/database_cleaner/sequel/deletion_spec.rb +0 -58
  118. data/spec/database_cleaner/sequel/transaction_spec.rb +0 -21
  119. data/spec/database_cleaner/sequel/truncation/sqlite3_spec.rb +0 -0
  120. data/spec/database_cleaner/sequel/truncation_spec.rb +0 -182
  121. data/spec/database_cleaner/shared_strategy.rb +0 -15
  122. data/spec/rcov.opts +0 -1
  123. data/spec/spec_helper.rb +0 -21
  124. data/spec/support/active_record/database_setup.rb +0 -6
  125. data/spec/support/active_record/migrations/20150101010000_create_users.rb +0 -14
  126. data/spec/support/active_record/migrations/20150101020000_create_agents.rb +0 -14
  127. data/spec/support/active_record/mysql2_setup.rb +0 -39
  128. data/spec/support/active_record/mysql_setup.rb +0 -38
  129. data/spec/support/active_record/postgresql_setup.rb +0 -48
  130. data/spec/support/active_record/schema_setup.rb +0 -17
  131. data/spec/support/active_record/sqlite3_setup.rb +0 -40
  132. data/spec/support/data_mapper/schema_setup.rb +0 -15
  133. data/spec/support/data_mapper/sqlite3_setup.rb +0 -39
@@ -1,176 +0,0 @@
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) { double("connection") }
10
- let (:connection_2) { double("connection") }
11
- let (:connection_pool) { double("connection_pool")}
12
- before(:each) do
13
- ::ActiveRecord::Base.stub(:connection_pool).and_return(connection_pool)
14
- connection_pool.stub(:connections).and_return([connection])
15
- ::ActiveRecord::Base.stub(:connection).and_return(connection)
16
- end
17
-
18
- describe "#start" do
19
- [:begin_transaction, :begin_db_transaction].each do |begin_transaction_method|
20
- context "using #{begin_transaction_method}" do
21
- before do
22
- connection.stub(:transaction)
23
- connection.stub(begin_transaction_method)
24
- connection.stub(:respond_to?).with(:begin_transaction).and_return(:begin_transaction == begin_transaction_method)
25
- end
26
-
27
- it "should increment open transactions if possible" do
28
- connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true)
29
- connection.should_receive(:increment_open_transactions)
30
- Transaction.new.start
31
- end
32
-
33
- it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do
34
- connection.stub(:respond_to?).with(:increment_open_transactions).and_return(false)
35
- ::ActiveRecord::Base.should_receive(:increment_open_transactions)
36
- Transaction.new.start
37
- end
38
-
39
- it "should start a transaction" do
40
- connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true)
41
- connection.stub(:increment_open_transactions)
42
- connection.should_receive(begin_transaction_method)
43
- connection.should_receive(:transaction)
44
- Transaction.new.start
45
- end
46
- end
47
- end
48
- end
49
-
50
- describe "#clean" do
51
- context "manual accounting of transaction count" do
52
- it "should start a transaction" do
53
- connection.should_receive(:open_transactions).and_return(1)
54
-
55
- connection.stub(:decrement_open_transactions)
56
-
57
- connection.should_receive(:rollback_db_transaction)
58
- Transaction.new.clean
59
- end
60
-
61
- it "should decrement open transactions if possible" do
62
- connection.should_receive(:open_transactions).and_return(1)
63
-
64
- connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(true)
65
- connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
66
- connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
67
- connection.stub(:rollback_db_transaction)
68
-
69
- connection.should_receive(:decrement_open_transactions)
70
- Transaction.new.clean
71
- end
72
-
73
- it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
74
- connection.should_receive(:open_transactions).and_return(0)
75
-
76
- Transaction.new.clean
77
- end
78
-
79
- it "should decrement connection via ActiveRecord::Base if connection won't" do
80
- connection.should_receive(:open_transactions).and_return(1)
81
- connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false)
82
- connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
83
- connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
84
- connection.stub(:rollback_db_transaction)
85
-
86
- ::ActiveRecord::Base.should_receive(:decrement_open_transactions)
87
- Transaction.new.clean
88
- end
89
-
90
- it "should rollback open transactions in all connections" do
91
- connection_pool.stub(:connections).and_return([connection, connection_2])
92
-
93
- connection.should_receive(:open_transactions).and_return(1)
94
- connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false)
95
- connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
96
- connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
97
- connection.stub(:rollback_db_transaction)
98
-
99
- connection_2.should_receive(:open_transactions).and_return(1)
100
- connection_2.stub(:respond_to?).with(:decrement_open_transactions).and_return(false)
101
- connection_2.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
102
- connection_2.stub(:respond_to?).with(:rollback_transaction).and_return(false)
103
- connection_2.stub(:rollback_db_transaction)
104
-
105
- ::ActiveRecord::Base.should_receive(:decrement_open_transactions).twice
106
- Transaction.new.clean
107
- end
108
-
109
- it "should rollback open transactions in all connections with an open transaction" do
110
- connection_pool.stub(:connections).and_return([connection, connection_2])
111
-
112
- connection.should_receive(:open_transactions).and_return(1)
113
- connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false)
114
- connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
115
- connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
116
- connection.stub(:rollback_db_transaction)
117
-
118
- connection_2.should_receive(:open_transactions).and_return(0)
119
-
120
- ::ActiveRecord::Base.should_receive(:decrement_open_transactions).exactly(1).times
121
- Transaction.new.clean
122
- end
123
- end
124
-
125
- context "automatic accounting of transaction count (AR 4)" do
126
- before {stub_const("ActiveRecord::VERSION::MAJOR", 4) }
127
-
128
- it "should start a transaction" do
129
- connection.stub(:rollback_db_transaction)
130
- connection.should_receive(:open_transactions).and_return(1)
131
-
132
- connection.should_not_receive(:decrement_open_transactions)
133
- connection.should_receive(:rollback_transaction)
134
- Transaction.new.clean
135
- end
136
-
137
- it "should decrement open transactions if possible" do
138
- connection.stub(:rollback_transaction)
139
- connection.should_receive(:open_transactions).and_return(1)
140
-
141
- connection.should_not_receive(:decrement_open_transactions)
142
- Transaction.new.clean
143
- end
144
-
145
- it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
146
- connection.should_receive(:open_transactions).and_return(0)
147
-
148
- Transaction.new.clean
149
- end
150
-
151
- it "should decrement connection via ActiveRecord::Base if connection won't" do
152
- connection.should_receive(:open_transactions).and_return(1)
153
- connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false)
154
- connection.stub(:respond_to?).with(:rollback_transaction).and_return(true)
155
- connection.stub(:rollback_transaction)
156
-
157
- ::ActiveRecord::Base.should_not_receive(:decrement_open_transactions)
158
- Transaction.new.clean
159
- end
160
- end
161
- end
162
-
163
- describe "#connection_maintains_transaction_count?" do
164
- it "should return true if the major active record version is < 4" do
165
- stub_const("ActiveRecord::VERSION::MAJOR", 3)
166
- Transaction.new.connection_maintains_transaction_count?.should be_true
167
- end
168
- it "should return false if the major active record version is > 3" do
169
- stub_const("ActiveRecord::VERSION::MAJOR", 4)
170
- Transaction.new.connection_maintains_transaction_count?.should be_false
171
- end
172
- end
173
-
174
- end
175
- end
176
- end
@@ -1,38 +0,0 @@
1
- require 'spec_helper'
2
- require 'active_record'
3
- require 'support/active_record/mysql2_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_mysql2_setup }
11
-
12
- let(:connection) { active_record_mysql2_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_mysql2_connection }
34
- end
35
- end
36
- end
37
- end
38
-
@@ -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