spec for distance traveled

stvnrlly 9 years ago
parent
commit
45bd695326
2 changed files with 32 additions and 1 deletions
  1. 2 0
      app/models/agents/user_location_agent.rb
  2. 30 1
      spec/models/agents/user_location_agent_spec.rb

+ 2 - 0
app/models/agents/user_location_agent.rb

@@ -13,6 +13,8 @@ module Agents
13 13
         Your POST path will be `https://#{ENV['DOMAIN']}/users/#{user.id}/update_location/:secret` where `:secret` is specified in your options.
14 14
 
15 15
         If you want to only keep more precise locations, set `max_accuracy` to the upper bound, in meters. The default name for this field is `accuracy`, but you can change this by setting a value for `accuracy_field`.
16
+
17
+        If you want to require a certain distance traveled, set `distance` to the minimum distance, in meters. Note that GPS readings and the measurement itself aren't exact, so don't rely on this for precision filtering.
16 18
       MD
17 19
     end
18 20
 

+ 30 - 1
spec/models/agents/user_location_agent_spec.rb

@@ -5,7 +5,8 @@ describe Agents::UserLocationAgent do
5 5
     @agent = Agent.build_for_type('Agents::UserLocationAgent', users(:bob),
6 6
                                   :name => 'something',
7 7
                                   :options => { :secret => 'my_secret',
8
-                                    :max_accuracy => '50' })
8
+                                    :max_accuracy => '50',
9
+                                    :distance => '50' })
9 10
     @agent.save!
10 11
   end
11 12
 
@@ -90,4 +91,32 @@ describe Agents::UserLocationAgent do
90 91
     expect(@agent.events.last.lat).to eq(45)
91 92
     expect(@agent.events.last.lng).to eq(123)
92 93
   end
94
+
95
+  it 'does create an event when far enough' do
96
+    @agent.memory["last_location"] = { 'longitude' => 12, 'latitude' => 34, 'something' => 'else' }
97
+    event = Event.new
98
+    event.agent = agents(:bob_weather_agent)
99
+    event.created_at = Time.now
100
+    event.payload = { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
101
+
102
+    expect {
103
+      @agent.receive([event])
104
+    }.to change { @agent.events.count }.by(1)
105
+
106
+    expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'something' => 'else' })
107
+    expect(@agent.events.last.lat).to eq(45)
108
+    expect(@agent.events.last.lng).to eq(123)
109
+  end
110
+
111
+  it 'does not create an event when too close' do
112
+    @agent.memory["last_location"] = { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
113
+    event = Event.new
114
+    event.agent = agents(:bob_weather_agent)
115
+    event.created_at = Time.now
116
+    event.payload = { 'longitude' => 123, 'latitude' => 45, 'something' => 'else' }
117
+
118
+    expect {
119
+      @agent.receive([event])
120
+    }.to change { @agent.events.count }.by(0)
121
+  end
93 122
 end