Created
August 5, 2013 12:44
-
-
Save rosenfeld/6155645 to your computer and use it in GitHub Desktop.
RSpec run_all_in_transaction helper for running before(:all) blocks under a transaction with Sequel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # support/group_helpers.rb | |
| module GroupHelpers | |
| def self.start_transaction | |
| # @_conn = DB.pool.send :acquire, Thread.current | |
| @_conn = DB.pool.hold{|conn| conn } | |
| DB.send(:add_transaction, @_conn, {savepoint: true}) | |
| DB.send(:begin_transaction, @_conn, savepoint: true) | |
| end | |
| def self.rollback | |
| DB.send(:rollback_transaction, @_conn, savepoint: true) | |
| DB.send(:remove_transaction, @_conn, false) | |
| # DB.pool.send(:sync){DB.pool.send(:release, Thread.current)} | |
| end | |
| def run_all_in_transaction | |
| before(:all){ GroupHelpers.start_transaction } | |
| after(:all){ GroupHelpers.rollback } | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require_relative 'support/group_helpers' | |
| RSpec.configure do |c| | |
| c.extend GroupHelpers | |
| c.around(:each) do |example| | |
| DB.transaction(savepoint: true, rollback: :always){ example.run } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment