Created
February 17, 2016 17:49
-
-
Save felixbuenemann/ce8a7c9844d579b4f624 to your computer and use it in GitHub Desktop.
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
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
# gem 'activerecord', '5.0.0.beta2' | |
# gem 'activerecord', '4.2.5.1' | |
# gem 'activerecord', '4.2.0' | |
# gem 'activerecord', '4.1.14.1' | |
# gem 'activerecord', '4.1.2' | |
# gem 'activerecord', github: 'piotrj/rails', branch: 'issue_23265' | |
gem 'activerecord', github: 'rails/rails' | |
gem 'sqlite3' | |
end | |
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 :products, force: true do |t| | |
end | |
create_table :classifications, force: true do |t| | |
t.integer :product_id | |
t.integer :taxon_id | |
end | |
create_table :taxons, force: true do |t| | |
t.integer :products_count, default: 0 | |
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