Skip to content

Commit c39366b

Browse files
get rspec tests running in ar adapter gem.
1 parent ed0e38f commit c39366b

35 files changed

+643
-124
lines changed

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ bundled_gems/
77
vendor/
88
examples/db/*.db
99
examples/config/database.yml
10-
db/config.yml
11-
db/test.sqlite3
10+
spec/support/config.yml
11+
tmp/*
12+
!tmp/.keep
1213
.rbenv-version
1314
.rvmrc
1415
.ruby-version

CONTRIBUTE.markdown

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ upstream:
1111
## 2. Make sure the tests run fine
1212

1313
- `bundle install`
14-
- Copy `db/sample.config.yml` to `db/config.yml` and edit it
15-
- Make sure to create the databases specified in `db/config.yml`
14+
- Copy `spec/support/sample.config.yml` to `spec/support/config.yml` and edit it
1615
- Run the tests with `bundle exec rspec`
1716

1817
Note that if you don't have all the supported databases installed and running,

Gemfile.lock

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
PATH
2-
remote: adapters
2+
remote: .
33
specs:
4-
database_cleaner-active_record (0.1.0)
4+
database_cleaner (1.8.0)
55

66
PATH
7-
remote: .
7+
remote: adapters
88
specs:
9-
database_cleaner (1.7.0)
9+
database_cleaner-active_record (0.1.0)
10+
activerecord
11+
database_cleaner (~> 1.8.0)
1012

1113
GEM
1214
remote: https://rubygems.org/

adapters/database_cleaner-active_record/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
/doc/
66
/pkg/
77
/spec/reports/
8+
/spec/support/config.yml
89
/tmp/
10+
!/tmp/.keep
911

1012
# rspec failure tracking
1113
.rspec_status

adapters/database_cleaner-active_record/lib/database_cleaner/active_record.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'database_cleaner/active_record/version'
12
require 'database_cleaner'
23
require 'database_cleaner/active_record/deletion'
34
require 'database_cleaner/active_record/transaction'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
require 'active_record'
2+
require 'database_cleaner/active_record/base'
3+
require 'database_cleaner/spec'
4+
5+
class FakeModel
6+
def self.connection
7+
:fake_connection
8+
end
9+
end
10+
11+
RSpec.describe DatabaseCleaner::ActiveRecord do
12+
it { is_expected.to respond_to(:available_strategies) }
13+
14+
describe "config_file_location" do
15+
after do
16+
# prevent global state leakage
17+
DatabaseCleaner::ActiveRecord.config_file_location=nil
18+
DatabaseCleaner.app_root = nil
19+
end
20+
21+
it "should default to \#{DatabaseCleaner.app_root}/config/database.yml" do
22+
DatabaseCleaner::ActiveRecord.config_file_location = nil
23+
DatabaseCleaner.app_root = "/path/to"
24+
expect(DatabaseCleaner::ActiveRecord.config_file_location).to eq '/path/to/config/database.yml'
25+
end
26+
end
27+
end
28+
29+
module DatabaseCleaner
30+
module ActiveRecord
31+
class ExampleStrategy
32+
include DatabaseCleaner::ActiveRecord::Base
33+
end
34+
35+
RSpec.describe ExampleStrategy do
36+
let(:config_location) { '/path/to/config/database.yml' }
37+
38+
around do |example|
39+
DatabaseCleaner::ActiveRecord.config_file_location = config_location
40+
example.run
41+
DatabaseCleaner::ActiveRecord.config_file_location = nil
42+
end
43+
44+
it_should_behave_like "a generic strategy"
45+
46+
describe "db" do
47+
it "should store my desired db" do
48+
subject.db = :my_db
49+
expect(subject.db).to eq :my_db
50+
end
51+
52+
it "should default to :default" do
53+
expect(subject.db).to eq :default
54+
end
55+
end
56+
57+
describe "db=" do
58+
let(:config_location) { "spec/support/example.database.yml" }
59+
60+
it "should process erb in the config" do
61+
subject.db = :my_db
62+
expect(subject.connection_hash).to eq({ "database" => "one" })
63+
end
64+
65+
context 'when config file differs from established ActiveRecord configuration' do
66+
before do
67+
allow(::ActiveRecord::Base).to receive(:configurations).and_return({ "my_db" => { "database" => "two"} })
68+
end
69+
70+
it 'uses the ActiveRecord configuration' do
71+
subject.db = :my_db
72+
expect(subject.connection_hash).to eq({ "database" => "two"})
73+
end
74+
end
75+
76+
context 'when config file agrees with ActiveRecord configuration' do
77+
before do
78+
allow(::ActiveRecord::Base).to receive(:configurations).and_return({ "my_db" => { "database" => "one"} })
79+
end
80+
81+
it 'uses the config file' do
82+
subject.db = :my_db
83+
expect(subject.connection_hash).to eq({ "database" => "one"})
84+
end
85+
end
86+
87+
context 'when ::ActiveRecord::Base.configurations nil' do
88+
before do
89+
allow(::ActiveRecord::Base).to receive(:configurations).and_return(nil)
90+
end
91+
92+
it 'uses the config file' do
93+
subject.db = :my_db
94+
expect(subject.connection_hash).to eq({ "database" => "one"})
95+
end
96+
end
97+
98+
context 'when ::ActiveRecord::Base.configurations empty' do
99+
before do
100+
allow(::ActiveRecord::Base).to receive(:configurations).and_return({})
101+
end
102+
103+
it 'uses the config file' do
104+
subject.db = :my_db
105+
expect(subject.connection_hash).to eq({ "database" => "one"})
106+
end
107+
end
108+
109+
context 'when config file is not available' do
110+
before do
111+
allow(File).to receive(:file?).with(config_location).and_return(false)
112+
end
113+
114+
it "should skip config" do
115+
subject.db = :my_db
116+
expect(subject.connection_hash).not_to be
117+
end
118+
end
119+
120+
it "skips the file when the model is set" do
121+
subject.db = FakeModel
122+
expect(subject.connection_hash).not_to be
123+
end
124+
125+
it "skips the file when the db is set to :default" do
126+
# to avoid https://github.com/bmabey/database_cleaner/issues/72
127+
subject.db = :default
128+
expect(subject.connection_hash).not_to be
129+
end
130+
end
131+
132+
describe "connection_class" do
133+
it "should default to ActiveRecord::Base" do
134+
expect(subject.connection_class).to eq ::ActiveRecord::Base
135+
end
136+
137+
context "with database models" do
138+
context "connection_hash is set" do
139+
it "reuses the model's connection" do
140+
subject.connection_hash = {}
141+
subject.db = FakeModel
142+
expect(subject.connection_class).to eq FakeModel
143+
end
144+
end
145+
146+
context "connection_hash is not set" do
147+
it "reuses the model's connection" do
148+
subject.db = FakeModel
149+
expect(subject.connection_class).to eq FakeModel
150+
end
151+
end
152+
end
153+
154+
context "when connection_hash is set" do
155+
let(:hash) { {} }
156+
before { subject.connection_hash = hash }
157+
158+
it "establishes a connection with it" do
159+
expect(::ActiveRecord::Base).to receive(:establish_connection).with(hash)
160+
expect(subject.connection_class).to eq ::ActiveRecord::Base
161+
end
162+
end
163+
end
164+
end
165+
end
166+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
require 'database_cleaner/active_record/transaction'
2+
require 'active_record'
3+
4+
module DatabaseCleaner
5+
module ActiveRecord
6+
7+
RSpec.describe Transaction do
8+
let(:connection) { double("connection") }
9+
let(:connection_2) { double("connection_2") }
10+
let(:connection_pool) { double("connection_pool") }
11+
12+
before do
13+
allow(::ActiveRecord::Base).to receive(:connection_pool).and_return(connection_pool)
14+
allow(connection_pool).to receive(:connections).and_return([connection])
15+
allow(::ActiveRecord::Base).to receive(: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+
allow(connection).to receive(:transaction)
23+
allow(connection).to receive(begin_transaction_method)
24+
end
25+
26+
it "should increment open transactions if possible" do
27+
expect(connection).to receive(:increment_open_transactions)
28+
subject.start
29+
end
30+
31+
it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do
32+
expect(::ActiveRecord::Base).to receive(:increment_open_transactions)
33+
subject.start
34+
end
35+
36+
it "should start a transaction" do
37+
allow(connection).to receive(:increment_open_transactions)
38+
expect(connection).to receive(begin_transaction_method)
39+
expect(connection).to receive(:transaction)
40+
subject.start
41+
end
42+
end
43+
end
44+
end
45+
46+
describe "#clean" do
47+
context "manual accounting of transaction count" do
48+
it "should start a transaction" do
49+
expect(connection).to receive(:open_transactions).and_return(1)
50+
51+
allow(connection).to receive(:decrement_open_transactions)
52+
53+
expect(connection).to receive(:rollback_db_transaction)
54+
subject.clean
55+
end
56+
57+
it "should decrement open transactions if possible" do
58+
expect(connection).to receive(:open_transactions).and_return(1)
59+
60+
allow(connection).to receive(:rollback_db_transaction)
61+
62+
expect(connection).to receive(:decrement_open_transactions)
63+
subject.clean
64+
end
65+
66+
it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
67+
expect(connection).to receive(:open_transactions).and_return(0)
68+
69+
subject.clean
70+
end
71+
72+
it "should decrement connection via ActiveRecord::Base if connection won't" do
73+
expect(connection).to receive(:open_transactions).and_return(1)
74+
allow(connection).to receive(:rollback_db_transaction)
75+
76+
expect(::ActiveRecord::Base).to receive(:decrement_open_transactions)
77+
subject.clean
78+
end
79+
80+
it "should rollback open transactions in all connections" do
81+
allow(connection_pool).to receive(:connections).and_return([connection, connection_2])
82+
83+
expect(connection).to receive(:open_transactions).and_return(1)
84+
allow(connection).to receive(:rollback_db_transaction)
85+
86+
expect(connection_2).to receive(:open_transactions).and_return(1)
87+
allow(connection_2).to receive(:rollback_db_transaction)
88+
89+
expect(::ActiveRecord::Base).to receive(:decrement_open_transactions).twice
90+
subject.clean
91+
end
92+
93+
it "should rollback open transactions in all connections with an open transaction" do
94+
allow(connection_pool).to receive(:connections).and_return([connection, connection_2])
95+
96+
expect(connection).to receive(:open_transactions).and_return(1)
97+
allow(connection).to receive(:rollback_db_transaction)
98+
99+
expect(connection_2).to receive(:open_transactions).and_return(0)
100+
101+
expect(::ActiveRecord::Base).to receive(:decrement_open_transactions).exactly(1).times
102+
subject.clean
103+
end
104+
end
105+
106+
context "automatic accounting of transaction count (AR 4)" do
107+
before { stub_const("ActiveRecord::VERSION::MAJOR", 4) }
108+
109+
it "should start a transaction" do
110+
allow(connection).to receive(:rollback_db_transaction)
111+
expect(connection).to receive(:open_transactions).and_return(1)
112+
113+
expect(connection).not_to receive(:decrement_open_transactions)
114+
expect(connection).to receive(:rollback_transaction)
115+
subject.clean
116+
end
117+
118+
it "should decrement open transactions if possible" do
119+
allow(connection).to receive(:rollback_transaction)
120+
expect(connection).to receive(:open_transactions).and_return(1)
121+
122+
expect(connection).not_to receive(:decrement_open_transactions)
123+
subject.clean
124+
end
125+
126+
it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
127+
expect(connection).to receive(:open_transactions).and_return(0)
128+
129+
subject.clean
130+
end
131+
132+
it "should decrement connection via ActiveRecord::Base if connection won't" do
133+
expect(connection).to receive(:open_transactions).and_return(1)
134+
allow(connection).to receive(:rollback_transaction)
135+
136+
expect(::ActiveRecord::Base).not_to receive(:decrement_open_transactions)
137+
subject.clean
138+
end
139+
end
140+
end
141+
142+
describe "#connection_maintains_transaction_count?" do
143+
it "should return true if the major active record version is < 4" do
144+
stub_const("ActiveRecord::VERSION::MAJOR", 3)
145+
expect(subject.connection_maintains_transaction_count?).to be_truthy
146+
end
147+
148+
it "should return false if the major active record version is > 3" do
149+
stub_const("ActiveRecord::VERSION::MAJOR", 4)
150+
expect(subject.connection_maintains_transaction_count?).to be_falsey
151+
end
152+
end
153+
end
154+
end
155+
end

0 commit comments

Comments
 (0)