2024-10-24 02:26:39 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2016-11-04 10:15:22 +08:00
|
|
|
require_relative '../environment'
|
|
|
|
|
|
|
|
|
|
class CounterCacheTest < ActiveSupport::TestCase
|
|
|
|
|
def test_counter_cache_when_creating
|
2021-01-01 11:32:49 +08:00
|
|
|
AncestryTestDatabase.with_model :depth => 2, :width => 2, :counter_cache => true do |_model, roots|
|
|
|
|
|
roots.each do |lvl0_node, _lvl0_children|
|
2016-11-04 10:15:22 +08:00
|
|
|
assert_equal 2, lvl0_node.reload.children_count
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_counter_cache_when_destroying
|
2021-01-01 11:32:49 +08:00
|
|
|
AncestryTestDatabase.with_model :depth => 2, :width => 2, :counter_cache => true do |_model, roots|
|
2016-11-04 10:15:22 +08:00
|
|
|
parent = roots.first.first
|
|
|
|
|
child = parent.children.first
|
|
|
|
|
assert_difference 'parent.reload.children_count', -1 do
|
|
|
|
|
child.destroy
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-11 11:11:49 +08:00
|
|
|
def test_counter_cache_when_reduplicate_destroying
|
2021-01-01 11:32:49 +08:00
|
|
|
AncestryTestDatabase.with_model :depth => 2, :width => 2, :counter_cache => true do |_model, roots|
|
2018-11-11 11:11:49 +08:00
|
|
|
parent = roots.first.first
|
|
|
|
|
child = parent.children.first
|
2021-01-01 11:32:49 +08:00
|
|
|
child2 = child.class.find(child.id)
|
2018-11-11 11:11:49 +08:00
|
|
|
|
|
|
|
|
assert_difference 'parent.reload.children_count', -1 do
|
|
|
|
|
child.destroy
|
2021-01-01 11:32:49 +08:00
|
|
|
child2.destroy
|
2018-11-11 11:11:49 +08:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-11-04 10:15:22 +08:00
|
|
|
def test_counter_cache_when_updating_parent
|
2021-01-01 11:32:49 +08:00
|
|
|
AncestryTestDatabase.with_model :depth => 2, :width => 2, :counter_cache => true do |_model, roots|
|
2016-11-04 10:15:22 +08:00
|
|
|
parent1 = roots.first.first
|
|
|
|
|
parent2 = roots.last.first
|
|
|
|
|
child = parent1.children.first
|
|
|
|
|
|
|
|
|
|
assert_difference 'parent1.reload.children_count', -1 do
|
|
|
|
|
assert_difference 'parent2.reload.children_count', 1 do
|
2019-05-27 14:55:07 +08:00
|
|
|
child.update parent: parent2
|
2016-11-04 10:15:22 +08:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_counter_cache_when_updating_parent_and_previous_is_nil
|
2021-01-01 11:32:49 +08:00
|
|
|
AncestryTestDatabase.with_model :depth => 2, :width => 2, :counter_cache => true do |_model, roots|
|
2016-11-04 10:15:22 +08:00
|
|
|
child = roots.first.first
|
|
|
|
|
parent = roots.last.first
|
|
|
|
|
|
|
|
|
|
assert_difference 'parent.reload.children_count', 1 do
|
2019-05-27 14:55:07 +08:00
|
|
|
child.update parent: parent
|
2016-11-04 10:15:22 +08:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_counter_cache_when_updating_parent_and_current_is_nil
|
2021-01-01 11:32:49 +08:00
|
|
|
AncestryTestDatabase.with_model :depth => 2, :width => 2, :counter_cache => true do |_model, roots|
|
2016-11-04 10:15:22 +08:00
|
|
|
parent = roots.first.first
|
|
|
|
|
child = parent.children.first
|
|
|
|
|
|
|
|
|
|
assert_difference 'parent.reload.children_count', -1 do
|
2019-05-27 14:55:07 +08:00
|
|
|
child.update parent: nil
|
2016-11-04 10:15:22 +08:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_custom_counter_cache_column
|
2021-01-01 11:32:49 +08:00
|
|
|
AncestryTestDatabase.with_model :depth => 2, :width => 2, :counter_cache => :nodes_count do |_model, roots|
|
|
|
|
|
roots.each do |lvl0_node, _lvl0_children|
|
2016-11-04 10:15:22 +08:00
|
|
|
assert_equal 2, lvl0_node.reload.nodes_count
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-09-11 23:06:42 +08:00
|
|
|
|
|
|
|
|
def test_counter_cache_when_updating_record
|
2021-01-01 11:32:49 +08:00
|
|
|
AncestryTestDatabase.with_model :depth => 2, :width => 2, :counter_cache => true, :extra_columns => {:name => :string} do |_model, roots|
|
2019-09-11 23:06:42 +08:00
|
|
|
parent = roots.first.first
|
|
|
|
|
child = parent.children.first
|
|
|
|
|
|
|
|
|
|
assert_difference 'parent.reload.children_count', 0 do
|
|
|
|
|
child.update :name => "name2"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-04-11 09:07:28 +08:00
|
|
|
|
|
|
|
|
def test_setting_counter_cache
|
2023-05-29 21:42:53 +08:00
|
|
|
AncestryTestDatabase.with_model :depth => 3, :width => 2, :counter_cache => true do |model, roots|
|
|
|
|
|
# ensure they are successfully built
|
|
|
|
|
roots.each do |lvl0_node, lvl0_children|
|
|
|
|
|
assert_equal 2, lvl0_node.reload.children_count
|
|
|
|
|
lvl0_children.each do |lvl1_node, lvl1_children|
|
|
|
|
|
assert_equal 2, lvl1_node.reload.children_count
|
|
|
|
|
lvl1_children.each do |lvl2_node, _lvl2_children|
|
|
|
|
|
assert_equal 0, lvl2_node.reload.children_count
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-04-11 09:07:28 +08:00
|
|
|
model.update_all(model.counter_cache_column => 0)
|
|
|
|
|
# ensure they are successfully broken
|
|
|
|
|
roots.each do |lvl0_node, _lvl0_children|
|
|
|
|
|
assert_equal 0, lvl0_node.reload.children_count
|
|
|
|
|
end
|
|
|
|
|
model.rebuild_counter_cache!
|
2023-05-29 21:42:53 +08:00
|
|
|
|
2023-04-11 09:07:28 +08:00
|
|
|
# ensure they are successfully built
|
2023-05-29 21:42:53 +08:00
|
|
|
roots.each do |lvl0_node, lvl0_children|
|
2023-04-11 09:07:28 +08:00
|
|
|
assert_equal 2, lvl0_node.reload.children_count
|
2023-05-29 21:42:53 +08:00
|
|
|
lvl0_children.each do |lvl1_node, lvl1_children|
|
|
|
|
|
assert_equal 2, lvl1_node.reload.children_count
|
|
|
|
|
lvl1_children.each do |lvl2_node, _lvl2_children|
|
|
|
|
|
assert_equal 0, lvl2_node.reload.children_count
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-04-11 09:07:28 +08:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-11-04 10:15:22 +08:00
|
|
|
end
|