File: transaction_spec.rb

package info (click to toggle)
ruby-database-cleaner 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 640 kB
  • ctags: 462
  • sloc: ruby: 3,895; makefile: 9; sh: 4
file content (138 lines) | stat: -rw-r--r-- 5,809 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require File.dirname(__FILE__) + '/../../spec_helper'
require 'database_cleaner/active_record/transaction'
require 'active_record'

module DatabaseCleaner
  module ActiveRecord

    describe Transaction do
      let (:connection) { double("connection") }
      before(:each) do
        ::ActiveRecord::Base.stub(:connection).and_return(connection)
      end

      describe "#start" do
        [:begin_transaction, :begin_db_transaction].each do |begin_transaction_method|
          context "using #{begin_transaction_method}" do
            before do
              connection.stub(:transaction)
              connection.stub(begin_transaction_method)
              connection.stub(:respond_to?).with(:begin_transaction).and_return(:begin_transaction == begin_transaction_method)
            end

            it "should increment open transactions if possible" do
              connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true)
              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)
              ::ActiveRecord::Base.should_receive(:increment_open_transactions)
              Transaction.new.start
            end

            it "should start a transaction" do
              connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true)
              connection.stub(:increment_open_transactions)
              connection.should_receive(begin_transaction_method)
              connection.should_receive(:transaction)
              Transaction.new.start
            end
          end
        end
      end

      describe "#clean" do
        context "manual accounting of transaction count" 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, true).and_return(false)
            connection.stub(:respond_to?).with(:rollback_transaction).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, true).and_return(false)
            connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
            connection.stub(:rollback_db_transaction)

            ::ActiveRecord::Base.should_receive(:decrement_open_transactions)
            Transaction.new.clean
          end
        end

        context "automatic accounting of transaction count (AR 4)" do
          before {stub_const("ActiveRecord::VERSION::MAJOR", 4) }

          it "should start a transaction" do
            connection.stub(:rollback_db_transaction)
            connection.should_receive(:open_transactions).and_return(1)

            connection.should_not_receive(:decrement_open_transactions)
            connection.should_receive(:rollback_transaction)
            Transaction.new.clean
          end

          it "should decrement open transactions if possible" do
            connection.stub(:rollback_transaction)
            connection.should_receive(:open_transactions).and_return(1)

            connection.should_not_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(:rollback_transaction_records, true).and_return(false)
            connection.stub(:respond_to?).with(:rollback_transaction).and_return(true)
            connection.stub(:rollback_transaction)

            ::ActiveRecord::Base.should_not_receive(:decrement_open_transactions)
            Transaction.new.clean
          end
        end
      end

      describe "#connection_maintains_transaction_count?" do
        it "should return true if the major active record version is < 4" do
          stub_const("ActiveRecord::VERSION::MAJOR", 3)
          Transaction.new.connection_maintains_transaction_count?.should be_true
        end
        it "should return false if the major active record version is > 3" do
          stub_const("ActiveRecord::VERSION::MAJOR", 4)
          Transaction.new.connection_maintains_transaction_count?.should be_false
        end
      end

    end
  end
end