test/fetch_test.rb in identity_cache-0.0.7 vs test/fetch_test.rb in identity_cache-0.1.0
- old
+ new
@@ -15,10 +15,18 @@
@record.encode_with(@cached_value)
@blob_key = "#{NAMESPACE}blob:Item:#{cache_hash("created_at:datetime,id:integer,item_id:integer,title:string,updated_at:datetime")}:1"
@index_key = "#{NAMESPACE}index:Item:title:#{cache_hash('bob')}"
end
+ def test_fetch_with_garbage_input
+ Item.connection.expects(:exec_query)
+ .with('SELECT `items`.* FROM `items` WHERE `items`.`id` = 0 LIMIT 1', anything)
+ .returns(ActiveRecord::Result.new([], []))
+
+ assert_equal nil, Item.fetch_by_id('garbage')
+ end
+
def test_fetch_cache_hit
IdentityCache.cache.expects(:read).with(@blob_key).returns(@cached_value)
assert_equal @record, Item.fetch(1)
end
@@ -62,10 +70,16 @@
IdentityCache.cache.expects(:write).with(@blob_key, @cached_value)
assert_equal @record, Item.fetch(1)
end
+ def test_fetch_miss_with_non_id_primary_key
+ hashed_key = Zlib::crc32("foo") % (2 ** 30 - 1)
+ fixture = KeyedRecord.create!(:value => "foo") { |r| r.hashed_key = hashed_key }
+ assert_equal fixture, KeyedRecord.fetch(hashed_key)
+ end
+
def test_fetch_by_id_not_found_should_return_nil
nonexistent_record_id = 10
IdentityCache.cache.expects(:write).with(@blob_key + '0', IdentityCache::CACHED_NIL)
assert_equal nil, Item.fetch_by_id(nonexistent_record_id)
@@ -91,11 +105,11 @@
def test_fetch_by_title_hit
# Read record with title bob
IdentityCache.cache.expects(:read).with(@index_key).returns(nil)
# - not found, use sql, SELECT id FROM records WHERE title = '...' LIMIT 1"
- Item.connection.expects(:select_value).returns(1)
+ Item.connection.expects(:exec_query).returns(ActiveRecord::Result.new(['id'], [[1]]))
# cache sql result
IdentityCache.cache.expects(:write).with(@index_key, 1)
# got id, do memcache lookup on that, hit -> done
@@ -111,20 +125,20 @@
assert_equal @record, Item.fetch_by_title('bob')
end
def test_fetch_by_title_stores_idcnil
- Item.connection.expects(:select_value).once.returns(nil)
- Rails.cache.expects(:write).with(@index_key, IdentityCache::CACHED_NIL)
- Rails.cache.expects(:read).with(@index_key).times(3).returns(nil, IdentityCache::CACHED_NIL, IdentityCache::CACHED_NIL)
- assert_equal nil, Item.fetch_by_title('bob') # select_value => nil
+ Item.connection.expects(:exec_query).once.returns(ActiveRecord::Result.new([], []))
+ IdentityCache.cache.expects(:write).with(@index_key, IdentityCache::CACHED_NIL)
+ IdentityCache.cache.expects(:read).with(@index_key).times(3).returns(nil, IdentityCache::CACHED_NIL, IdentityCache::CACHED_NIL)
+ assert_equal nil, Item.fetch_by_title('bob') # exec_query => nil
assert_equal nil, Item.fetch_by_title('bob') # returns cached nil
assert_equal nil, Item.fetch_by_title('bob') # returns cached nil
end
def test_fetch_by_bang_method
- Item.connection.expects(:select_value).returns(nil)
+ Item.connection.expects(:exec_query).returns(ActiveRecord::Result.new([], []))
assert_raises ActiveRecord::RecordNotFound do
Item.fetch_by_title!('bob')
end
end