de_duplication_agent_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require 'spec_helper'
  2. describe Agents::DeDuplicationAgent do
  3. def create_event(output=nil)
  4. event = Event.new
  5. event.agent = agents(:jane_weather_agent)
  6. event.payload = {
  7. :output => output
  8. }
  9. event.save!
  10. event
  11. end
  12. before do
  13. @valid_params = {
  14. :property => "{{output}}",
  15. :lookback => 3,
  16. :expected_update_period_in_days => "1",
  17. }
  18. @checker = Agents::DeDuplicationAgent.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 lookback" do
  27. @checker.options[:lookback] = 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 is unique" 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 event is a duplicate" 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. it "should respect the lookback value" do
  80. 3.times do |i|
  81. @event.payload[:output] = "2014-07-0#{i}"
  82. @checker.receive([@event])
  83. end
  84. @event.payload[:output] = "2014-07-05"
  85. expect {
  86. @checker.receive([@event])
  87. }.to change(Event, :count).by(1)
  88. expect(@checker.memory['properties'].length).to eq(3)
  89. expect(@checker.memory['properties']).to eq(["2014-07-01", "2014-07-02", "2014-07-05"])
  90. end
  91. it "should hash the value if its longer then 10 chars" do
  92. @event.payload[:output] = "01234567890"
  93. expect {
  94. @checker.receive([@event])
  95. }.to change(Event, :count).by(1)
  96. expect(@checker.memory['properties'].last).to eq('2256157795')
  97. end
  98. it "should use the whole event if :property is blank" do
  99. @checker.options['property'] = ''
  100. expect {
  101. @checker.receive([@event])
  102. }.to change(Event, :count).by(1)
  103. expect(@checker.memory['properties'].last).to eq('3023526198')
  104. end
  105. end
  106. end