change_detector_agent_spec.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. require 'spec_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. time_travel_to 2.days.from_now 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. end