<commit_before>require 'spec_helper'

describe "WithDisposableTransactionExtension#with_disposable_transaction" do
  let!(:blog) { Blog.create! :title => 'foo' }
  it "runs a transaction and rolls it back" do
    previous_open_transaction_count = Blog.connection.open_transactions
    Blog.with_disposable_transaction do
      blog.update_attributes! :title => 'bar'
      blog.reload.title.should == 'bar'
      Blog.connection.open_transactions.should == previous_open_transaction_count + 1
    end
    blog.reload.title.should == 'foo'
    Blog.connection.open_transactions.should == previous_open_transaction_count
  end
  it "preserves the return value" do
    result = Blog.with_disposable_transaction do
      1
    end
    result.should == 1
  end
  it "raises any StandardError occuring in the block" do
    expect do
      Blog.with_disposable_transaction do
        raise "Example Error"
      end
    end.to raise_error("Example Error")
  end
end

describe "Transactional wrapping" do
  shared_examples_for "synthetic after_commit" do
    it "calls after commit at this open transaction level, but not above" do
      blog = Blog.create! :title => 'foo'
      blog.called_after_commit.should == 1
      blog.update_attributes! :title => 'bar'
      blog.called_after_commit.should == 2
      ActiveRecord::Base.connection.transaction(:requires_new => true) do
        blog.update_attributes! :title => 'quux'
        blog.called_after_commit.should == 2
      end
      blog.called_after_commit.should == 3
      ActiveRecord::Base.connection.transaction(:requires_new => true) do
        blog.update_attributes! :title => 'ragz'
        blog.called_after_commit.should == 3
        raise ActiveRecord::Rollback
      end
      blog.called_after_commit.should == 3
    end
  end

  it "wraps describe blocks in a savepoint transaction" do
    ActiveRecord::Base.connection.current_transaction.should be_a ActiveRecord::ConnectionAdapters::SavepointTransaction
  end
  it "wrapped the suite before this describe block" do
    ActiveRecord::Base.connection.open_transactions.should == 2
  end<commit_msg>more after_commit coverage<commit_after>require 'spec_helper'

describe "WithDisposableTransactionExtension#with_disposable_transaction" do
  let!(:blog) { Blog.create! :title => 'foo' }
  it "runs a transaction and rolls it back" do
    previous_open_transaction_count = Blog.connection.open_transactions
    Blog.with_disposable_transaction do
      blog.update_attributes! :title => 'bar'
      blog.reload.title.should == 'bar'
      Blog.connection.open_transactions.should == previous_open_transaction_count + 1
    end
    blog.reload.title.should == 'foo'
    Blog.connection.open_transactions.should == previous_open_transaction_count
  end
  it "preserves the return value" do
    result = Blog.with_disposable_transaction do
      1
    end
    result.should == 1
  end
  it "raises any StandardError occuring in the block" do
    expect do
      Blog.with_disposable_transaction do
        raise "Example Error"
      end
    end.to raise_error("Example Error")
  end
end

describe "Transactional wrapping" do
  shared_examples_for "synthetic after_commit" do
    it "calls after_commit hooks at this open transaction level, but not above" do
      blog = Blog.create! :title => 'foo'
      blog.called_after_commit.should == 1
      blog.reload.title.should == 'foo'

      blog.update_attributes! :title => 'bar'
      blog.called_after_commit.should == 2
      blog.reload.title.should == 'bar'

      ActiveRecord::Base.connection.transaction(:requires_new => true) do
        blog.update_attributes! :title => 'quux'
        blog.called_after_commit.should == 2
      end
      blog.reload.title.should == 'quux'

      blog.called_after_commit.should == 3
      ActiveRecord::Base.connection.transaction(:requires_new => true) do
        blog.update_attributes! :title => 'ragz'
        blog.called_after_commit.should == 3
        raise ActiveRecord::Rollback
      end
      blog.called_after_commit.should == 3
      blog.reload.title.should == 'quux'
    end

    it "does not call after_commit hooks when an error is raised" do
      blog = Blog.create! :title => 'foo'
      blog.called_after_commit.should == 1

      # raise the level at which a synthetic commit should take place
      Blog.with_disposable_transaction do
        blog.update_attributes! :title => 'ragz'
        blog.called_after_commit.should == 2
      end

      # raise the level at which a synthetic commit should take place
      Blog.with_disposable_transaction do
        expect do
          ActiveRecord::Base.connection.transaction(:requires_new => true) do
            blog.update_attributes! :title => 'ragz'
            blog.called_after_commit.should == 2
            raise "Something blows up"
          end
        end.to raise_error("Something blows up")
        blog.called_after_commit.should == 2
        blog.reload.title.should == 'foo'
      end

      blog.called_after_commit.should == 2
      blog.reload.title.should == 'foo'
    end

  end

  it "wraps describe blocks in a savepoint transaction" do
    ActiveRecord::Base.connection.current_transaction.should be_a ActiveRecord::ConnectionAdapters::SavepointTransaction
  end
  it "wrapped the suite before this describe block" do
    ActiveRecord::Base.connection.open_transactions.should == 2
  end
{'fraction_non_alphanumeric': 0.08501949136810841, 'fraction_numerical': 0.004640801930573603, 'mean_word_length': 3.276190476190476, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '889151', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1508, 'n_words': 398}