-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hector Mendoza Jacobo
committed
Jun 8, 2021
1 parent
b7564e7
commit a2f57ea
Showing
9 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
require "test_helper" | ||
|
||
class ExpireAttributeByMultiTest < IdentityCache::TestCase | ||
NAMESPACE = IdentityCache::CacheKeyGeneration::DEFAULT_NAMESPACE | ||
|
||
def setup | ||
super | ||
AssociatedRecord.cache_attribute(:name, by: [:id, :item_id]) | ||
|
||
@parent = Item.create!(title: "bob") | ||
@record = @parent.associated_records.create!(name: "foo") | ||
@name_attribute_key = "#{NAMESPACE}attr:AssociatedRecord:name:id:#{cache_hash(@record.id.to_s.inspect)}" | ||
IdentityCache.cache.clear | ||
end | ||
|
||
def test_expire_attribute_by_multi | ||
assert_queries(1) do | ||
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_and_item_id(1, 1)) | ||
end | ||
|
||
AssociatedRecord.expire_name_by_id_and_item_id(1, 1) | ||
|
||
assert_queries(1) do | ||
assert_equal("foo", AssociatedRecord.fetch_name_by_id_and_item_id(1, 1)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
require "test_helper" | ||
|
||
class ExpireByKeyValuesTest < IdentityCache::TestCase | ||
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") | ||
@record = @parent.associated_records.create!(name: "foo") | ||
@name_attribute_key = "#{NAMESPACE}attr:AssociatedRecord:name:id:#{cache_hash(@record.id.to_s.inspect)}" | ||
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 | ||
|
||
expire_hash = { | ||
id: 1, | ||
item_id: 1, | ||
} | ||
AssociatedRecord.expire_by_key_values(expire_hash) | ||
|
||
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 | ||
missing_item_id_error_message = | ||
"AssociatedRecord attribute name expire_by_key_value - required fields: item_id. missing: item_id" | ||
missing_id_error_message = | ||
"AssociatedRecord attribute name expire_by_key_value - required fields: id. missing: id" | ||
missing_item_two_error_message = | ||
"AssociatedRecord attribute name expire_by_key_value - required fields: id, item_id, item_two. missing: item_two" | ||
|
||
AssociatedRecord.cache_attribute(:name, by: [:id, :item_id, :item_two]) | ||
missing_item_id_error = assert_raises(IdentityCache::MissingKeyName) do | ||
AssociatedRecord.expire_by_key_values({ id: 1 }) | ||
end | ||
missing_id_error = assert_raises(IdentityCache::MissingKeyName) do | ||
AssociatedRecord.expire_by_key_values({ item_id: 1 }) | ||
end | ||
missing_item_two_error = assert_raises(IdentityCache::MissingKeyName) do | ||
AssociatedRecord.expire_by_key_values({ id: 1, item_id: 1 }) | ||
end | ||
|
||
assert_equal(missing_item_id_error_message, missing_item_id_error.message) | ||
assert_equal(missing_id_error_message, missing_id_error.message) | ||
assert_equal(missing_item_two_error_message, missing_item_two_error.message) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters