2013-03-11 21:15:21 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'test_helper'
|
|
|
|
|
|
|
|
class ObjectCacheTest < ActiveSupport::TestCase
|
2015-06-18 22:39:34 +00:00
|
|
|
test 'organization cache' do
|
|
|
|
org = Organization.create_or_update(
|
|
|
|
name: 'some org cache member',
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
)
|
|
|
|
|
|
|
|
roles = Role.where( name: %w(Agent Admin) )
|
|
|
|
groups = Group.all
|
|
|
|
user1 = User.create_or_update(
|
|
|
|
login: 'object_cache1@example.org',
|
|
|
|
firstname: 'object_cache1',
|
|
|
|
lastname: 'object_cache1',
|
|
|
|
email: 'object_cache1@example.org',
|
|
|
|
password: 'some_pass',
|
|
|
|
active: true,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
organization_id: org.id,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
|
|
|
)
|
|
|
|
assets = org.assets({})
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(org.member_ids.sort, assets[:Organization][org.id]['member_ids'].sort)
|
2015-06-18 22:39:34 +00:00
|
|
|
|
|
|
|
user1.organization_id = nil
|
|
|
|
user1.save
|
|
|
|
|
|
|
|
assets = org.assets({})
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(org.member_ids.sort, assets[:Organization][org.id]['member_ids'].sort)
|
2015-06-18 22:39:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'user cache' do
|
|
|
|
roles = Role.where( name: %w(Agent Admin) )
|
|
|
|
groups = Group.all
|
2017-04-19 07:37:43 +00:00
|
|
|
|
|
|
|
# be sure that minimum one admin is available
|
|
|
|
User.create_or_update(
|
|
|
|
login: 'last_admin_check@example.org',
|
|
|
|
firstname: 'last_admin_check',
|
|
|
|
lastname: 'last_admin_check',
|
|
|
|
email: 'last_admin_check@example.org',
|
|
|
|
password: 'some_pass',
|
|
|
|
active: true,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
|
|
|
)
|
|
|
|
|
2015-06-18 22:39:34 +00:00
|
|
|
user1 = User.create_or_update(
|
|
|
|
login: 'object_cache1@example.org',
|
|
|
|
firstname: 'object_cache1',
|
|
|
|
lastname: 'object_cache1',
|
|
|
|
email: 'object_cache1@example.org',
|
|
|
|
password: 'some_pass',
|
|
|
|
active: true,
|
|
|
|
updated_by_id: 1,
|
|
|
|
created_by_id: 1,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups,
|
|
|
|
)
|
|
|
|
assets = user1.assets({})
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(user1.group_ids.sort, assets[:User][user1.id]['group_ids'].sort)
|
2015-06-18 22:39:34 +00:00
|
|
|
|
|
|
|
# update group
|
|
|
|
group1 = groups.first
|
2015-07-03 15:32:48 +00:00
|
|
|
group1.note = "some note #{rand(9_999_999_999)}"
|
2015-06-18 22:39:34 +00:00
|
|
|
group1.save
|
|
|
|
|
|
|
|
assets = user1.assets({})
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(group1.note, assets[:Group][group1.id]['note'])
|
2015-06-18 22:39:34 +00:00
|
|
|
|
|
|
|
# update group
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(user1.group_ids.sort, assets[:User][user1.id]['group_ids'].sort)
|
2015-06-18 22:39:34 +00:00
|
|
|
user1.group_ids = []
|
|
|
|
user1.save
|
|
|
|
|
|
|
|
assets = user1.assets({})
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(user1.group_ids.sort, assets[:User][user1.id]['group_ids'].sort)
|
2015-06-18 22:39:34 +00:00
|
|
|
|
|
|
|
# update role
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(user1.role_ids.sort, assets[:User][user1.id]['role_ids'].sort)
|
2015-06-18 22:39:34 +00:00
|
|
|
user1.role_ids = []
|
|
|
|
user1.save
|
|
|
|
|
|
|
|
assets = user1.assets({})
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(user1.role_ids.sort, assets[:User][user1.id]['role_ids'].sort)
|
2015-06-18 22:39:34 +00:00
|
|
|
|
|
|
|
# update groups
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(user1.organization_ids.sort, assets[:User][user1.id]['organization_ids'].sort)
|
2015-06-18 22:39:34 +00:00
|
|
|
user1.organization_ids = [1]
|
|
|
|
user1.save
|
|
|
|
|
|
|
|
assets = user1.assets({})
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_equal(user1.organization_ids.sort, assets[:User][user1.id]['organization_ids'].sort)
|
2015-06-18 22:39:34 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'group cache' do
|
2013-03-11 21:15:21 +00:00
|
|
|
|
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
|
|
|
)
|
2016-02-22 08:51:32 +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
|
2016-01-15 17:22:57 +00:00
|
|
|
cache_key = group_new.name.to_s
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_nil(Group.cache_get(cache_key))
|
2015-06-18 21:26:58 +00:00
|
|
|
|
2016-02-22 08:51:32 +00:00
|
|
|
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))
|
2015-06-18 21:26:58 +00:00
|
|
|
|
|
|
|
# lookup by id
|
2016-01-15 17:22:57 +00:00
|
|
|
cache_key = group_new.id.to_s
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_nil(Group.cache_get(cache_key))
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2016-02-22 08:51:32 +00:00
|
|
|
group_lookup_id = Group.lookup(id: group.id)
|
|
|
|
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
|
2016-01-15 17:22:57 +00:00
|
|
|
cache_key = group.name.to_s
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_nil(Group.cache_get(cache_key))
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2016-02-22 08:51:32 +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
|
|
|
|
2016-02-22 08:51:32 +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
|
|
|
|
2016-02-22 08:51:32 +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))
|
2015-06-18 21:26:58 +00:00
|
|
|
|
2016-02-22 08:51:32 +00:00
|
|
|
group_lookup_name = Group.lookup(name: group.name)
|
|
|
|
assert_equal(name_new, group_lookup_name[:name], 'verify by lookup.name name_new')
|
|
|
|
assert(Group.cache_get(cache_key))
|
2015-06-18 21:26:58 +00:00
|
|
|
|
|
|
|
# lookup by id
|
2016-01-15 17:22:57 +00:00
|
|
|
cache_key = group_new.id.to_s
|
2016-02-22 08:51:32 +00:00
|
|
|
assert_nil(Group.cache_get(cache_key))
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2016-02-22 08:51:32 +00:00
|
|
|
group_lookup_id = Group.lookup(id: group.id)
|
|
|
|
assert_equal(name_new, group_lookup_id[:name], 'verify by lookup.id')
|
|
|
|
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
|
2016-02-22 08:51:32 +00:00
|
|
|
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
|
|
|
|
2016-02-22 08:51:32 +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
|
|
|
|
2016-02-22 08:51:32 +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
|
|
|
|
2016-02-22 08:51:32 +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
|
2016-02-22 08:51:32 +00:00
|
|
|
group_lookup_id = Group.lookup(id: group.id)
|
|
|
|
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
|