/
transaction_spec.rb
79 lines (60 loc) · 2.8 KB
/
transaction_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require File.dirname(__FILE__) + '/../../spec_helper'
require 'database_cleaner/active_record/transaction'
require 'active_record'
module DatabaseCleaner
module ActiveRecord
describe Transaction do
let (:connection) { mock("connection") }
before(:each) do
::ActiveRecord::Base.stub!(:connection).and_return(connection)
end
describe "#start" do
it "should increment open transactions if possible" do
connection.stub!(:respond_to?).with(:increment_open_transactions).and_return(true)
connection.stub!(:begin_db_transaction)
connection.should_receive(:increment_open_transactions)
Transaction.new.start
end
it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do
connection.stub!(:respond_to?).with(:increment_open_transactions).and_return(false)
connection.stub!(:begin_db_transaction)
::ActiveRecord::Base.should_receive(:increment_open_transactions)
Transaction.new.start
end
it "should start a transaction" do
connection.stub!(:increment_open_transactions)
connection.should_receive(:begin_db_transaction)
Transaction.new.start
end
end
describe "#clean" do
it "should start a transaction" do
connection.should_receive(:open_transactions).and_return(1)
connection.stub!(:decrement_open_transactions)
connection.should_receive(:rollback_db_transaction)
Transaction.new.clean
end
it "should decrement open transactions if possible" do
connection.should_receive(:open_transactions).and_return(1)
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
connection.stub!(:rollback_db_transaction)
connection.should_receive(:decrement_open_transactions)
Transaction.new.clean
end
it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
connection.should_receive(:open_transactions).and_return(0)
Transaction.new.clean
end
it "should decrement connection via ActiveRecord::Base if connection won't" do
connection.should_receive(:open_transactions).and_return(1)
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(false)
connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
connection.stub!(:rollback_db_transaction)
::ActiveRecord::Base.should_receive(:decrement_open_transactions)
Transaction.new.clean
end
end
end
end
end