de_duplication_agent_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. require 'rails_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 '#initialize_memory' do
  36. it 'sets properties to an empty array' do
  37. expect(@checker.memory['properties']).to eq([])
  38. end
  39. it 'does not override an existing value' do
  40. @checker.memory['properties'] = [1,2,3]
  41. @checker.save
  42. @checker.reload
  43. expect(@checker.memory['properties']).to eq([1,2,3])
  44. end
  45. end
  46. describe "#working?" do
  47. before :each do
  48. # Need to create an event otherwise event_created_within? returns nil
  49. event = create_event
  50. @checker.receive([event])
  51. end
  52. it "is when event created within :expected_update_period_in_days" do
  53. @checker.options[:expected_update_period_in_days] = 2
  54. expect(@checker).to be_working
  55. end
  56. it "isnt when event created outside :expected_update_period_in_days" do
  57. @checker.options[:expected_update_period_in_days] = 2
  58. travel 49.hours do
  59. expect(@checker).not_to be_working
  60. end
  61. end
  62. end
  63. describe "#receive" do
  64. before :each do
  65. @event = create_event("2014-07-01")
  66. end
  67. it "creates events when memory is empty" do
  68. @event.payload[:output] = "2014-07-01"
  69. expect {
  70. @checker.receive([@event])
  71. }.to change(Event, :count).by(1)
  72. expect(Event.last.payload[:command]).to eq(@event.payload[:command])
  73. expect(Event.last.payload[:output]).to eq(@event.payload[:output])
  74. end
  75. it "creates events when new event is unique" do
  76. @event.payload[:output] = "2014-07-01"
  77. @checker.receive([@event])
  78. event = create_event("2014-08-01")
  79. expect {
  80. @checker.receive([event])
  81. }.to change(Event, :count).by(1)
  82. end
  83. it "does not create event when event is a duplicate" do
  84. @event.payload[:output] = "2014-07-01"
  85. @checker.receive([@event])
  86. expect {
  87. @checker.receive([@event])
  88. }.to change(Event, :count).by(0)
  89. end
  90. it "should respect the lookback value" do
  91. 3.times do |i|
  92. @event.payload[:output] = "2014-07-0#{i}"
  93. @checker.receive([@event])
  94. end
  95. @event.payload[:output] = "2014-07-05"
  96. expect {
  97. @checker.receive([@event])
  98. }.to change(Event, :count).by(1)
  99. expect(@checker.memory['properties'].length).to eq(3)
  100. expect(@checker.memory['properties']).to eq(["2014-07-01", "2014-07-02", "2014-07-05"])
  101. end
  102. it "should hash the value if its longer then 10 chars" do
  103. @event.payload[:output] = "01234567890"
  104. expect {
  105. @checker.receive([@event])
  106. }.to change(Event, :count).by(1)
  107. expect(@checker.memory['properties'].last).to eq('2256157795')
  108. end
  109. it "should use the whole event if :property is blank" do
  110. @checker.options['property'] = ''
  111. expect {
  112. @checker.receive([@event])
  113. }.to change(Event, :count).by(1)
  114. expect(@checker.memory['properties'].last).to eq('3023526198')
  115. end
  116. it "should still work after the memory was cleared" do
  117. @checker.memory = {}
  118. @checker.save
  119. @checker.reload
  120. expect {
  121. @checker.receive([@event])
  122. }.not_to raise_error
  123. end
  124. end
  125. end