increment_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. require 'rails_helper'
  2. RSpec.describe Ticket::Number::Increment do
  3. describe '.generate' do
  4. let(:number) { described_class.generate }
  5. let(:system_id) { Setting.get('system_id') }
  6. let(:ticket_count) { Ticket::Counter.find_by(generator: 'Increment').content }
  7. it 'updates the "Increment" Ticket::Counter' do
  8. expect { number }
  9. .to change { Ticket::Counter.find_by(generator: 'Increment').content }
  10. end
  11. context 'with a "ticket_number_increment" setting with...' do
  12. context 'min_size: 5' do
  13. before { Setting.set('ticket_number_increment', { min_size: 5 }) }
  14. it 'returns a 5-character string' do
  15. expect(number).to be_a(String)
  16. expect(number.length).to be(5)
  17. end
  18. context 'when "system_id" setting exceeds :min_size' do
  19. before { Setting.set('system_id', 123_456) }
  20. it 'still adheres to numbering pattern (and does not require padding zeroes)' do
  21. expect(number).to match(/^#{system_id}#{ticket_count}$/)
  22. end
  23. end
  24. it 'returns a string following the pattern system_id + padding zeroes + ticket_count' do
  25. expect(number).to match(/^#{system_id}0*#{ticket_count}$/)
  26. end
  27. context '/ checksum: false (default)' do
  28. before { Setting.set('ticket_number_increment', { min_size: 5, checksum: false }) }
  29. it 'returns a 5-character string' do
  30. expect(number).to be_a(String)
  31. expect(number.length).to be(5)
  32. end
  33. it 'returns a string following the pattern system_id + padding zeroes + ticket_counter' do
  34. expect(number).to match(/^#{system_id}0*#{ticket_count}$/)
  35. end
  36. context 'when "system_id" setting exceeds :min_size' do
  37. before { Setting.set('system_id', 123_456) }
  38. it 'still adheres to numbering pattern (and does not require padding zeroes)' do
  39. expect(number).to match(/^#{system_id}#{ticket_count}$/)
  40. end
  41. end
  42. end
  43. context '/ checksum: true' do
  44. before { Setting.set('ticket_number_increment', { min_size: 5, checksum: true }) }
  45. it 'returns a 5-character string' do
  46. expect(number).to be_a(String)
  47. expect(number).to eq(number.to_i.to_s)
  48. expect(number.length).to be(5)
  49. end
  50. it 'returns a string following the pattern system_id + padding zeroes + ticket_counter + checksum' do
  51. expect(number).to match(/^#{system_id}0*#{ticket_count}\d$/)
  52. end
  53. context 'when "system_id" setting exceeds :min_size' do
  54. before { Setting.set('system_id', 123_456) }
  55. it 'still adheres to numbering pattern (and does not require padding zeroes)' do
  56. expect(number).to match(/^#{system_id}#{ticket_count}\d$/)
  57. end
  58. end
  59. end
  60. end
  61. end
  62. end
  63. describe '.check' do
  64. context 'for tickets with increment-style numbers' do
  65. let(:ticket) { create(:ticket, number: ticket_number) }
  66. let(:ticket_number) { "#{Setting.get('system_id')}0001" }
  67. let(:check_query) { ticket.subject_build(ticket.title) }
  68. context 'when system_id is the same as when ticket was created' do
  69. before do
  70. Setting.set('system_id', 1)
  71. ticket # create ticket
  72. end
  73. it 'returns the ticket matching the number in the given string' do
  74. expect(described_class.check(check_query)).to eq(ticket)
  75. end
  76. end
  77. context 'when system_id is different from when ticket was created' do
  78. before do
  79. Setting.set('system_id', 1)
  80. ticket # create ticket
  81. Setting.set('system_id', 999)
  82. end
  83. it 'returns nil' do
  84. expect(described_class.check(check_query)).to be(nil)
  85. end
  86. context 'and "ticket_number_ignore_system_id" is true' do
  87. before { Setting.set('ticket_number_ignore_system_id', true) }
  88. it 'returns the ticket matching the number in the given string' do
  89. expect(described_class.check(check_query)).to eq(ticket)
  90. end
  91. end
  92. end
  93. end
  94. end
  95. end