add spec, convert strings to integers for comparison

stvnrlly лет %!s(int64=10): %!d(string=назад)
Родитель
Сommit
50a5d2622d
2 измененных файлов с 31 добавлено и 2 удалено
  1. 1 1
      app/models/agents/user_location_agent.rb
  2. 30 1
      spec/models/agents/user_location_agent_spec.rb

+ 1 - 1
app/models/agents/user_location_agent.rb

@@ -79,7 +79,7 @@ module Agents
79 79
         accuracy_field = 'accuracy'
80 80
       end
81 81
 
82
-      if location.present? && (!interpolated[:max_accuracy].present? || !payload[accuracy_field] || payload[accuracy_field] < interpolated[:max_accuracy])
82
+      if location.present? && (!interpolated[:max_accuracy].present? || !payload[accuracy_field] || payload[accuracy_field].to_i < interpolated[:max_accuracy].to_i)
83 83
         if !payload[accuracy_field]
84 84
           log "Accuracy field missing; all locations will be kept"
85 85
         end

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

@@ -2,7 +2,10 @@ require 'spec_helper'
2 2
 
3 3
 describe Agents::UserLocationAgent do
4 4
   before do
5
-    @agent = Agent.build_for_type('Agents::UserLocationAgent', users(:bob), :name => 'something', :options => { :secret => 'my_secret' })
5
+    @agent = Agent.build_for_type('Agents::UserLocationAgent', users(:bob),
6
+                                  :name => 'something',
7
+                                  :options => { :secret => 'my_secret',
8
+                                    :max_accuracy => '50' })
6 9
     @agent.save!
7 10
   end
8 11
 
@@ -45,4 +48,30 @@ describe Agents::UserLocationAgent do
45 48
     expect(@agent.events.last.lat).to eq(45)
46 49
     expect(@agent.events.last.lng).to eq(123)
47 50
   end
51
+
52
+  it 'does not create event when too inaccurate' do
53
+    event = Event.new
54
+    event.agent = agents(:bob_weather_agent)
55
+    event.created_at = Time.now
56
+    event.payload = { 'longitude' => 123, 'latitude' => 45, 'accuracy' => '100', 'something' => 'else' }
57
+
58
+    expect {
59
+      @agent.receive([event])
60
+    }.to change { @agent.events.count }.by(0)
61
+  end
62
+
63
+  it 'does create event when accurate enough' do
64
+    event = Event.new
65
+    event.agent = agents(:bob_weather_agent)
66
+    event.created_at = Time.now
67
+    event.payload = { 'longitude' => 123, 'latitude' => 45, 'accuracy' => '20', 'something' => 'else' }
68
+
69
+    expect {
70
+      @agent.receive([event])
71
+    }.to change { @agent.events.count }.by(1)
72
+
73
+    expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'accuracy' => '20', 'something' => 'else' })
74
+    expect(@agent.events.last.lat).to eq(45)
75
+    expect(@agent.events.last.lng).to eq(123)
76
+  end
48 77
 end