test/fetch_test.rb in identity_cache-0.0.3 vs test/fetch_test.rb in identity_cache-0.0.4
- old
+ new
@@ -1,29 +1,42 @@
require "test_helper"
class FetchTest < IdentityCache::TestCase
+ NAMESPACE = IdentityCache::CacheKeyGeneration::DEFAULT_NAMESPACE
+
def setup
super
Record.cache_index :title, :unique => true
Record.cache_index :id, :title, :unique => true
@record = Record.new
@record.id = 1
@record.title = 'bob'
- @blob_key = "IDC:blob:Record:#{cache_hash("created_at:datetime,id:integer,record_id:integer,title:string,updated_at:datetime")}:1"
- @index_key = "IDC:index:Record:title:#{cache_hash('bob')}"
+ @cached_value = {:class => @record.class}
+ @record.encode_with(@cached_value)
+ @blob_key = "#{NAMESPACE}blob:Record:#{cache_hash("created_at:datetime,id:integer,record_id:integer,title:string,updated_at:datetime")}:1"
+ @index_key = "#{NAMESPACE}index:Record:title:#{cache_hash('bob')}"
end
-
def test_fetch_cache_hit
- IdentityCache.cache.expects(:read).with(@blob_key).returns(@record)
+ IdentityCache.cache.expects(:read).with(@blob_key).returns(@cached_value)
assert_equal @record, Record.fetch(1)
end
+ def test_fetch_hit_cache_namespace
+ Record.send(:include, SwitchNamespace)
+ Record.namespace = 'test_namespace'
+
+ new_blob_key = "test_namespace:#{@blob_key}"
+ IdentityCache.cache.expects(:read).with(new_blob_key).returns(@cached_value)
+
+ assert_equal @record, Record.fetch(1)
+ end
+
def test_exists_with_identity_cache_when_cache_hit
- IdentityCache.cache.expects(:read).with(@blob_key).returns(@record)
+ IdentityCache.cache.expects(:read).with(@blob_key).returns(@cached_value)
assert Record.exists_with_identity_cache?(1)
end
def test_exists_with_identity_cache_when_cache_miss_and_in_db
@@ -42,11 +55,11 @@
def test_fetch_miss
Record.expects(:find_by_id).with(1, :include => []).returns(@record)
IdentityCache.cache.expects(:read).with(@blob_key).returns(nil)
- IdentityCache.cache.expects(:write).with(@blob_key, @record)
+ IdentityCache.cache.expects(:write).with(@blob_key, @cached_value)
assert_equal @record, Record.fetch(1)
end
def test_fetch_by_id_not_found_should_return_nil
@@ -82,10 +95,18 @@
# cache sql result
IdentityCache.cache.expects(:write).with(@index_key, 1)
# got id, do memcache lookup on that, hit -> done
- IdentityCache.cache.expects(:read).with(@blob_key).returns(@record)
+ IdentityCache.cache.expects(:read).with(@blob_key).returns(@cached_value)
+
+ assert_equal @record, Record.fetch_by_title('bob')
+ end
+
+ def test_fetch_by_title_cache_namespace
+ Record.send(:include, SwitchNamespace)
+ IdentityCache.cache.expects(:read).with("ns:#{@index_key}").returns(1)
+ IdentityCache.cache.expects(:read).with("ns:#{@blob_key}").returns(@cached_value)
assert_equal @record, Record.fetch_by_title('bob')
end
def test_fetch_by_title_stores_idcnil