model_test.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class ModelTest < ActiveSupport::TestCase
  4. test 'references test' do
  5. # create base
  6. groups = Group.where( name: 'Users' )
  7. roles = Role.where( name: %w(Agent Admin) )
  8. agent1 = User.create_or_update(
  9. login: 'model-agent1@example.com',
  10. firstname: 'Model',
  11. lastname: 'Agent1',
  12. email: 'model-agent1@example.com',
  13. password: 'agentpw',
  14. active: true,
  15. roles: roles,
  16. groups: groups,
  17. updated_at: '2015-02-05 16:37:00',
  18. updated_by_id: 1,
  19. created_by_id: 1,
  20. )
  21. organization1 = Organization.create_if_not_exists(
  22. name: 'Model Org 1',
  23. updated_at: '2015-02-05 16:37:00',
  24. updated_by_id: 1,
  25. created_by_id: 1,
  26. )
  27. organization2 = Organization.create_if_not_exists(
  28. name: 'Model Org 2',
  29. updated_at: '2015-02-05 16:37:00',
  30. updated_by_id: agent1.id,
  31. created_by_id: 1,
  32. )
  33. roles = Role.where( name: 'Customer' )
  34. customer1 = User.create_or_update(
  35. login: 'model-customer1@example.com',
  36. firstname: 'Model',
  37. lastname: 'Customer1',
  38. email: 'model-customer1@example.com',
  39. password: 'customerpw',
  40. active: true,
  41. organization_id: organization1.id,
  42. roles: roles,
  43. updated_at: '2015-02-05 16:37:00',
  44. updated_by_id: 1,
  45. created_by_id: 1,
  46. )
  47. customer2 = User.create_or_update(
  48. login: 'model-customer2@example.com',
  49. firstname: 'Model',
  50. lastname: 'Customer2',
  51. email: 'model-customer2@example.com',
  52. password: 'customerpw',
  53. active: true,
  54. organization_id: nil,
  55. roles: roles,
  56. updated_at: '2015-02-05 16:37:00',
  57. updated_by_id: agent1.id,
  58. created_by_id: 1,
  59. )
  60. customer3 = User.create_or_update(
  61. login: 'model-customer3@example.com',
  62. firstname: 'Model',
  63. lastname: 'Customer3',
  64. email: 'model-customer3@example.com',
  65. password: 'customerpw',
  66. active: true,
  67. organization_id: nil,
  68. roles: roles,
  69. updated_at: '2015-02-05 16:37:00',
  70. updated_by_id: agent1.id,
  71. created_by_id: agent1.id,
  72. )
  73. references = Models.references('User', agent1.id)
  74. assert_equal(references[:model]['User'], 3)
  75. assert_equal(references[:model]['Organization'], 1)
  76. assert_equal(references[:model]['Group'], 0)
  77. assert_equal(references[:total], 6)
  78. end
  79. end