test_after_commit 0.2.5 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +22 -0
- data/Readme.md +27 -0
- data/lib/test_after_commit.rb +13 -65
- data/lib/test_after_commit/database_statements.rb +45 -0
- data/lib/test_after_commit/version.rb +1 -1
- data/lib/test_after_commit/with_transaction_state.rb +10 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 476b2d55df60abf6d74c93b6b708bb1fb84fe8eb
|
4
|
+
data.tar.gz: 3124e64478bec9c3019f7ef86e872c3a90b725b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de5dbe1aad7eb2dfb337a019c30a1a30045fefcda15062805bff7c9e432f276d0544726d76e1c7c6173c8af35a67d50131dad1ed83d16d2fe1a43356037c2110
|
7
|
+
data.tar.gz: 098b7b282ceffaf27fd0829ffd6e6979286430537b2b9ebf628d46f796210c9a8b009902c66042f02dcdf2b8c24aef693ab05fd65a5fba63adbb5a81f0757bb9
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Michael Grosser <michael@grosser.it>
|
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
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
Make after_commit callbacks fire in tests for Rails 3+ with transactional_fixtures = true.
|
2
2
|
|
3
|
+
**Deprecation** this is no longer needed on rails 5.0+ https://github.com/rails/rails/pull/18458
|
4
|
+
|
3
5
|
Install
|
4
6
|
=======
|
5
7
|
|
@@ -30,6 +32,26 @@ it "sets $foo on commit" do
|
|
30
32
|
end
|
31
33
|
```
|
32
34
|
|
35
|
+
### Temporary disable after commit hooks
|
36
|
+
|
37
|
+
In your test_helper, you can specify the default
|
38
|
+
|
39
|
+
```
|
40
|
+
TestAfterCommit.enabled = true
|
41
|
+
```
|
42
|
+
|
43
|
+
Then use blocks in your tests to change the behavior:
|
44
|
+
|
45
|
+
```
|
46
|
+
TestAfterCommit.with_commits(true) do
|
47
|
+
my_tests
|
48
|
+
end
|
49
|
+
|
50
|
+
TestAfterCommit.with_commits(false) do
|
51
|
+
my_tests
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
33
55
|
TIPS
|
34
56
|
====
|
35
57
|
- hooks do not re-raise errors (with or without this gem) use [after_commit_exception_notification](https://github.com/grosser/after_commit_exception_notification)
|
@@ -44,6 +66,11 @@ Inspired by https://gist.github.com/1305285
|
|
44
66
|
- [emirose](https://github.com/emirose)
|
45
67
|
- [Brad Gessler](https://github.com/bradgessler)
|
46
68
|
- [Rohan McGovern](https://github.com/rohanpm)
|
69
|
+
- [lsylvester](https://github.com/lsylvester)
|
70
|
+
- [Tony Novak](https://github.com/afn)
|
71
|
+
- [Brian Palmer](https://github.com/codekitchen)
|
72
|
+
- [Oleg Dashevskii](https://github.com/be9)
|
73
|
+
- [Jonathan Spies](https://github.com/jspies)
|
47
74
|
|
48
75
|
[Michael Grosser](http://grosser.it)<br/>
|
49
76
|
michael@grosser.it<br/>
|
data/lib/test_after_commit.rb
CHANGED
@@ -1,76 +1,24 @@
|
|
1
1
|
require 'test_after_commit/version'
|
2
2
|
|
3
|
-
|
3
|
+
if ActiveRecord::VERSION::MAJOR >= 4
|
4
|
+
require 'test_after_commit/with_transaction_state'
|
5
|
+
ActiveRecord::Base.send(:prepend, TestAfterCommit::WithTransactionState)
|
4
6
|
end
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
@test_open_transactions ||= 0
|
9
|
-
transaction_without_transactional_fixtures(*args) do
|
10
|
-
begin
|
11
|
-
@test_open_transactions += 1
|
12
|
-
result = yield
|
13
|
-
rescue Exception => e
|
14
|
-
rolled_back = true
|
15
|
-
raise e
|
16
|
-
ensure
|
17
|
-
if @test_open_transactions == 1 && !rolled_back
|
18
|
-
test_commit_records
|
19
|
-
end
|
20
|
-
@test_open_transactions -= 1
|
21
|
-
result
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
alias_method_chain :transaction, :transactional_fixtures
|
26
|
-
|
27
|
-
def test_commit_records
|
28
|
-
if ActiveRecord::VERSION::MAJOR == 3
|
29
|
-
commit_transaction_records(false)
|
30
|
-
else
|
31
|
-
@transaction.commit_records
|
32
|
-
@transaction.records.clear # prevent duplicate .commit!
|
33
|
-
@transaction.instance_variable_get(:@state).set_state(nil)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
if ActiveRecord::VERSION::MAJOR == 3
|
38
|
-
# The @_current_transaction_records is a stack of arrays, each one
|
39
|
-
# containing the records associated with the corresponding transaction
|
40
|
-
# in the transaction stack. This is used by the
|
41
|
-
# `rollback_transaction_records` method (to only send a rollback hook to
|
42
|
-
# models attached to the transaction being rolled back) but is usually
|
43
|
-
# ignored by the `commit_transaction_records` method. Here we
|
44
|
-
# monkey-patch it to temporarily replace the array with only the records
|
45
|
-
# for the top-of-stack transaction, so the real
|
46
|
-
# `commit_transaction_records` method only sends callbacks to those.
|
47
|
-
#
|
48
|
-
def commit_transaction_records_with_transactional_fixtures(commit = true)
|
49
|
-
return commit_transaction_records_without_transactional_fixtures if commit
|
8
|
+
require 'test_after_commit/database_statements'
|
9
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:prepend, TestAfterCommit::DatabaseStatements)
|
50
10
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
alias_method_chain :commit_transaction_records, :transactional_fixtures
|
11
|
+
module TestAfterCommit
|
12
|
+
@enabled = true
|
13
|
+
class << self
|
14
|
+
attr_accessor :enabled
|
57
15
|
|
58
|
-
def
|
59
|
-
|
16
|
+
def with_commits(value = true)
|
17
|
+
old = enabled
|
18
|
+
self.enabled = value
|
60
19
|
yield
|
61
20
|
ensure
|
62
|
-
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
if ActiveRecord::VERSION::MAJOR >= 4
|
68
|
-
# disable parts of the sync code that starts looping
|
69
|
-
ActiveRecord::Base.class_eval do
|
70
|
-
alias_method :sync_with_transaction_state_with_state, :sync_with_transaction_state
|
71
|
-
def sync_with_transaction_state
|
72
|
-
@reflects_state[0] = true
|
73
|
-
sync_with_transaction_state_with_state
|
21
|
+
self.enabled = old
|
74
22
|
end
|
75
23
|
end
|
76
24
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module TestAfterCommit::DatabaseStatements
|
2
|
+
def transaction(*)
|
3
|
+
@test_open_transactions ||= 0
|
4
|
+
|
5
|
+
super do
|
6
|
+
begin
|
7
|
+
@test_open_transactions += 1
|
8
|
+
if ActiveRecord::VERSION::MAJOR == 3
|
9
|
+
@_current_transaction_records.push([]) if @_current_transaction_records.empty?
|
10
|
+
end
|
11
|
+
result = yield
|
12
|
+
rescue Exception
|
13
|
+
rolled_back = true
|
14
|
+
raise
|
15
|
+
ensure
|
16
|
+
begin
|
17
|
+
@test_open_transactions -= 1
|
18
|
+
if TestAfterCommit.enabled && @test_open_transactions == 0 && !rolled_back
|
19
|
+
test_commit_records
|
20
|
+
end
|
21
|
+
ensure
|
22
|
+
result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_commit_records
|
29
|
+
if ActiveRecord::VERSION::MAJOR == 3
|
30
|
+
commit_transaction_records
|
31
|
+
else
|
32
|
+
# To avoid an infinite loop, we need to copy the transaction locally, and clear out
|
33
|
+
# `records` on the copy that stays in the AR stack. Otherwise new
|
34
|
+
# transactions inside a commit callback will cause an infinite loop.
|
35
|
+
#
|
36
|
+
# This is because we're re-using the transaction on the stack, before
|
37
|
+
# it's been popped off and re-created by the AR code.
|
38
|
+
original = @transaction || @transaction_manager.current_transaction
|
39
|
+
transaction = original.dup
|
40
|
+
transaction.instance_variable_set(:@records, transaction.records.dup) # deep clone of records array
|
41
|
+
original.records.clear # so that this clear doesn't clear out both copies
|
42
|
+
transaction.commit_records
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_after_commit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Grosser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -100,9 +100,12 @@ executables: []
|
|
100
100
|
extensions: []
|
101
101
|
extra_rdoc_files: []
|
102
102
|
files:
|
103
|
+
- MIT-LICENSE
|
103
104
|
- Readme.md
|
104
105
|
- lib/test_after_commit.rb
|
106
|
+
- lib/test_after_commit/database_statements.rb
|
105
107
|
- lib/test_after_commit/version.rb
|
108
|
+
- lib/test_after_commit/with_transaction_state.rb
|
106
109
|
homepage: https://github.com/grosser/test_after_commit
|
107
110
|
licenses:
|
108
111
|
- MIT
|
@@ -115,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
118
|
requirements:
|
116
119
|
- - ">="
|
117
120
|
- !ruby/object:Gem::Version
|
118
|
-
version:
|
121
|
+
version: 2.0.0
|
119
122
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
123
|
requirements:
|
121
124
|
- - ">="
|
@@ -123,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
126
|
version: '0'
|
124
127
|
requirements: []
|
125
128
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.4.5.1
|
127
130
|
signing_key:
|
128
131
|
specification_version: 4
|
129
132
|
summary: makes after_commit callbacks testable in Rails 3+ with transactional_fixtures
|