-
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.
Merge pull request #368 from Shopify/cache-index-fetch-multi
Add fetch_multi_by_* support for cache_index with a single field
- Loading branch information
Showing
4 changed files
with
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
require "test_helper" | ||
|
||
class FetchMultiByTest < IdentityCache::TestCase | ||
NAMESPACE = IdentityCache::CacheKeyGeneration::DEFAULT_NAMESPACE | ||
|
||
def setup | ||
super | ||
@bob = Item.new | ||
@bob.id = 1 | ||
@bob.title = 'bob' | ||
|
||
@bertha = Item.new | ||
@bertha.id = 2 | ||
@bertha.title = 'bertha' | ||
end | ||
|
||
def test_fetch_multi_by_cache_key | ||
Item.cache_index :title, unique: false | ||
|
||
@bob.save! | ||
@bertha.save! | ||
|
||
assert_equal [@bob], Item.fetch_by_title('bob') | ||
|
||
assert_equal [@bob, @bertha], Item.fetch_multi_by_title(['bob', 'bertha']) | ||
end | ||
|
||
def test_fetch_multi_by_cache_key_with_unknown_key | ||
Item.cache_index :title, unique: false | ||
|
||
@bob.save! | ||
@bertha.save! | ||
|
||
assert_equal [@bob], Item.fetch_multi_by_title(['bob', 'garbage_title']) | ||
end | ||
|
||
def test_fetch_multi_by_unique_cache_key | ||
Item.cache_index :title, unique: true | ||
|
||
@bob.save! | ||
@bertha.save! | ||
|
||
assert_equal @bob, Item.fetch_by_title('bob') | ||
|
||
assert_equal [@bob, @bertha], Item.fetch_multi_by_title(['bob', 'bertha']) | ||
end | ||
|
||
def test_fetch_multi_attribute_by_cache_key | ||
Item.cache_attribute :title, by: :id, unique: false | ||
|
||
@bob.save! | ||
@bertha.save! | ||
|
||
assert_equal ['bob'], Item.fetch_title_by_id(1) | ||
|
||
assert_equal({ 1 => ['bob'], 2 => ['bertha'] }, Item.fetch_multi_title_by_id([1, 2])) | ||
end | ||
|
||
def test_fetch_multi_attribute_by_cache_key_with_unknown_key | ||
Item.cache_attribute :title, by: :id, unique: false | ||
|
||
@bob.save! | ||
@bertha.save! | ||
|
||
assert_equal({ 1 => ['bob'], 999 => [] }, Item.fetch_multi_title_by_id([1, 999])) | ||
end | ||
|
||
def test_fetch_multi_attribute_by_unique_cache_key | ||
Item.cache_attribute :title, by: :id, unique: true | ||
|
||
@bob.save! | ||
@bertha.save! | ||
|
||
assert_equal 'bob', Item.fetch_title_by_id(1) | ||
|
||
assert_equal({ 1 => 'bob', 2 => 'bertha' }, Item.fetch_multi_title_by_id([1, 2])) | ||
end | ||
|
||
def test_fetch_multi_attribute_by_unique_cache_key_with_unknown_key | ||
Item.cache_attribute :title, by: :id, unique: true | ||
|
||
@bob.save! | ||
@bertha.save! | ||
|
||
assert_equal({ 1 => 'bob', 999 => nil }, Item.fetch_multi_title_by_id([1, 999])) | ||
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