ticket_number_test.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. require 'test_helper'
  2. class TicketNumberTest < ActiveSupport::TestCase
  3. test 'number' do
  4. Setting.set('ticket_number_increment', { checksum: false, min_size: 5 })
  5. Setting.set('system_id', 1)
  6. number = Ticket::Number.generate
  7. assert_equal(number.to_s.length, 5)
  8. Setting.set('ticket_number_increment', { checksum: false, min_size: 10 })
  9. Setting.set('system_id', 1)
  10. number = Ticket::Number.generate
  11. assert_equal(number.to_s.length, 10)
  12. Setting.set('ticket_number_increment', { checksum: true, min_size: 5 })
  13. Setting.set('system_id', 1)
  14. number = Ticket::Number.generate
  15. assert_equal(number.to_s.length, 5)
  16. Setting.set('ticket_number_increment', { checksum: true, min_size: 10 })
  17. Setting.set('system_id', 1)
  18. number = Ticket::Number.generate
  19. assert_equal(number.to_s.length, 10)
  20. Setting.set('ticket_number_increment', { checksum: false, min_size: 5 })
  21. Setting.set('system_id', 88)
  22. number = Ticket::Number.generate
  23. assert_equal(number.to_s.length, 5)
  24. Setting.set('ticket_number_increment', { checksum: false, min_size: 10 })
  25. Setting.set('system_id', 88)
  26. number = Ticket::Number.generate
  27. assert_equal(number.to_s.length, 10)
  28. Setting.set('ticket_number_increment', { checksum: true, min_size: 5 })
  29. Setting.set('system_id', 88)
  30. number = Ticket::Number.generate
  31. assert_equal(number.to_s.length, 5)
  32. Setting.set('ticket_number_increment', { checksum: true, min_size: 10 })
  33. Setting.set('system_id', 88)
  34. number = Ticket::Number.generate
  35. assert_equal(number.to_s.length, 10)
  36. 150.times do
  37. number = Ticket::Number.generate
  38. assert_equal(number.to_s.length, 10)
  39. end
  40. end
  41. test 'number check' do
  42. Setting.set('ticket_number_increment', { checksum: false, min_size: 5 })
  43. Setting.set('system_id', 1)
  44. ticket = Ticket.create!(
  45. title: 'test 1',
  46. group: Group.lookup(name: 'Users'),
  47. customer_id: 2,
  48. state: Ticket::State.lookup(name: 'new'),
  49. priority: Ticket::Priority.lookup(name: '2 normal'),
  50. updated_by_id: 1,
  51. created_by_id: 1,
  52. )
  53. subject = ticket.subject_build(ticket.title)
  54. ticket_check = Ticket::Number.check(subject)
  55. assert_equal(ticket.id, ticket_check.id)
  56. Setting.set('system_id', 999)
  57. ticket_check = Ticket::Number.check(subject)
  58. assert_not(ticket_check)
  59. Setting.set('ticket_number_ignore_system_id', true)
  60. ticket_check = Ticket::Number.check(subject)
  61. assert_equal(ticket.id, ticket_check.id)
  62. end
  63. test 'date' do
  64. Setting.set('ticket_number', 'Ticket::Number::Date')
  65. Setting.set('ticket_number_date', { checksum: false })
  66. Setting.set('system_id', 1)
  67. system_id = Setting.get('system_id')
  68. number_prefix = "#{Time.zone.now.strftime('%Y%m%d')}#{system_id}"
  69. number = Ticket::Number.generate
  70. assert_equal(number.to_s.length, 13)
  71. assert_match(/#{number_prefix}/, number.to_s)
  72. Setting.set('ticket_number_date', { checksum: false })
  73. Setting.set('system_id', 88)
  74. number = Ticket::Number.generate
  75. system_id = Setting.get('system_id')
  76. number_prefix = "#{Time.zone.now.strftime('%Y%m%d')}#{system_id}"
  77. assert_equal(number.to_s.length, 14)
  78. assert_match(/#{number_prefix}/, number.to_s)
  79. Setting.set('ticket_number_date', { checksum: true })
  80. Setting.set('system_id', 1)
  81. number = Ticket::Number.generate
  82. system_id = Setting.get('system_id')
  83. number_prefix = "#{Time.zone.now.strftime('%Y%m%d')}#{system_id}"
  84. assert_equal(number.to_s.length, 14)
  85. assert_match(/#{number_prefix}/, number.to_s)
  86. Setting.set('ticket_number_date', { checksum: true })
  87. Setting.set('system_id', 88)
  88. number = Ticket::Number.generate
  89. system_id = Setting.get('system_id')
  90. number_prefix = "#{Time.zone.now.strftime('%Y%m%d')}#{system_id}"
  91. assert_equal(number.to_s.length, 15)
  92. assert_match(/#{number_prefix}/, number.to_s)
  93. 150.times do
  94. number = Ticket::Number.generate
  95. assert_equal(number.to_s.length, 15)
  96. end
  97. end
  98. test 'date check' do
  99. Setting.set('ticket_number', 'Ticket::Number::Date')
  100. Setting.set('ticket_number_date', { checksum: false })
  101. Setting.set('system_id', 1)
  102. ticket = Ticket.create!(
  103. title: 'test 1',
  104. group: Group.lookup(name: 'Users'),
  105. customer_id: 2,
  106. state: Ticket::State.lookup(name: 'new'),
  107. priority: Ticket::Priority.lookup(name: '2 normal'),
  108. updated_by_id: 1,
  109. created_by_id: 1,
  110. )
  111. subject = ticket.subject_build(ticket.title)
  112. ticket_check = Ticket::Number.check(subject)
  113. assert_equal(ticket.id, ticket_check.id)
  114. Setting.set('system_id', 999)
  115. ticket_check = Ticket::Number.check(subject)
  116. assert_not(ticket_check)
  117. Setting.set('ticket_number_ignore_system_id', true)
  118. ticket_check = Ticket::Number.check(subject)
  119. assert_equal(ticket.id, ticket_check.id)
  120. end
  121. end