Skip to content

Commit

Permalink
Add testing for update, delete and insert cache expirations
Browse files Browse the repository at this point in the history
  • Loading branch information
Hector Mendoza Jacobo committed Jun 28, 2021
1 parent 5465c74 commit 7a1265a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/identity_cache/without_primary_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def expire_cache_for_update(old_indexed_values, changes)
check_for_unsupported_parent_expiration_entries
end

private def expire_cache_for_insert_or_delete(indexed_values)
def expire_cache_for_insert_or_delete(indexed_values)
if primary_cache_index_enabled
id = indexed_values.fetch(primary_key.to_sym)
expire_primary_key_cache_index(id)
Expand All @@ -76,6 +76,8 @@ def expire_cache_for_update(old_indexed_values, changes)

alias_method :expire_cache_for_delete, :expire_cache_for_insert_or_delete

private :expire_cache_for_insert_or_delete

private

def check_for_unsupported_parent_expiration_entries
Expand Down
89 changes: 89 additions & 0 deletions test/cache_manual_expire_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# frozen_string_literal: true
require "test_helper"

module IdentityCache
class CacheManualExpireTest < IdentityCache::TestCase
def setup
super
AssociatedRecord.cache_attribute(:name)

@parent = Item.create!(title: "bob")
@record = @parent.associated_records.create!(name: "foo")
IdentityCache.cache.clear
end

def test_cache_indexed_columns_returns_the_correct_columns_for_expiration
AssociatedRecord.cache_attribute(:name, by: :item_id)
expected_result = [:id, :item_id]
assert_equal(expected_result, AssociatedRecord.cache_indexed_columns)
end

def test_expire_cache_for_udate
id = 1
item_id = 1
AssociatedRecord.cache_attribute(:item_id, by: :name)

assert_queries(1) do
assert_equal(item_id, AssociatedRecord.fetch_item_id_by_name("foo"))
end

AssociatedRecord.where(id: 1).update_all(name: "bar")
old_values = {
name: "foo",
id: id,
}
new_values = {
name: "bar",
id: id,
}

AssociatedRecord.expire_cache_for_update(old_values, new_values)
assert_queries(2) do
assert_equal(item_id, AssociatedRecord.fetch_item_id_by_name("bar"))
assert_nil(AssociatedRecord.fetch_item_id_by_name("foo"))
end
end

def test_expire_cache_for_udate_raises_when_a_hash_is_missing_an_index_key
expected_error_message = "key not found: :id"
old_values = {
name: "foo",
}
new_values = {
name: "bar",
}

error = assert_raises do
AssociatedRecord.expire_cache_for_update(old_values, new_values)
end

assert_equal(expected_error_message, error.message)
end

def test_expire_cache_for_insert
id = 1
AssociatedRecord.fetch_name_by_id(id)
expire_hash_keys = {
id: id
}

AssociatedRecord.expire_cache_for_insert(expire_hash_keys)
assert_queries(1) do
assert_equal("foo", AssociatedRecord.fetch_name_by_id(id))
end
end

def test_expire_cache_for_delete
id = 1
AssociatedRecord.fetch_name_by_id(1)
expire_hash_keys = {
id: id
}

AssociatedRecord.expire_cache_for_delete(expire_hash_keys)
assert_queries(1) do
assert_equal("foo", AssociatedRecord.fetch_name_by_id(id))
end
end
end
end

0 comments on commit 7a1265a

Please sign in to comment.