object_cache_test.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class ObjectCacheTest < ActiveSupport::TestCase
  4. test 'organization cache' do
  5. org = Organization.create_or_update(
  6. name: 'some org cache member',
  7. updated_by_id: 1,
  8. created_by_id: 1,
  9. )
  10. roles = Role.where( name: %w(Agent Admin) )
  11. groups = Group.all
  12. user1 = User.create_or_update(
  13. login: 'object_cache1@example.org',
  14. firstname: 'object_cache1',
  15. lastname: 'object_cache1',
  16. email: 'object_cache1@example.org',
  17. password: 'some_pass',
  18. active: true,
  19. updated_by_id: 1,
  20. created_by_id: 1,
  21. organization_id: org.id,
  22. roles: roles,
  23. groups: groups,
  24. )
  25. assets = org.assets({})
  26. assert_equal(org.member_ids.sort, assets[:Organization][org.id]['member_ids'].sort)
  27. user1.organization_id = nil
  28. user1.save
  29. assets = org.assets({})
  30. assert_equal(org.member_ids.sort, assets[:Organization][org.id]['member_ids'].sort)
  31. end
  32. test 'user cache' do
  33. roles = Role.where( name: %w(Agent Admin) )
  34. groups = Group.all
  35. user1 = User.create_or_update(
  36. login: 'object_cache1@example.org',
  37. firstname: 'object_cache1',
  38. lastname: 'object_cache1',
  39. email: 'object_cache1@example.org',
  40. password: 'some_pass',
  41. active: true,
  42. updated_by_id: 1,
  43. created_by_id: 1,
  44. roles: roles,
  45. groups: groups,
  46. )
  47. assets = user1.assets({})
  48. assert_equal(user1.group_ids.sort, assets[:User][user1.id]['group_ids'].sort)
  49. # update group
  50. group1 = groups.first
  51. group1.note = "some note #{rand(9_999_999_999)}"
  52. group1.save
  53. assets = user1.assets({})
  54. assert_equal(group1.note, assets[:Group][group1.id]['note'])
  55. # update group
  56. assert_equal(user1.group_ids.sort, assets[:User][user1.id]['group_ids'].sort)
  57. user1.group_ids = []
  58. user1.save
  59. assets = user1.assets({})
  60. assert_equal(user1.group_ids.sort, assets[:User][user1.id]['group_ids'].sort)
  61. # update role
  62. assert_equal(user1.role_ids.sort, assets[:User][user1.id]['role_ids'].sort)
  63. user1.role_ids = []
  64. user1.save
  65. assets = user1.assets({})
  66. assert_equal(user1.role_ids.sort, assets[:User][user1.id]['role_ids'].sort)
  67. # update groups
  68. assert_equal(user1.organization_ids.sort, assets[:User][user1.id]['organization_ids'].sort)
  69. user1.organization_ids = [1]
  70. user1.save
  71. assets = user1.assets({})
  72. assert_equal(user1.organization_ids.sort, assets[:User][user1.id]['organization_ids'].sort)
  73. end
  74. test 'group cache' do
  75. name = 'object cache test ' + rand(9_999_999).to_s
  76. group = Group.create(
  77. name: name,
  78. updated_by_id: 1,
  79. created_by_id: 1,
  80. )
  81. group_new = Group.where( name: name).first
  82. assert_equal(name, group_new[:name], 'verify by where')
  83. # lookup by name
  84. cache_key = group_new.name.to_s
  85. assert_nil(Group.cache_get(cache_key))
  86. group_lookup_name = Group.lookup(name: group_new.name)
  87. assert_equal(group_new.name, group_lookup_name[:name], 'verify by lookup.name')
  88. assert(Group.cache_get(cache_key))
  89. # lookup by id
  90. cache_key = group_new.id.to_s
  91. assert_nil(Group.cache_get(cache_key))
  92. group_lookup_id = Group.lookup(id: group.id)
  93. assert_equal(group_new.name, group_lookup_id[:name], 'verify by lookup.id')
  94. assert(Group.cache_get(cache_key))
  95. # update / check if old name caches are deleted
  96. name_new = name + ' next'
  97. group.name = name_new
  98. group.save
  99. # lookup by name
  100. cache_key = group.name.to_s
  101. assert_nil(Group.cache_get(cache_key))
  102. group_lookup = Group.where(name: group_new.name).first
  103. assert_nil(group_lookup, 'verify by where name_old')
  104. assert_nil(Group.cache_get(cache_key))
  105. group_lookup = Group.where(name: group.name).first
  106. assert_equal(name_new, group_lookup[:name], 'verify by where name_new')
  107. assert_nil(Group.cache_get(cache_key))
  108. group_lookup_name = Group.lookup(name: group_new.name)
  109. assert_nil(group_lookup_name, 'verify by lookup.name name_old')
  110. assert_nil(Group.cache_get(cache_key))
  111. group_lookup_name = Group.lookup(name: group.name)
  112. assert_equal(name_new, group_lookup_name[:name], 'verify by lookup.name name_new')
  113. assert(Group.cache_get(cache_key))
  114. # lookup by id
  115. cache_key = group_new.id.to_s
  116. assert_nil(Group.cache_get(cache_key))
  117. group_lookup_id = Group.lookup(id: group.id)
  118. assert_equal(name_new, group_lookup_id[:name], 'verify by lookup.id')
  119. assert(Group.cache_get(cache_key))
  120. group.destroy
  121. # lookup by name
  122. group_lookup = Group.where(name: group_new.name).first
  123. assert_nil(group_lookup, 'verify by where name_old')
  124. group_lookup = Group.where(name: group.name).first
  125. assert_nil(group_lookup, 'verify by where name_new')
  126. group_lookup_name = Group.lookup(name: group_new.name)
  127. assert_nil(group_lookup_name, 'verify by lookup.name name_old')
  128. group_lookup_name = Group.lookup(name: group.name)
  129. assert_nil(group_lookup_name, 'verify by lookup.name name_new')
  130. # lookup by id
  131. group_lookup_id = Group.lookup(id: group.id)
  132. assert_nil(group_lookup_id, 'verify by lookup.id')
  133. end
  134. end