sequel_test_after_commit 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e2b6c6045f7a7aea1b9b2ebf1a967bc23e1621d
4
+ data.tar.gz: 8c3ef43170b3450e21aa69dbbdaee940a0a2d92e
5
+ SHA512:
6
+ metadata.gz: b9cb0c7ed112bb34412470ea1e6eb3958571c5e1bcaaf2f3b028a79f8f7a1ee856a0789fde65e763f69ece04dcfbe112e5c646352b4f84bfe3ba27a9d8bfa726
7
+ data.tar.gz: a40671acafeb78ee72ffee317ad030080e7814f49ea6753739027ad5117d1a7d76d8e620238d2e7637fb3d087ebaea040fe8627948231321c630730187d356bd
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sequel_test_after_commit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Chris Hanks
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # sequel_test_after_commit
2
+
3
+ A gem to make transactional tests work with after_commit and after_rollback hooks in Sequel.
4
+
5
+ ```ruby
6
+ DB = Sequel.connect(...)
7
+ DB.extension :test_after_commit
8
+ ```
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'sequel_test_after_commit'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install sequel_test_after_commit
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( https://github.com/[my-github-username]/sequel_test_after_commit/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,57 @@
1
+ module Sequel
2
+ module TestAfterCommit
3
+ def remove_transaction(conn, committed)
4
+ level = savepoint_level(conn)
5
+
6
+ if h = _trans(conn)
7
+ if after_commit = h[:after_commit]
8
+ if hooks = after_commit.delete(level)
9
+ hooks.each(&:call) if committed
10
+ end
11
+ end
12
+
13
+ if after_rollback = h[:after_rollback]
14
+ if hooks = after_rollback.delete(level)
15
+ hooks.each(&:call) unless committed
16
+ end
17
+ end
18
+ end
19
+
20
+ super
21
+ end
22
+
23
+ def after_commit(opts=OPTS, &block)
24
+ raise Error, "must provide block to after_commit" unless block
25
+ synchronize(opts[:server]) do |conn|
26
+ if h = _trans(conn)
27
+ raise Error, "cannot call after_commit in a prepared transaction" if h[:prepare]
28
+ level = savepoint_level(conn)
29
+ hooks = h[:after_commit] ||= {}
30
+ hooks[level] ||= []
31
+ hooks[level] << block
32
+ else
33
+ yield
34
+ end
35
+ end
36
+ end
37
+
38
+ def after_rollback(opts=OPTS, &block)
39
+ raise Error, "must provide block to after_rollback" unless block
40
+ synchronize(opts[:server]) do |conn|
41
+ if h = _trans(conn)
42
+ raise Error, "cannot call after_rollback in a prepared transaction" if h[:prepare]
43
+ level = savepoint_level(conn)
44
+ hooks = h[:after_rollback] ||= {}
45
+ hooks[level] ||= []
46
+ hooks[level] << block
47
+ end
48
+ end
49
+ end
50
+
51
+ # The methods that usually trigger the callbacks become no-ops.
52
+ def after_transaction_commit(conn); end
53
+ def after_transaction_rollback(conn); end
54
+ end
55
+
56
+ Database.register_extension(:test_after_commit) { |db| (class << db; self; end).send(:prepend, TestAfterCommit) }
57
+ end
@@ -0,0 +1,5 @@
1
+ module Sequel
2
+ module TestAfterCommit
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require 'sequel'
2
+ require 'sequel/extensions/test_after_commit'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sequel_test_after_commit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sequel_test_after_commit'
8
+ spec.version = Sequel::TestAfterCommit::VERSION
9
+ spec.authors = ["Chris Hanks"]
10
+ spec.email = ["christopher.m.hanks@gmail.com"]
11
+ spec.summary = %q{Support for after_commit with savepoints in Sequel.}
12
+ spec.description = %q{Makes after_commit and after_rollback hooks work with Sequel's savepoint support.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sequel"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1 @@
1
+ require 'sequel_test_after_commit'
@@ -0,0 +1,173 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sequel::TestAfterCommit do
4
+ before do
5
+ @db = Sequel.mock
6
+ @db.extension :test_after_commit
7
+ @callbacks = []
8
+ end
9
+
10
+ it "should still run after_commit callbacks immediately when outside a transaction" do
11
+ @db.after_commit { @callbacks << :after_commit }
12
+ @callbacks.should == [:after_commit]
13
+ end
14
+
15
+ it "should still not run after_rollback hooks at all when outside a transaction" do
16
+ @db.after_rollback { @callbacks << :after_rollback }
17
+ @callbacks.should == []
18
+ end
19
+
20
+ it "should still not run hooks until after the transaction finishes" do
21
+ @db.transaction do
22
+ @db.after_commit { @db.execute "after_commit" }
23
+ @db.after_rollback { @db.execute "after_rollback" }
24
+ end
25
+
26
+ @db.sqls.should == ["BEGIN", "COMMIT", "after_commit"]
27
+ @db.sqls.clear
28
+
29
+ @db.transaction do
30
+ @db.after_commit { @db.execute "after_commit" }
31
+ @db.after_rollback { @db.execute "after_rollback" }
32
+ raise Sequel::Rollback
33
+ end
34
+
35
+ @db.sqls.should == ["BEGIN", "ROLLBACK", "after_rollback"]
36
+ end
37
+
38
+ it "should not interfere with the usual operation of the hooks when a transaction commits" do
39
+ @db.transaction do
40
+ @db.after_commit { @callbacks << :first_after_commit }
41
+ @db.after_commit { @callbacks << :second_after_commit }
42
+ @db.after_rollback { @callbacks << :first_after_rollback }
43
+ @db.after_rollback { @callbacks << :second_after_rollback }
44
+ @callbacks.should == []
45
+ end
46
+
47
+ @callbacks.should == [:first_after_commit, :second_after_commit]
48
+ end
49
+
50
+ it "should not interfere with the usual operation of the hooks when a transaction rolls back" do
51
+ @db.transaction do
52
+ @db.after_commit { @callbacks << :first_after_commit }
53
+ @db.after_commit { @callbacks << :second_after_commit }
54
+ @db.after_rollback { @callbacks << :first_after_rollback }
55
+ @db.after_rollback { @callbacks << :second_after_rollback }
56
+ @callbacks.should == []
57
+ raise Sequel::Rollback
58
+ end
59
+
60
+ @callbacks.should == [:first_after_rollback, :second_after_rollback]
61
+ end
62
+
63
+ it "should run after_commit callbacks after the subtransaction in which they are declared commits" do
64
+ @db.transaction do
65
+ @db.after_commit { @callbacks << :first_after_commit }
66
+ @db.after_rollback { @callbacks << :first_after_rollback }
67
+ @callbacks.should == []
68
+
69
+ @db.transaction :savepoint => true do
70
+ @db.after_commit { @callbacks << :second_after_commit }
71
+ @db.after_rollback { @callbacks << :second_after_rollback }
72
+ @callbacks.should == []
73
+ end
74
+
75
+ @callbacks.should == [:second_after_commit]
76
+
77
+ @db.transaction :savepoint => true do
78
+ @db.after_commit { @callbacks << :third_after_commit }
79
+ @db.after_rollback { @callbacks << :third_after_rollback }
80
+ @callbacks.should == [:second_after_commit]
81
+ end
82
+
83
+ @callbacks.should == [:second_after_commit, :third_after_commit]
84
+ end
85
+
86
+ @callbacks.should == [:second_after_commit, :third_after_commit, :first_after_commit]
87
+ end
88
+
89
+ it "should run after_rollback callbacks after the subtransaction in which they are declared rolls back" do
90
+ @db.transaction do
91
+ @db.after_commit { @callbacks << :first_after_commit }
92
+ @db.after_rollback { @callbacks << :first_after_rollback }
93
+ @callbacks.should == []
94
+
95
+ @db.transaction :savepoint => true do
96
+ @db.after_commit { @callbacks << :second_after_commit }
97
+ @db.after_rollback { @callbacks << :second_after_rollback }
98
+ @callbacks.should == []
99
+ raise Sequel::Rollback
100
+ end
101
+
102
+ @callbacks.should == [:second_after_rollback]
103
+
104
+ @db.transaction :savepoint => true do
105
+ @db.after_commit { @callbacks << :third_after_commit }
106
+ @db.after_rollback { @callbacks << :third_after_rollback }
107
+ @callbacks.should == [:second_after_rollback]
108
+ raise Sequel::Rollback
109
+ end
110
+
111
+ @callbacks.should == [:second_after_rollback, :third_after_rollback]
112
+ raise Sequel::Rollback
113
+ end
114
+
115
+ @callbacks.should == [:second_after_rollback, :third_after_rollback, :first_after_rollback]
116
+ end
117
+
118
+ it "should run after_commit hooks correctly with auto_savepoint" do
119
+ @db.transaction :auto_savepoint => true do
120
+ @db.after_commit { @callbacks << :first_after_commit }
121
+ @db.after_rollback { @callbacks << :first_after_rollback }
122
+ @callbacks.should == []
123
+
124
+ @db.transaction do
125
+ @db.after_commit { @callbacks << :second_after_commit }
126
+ @db.after_rollback { @callbacks << :second_after_rollback }
127
+ @callbacks.should == []
128
+ end
129
+
130
+ @callbacks.should == [:second_after_commit]
131
+ end
132
+
133
+ @callbacks.should == [:second_after_commit, :first_after_commit]
134
+ end
135
+
136
+ it "should run after_rollback hooks correctly with auto_savepoint" do
137
+ @db.transaction :auto_savepoint => true do
138
+ @db.after_commit { @callbacks << :first_after_commit }
139
+ @db.after_rollback { @callbacks << :first_after_rollback }
140
+ @callbacks.should == []
141
+
142
+ @db.transaction do
143
+ @db.after_commit { @callbacks << :second_after_commit }
144
+ @db.after_rollback { @callbacks << :second_after_rollback }
145
+ @callbacks.should == []
146
+ raise Sequel::Rollback
147
+ end
148
+
149
+ @callbacks.should == [:second_after_rollback]
150
+ raise Sequel::Rollback
151
+ end
152
+
153
+ @callbacks.should == [:second_after_rollback, :first_after_rollback]
154
+ end
155
+
156
+ it "doesn't affect the behavior of databases not extended with it" do
157
+ @other = Sequel.mock
158
+
159
+ @other.transaction do
160
+ @other.after_commit { @callbacks << :first_after_commit }
161
+ @callbacks.should == []
162
+
163
+ @other.transaction :savepoint => true do
164
+ @other.after_commit { @callbacks << :second_after_commit }
165
+ @callbacks.should == []
166
+ end
167
+
168
+ @callbacks.should == []
169
+ end
170
+
171
+ @callbacks.should == [:first_after_commit, :second_after_commit]
172
+ end
173
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel_test_after_commit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Chris Hanks
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Makes after_commit and after_rollback hooks work with Sequel's savepoint
56
+ support.
57
+ email:
58
+ - christopher.m.hanks@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/sequel/extensions/test_after_commit.rb
69
+ - lib/sequel_test_after_commit.rb
70
+ - lib/sequel_test_after_commit/version.rb
71
+ - sequel_test_after_commit.gemspec
72
+ - spec/spec_helper.rb
73
+ - spec/test_after_commit_spec.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Support for after_commit with savepoints in Sequel.
98
+ test_files:
99
+ - spec/spec_helper.rb
100
+ - spec/test_after_commit_spec.rb