change_detector_agent_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. require 'rails_helper'
  2. describe Agents::ChangeDetectorAgent do
  3. def create_event(output=nil)
  4. event = Event.new
  5. event.agent = agents(:jane_weather_agent)
  6. event.payload = {
  7. :command => 'some-command',
  8. :output => output
  9. }
  10. event.save!
  11. event
  12. end
  13. before do
  14. @valid_params = {
  15. :property => "{{output}}",
  16. :expected_update_period_in_days => "1",
  17. }
  18. @checker = Agents::ChangeDetectorAgent.new(:name => "somename", :options => @valid_params)
  19. @checker.user = users(:jane)
  20. @checker.save!
  21. end
  22. describe "validation" do
  23. before do
  24. expect(@checker).to be_valid
  25. end
  26. it "should validate presence of property" do
  27. @checker.options[:property] = nil
  28. expect(@checker).not_to be_valid
  29. end
  30. it "should validate presence of property" do
  31. @checker.options[:expected_update_period_in_days] = nil
  32. expect(@checker).not_to be_valid
  33. end
  34. end
  35. describe "#working?" do
  36. before :each do
  37. # Need to create an event otherwise event_created_within? returns nil
  38. event = create_event
  39. @checker.receive([event])
  40. end
  41. it "is when event created within :expected_update_period_in_days" do
  42. @checker.options[:expected_update_period_in_days] = 2
  43. expect(@checker).to be_working
  44. end
  45. it "isnt when event created outside :expected_update_period_in_days" do
  46. @checker.options[:expected_update_period_in_days] = 2
  47. travel 49.hours do
  48. expect(@checker).not_to be_working
  49. end
  50. end
  51. end
  52. describe "#receive" do
  53. before :each do
  54. @event = create_event("2014-07-01")
  55. end
  56. it "creates events when memory is empty" do
  57. @event.payload[:output] = "2014-07-01"
  58. expect {
  59. @checker.receive([@event])
  60. }.to change(Event, :count).by(1)
  61. expect(Event.last.payload[:command]).to eq(@event.payload[:command])
  62. expect(Event.last.payload[:output]).to eq(@event.payload[:output])
  63. end
  64. it "creates events when new event changed" do
  65. @event.payload[:output] = "2014-07-01"
  66. @checker.receive([@event])
  67. event = create_event("2014-08-01")
  68. expect {
  69. @checker.receive([event])
  70. }.to change(Event, :count).by(1)
  71. end
  72. it "does not create event when no change" do
  73. @event.payload[:output] = "2014-07-01"
  74. @checker.receive([@event])
  75. expect {
  76. @checker.receive([@event])
  77. }.to change(Event, :count).by(0)
  78. end
  79. end
  80. describe "#receive using last_property to track lowest value" do
  81. before :each do
  82. @event = create_event("100")
  83. end
  84. before do
  85. # Evaluate the output as number and detect a new lowest value
  86. @checker.options['property'] = '{% assign drop = last_property | minus: output %}{% if last_property == blank or drop > 0 %}{{ output | default: last_property }}{% else %}{{ last_property }}{% endif %}'
  87. end
  88. it "creates events when the value drops" do
  89. @checker.receive([@event])
  90. event = create_event("90")
  91. expect {
  92. @checker.receive([event])
  93. }.to change(Event, :count).by(1)
  94. expect(@checker.memory['last_property']).to eq "90"
  95. end
  96. it "does not create event when the value does not change" do
  97. @checker.receive([@event])
  98. event = create_event("100")
  99. expect {
  100. @checker.receive([event])
  101. }.not_to change(Event, :count)
  102. expect(@checker.memory['last_property']).to eq "100"
  103. end
  104. it "does not create event when the value rises" do
  105. @checker.receive([@event])
  106. event = create_event("110")
  107. expect {
  108. @checker.receive([event])
  109. }.not_to change(Event, :count)
  110. expect(@checker.memory['last_property']).to eq "100"
  111. end
  112. it "does not create event when the value is blank" do
  113. @checker.receive([@event])
  114. event = create_event("")
  115. expect {
  116. @checker.receive([event])
  117. }.not_to change(Event, :count)
  118. expect(@checker.memory['last_property']).to eq "100"
  119. end
  120. it "creates events when memory is empty" do
  121. expect {
  122. @checker.receive([@event])
  123. }.to change(Event, :count).by(1)
  124. expect(@checker.memory['last_property']).to eq "100"
  125. end
  126. end
  127. end