-
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 expire methods by key values #495
Changes from all commits
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,70 @@ | ||
# frozen_string_literal: true | ||
require "test_helper" | ||
|
||
class ExpireByKeyValuesTest < IdentityCache::TestCase | ||
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. Note that I believe that these feature based unit test files are an anti-pattern (#498). I just hadn't gotten around to actually refactoring the tests around the code |
||
NAMESPACE = IdentityCache::CacheKeyGeneration::DEFAULT_NAMESPACE | ||
|
||
def setup | ||
super | ||
AssociatedRecord.cache_attribute(:name) | ||
AssociatedRecord.cache_attribute(:name, by: :item_id) | ||
AssociatedRecord.cache_attribute(:name, by: [:id, :item_id]) | ||
|
||
parent = Item.create!(title: "bob") | ||
parent.associated_records.create!(name: "foo") | ||
IdentityCache.cache.clear | ||
end | ||
|
||
def test_expire_by_key_values | ||
assert_queries(3) do | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_id(1)) | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_item_id(1)) | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_id_and_item_id(1, 1)) | ||
end | ||
|
||
assert_queries(0) do | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_id(1)) | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_item_id(1)) | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_id_and_item_id(1, 1)) | ||
end | ||
|
||
key_values = { | ||
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. Could it be this hash that crashes the cop?
Or maybe because 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. The rubocop failure was a bug and the fix hasn't been released yet. I opened #496 to fix that. |
||
id: 1, | ||
item_id: 1, | ||
} | ||
|
||
AssociatedRecord.cache_indexes.each do |index| | ||
index.expire_by_key_value(key_values) | ||
end | ||
|
||
assert_queries(3) do | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_id(1)) | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_item_id(1)) | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_id_and_item_id(1, 1)) | ||
end | ||
end | ||
|
||
def test_expire_by_key_values_raises_exception_on_missing_key | ||
by_id_index, by_item_id_index, by_multi_index = AssociatedRecord.cache_indexes | ||
missing_id_error_message = | ||
"AssociatedRecord attribute name expire_by_key_value - required fields: id. missing: id" | ||
missing_item_id_error_message = | ||
"AssociatedRecord attribute name expire_by_key_value - required fields: item_id. missing: item_id" | ||
missing_id_key_in_multi_error_message = | ||
"AssociatedRecord attribute name expire_by_key_value - required fields: id, item_id. missing: id" | ||
|
||
missing_id_error = assert_raises(IdentityCache::MissingKeyName) do | ||
by_id_index.expire_by_key_value({ item_id: 1 }) | ||
end | ||
missing_item_id_error = assert_raises(IdentityCache::MissingKeyName) do | ||
by_item_id_index.expire_by_key_value({ id: 1 }) | ||
end | ||
missing_id_in_multi_error = assert_raises(IdentityCache::MissingKeyName) do | ||
by_multi_index.expire_by_key_value({ item_id: 1 }) | ||
end | ||
|
||
assert_equal(missing_id_error_message, missing_id_error.message) | ||
assert_equal(missing_item_id_error_message, missing_item_id_error.message) | ||
assert_equal(missing_id_key_in_multi_error_message, missing_id_in_multi_error.message) | ||
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.
We don't just need the name of the key, so MissingKey seems like a more appropriate name, but then why not just use
KeyError
?