2013-03-11 21:15:21 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class ObjectCacheTest < ActiveSupport::TestCase
|
|
|
|
test 'object cache' do
|
|
|
|
|
2015-04-27 13:43:34 +00:00
|
|
|
name = 'object cache test ' + rand(9_999_999).to_s
|
2013-03-11 21:15:21 +00:00
|
|
|
group = Group.create(
|
2015-04-27 13:42:53 +00:00
|
|
|
name: name,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
2013-03-11 21:15:21 +00:00
|
|
|
)
|
2015-06-18 21:26:58 +00:00
|
|
|
group_new = Group.where( name: name ).first
|
|
|
|
assert_equal( name, group_new[:name], 'verify by where' )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
# lookup by name
|
|
|
|
cache_key = "#{group_new.name}"
|
|
|
|
assert_nil( Group.cache_get(cache_key) )
|
|
|
|
|
|
|
|
group_lookup_name = Group.lookup( name: group_new.name )
|
|
|
|
assert_equal( group_new.name, group_lookup_name[:name], 'verify by lookup.name' )
|
|
|
|
assert( Group.cache_get(cache_key) )
|
|
|
|
|
|
|
|
# lookup by id
|
|
|
|
cache_key = "#{group_new.id}"
|
|
|
|
assert_nil( Group.cache_get(cache_key) )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
group_lookup_id = Group.lookup( id: group.id )
|
2015-06-18 21:26:58 +00:00
|
|
|
assert_equal( group_new.name, group_lookup_id[:name], 'verify by lookup.id' )
|
|
|
|
assert( Group.cache_get(cache_key) )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
# update / check if old name caches are deleted
|
2013-03-11 21:15:21 +00:00
|
|
|
name_new = name + ' next'
|
|
|
|
group.name = name_new
|
|
|
|
group.save
|
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
# lookup by name
|
|
|
|
cache_key = "#{group.name}"
|
|
|
|
assert_nil( Group.cache_get(cache_key) )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
group_lookup = Group.where( name: group_new.name ).first
|
|
|
|
assert_nil( group_lookup, 'verify by where name_old' )
|
|
|
|
assert_nil( Group.cache_get(cache_key) )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
group_lookup = Group.where( name: group.name ).first
|
|
|
|
assert_equal( name_new, group_lookup[:name], 'verify by where name_new' )
|
|
|
|
assert_nil( Group.cache_get(cache_key) )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
group_lookup_name = Group.lookup( name: group_new.name )
|
|
|
|
assert_nil( group_lookup_name, 'verify by lookup.name name_old' )
|
|
|
|
assert_nil( Group.cache_get(cache_key) )
|
|
|
|
|
|
|
|
group_lookup_name = Group.lookup( name: group.name )
|
2013-03-11 21:15:21 +00:00
|
|
|
assert_equal( name_new, group_lookup_name[:name], 'verify by lookup.name name_new' )
|
2015-06-18 21:26:58 +00:00
|
|
|
assert( Group.cache_get(cache_key) )
|
|
|
|
|
|
|
|
# lookup by id
|
|
|
|
cache_key = "#{group_new.id}"
|
|
|
|
assert_nil( Group.cache_get(cache_key) )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
group_lookup_id = Group.lookup( id: group.id )
|
2013-03-11 21:15:21 +00:00
|
|
|
assert_equal( name_new, group_lookup_id[:name], 'verify by lookup.id' )
|
2015-06-18 21:26:58 +00:00
|
|
|
assert( Group.cache_get(cache_key) )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
|
|
|
group.destroy
|
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
# lookup by name
|
|
|
|
group_lookup = Group.where( name: group_new.name ).first
|
|
|
|
assert_nil( group_lookup, 'verify by where name_old' )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
group_lookup = Group.where( name: group.name ).first
|
|
|
|
assert_nil( group_lookup, 'verify by where name_new' )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
group_lookup_name = Group.lookup( name: group_new.name )
|
|
|
|
assert_nil( group_lookup_name, 'verify by lookup.name name_old' )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
group_lookup_name = Group.lookup( name: group.name )
|
|
|
|
assert_nil( group_lookup_name, 'verify by lookup.name name_new' )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 21:26:58 +00:00
|
|
|
# lookup by id
|
2015-04-27 13:42:53 +00:00
|
|
|
group_lookup_id = Group.lookup( id: group.id )
|
2015-06-18 21:26:58 +00:00
|
|
|
assert_nil( group_lookup_id, 'verify by lookup.id' )
|
2013-03-11 21:15:21 +00:00
|
|
|
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|