Skip to content

Commit

Permalink
Add test for touch and counter_cache issue
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldruziak committed Aug 17, 2020
1 parent 09526c8 commit 8e2c99b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,20 @@ def test_decrement_custom_counters
assert_equal 0, paranoid_boolean.reload.custom_counter_cache
end

def test_decrement_counters_and_touch
paranoid_boolean = ParanoidBoolean.create!
paranoid_with_counter_cache = ParanoidWithCounterCache
.create!(paranoid_boolean: paranoid_boolean)

assert_equal 1, paranoid_boolean.paranoid_with_counter_caches_count
updated_at = paranoid_boolean.updated_at

paranoid_with_counter_cache.destroy

assert_equal 0, paranoid_boolean.reload.paranoid_with_counter_caches_count
assert_not_equal updated_at, paranoid_boolean.reload.updated_at
end

def test_destroy_with_optional_belongs_to_and_counter_cache
ps = ParanoidWithCounterCacheOnOptionalBelognsTo.create!
ps.destroy
Expand Down
7 changes: 7 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class ParanoidBoolean < ActiveRecord::Base
has_one :paranoid_has_one_dependant, dependent: :destroy
has_many :paranoid_with_counter_cache, dependent: :destroy
has_many :paranoid_with_custom_counter_cache, dependent: :destroy
has_many :paranoid_with_touch_and_counter_cache, dependent: :destroy
end

class ParanoidString < ActiveRecord::Base
Expand Down Expand Up @@ -322,6 +323,12 @@ class ParanoidWithCounterCacheOnOptionalBelognsTo < ActiveRecord::Base
end
end

class ParanoidWithTouchAndCounterCache < ActiveRecord::Base
self.table_name = "paranoid_with_counter_caches"
acts_as_paranoid
belongs_to :paranoid_boolean, counter_cache: :custom_counter_cache, touch: true
end

class ParanoidHasManyDependant < ActiveRecord::Base
acts_as_paranoid
belongs_to :paranoid_time
Expand Down

0 comments on commit 8e2c99b

Please sign in to comment.