model_test.rb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class ModelTest < ActiveSupport::TestCase
  4. test 'create_if_not_exists test' do
  5. group1 = Group.create_if_not_exists(
  6. name: 'model1-create_if_not_exists',
  7. active: true,
  8. updated_at: '2015-02-05 16:37:00',
  9. updated_by_id: 1,
  10. created_by_id: 1,
  11. )
  12. assert_raises( ActiveRecord::RecordNotUnique ) {
  13. Group.create_if_not_exists(
  14. name: 'model1-Create_If_Not_Exists',
  15. active: true,
  16. updated_at: '2015-02-05 16:37:00',
  17. updated_by_id: 1,
  18. created_by_id: 1,
  19. )
  20. }
  21. group2 = Group.create_if_not_exists(
  22. name: 'model1-create_if_not_exists',
  23. active: true,
  24. updated_at: '2015-02-05 16:39:00',
  25. updated_by_id: 1,
  26. created_by_id: 1,
  27. )
  28. assert_equal(group1.id, group2.id)
  29. assert_equal(group2.updated_at.to_s, '2015-02-05 16:37:00 UTC')
  30. end
  31. test 'create_or_update test' do
  32. group1 = Group.create_or_update(
  33. name: 'model1-create_or_update',
  34. active: true,
  35. updated_at: '2015-02-05 16:37:00',
  36. updated_by_id: 1,
  37. created_by_id: 1,
  38. )
  39. assert_raises( ActiveRecord::RecordNotUnique ) {
  40. Group.create_or_update(
  41. name: 'model1-Create_Or_Update',
  42. active: true,
  43. updated_at: '2015-02-05 16:37:00',
  44. updated_by_id: 1,
  45. created_by_id: 1,
  46. )
  47. }
  48. group2 = Group.create_or_update(
  49. name: 'model1-create_or_update',
  50. active: true,
  51. updated_at: '2015-02-05 16:39:00',
  52. updated_by_id: 1,
  53. created_by_id: 1,
  54. )
  55. assert_equal(group1.id, group2.id)
  56. assert_equal(group2.updated_at.to_s, '2015-02-05 16:39:00 UTC')
  57. end
  58. test 'references test' do
  59. # create base
  60. groups = Group.where( name: 'Users' )
  61. roles = Role.where( name: %w(Agent Admin) )
  62. agent1 = User.create_or_update(
  63. login: 'model-agent1@example.com',
  64. firstname: 'Model',
  65. lastname: 'Agent1',
  66. email: 'model-agent1@example.com',
  67. password: 'agentpw',
  68. active: true,
  69. roles: roles,
  70. groups: groups,
  71. updated_at: '2015-02-05 16:37:00',
  72. updated_by_id: 1,
  73. created_by_id: 1,
  74. )
  75. agent2 = User.create_or_update(
  76. login: 'model-agent2@example.com',
  77. firstname: 'Model',
  78. lastname: 'Agent2',
  79. email: 'model-agent2@example.com',
  80. password: 'agentpw',
  81. active: true,
  82. roles: roles,
  83. groups: groups,
  84. updated_at: '2015-02-05 17:37:00',
  85. updated_by_id: agent1.id,
  86. created_by_id: 1,
  87. )
  88. organization1 = Organization.create_if_not_exists(
  89. name: 'Model Org 1',
  90. updated_at: '2015-02-05 16:37:00',
  91. updated_by_id: 1,
  92. created_by_id: 1,
  93. )
  94. organization2 = Organization.create_if_not_exists(
  95. name: 'Model Org 2',
  96. updated_at: '2015-02-05 16:37:00',
  97. updated_by_id: agent1.id,
  98. created_by_id: 1,
  99. )
  100. roles = Role.where( name: 'Customer' )
  101. customer1 = User.create_or_update(
  102. login: 'model-customer1@example.com',
  103. firstname: 'Model',
  104. lastname: 'Customer1',
  105. email: 'model-customer1@example.com',
  106. password: 'customerpw',
  107. active: true,
  108. organization_id: organization1.id,
  109. roles: roles,
  110. updated_at: '2015-02-05 16:37:00',
  111. updated_by_id: 1,
  112. created_by_id: 1,
  113. )
  114. customer2 = User.create_or_update(
  115. login: 'model-customer2@example.com',
  116. firstname: 'Model',
  117. lastname: 'Customer2',
  118. email: 'model-customer2@example.com',
  119. password: 'customerpw',
  120. active: true,
  121. organization_id: nil,
  122. roles: roles,
  123. updated_at: '2015-02-05 16:37:00',
  124. updated_by_id: agent1.id,
  125. created_by_id: 1,
  126. )
  127. customer3 = User.create_or_update(
  128. login: 'model-customer3@example.com',
  129. firstname: 'Model',
  130. lastname: 'Customer3',
  131. email: 'model-customer3@example.com',
  132. password: 'customerpw',
  133. active: true,
  134. organization_id: nil,
  135. roles: roles,
  136. updated_at: '2015-02-05 16:37:00',
  137. updated_by_id: agent1.id,
  138. created_by_id: agent1.id,
  139. )
  140. # user
  141. # verify agent1
  142. references1 = Models.references('User', agent1.id)
  143. assert_equal(references1['User']['updated_by_id'], 3)
  144. assert_equal(references1['User']['created_by_id'], 1)
  145. assert_equal(references1['Organization']['updated_by_id'], 1)
  146. assert(!references1['Group'])
  147. references_total1 = Models.references_total('User', agent1.id)
  148. assert_equal(references_total1, 7)
  149. # verify agent2
  150. references2 = Models.references('User', agent2.id)
  151. assert(!references2['User'])
  152. assert(!references2['Organization'])
  153. assert(!references2['Group'])
  154. assert(references2.empty?)
  155. references_total2 = Models.references_total('User', agent2.id)
  156. assert_equal(references_total2, 0)
  157. Models.merge('User', agent2.id, agent1.id)
  158. # verify agent1
  159. references1 = Models.references('User', agent1.id)
  160. assert(!references1['User'])
  161. assert(!references1['Organization'])
  162. assert(!references1['Group'])
  163. assert(references1.empty?)
  164. references_total1 = Models.references_total('User', agent1.id)
  165. assert_equal(references_total1, 0)
  166. # verify agent2
  167. references2 = Models.references('User', agent2.id)
  168. assert_equal(references2['User']['updated_by_id'], 3)
  169. assert_equal(references2['User']['created_by_id'], 1)
  170. assert_equal(references2['Organization']['updated_by_id'], 1)
  171. assert(!references2['Group'])
  172. references_total2 = Models.references_total('User', agent2.id)
  173. assert_equal(references_total2, 7)
  174. # org
  175. # verify agent1
  176. references1 = Models.references('Organization', organization1.id)
  177. assert_equal(references1['User']['organization_id'], 1)
  178. assert(!references1['Organization'])
  179. assert(!references1['Group'])
  180. references_total1 = Models.references_total('Organization', organization1.id)
  181. assert_equal(references_total1, 1)
  182. # verify agent2
  183. references2 = Models.references('Organization', organization2.id)
  184. assert(references2.empty?)
  185. references_total2 = Models.references_total('Organization', organization2.id)
  186. assert_equal(references_total2, 0)
  187. Models.merge('Organization', organization2.id, organization1.id)
  188. # verify agent1
  189. references1 = Models.references('Organization', organization1.id)
  190. assert(references1.empty?)
  191. references_total1 = Models.references_total('Organization', organization1.id)
  192. assert_equal(references_total1, 0)
  193. # verify agent2
  194. references2 = Models.references('Organization', organization2.id)
  195. assert_equal(references2['User']['organization_id'], 1)
  196. assert(!references2['Organization'])
  197. assert(!references2['Group'])
  198. references_total2 = Models.references_total('Organization', organization2.id)
  199. assert_equal(references_total2, 1)
  200. end
  201. test 'searchable test' do
  202. searchable = Models.searchable
  203. assert(searchable.include?(Ticket))
  204. assert(searchable.include?(User))
  205. assert(searchable.include?(Organization))
  206. assert_equal(3, searchable.count)
  207. end
  208. end