attribute_difference_agent_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. require 'rails_helper'
  2. describe Agents::AttributeDifferenceAgent do
  3. def create_event(value=nil)
  4. event = Event.new
  5. event.agent = agents(:jane_weather_agent)
  6. event.payload = {
  7. rate: value
  8. }
  9. event.save!
  10. event
  11. end
  12. before do
  13. @valid_params = {
  14. path: 'rate',
  15. output: 'rate_diff',
  16. method: 'integer_difference',
  17. expected_update_period_in_days: '1'
  18. }
  19. @checker = Agents::AttributeDifferenceAgent.new(name: 'somename', options: @valid_params)
  20. @checker.user = users(:jane)
  21. @checker.save!
  22. end
  23. describe 'validation' do
  24. before do
  25. expect(@checker).to be_valid
  26. end
  27. it 'should validate presence of output' do
  28. @checker.options[:output] = nil
  29. expect(@checker).not_to be_valid
  30. end
  31. it 'should validate presence of path' do
  32. @checker.options[:path] = nil
  33. expect(@checker).not_to be_valid
  34. end
  35. it 'should validate presence of method' do
  36. @checker.options[:method] = nil
  37. expect(@checker).not_to be_valid
  38. end
  39. it 'should validate presence of expected_update_period_in_days' do
  40. @checker.options[:expected_update_period_in_days] = nil
  41. expect(@checker).not_to be_valid
  42. end
  43. end
  44. describe '#working?' do
  45. before :each do
  46. # Need to create an event otherwise event_created_within? returns nil
  47. event = create_event
  48. @checker.receive([event])
  49. end
  50. it 'is when event created within :expected_update_period_in_days' do
  51. @checker.options[:expected_update_period_in_days] = 2
  52. expect(@checker).to be_working
  53. end
  54. it 'isnt when event created outside :expected_update_period_in_days' do
  55. @checker.options[:expected_update_period_in_days] = 2
  56. travel 49.hours do
  57. expect(@checker).not_to be_working
  58. end
  59. end
  60. end
  61. describe '#receive' do
  62. before :each do
  63. @event = create_event('5.5')
  64. end
  65. it 'creates events when memory is empty' do
  66. expect {
  67. @checker.receive([@event])
  68. }.to change(Event, :count).by(1)
  69. expect(Event.last.payload[:rate_diff]).to eq(0)
  70. end
  71. it 'creates event with extra attribute for integer_difference' do
  72. @checker.receive([@event])
  73. event = create_event('6.5')
  74. expect {
  75. @checker.receive([event])
  76. }.to change(Event, :count).by(1)
  77. expect(Event.last.payload[:rate_diff]).to eq(1)
  78. end
  79. it 'creates event with extra attribute for decimal_difference' do
  80. @checker.options[:method] = 'decimal_difference'
  81. @checker.receive([@event])
  82. event = create_event('6.4')
  83. expect {
  84. @checker.receive([event])
  85. }.to change(Event, :count).by(1)
  86. expect(Event.last.payload[:rate_diff]).to eq(0.9)
  87. end
  88. it 'creates event with extra attribute for percentage_change' do
  89. @checker.options[:method] = 'percentage_change'
  90. @checker.receive([@event])
  91. event = create_event('9')
  92. expect {
  93. @checker.receive([event])
  94. }.to change(Event, :count).by(1)
  95. expect(Event.last.payload[:rate_diff]).to eq(63.636)
  96. end
  97. it 'creates event with extra attribute for percentage_change with the correct rounding' do
  98. @checker.options[:method] = 'percentage_change'
  99. @checker.options[:decimal_precision] = 5
  100. @checker.receive([@event])
  101. event = create_event('9')
  102. expect {
  103. @checker.receive([event])
  104. }.to change(Event, :count).by(1)
  105. expect(Event.last.payload[:rate_diff]).to eq(63.63636)
  106. end
  107. end
  108. end