From 2d4ecedc26875acd8328d38f4432515c689d56ad Mon Sep 17 00:00:00 2001 From: ts Date: Sun, 27 Sep 2009 05:09:52 +0530 Subject: [PATCH] Test for counter cache in polymorphic associations --- .../associations/belongs_to_associations_test.rb | 21 ++++++++++++++++++++ activerecord/test/models/image.rb | 3 ++ activerecord/test/models/window.rb | 3 ++ activerecord/test/schema/schema.rb | 8 +++++++ 4 files changed, 35 insertions(+), 0 deletions(-) create mode 100644 activerecord/test/models/image.rb create mode 100644 activerecord/test/models/window.rb diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 9f3945f..2b906e7 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -15,6 +15,8 @@ require 'models/comment' require 'models/sponsor' require 'models/member' require 'models/essay' +require 'models/image' +require 'models/window' class BelongsToAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :developers, :projects, :topics, @@ -25,6 +27,25 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_equal companies(:first_firm).name, Client.find(3).firm.name assert !Client.find(3).firm.nil?, "Microsoft should have a firm" end + + def test_polymorphic_counter_cache + window = Window.create + + # Using create increments the countercache and appends to the array + assert_difference "window.reload.resource_count", 1 do + assert_difference "window.reload.images.size", 1 do + window.images.create! + end + end + + # << operator appends to the array, but fails to refresh counter cache + assert_difference "window.reload.resource_count", 1 do + assert_difference "window.reload.images.size", 1 do + window.images << Image.create + end + end + end + def test_belongs_to_with_primary_key client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name) diff --git a/activerecord/test/models/image.rb b/activerecord/test/models/image.rb new file mode 100644 index 0000000..ea0f394 --- /dev/null +++ b/activerecord/test/models/image.rb @@ -0,0 +1,3 @@ +class Image < ActiveRecord::Base + belongs_to :resource, :polymorphic => true, :counter_cache => :resource_count +end \ No newline at end of file diff --git a/activerecord/test/models/window.rb b/activerecord/test/models/window.rb new file mode 100644 index 0000000..4319f0f --- /dev/null +++ b/activerecord/test/models/window.rb @@ -0,0 +1,3 @@ +class Window < ActiveRecord::Base + has_many :images, :as => :resource, :dependent => :destroy +end \ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 984c5ba..5dc153b 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -478,6 +478,14 @@ ActiveRecord::Schema.define do t.integer :"c_int_#{i}", :limit => i end end + + create_table :images, :force => true do |t| + t.belongs_to(:resource, :polymorphic => true) + end + + create_table :windows, :force => true do |t| + t.integer :resource_count, :default => 0 + end except 'SQLite' do # fk_test_has_fk should be before fk_test_has_pk -- 1.5.6.3