-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for manual expiration for after direct DB manipulation #500
base: main
Are you sure you want to change the base?
Changes from 1 commit
c5038e5
4f7e0da
312f7c6
27dac82
d96fc9d
5465c74
136c43a
dfe0992
f78fed9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
expected_error_message = "key not found: :id" | ||||||
old_values = { | ||||||
name: "foo", | ||||||
} | ||||||
new_values = { | ||||||
name: "bar", | ||||||
} | ||||||
|
||||||
error = assert_raises do | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is there no assertion for what value is returned by this? Also, why is this making an assertion for a hard code id? Shouldn't this test be making the insertion after this using something like |
||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There isn't a corresponding deletion in this test, so this isn't really testing deletion. |
||||||
assert_queries(1) do | ||||||
assert_equal("foo", AssociatedRecord.fetch_name_by_id(id)) | ||||||
end | ||||||
end | ||||||
end | ||||||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test files should be organized around the code it tests as explained in #498