rspec_nested_transactions 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rspec_nested_transactions.rb +88 -0
- data/lib/rspec_nested_transactions/version.rb +5 -0
- data/rspec_nested_transactions.gemspec +25 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dcdc9084dfae8ca0623c164885b9d695190c336b
|
4
|
+
data.tar.gz: 64e6b4c8002206b296f5ce9f9c027f7eeded3264
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 16d9fc5ee5fa0a7efbd516ba729298a2f7b2236d66235329164c5fd850cf4ad325855c7cdf0c576a5bea14003bb47c272e900d5bfc58189e3c4bb98a5e4f144f
|
7
|
+
data.tar.gz: 67ba6f6788e5ec27c795363a43b2d28c2a970bc7e3d771a17a1e3db919f4a594e557bccbcc254d58cefcd6a632d3a9cd9a9c69e357c6a24c39331fb96605ebe8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Rodrigo Rosenfeld Rosas
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# RSpec Nested Transactions [](https://travis-ci.org/rosenfeld/rspec_nested_transactions)
|
2
|
+
|
3
|
+
Creating the same records in the database for every single test in a group can slow down your
|
4
|
+
suite. This is a common approach when using factories rather than fixtures loaded in suite
|
5
|
+
initialization. I much prefer factories over fixtures but for a long time I employ a technique
|
6
|
+
that enable a test suite to perform as well as using fixtures (or better if you're running
|
7
|
+
just a few tests from the suite) and read as good as you are used to when using factories.
|
8
|
+
|
9
|
+
Some databases, like PostgreSQL, allow transaction savepoints. That means it's possible to
|
10
|
+
run inner transactions inside an outer transaction to put in simple words.
|
11
|
+
|
12
|
+
So, if you were able to create a set of records in a before(:all) context and rollback those
|
13
|
+
changes once the context is finished, then there would no need to recreate those records for
|
14
|
+
each example in the context. If you also run each example in an inner transaction, that
|
15
|
+
means the examples will be still isolated from each other with regards to the database state.
|
16
|
+
|
17
|
+
RSpec doesn't allow one to run an `around` hook that would include contexts besides examples.
|
18
|
+
|
19
|
+
[Myron Marston wrote an article explaining how to use Ruby Fibers to implement around(:all)
|
20
|
+
on RSpec](http://myronmars.to/n/dev-blog/2012/03/building-an-around-hook-using-fibers). It's
|
21
|
+
an interesting idea and I used the concept to implement this feature.
|
22
|
+
|
23
|
+
# Requirements
|
24
|
+
|
25
|
+
The specs for this project will only run on Ruby 2.3 and above due to the `<<~` heredoc usage,
|
26
|
+
but I suspect it works on any Ruby >= 2.0 (due to `prepend` usage).
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
Add this line to your application's Gemfile:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'rspec_nested_transactions'
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
$ bundle
|
39
|
+
|
40
|
+
Or install it yourself as:
|
41
|
+
|
42
|
+
$ gem install rspec_nested_transactions
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
RSpec.configure do |c|
|
48
|
+
c.nested_transaction do |example_or_group, run|
|
49
|
+
(run[]; next) unless example_or_group.metadata[:db] # or delete this line if you don't care
|
50
|
+
# With Sequel and PostgreSQL:
|
51
|
+
DB.transaction(savepoint: true, rollback: :always, &run)
|
52
|
+
|
53
|
+
# Alternatively:
|
54
|
+
# conditionally issue a "BEGIN" or "SAVEPOINT #{dynamic_savepoint_name}"
|
55
|
+
# run[]
|
56
|
+
# conditionally issue a "ROLLBACK" or "ROLLBACK TO SAVEPOINT #{dynamic_savepoint_name}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
After that every context and example will be enclosed in a (inner) transaction. No need for
|
62
|
+
DatabaseCleaner (rolling back transactions are usually faster than truncate).
|
63
|
+
|
64
|
+
`nested_transaction` can also be called from inside `describe`/`context` groups just like
|
65
|
+
`around`:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
RSpec.describe "Some Feature" do
|
69
|
+
nested_transaction do |example_or_group, run|
|
70
|
+
# do something and call run[] ( or run.call/run.(), your call )
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
## Is it "production" safe?
|
76
|
+
|
77
|
+
Well, I've been using this technique since 2013 using [my fork of rspec_around_all](https://github.com/rosenfeld/rspec_around_all/tree/config_around).
|
78
|
+
|
79
|
+
Actually I've been using it for much longer, but using some monkey patches around Sequel, before
|
80
|
+
I read Myron's post. However, that code from 2013 is used to this date without changes so I
|
81
|
+
consider it to be stable. That's why I decided to clean it up by removing the parts I didn't
|
82
|
+
need, used a separate methods (instead of reusing `around`) and improved the specs to be clearer.
|
83
|
+
As a result they now require Ruby 2.3 to run. Also, it now uses `prepend` rather than
|
84
|
+
`Object.instance_method(:extend).bind(c).call`, which means it now requires Ruby >= 2.
|
85
|
+
|
86
|
+
I never had problems with my test database becoming inconsistents during all those years.
|
87
|
+
|
88
|
+
## Development
|
89
|
+
|
90
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to
|
91
|
+
run the tests. You can also run `bin/console` for an interactive prompt that will allow you to
|
92
|
+
experiment.
|
93
|
+
|
94
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new
|
95
|
+
version, update the version number in `version.rb`, and then run `bundle exec rake release`,
|
96
|
+
which will create a git tag for the version, push git commits and tags, and push the `.gem`
|
97
|
+
file to [rubygems.org](https://rubygems.org).
|
98
|
+
|
99
|
+
## Contributing
|
100
|
+
|
101
|
+
Bug reports and pull requests are welcome [on GitHub](https://github.com/rosenfeld/rspec_nested_transactions).
|
102
|
+
|
103
|
+
|
104
|
+
## License
|
105
|
+
|
106
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
107
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'rspec_nested_transactions'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'delegate'
|
4
|
+
require 'fiber'
|
5
|
+
require 'rspec_nested_transactions/version'
|
6
|
+
|
7
|
+
module RspecNestedTransactions
|
8
|
+
class FiberAwareGroup < SimpleDelegator
|
9
|
+
def run_examples
|
10
|
+
Fiber.yield
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_proc
|
14
|
+
proc { run_examples }
|
15
|
+
end
|
16
|
+
|
17
|
+
def class
|
18
|
+
__getobj__.class
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def nested_transaction(&block)
|
23
|
+
around &wrapped_block(&block)
|
24
|
+
nested_transaction_contexts(&block)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
FIBERS_STACK = []
|
30
|
+
|
31
|
+
def wrapped_block
|
32
|
+
->(c){ yield c, ->(*args){ c.respond_to?(:run) ? c.run : c.run_examples } }
|
33
|
+
end
|
34
|
+
|
35
|
+
def nested_transaction_contexts(&block)
|
36
|
+
(handle_config_around █ return) if ::RSpec::Core::Configuration === self
|
37
|
+
around_all(false) do |group|
|
38
|
+
group.children.each {|c| c.send :nested_transaction_contexts, &block }
|
39
|
+
block[ group, ->(*args){ group.run_examples } ]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
CONFIG_AROUND_ALL_NESTED_BLOCKS = []
|
44
|
+
CONFIG_AROUND_ALL_PROCESSED_BY_GROUP = {}
|
45
|
+
def handle_config_around(store = true, prepend = false, &block)
|
46
|
+
blocks = CONFIG_AROUND_ALL_NESTED_BLOCKS
|
47
|
+
blocks << block if store
|
48
|
+
around_all(prepend) do |group|
|
49
|
+
unless CONFIG_AROUND_ALL_PROCESSED_BY_GROUP[group.name]
|
50
|
+
CONFIG_AROUND_ALL_PROCESSED_BY_GROUP[group.name] = true
|
51
|
+
blocks.reverse_each do |b|
|
52
|
+
group.children.each{|c| c.send :handle_config_around, false, true, &b }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
block[ group, ->(*args){group.run_examples } ]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def around_all(prepend, &block)
|
60
|
+
methods = {
|
61
|
+
before: method(prepend ? :prepend_before : :before),
|
62
|
+
after: method(prepend ? :prepend_after : :after),
|
63
|
+
}
|
64
|
+
|
65
|
+
methods[:before].call :all do |group|
|
66
|
+
fiber = Fiber.new(&block)
|
67
|
+
FIBERS_STACK << fiber
|
68
|
+
fiber.resume(FiberAwareGroup.new(group.class))
|
69
|
+
end
|
70
|
+
|
71
|
+
methods[:after].call :all do
|
72
|
+
fiber = FIBERS_STACK.pop
|
73
|
+
fiber.resume
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
RSpec.configure do |c|
|
79
|
+
# make it available to example groups:
|
80
|
+
# c.extend overrides the original Object#extend method.
|
81
|
+
# See discussion in https://github.com/rspec/rspec-core/issues/1031#issuecomment-22264638
|
82
|
+
c.extend RspecNestedTransactions
|
83
|
+
|
84
|
+
# Add config.nested_transaction{}
|
85
|
+
c.class.prepend RspecNestedTransactions
|
86
|
+
# or RSpec::Core::Configuration.prepend RspecNestedTransactions
|
87
|
+
end
|
88
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('lib')
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'rspec_nested_transactions/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'rspec_nested_transactions'
|
10
|
+
spec.version = RspecNestedTransactions::VERSION
|
11
|
+
spec.authors = ['Rodrigo Rosenfeld Rosas']
|
12
|
+
spec.email = ['rr.rosas@gmail.com']
|
13
|
+
|
14
|
+
spec.summary = %q{Enable nested transactions for suites, contexts and examples. Useful to rollback DB changes in before(:all) blocks.}
|
15
|
+
spec.homepage = 'https://github.com/rosenfeld/rspec_nested_transactions'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
24
|
+
spec.add_runtime_dependency 'rspec'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec_nested_transactions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rodrigo Rosenfeld Rosas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- rr.rosas@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- lib/rspec_nested_transactions.rb
|
86
|
+
- lib/rspec_nested_transactions/version.rb
|
87
|
+
- rspec_nested_transactions.gemspec
|
88
|
+
homepage: https://github.com/rosenfeld/rspec_nested_transactions
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.5.1
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Enable nested transactions for suites, contexts and examples. Useful to rollback
|
112
|
+
DB changes in before(:all) blocks.
|
113
|
+
test_files: []
|