Skip to content

Instantly share code, notes, and snippets.

@prathamesh-sonpatki
Last active August 29, 2015 14:16
Show Gist options
  • Save prathamesh-sonpatki/69b00155d3990e3e507e to your computer and use it in GitHub Desktop.
Save prathamesh-sonpatki/69b00155d3990e3e507e to your computer and use it in GitHub Desktop.
gem 'activerecord', '4.2.0'
# gem 'activerecord', '4.1.8'
# gem 'activerecord', '4.0.12'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'ar_test_postgres')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.text :content
t.string :author
t.integer :total_lines
end
end
class Post < ActiveRecord::Base
after_commit :calculate_total_lines, if: -> (post) { post.previous_changes.include?(:content) }
def calculate_total_lines
update! total_lines: content.split("\n").length
end
end
class BugTest < Minitest::Test
def test_without_transaction
puts "ActiveRecord #{ActiveRecord.version}"
post = Post.create! content: "Lets discuss Rails 5.\n", author: 'Prathamesh'
assert_equal 1, post.total_lines
end
def test_within_transaction
puts "ActiveRecord #{ActiveRecord.version}"
Post.transaction do
post = Post.create! content: "Lets discuss Ruby 2.2 today.\n", author: 'Prathamesh'
assert_equal 1, post.total_lines
end
end
end
# ActiveRecord 4.0.12
# 1) Failure:
# BugTest#test_within_transaction [after_commit_test.rb:45]:
# Expected: 1
# Actual: nil
# 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
# ActiveRecord 4.1.8
# 1) Failure:
# BugTest#test_within_transaction [after_commit_test.rb:45]:
# Expected: 1
# Actual: nil
# 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
# Finished in 0.026370s, 75.8438 runs/s, 75.8438 assertions/s.
# ActiveRecord 4.2.0
# 1) Failure:
# BugTest#test_within_transaction [after_commit_test.rb:45]:
# Expected: 1
# Actual: nil
# 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
# ActiveRecord 5.0.0.alpha
# 1) Failure:
# BugTest#test_within_transaction [after_commit_test.rb:48]:
# Expected: 1
# Actual: nil
# 2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment