Skip to content

Instantly share code, notes, and snippets.

@maclover7
Last active May 1, 2019 01:23
Show Gist options
  • Save maclover7/e3647aac47606b26afb0 to your computer and use it in GitHub Desktop.
Save maclover7/e3647aac47606b26afb0 to your computer and use it in GitHub Desktop.

Related Issues:

Related PRs:

Tests Status:

4.1.14.1 4.2.5.1 master
assign 1 πŸ‘ 2 πŸ‘Ž 1 πŸ‘
assign + reload 2 πŸ‘Ž 1 πŸ‘ 1 πŸ‘
clear by re-assign 0 πŸ‘ 3 πŸ‘Ž 1 πŸ‘Ž
clear by re-assign + reload 1 πŸ‘Ž 1 πŸ‘Ž 1 πŸ‘Ž
clear by destroy_all 0 πŸ‘ 2 πŸ‘Ž 0 πŸ‘
clear by destroy_all + reload 0 πŸ‘ 0 πŸ‘ 0 πŸ‘
failed 2 4 2

implicit counter cache (cache column, but no counter_cache on belongs_to):

4.1.14.1 4.2.5.1 master
assign 1 πŸ‘ 1 πŸ‘ 1 πŸ‘
assign + reload 1 πŸ‘ 1 πŸ‘ 1 πŸ‘
clear by re-assign 0 πŸ‘ 2 πŸ‘Ž 1 πŸ‘Ž
clear by re-assign + reload 0 πŸ‘ 1 πŸ‘Ž 1 πŸ‘Ž
clear by destroy_all 0 πŸ‘ 1 πŸ‘Ž 0 πŸ‘
clear by destroy_all + reload 0 πŸ‘ 1 πŸ‘Ž 0 πŸ‘
failed 0 4 2
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: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :classifications, force: true do |t|
t.integer :product_id
t.integer :taxon_id
end
create_table :products, force: true do |t|
end
create_table :taxons, force: true do |t|
t.integer :products_count
end
end
class Product < ActiveRecord::Base
has_many :classifications
has_many :taxons, through: :classifications
end
class Classification < ActiveRecord::Base
belongs_to :product
belongs_to :taxon, counter_cache: :products_count
end
class Taxon < ActiveRecord::Base
has_many :classifications
has_many :products, through: :classifications
end
class BugTest < Minitest::Test
def taxon
@taxon ||= Taxon.create!
end
def product
@product ||= Product.create!
end
def test_counter_cache_initial_value
assert_equal 0, taxon.products_count
end
def test_counter_cache_after_assign
taxon.products = [product]
assert_equal 1, taxon.products_count
end
def test_counter_cache_after_assign_and_reload
taxon.products = [product]
assert_equal 1, taxon.reload.products_count
end
def test_counter_cache_after_clear_by_assign
taxon.products = [product]
taxon.products = []
assert_equal 0, taxon.products_count
end
def test_counter_cache_after_clear_by_assign_and_reload
taxon.products = [product]
taxon.products = []
assert_equal 0, taxon.reload.products_count
end
def test_counter_cache_after_clear_by_destroy_all
taxon.products = [product]
taxon.products.destroy_all
assert_equal 0, taxon.products_count
end
def test_counter_cache_after_clear_by_destroy_all_and_reload
taxon.products = [product]
taxon.products.destroy_all
assert_equal 0, taxon.reload.products_count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment