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

Albert Sun 11 年 前
コミット
9ce8b40a67
共有2 個のファイルを変更した32 個の追加0 個の削除を含む
  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
26 26
   before_validation :set_default_schedule
27 27
   before_validation :unschedule_if_cannot_schedule
28 28
   before_save :unschedule_if_cannot_schedule
29
+  before_create :set_last_checked_event_id
29 30
 
30 31
   belongs_to :user, :inverse_of => :agents
31 32
   has_many :events, :dependent => :delete_all, :inverse_of => :agent, :order => "events.id desc"
@@ -132,6 +133,10 @@ class Agent < ActiveRecord::Base
132 133
     !cannot_receive_events?
133 134
   end
134 135
 
136
+  def set_last_checked_event_id
137
+    self.last_checked_event_id = Event.last.id
138
+  end
139
+
135 140
   # Class Methods
136 141
   class << self
137 142
     def cannot_be_scheduled!

+ 27 - 0
spec/models/agent_spec.rb

@@ -171,6 +171,33 @@ describe Agent do
171 171
       end
172 172
     end
173 173
 
174
+    describe "creating a new agent and then .receive!" do
175
+      before do
176
+        stub_request(:any, /wunderground/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/weather.json")), :status => 200)
177
+        stub.any_instance_of(Agents::WeatherAgent).is_tomorrow?(anything) { true }
178
+      end
179
+
180
+      it "should not backfill events for a newly created agent" do
181
+        Agent.async_check(agents(:bob_weather_agent).id)
182
+        Agent.receive!
183
+        checker = Agents::TriggerAgent.new(:name => "New trigger agent", :options => {
184
+          :expected_receive_period_in_days => "2",
185
+          :rules => [{
186
+            :type => "regex",
187
+            :value => "rain",
188
+            :path => "conditions"
189
+          }],
190
+          :message => "Just so you know, it looks like '<conditions>' tomorrow in <zipcode>"
191
+        })
192
+        checker.user = users(:bob)
193
+        checker.sources << agents(:bob_weather_agent)
194
+        checker.save!
195
+        checker.sources.first.events.count.should be > 0
196
+        Agent.receive!
197
+        checker.events.count.should eq(0)
198
+      end
199
+    end
200
+
174 201
     describe "validations" do
175 202
       it "calls validate_options" do
176 203
         agent = Agents::SomethingSource.new(:name => "something")