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 18, 2020
1 parent 09526c8 commit c3b3843
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/test_core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,32 @@ 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 = ParanoidWithTouchAndCounterCache
.create!(paranoid_boolean: paranoid_boolean)

assert_equal 1, paranoid_boolean.paranoid_with_touch_and_counter_caches_count
updated_at = paranoid_boolean.reload.updated_at

paranoid_with_counter_cache.destroy

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

def test_touch_belongs_to
paranoid_boolean = ParanoidBoolean.create!
paranoid_with_counter_cache = ParanoidWithTouch
.create!(paranoid_boolean: paranoid_boolean)

updated_at = paranoid_boolean.reload.updated_at

paranoid_with_counter_cache.destroy

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
15 changes: 15 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def setup_db
t.boolean :is_deleted
t.integer :paranoid_time_id
t.integer :paranoid_with_counter_caches_count
t.integer :paranoid_with_touch_and_counter_caches_count
t.integer :custom_counter_cache
timestamps t
end
Expand Down Expand Up @@ -271,6 +272,8 @@ 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
has_many :paranoid_with_touch, dependent: :destroy
end

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

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

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

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

0 comments on commit c3b3843

Please sign in to comment.