Browse Source

fixing #64, don't backfill a newly created agent wiht events from its sources

Albert Sun 11 years ago
parent
commit
9ce8b40a67
2 changed files with 32 additions and 0 deletions
  1. 5 0
      app/models/agent.rb
  2. 27 0
      spec/models/agent_spec.rb

+ 5 - 0
app/models/agent.rb

@@ -26,6 +26,7 @@ class Agent < ActiveRecord::Base
   before_validation :set_default_schedule
   before_validation :unschedule_if_cannot_schedule
   before_save :unschedule_if_cannot_schedule
+  before_create :set_last_checked_event_id
 
   belongs_to :user, :inverse_of => :agents
   has_many :events, :dependent => :delete_all, :inverse_of => :agent, :order => "events.id desc"
@@ -132,6 +133,10 @@ class Agent < ActiveRecord::Base
     !cannot_receive_events?
   end
 
+  def set_last_checked_event_id
+    self.last_checked_event_id = Event.last.id
+  end
+
   # Class Methods
   class << self
     def cannot_be_scheduled!

+ 27 - 0
spec/models/agent_spec.rb

@@ -171,6 +171,33 @@ describe Agent do
       end
     end
 
+    describe "creating a new agent and then .receive!" do
+      before do
+        stub_request(:any, /wunderground/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
+        stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true }
+      end
+
+      it "should not backfill events for a newly created agent" do
+        Agent.async_check(agents(:bob_weather_agent).id)
+        Agent.receive!
+        checker = Agents::TriggerAgent.new(:name => "New trigger agent", :options => {
+          :expected_receive_period_in_days => "2",
+          :rules => [{
+            :type => "regex",
+            :value => "rain",
+            :path => "conditions"
+          }],
+          :message => "Just so you know, it looks like '<conditions>' tomorrow in <zipcode>"
+        })
+        checker.user = users(:bob)
+        checker.sources << agents(:bob_weather_agent)
+        checker.save!
+        checker.sources.first.events.count.should be > 0
+        Agent.receive!
+        checker.events.count.should eq(0)
+      end
+    end
+
     describe "validations" do
       it "calls validate_options" do
         agent = Agents::SomethingSource.new(:name => "something")