Last active
December 20, 2015 02:38
-
-
Save jimjh/6057340 to your computer and use it in GitHub Desktop.
test script for rails/rails#10865
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
#!/usr/bin/env ruby | |
unless File.exists?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails', github: 'rails/rails', branch: 'master' | |
gem 'sqlite3' | |
GEMFILE | |
system 'bundle' | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :posts do |t| | |
t.integer :comments_count | |
end | |
create_table :comments do |t| | |
t.references :post | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post, counter_cache: true | |
end | |
class BugTest < MiniTest::Unit::TestCase | |
# this test case | |
# - passes with master, 4.0.0 | |
# - fails with 3.2.13 | |
def test_belongs_to_counter_with_append | |
post = Post.create! | |
comment = Comment.create! | |
post.comments << comment | |
post.reload | |
assert_equal 1, post.comments.count | |
assert_equal 1, post.comments_count | |
end | |
# this test case passes with master, 4.0.0 | |
def test_belongs_to_counter_with_assignment | |
post = Post.create! | |
comment = Comment.new | |
comment.post = post | |
comment.save | |
post.reload | |
assert_equal 1, post.comments_count | |
assert_equal 1, post.comments.count | |
end | |
# this test case | |
# - fails with master, 4.0.0 | |
# - passes with 3.2.13 | |
def test_belongs_to_counter_with_assignment | |
post = Post.create! | |
comment = Comment.create! | |
comment.post = post | |
post.reload | |
# changes to comment has not been saved yet, but counter cache has been | |
# updated | |
assert_equal 0, post.comments.count | |
assert_equal 1, post.comments_count | |
comment.save | |
post.reload | |
# changes to comment has been saved, and counter cache is updated again | |
assert_equal 1, post.comments.count | |
assert_equal 1, post.comments_count # fail | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment