geokit-rails 2.5.0 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +125 -0
  3. data/.gitignore +44 -37
  4. data/CHANGELOG.md +112 -101
  5. data/CONFIG.markdown +67 -67
  6. data/Gemfile +3 -3
  7. data/MIT-LICENSE +19 -19
  8. data/README.markdown +726 -726
  9. data/Rakefile +17 -17
  10. data/gemfiles/rails7.1.gemfile +11 -0
  11. data/gemfiles/{rails5.gemfile → rails7.2.gemfile} +7 -7
  12. data/gemfiles/{rails6.0.gemfile → rails8.0.gemfile} +7 -8
  13. data/gemfiles/{rails6.1.gemfile → rails8.1.gemfile} +7 -8
  14. data/geokit-rails.gemspec +35 -34
  15. data/lib/generators/geokit_rails/install_generator.rb +13 -13
  16. data/lib/generators/templates/geokit_config.rb +100 -100
  17. data/lib/geokit-rails/acts_as_mappable/class_methods.rb +301 -297
  18. data/lib/geokit-rails/acts_as_mappable/glue.rb +54 -54
  19. data/lib/geokit-rails/acts_as_mappable.rb +78 -78
  20. data/lib/geokit-rails/adapters/abstract.rb +30 -30
  21. data/lib/geokit-rails/adapters/mysql.rb +21 -21
  22. data/lib/geokit-rails/adapters/mysql2.rb +8 -8
  23. data/lib/geokit-rails/adapters/mysql2spatial.rb +8 -8
  24. data/lib/geokit-rails/adapters/oracleenhanced.rb +32 -32
  25. data/lib/geokit-rails/adapters/postgresql.rb +21 -21
  26. data/lib/geokit-rails/adapters/sqlite.rb +53 -53
  27. data/lib/geokit-rails/adapters/sqlserver.rb +45 -45
  28. data/lib/geokit-rails/adapters/trilogy.rb +10 -0
  29. data/lib/geokit-rails/distance_collection.rb +8 -8
  30. data/lib/geokit-rails/geocoder_control.rb +19 -19
  31. data/lib/geokit-rails/ip_geocode_lookup.rb +44 -45
  32. data/lib/geokit-rails/railtie.rb +37 -37
  33. data/lib/geokit-rails/version.rb +3 -3
  34. data/lib/geokit-rails.rb +11 -11
  35. data/test/acts_as_mappable_test.rb +491 -478
  36. data/test/adapter_resolution_test.rb +59 -0
  37. data/test/boot.rb +40 -32
  38. data/test/database.yml +36 -28
  39. data/test/distance_collection_test.rb +41 -41
  40. data/test/dummy/Rakefile +6 -6
  41. data/test/dummy/app/assets/stylesheets/application.css +1 -1
  42. data/test/dummy/app/channels/application_cable/channel.rb +4 -4
  43. data/test/dummy/app/channels/application_cable/connection.rb +4 -4
  44. data/test/dummy/app/controllers/application_controller.rb +3 -3
  45. data/test/dummy/app/controllers/location_aware_controller.rb +42 -42
  46. data/test/dummy/app/helpers/application_helper.rb +2 -2
  47. data/test/dummy/app/jobs/application_job.rb +7 -7
  48. data/test/dummy/app/mailers/application_mailer.rb +4 -4
  49. data/test/dummy/app/models/application_record.rb +3 -3
  50. data/test/dummy/app/views/layouts/application.html.erb +15 -15
  51. data/test/dummy/app/views/layouts/mailer.html.erb +13 -13
  52. data/test/dummy/app/views/layouts/mailer.text.erb +1 -1
  53. data/test/dummy/bin/rails +4 -4
  54. data/test/dummy/bin/rake +4 -4
  55. data/test/dummy/bin/setup +33 -33
  56. data/test/dummy/config/application.rb +22 -22
  57. data/test/dummy/config/boot.rb +5 -5
  58. data/test/dummy/config/cable.yml +10 -10
  59. data/test/dummy/config/database.yml +25 -25
  60. data/test/dummy/config/environment.rb +5 -5
  61. data/test/dummy/config/environments/development.rb +68 -68
  62. data/test/dummy/config/environments/production.rb +87 -87
  63. data/test/dummy/config/environments/test.rb +60 -60
  64. data/test/dummy/config/initializers/content_security_policy.rb +25 -25
  65. data/test/dummy/config/initializers/filter_parameter_logging.rb +8 -8
  66. data/test/dummy/config/initializers/inflections.rb +16 -16
  67. data/test/dummy/config/initializers/permissions_policy.rb +11 -11
  68. data/test/dummy/config/locales/en.yml +33 -33
  69. data/test/dummy/config/puma.rb +43 -43
  70. data/test/dummy/config/routes.rb +6 -6
  71. data/test/dummy/config/storage.yml +34 -34
  72. data/test/dummy/config.ru +6 -6
  73. data/test/dummy/public/404.html +67 -67
  74. data/test/dummy/public/422.html +67 -67
  75. data/test/dummy/public/500.html +66 -66
  76. data/test/fixtures/companies.yml +9 -9
  77. data/test/fixtures/custom_locations.yml +61 -61
  78. data/test/fixtures/locations.yml +61 -61
  79. data/test/fixtures/mock_addresses.yml +16 -16
  80. data/test/fixtures/mock_families.yml +2 -2
  81. data/test/fixtures/mock_houses.yml +8 -8
  82. data/test/fixtures/mock_organizations.yml +4 -4
  83. data/test/fixtures/mock_people.yml +4 -4
  84. data/test/ip_geocode_lookup_test.rb +52 -48
  85. data/test/models/company.rb +2 -2
  86. data/test/models/custom_location.rb +11 -11
  87. data/test/models/location.rb +3 -3
  88. data/test/models/mock_address.rb +3 -3
  89. data/test/models/mock_family.rb +4 -4
  90. data/test/models/mock_house.rb +2 -2
  91. data/test/models/mock_organization.rb +5 -5
  92. data/test/models/mock_person.rb +6 -6
  93. data/test/models/store.rb +2 -2
  94. data/test/schema.rb +61 -61
  95. data/test/tasks.rake +39 -37
  96. data/test/test_helper.rb +83 -73
  97. metadata +28 -15
  98. data/.travis.yml +0 -55
  99. data/gemfiles/rails3.gemfile +0 -8
  100. data/gemfiles/rails4.gemfile +0 -8
@@ -1,478 +1,491 @@
1
- require 'test_helper'
2
-
3
- Geokit::Geocoders::provider_order = [:google, :us]
4
-
5
- class ActsAsMappableTest < GeokitTestCase
6
-
7
- LOCATION_A_IP = "217.10.83.5"
8
-
9
- def setup
10
- @location_a = Geokit::GeoLoc.new
11
- @location_a.lat = 32.918593
12
- @location_a.lng = -96.958444
13
- @location_a.city = "Irving"
14
- @location_a.state = "TX"
15
- @location_a.country_code = "US"
16
- @location_a.success = true
17
-
18
- @sw = Geokit::LatLng.new(32.91663,-96.982841)
19
- @ne = Geokit::LatLng.new(32.96302,-96.919495)
20
- @bounds_center=Geokit::LatLng.new((@sw.lat+@ne.lat)/2,(@sw.lng+@ne.lng)/2)
21
-
22
- @starbucks = companies(:starbucks)
23
- @loc_a = locations(:a)
24
- @custom_loc_a = custom_locations(:a)
25
- @loc_e = locations(:e)
26
- @custom_loc_e = custom_locations(:e)
27
-
28
- @barnes_and_noble = mock_organizations(:barnes_and_noble)
29
- @address = mock_addresses(:address_barnes_and_noble)
30
- end
31
-
32
- def test_override_default_units_the_hard_way
33
- Location.default_units = :kms
34
- #locations = Location.geo_scope(:origin => @loc_a).where("distance < 3.97")
35
- locations = Location.within(3.9, :origin => @loc_a)
36
- assert_equal 5, locations.to_a.size
37
- assert_equal 5, locations.count
38
- Location.default_units = :miles
39
- end
40
-
41
- def test_include
42
- #locations = Location.geo_scope(:origin => @loc_a).includes(:company).where("company_id = 1").all
43
- locations = Location.includes(:company).where("company_id = 1").to_a
44
- assert !locations.empty?
45
- assert_equal 1, locations[0].company.id
46
- assert_equal 'Starbucks', locations[0].company.name
47
- end
48
-
49
- def test_distance_between_geocoded
50
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
51
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("San Francisco, CA").returns(@location_a)
52
- assert_equal 0, Location.distance_between("Irving, TX", "San Francisco, CA")
53
- end
54
-
55
- def test_distance_to_geocoded
56
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
57
- assert_equal 0, @custom_loc_a.distance_to("Irving, TX")
58
- end
59
-
60
- def test_distance_to_geocoded_error
61
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(Geokit::GeoLoc.new)
62
- assert_raise(Geokit::Geocoders::GeocodeError) { @custom_loc_a.distance_to("Irving, TX") }
63
- end
64
-
65
- def test_custom_attributes_distance_calculations
66
- assert_equal 0, @custom_loc_a.distance_to(@loc_a)
67
- assert_equal 0, CustomLocation.distance_between(@custom_loc_a, @loc_a)
68
- end
69
-
70
- def test_distance_column_in_select
71
- #locations = Location.geo_scope(:origin => @loc_a).order("distance ASC")
72
- locations = Location.by_distance(:origin => @loc_a)
73
- assert_equal 6, locations.to_a.size
74
- assert_equal 0, @loc_a.distance_to(locations.first)
75
- assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
76
- end
77
-
78
- def test_find_with_distance_condition
79
- #locations = Location.geo_scope(:origin => @loc_a, :within => 3.97)
80
- locations = Location.within(3.97, :origin => @loc_a)
81
- assert_equal 5, locations.to_a.size
82
- assert_equal 5, locations.count
83
- end
84
-
85
- def test_find_with_distance_condition_with_units_override
86
- #locations = Location.geo_scope(:origin => @loc_a, :units => :kms, :within => 6.387)
87
- locations = Location.within(6.387, :origin => @loc_a, :units => :kms)
88
- assert_equal 5, locations.to_a.size
89
- assert_equal 5, locations.count
90
- end
91
-
92
- def test_find_with_distance_condition_with_formula_override
93
- #locations = Location.geo_scope(:origin => @loc_a, :formula => :flat, :within => 6.387)
94
- locations = Location.within(6.387, :origin => @loc_a, :formula => :flat)
95
- assert_equal 6, locations.to_a.size
96
- assert_equal 6, locations.count
97
- end
98
-
99
- def test_find_within
100
- locations = Location.within(3.97, :origin => @loc_a)
101
- assert_equal 5, locations.to_a.size
102
- assert_equal 5, locations.count
103
- end
104
-
105
- def test_find_within_with_coordinates
106
- locations = Location.within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
107
- assert_equal 5, locations.to_a.size
108
- assert_equal 5, locations.count
109
- end
110
-
111
- def test_find_within_with_column_as_distance
112
- locations = Location.joins(:company).within(Company.arel_table[:max_distance], origin: @loc_a)
113
- assert_equal 4, locations.to_a.size
114
- assert_equal 4, locations.count
115
- end
116
-
117
- def test_find_with_compound_condition
118
- #locations = Location.geo_scope(:origin => @loc_a).where("distance < 5 and city = 'Coppell'")
119
- locations = Location.within(5, :origin => @loc_a).where("city = 'Coppell'")
120
- assert_equal 2, locations.to_a.size
121
- assert_equal 2, locations.count
122
- end
123
-
124
- def test_find_with_secure_compound_condition
125
- #locations = Location.geo_scope(:origin => @loc_a).where(["distance < ? and city = ?", 5, 'Coppell'])
126
- locations = Location.within(5, :origin => @loc_a).where(["city = ?", 'Coppell'])
127
- assert_equal 2, locations.to_a.size
128
- assert_equal 2, locations.count
129
- end
130
-
131
- def test_find_beyond
132
- locations = Location.beyond(3.95, :origin => @loc_a)
133
- assert_equal 1, locations.to_a.size
134
- assert_equal 1, locations.count
135
- end
136
-
137
- def test_find_beyond_with_token
138
- # locations = Location.find(:all, :beyond => 3.95, :origin => @loc_a)
139
- #locations = Location.geo_scope(:beyond => 3.95, :origin => @loc_a)
140
- locations = Location.beyond(3.95, :origin => @loc_a)
141
- assert_equal 1, locations.to_a.size
142
- assert_equal 1, locations.count
143
- end
144
-
145
- def test_find_beyond_with_coordinates
146
- locations = Location.beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
147
- assert_equal 1, locations.to_a.size
148
- assert_equal 1, locations.count
149
- end
150
-
151
- def test_find_range_with_token
152
- locations = Location.in_range(0..10, :origin => @loc_a)
153
- assert_equal 6, locations.to_a.size
154
- assert_equal 6, locations.count
155
- end
156
-
157
- def test_find_range_with_token_with_conditions
158
- locations = Location.in_range(0..10, :origin => @loc_a).where(["city = ?", 'Coppell'])
159
- assert_equal 2, locations.to_a.size
160
- assert_equal 2, locations.count
161
- end
162
-
163
- def test_find_range_with_token_with_hash_conditions
164
- locations = Location.in_range(0..10, :origin => @loc_a).where(:city => 'Coppell')
165
- assert_equal 2, locations.to_a.size
166
- assert_equal 2, locations.count
167
- end
168
-
169
- def test_find_range_with_token_excluding_end
170
- #locations = Location.geo_scope(:range => 0...10, :origin => @loc_a)
171
- locations = Location.in_range(0...10, :origin => @loc_a)
172
- assert_equal 6, locations.to_a.size
173
- assert_equal 6, locations.count
174
- end
175
-
176
- def test_find_nearest
177
- assert_equal @loc_a, Location.nearest(:origin => @loc_a).first
178
- end
179
-
180
- def test_find_nearest_with_coordinates
181
- assert_equal @loc_a, Location.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
182
- end
183
-
184
- def test_find_nearest_is_scope
185
- assert Location.nearest(:origin => @loc_a).respond_to? :where
186
- end
187
-
188
- def test_find_farthest
189
- assert_equal @loc_e, Location.farthest(:origin => @loc_a).first
190
- end
191
-
192
- def test_find_farthest_with_coordinates
193
- assert_equal @loc_e, Location.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
194
- end
195
-
196
- def test_find_farthest_is_scope
197
- assert Location.farthest(:origin => @loc_a).respond_to? :where
198
- end
199
-
200
- def test_scoped_distance_column_in_select
201
- #locations = @starbucks.locations.geo_scope(:origin => @loc_a).order("distance ASC")
202
- locations = @starbucks.locations.by_distance(:origin => @loc_a)
203
- assert_equal 5, locations.to_a.size
204
- assert_equal 0, @loc_a.distance_to(locations.first)
205
- assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
206
- end
207
-
208
- def test_scoped_find_with_distance_condition
209
- #locations = @starbucks.locations.geo_scope(:origin => @loc_a).where("distance < 3.97")
210
- locations = @starbucks.locations.within(3.97, :origin => @loc_a)
211
- assert_equal 4, locations.to_a.size
212
- assert_equal 4, locations.count
213
- end
214
-
215
- def test_scoped_find_within
216
- locations = @starbucks.locations.within(3.97, :origin => @loc_a)
217
- assert_equal 4, locations.to_a.size
218
- assert_equal 4, locations.count
219
- end
220
-
221
- def test_scoped_find_with_compound_condition
222
- locations = @starbucks.locations.within(5, :origin => @loc_a).where("city = 'Coppell'")
223
- assert_equal 2, locations.to_a.size
224
- assert_equal 2, locations.count
225
- end
226
-
227
- def test_scoped_find_beyond
228
- locations = @starbucks.locations.beyond(3.95, :origin => @loc_a)
229
- assert_equal 1, locations.to_a.size
230
- assert_equal 1, locations.count
231
- end
232
-
233
- def test_scoped_find_nearest
234
- assert_equal @loc_a, @starbucks.locations.nearest(:origin => @loc_a).first
235
- end
236
-
237
- def test_scoped_find_farthest
238
- assert_equal @loc_e, @starbucks.locations.farthest(:origin => @loc_a).first
239
- end
240
-
241
- def test_ip_geocoded_distance_column_in_select
242
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
243
- locations = Location.by_distance(:origin => LOCATION_A_IP)
244
- assert_equal 6, locations.to_a.size
245
- assert_equal 0, @loc_a.distance_to(locations.first)
246
- assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
247
- end
248
-
249
- def test_ip_geocoded_find_with_distance_condition
250
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
251
- locations = Location.within(3.97, :origin => LOCATION_A_IP)
252
- assert_equal 5, locations.to_a.size
253
- assert_equal 5, locations.count
254
- end
255
-
256
- def test_ip_geocoded_find_within
257
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
258
- locations = Location.within(3.97, :origin => LOCATION_A_IP)
259
- assert_equal 5, locations.to_a.size
260
- assert_equal 5, locations.count
261
- end
262
-
263
- def test_ip_geocoded_find_with_compound_condition
264
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
265
- locations = Location.within(5, :origin => LOCATION_A_IP).where("city = 'Coppell'")
266
- assert_equal 2, locations.to_a.size
267
- assert_equal 2, locations.count
268
- end
269
-
270
- def test_ip_geocoded_find_with_secure_compound_condition
271
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
272
- locations = Location.within(5, :origin => LOCATION_A_IP).where(["city = ?", 'Coppell'])
273
- assert_equal 2, locations.to_a.size
274
- assert_equal 2, locations.count
275
- end
276
-
277
- def test_ip_geocoded_find_beyond
278
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
279
- locations = Location.beyond(3.95, :origin => LOCATION_A_IP)
280
- assert_equal 1, locations.to_a.size
281
- assert_equal 1, locations.count
282
- end
283
-
284
- def test_ip_geocoded_find_nearest
285
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
286
- assert_equal @loc_a, Location.nearest(:origin => LOCATION_A_IP).first
287
- end
288
-
289
- def test_ip_geocoded_find_farthest
290
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
291
- assert_equal @loc_e, Location.farthest(:origin => LOCATION_A_IP).first
292
- end
293
-
294
- def test_ip_geocoder_exception
295
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with('127.0.0.1').returns(Geokit::GeoLoc.new)
296
- assert_raises Geokit::Geocoders::GeocodeError do
297
- Location.farthest(:origin => '127.0.0.1').first
298
- end
299
- end
300
-
301
- def test_address_geocode
302
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with('Irving, TX').twice.returns(@location_a)
303
- #locations = Location.geo_scope(:origin => 'Irving, TX').where(["distance < ? and city = ?", 5, 'Coppell'])
304
- locations = Location.within(5, :origin => 'Irving, TX').where(["city = ?", 'Coppell'])
305
- assert_equal 2, locations.to_a.size
306
- assert_equal 2, locations.count
307
- end
308
-
309
- def test_find_with_custom_distance_condition
310
- locations = CustomLocation.within(3.97, :origin => @loc_a)
311
- assert_equal 5, locations.to_a.size
312
- assert_equal 5, locations.count
313
- end
314
-
315
- def test_find_with_custom_distance_condition_using_custom_origin
316
- #locations = CustomLocation.geo_scope(:origin => @custom_loc_a).where("dist < 3.97")
317
- locations = CustomLocation.within(3.97, :origin => @custom_loc_a)
318
- assert_equal 5, locations.to_a.size
319
- assert_equal 5, locations.count
320
- end
321
-
322
- def test_find_within_with_custom
323
- locations = CustomLocation.within(3.97, :origin => @loc_a)
324
- assert_equal 5, locations.to_a.size
325
- assert_equal 5, locations.count
326
- end
327
-
328
- def test_find_within_with_coordinates_with_custom
329
- locations = CustomLocation.within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
330
- assert_equal 5, locations.to_a.size
331
- assert_equal 5, locations.count
332
- end
333
-
334
- def test_find_with_compound_condition_with_custom
335
- #locations = CustomLocation.geo_scope(:origin => @loc_a).where("dist < 5 and city = 'Coppell'")
336
- locations = CustomLocation.within(5, :origin => @loc_a).where("city = 'Coppell'")
337
- assert_equal 1, locations.to_a.size
338
- assert_equal 1, locations.count
339
- end
340
-
341
- # REMOVED AS SAME AS BELOW
342
- #def test_find_with_secure_compound_condition_with_custom
343
- # locations = CustomLocation.geo_scope(:origin => @loc_a).where(["dist < ? and city = ?", 5, 'Coppell'])
344
- # assert_equal 1, locations.all.size
345
- # assert_equal 1, locations.count
346
- #end
347
-
348
- def test_find_beyond_with_custom
349
- locations = CustomLocation.beyond(3.95, :origin => @loc_a)
350
- assert_equal 1, locations.to_a.size
351
- assert_equal 1, locations.count
352
- end
353
-
354
- def test_find_beyond_with_coordinates_with_custom
355
- locations = CustomLocation.beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
356
- assert_equal 1, locations.to_a.size
357
- assert_equal 1, locations.count
358
- end
359
-
360
- def test_find_nearest_with_custom
361
- assert_equal @custom_loc_a, CustomLocation.nearest(:origin => @loc_a).first
362
- end
363
-
364
- def test_find_nearest_with_coordinates_with_custom
365
- assert_equal @custom_loc_a, CustomLocation.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
366
- end
367
-
368
- def test_find_farthest_with_custom
369
- assert_equal @custom_loc_e, CustomLocation.farthest(:origin => @loc_a).first
370
- end
371
-
372
- def test_find_farthest_with_coordinates_with_custom
373
- assert_equal @custom_loc_e, CustomLocation.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
374
- end
375
-
376
- def test_find_with_array_origin
377
- #locations = Location.geo_scope(:origin =>[@loc_a.lat,@loc_a.lng]).where("distance < 3.97")
378
- locations = Location.within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
379
- assert_equal 5, locations.to_a.size
380
- assert_equal 5, locations.count
381
- end
382
-
383
-
384
- # Bounding box tests
385
-
386
- def test_find_within_bounds
387
- locations = Location.in_bounds([@sw,@ne])
388
- assert_equal 2, locations.to_a.size
389
- assert_equal 2, locations.count
390
- end
391
-
392
- def test_find_within_bounds_with_inclusive
393
- sw = Geokit::LatLng.new(@loc_a.lat,@loc_a.lng)
394
- ne = sw
395
- bounds = [sw,ne]
396
- locations = Location.in_bounds(bounds, :inclusive => false)
397
- assert_equal 0, locations.count
398
- locations = Location.in_bounds(bounds, :inclusive => true)
399
- assert_equal 1, locations.count
400
- end
401
-
402
- def test_find_within_bounds_ordered_by_distance
403
- #locations = Location.in_bounds([@sw,@ne], :origin=>@bounds_center).order('distance asc')
404
- locations = Location.in_bounds([@sw,@ne]).by_distance(:origin => @bounds_center)
405
- assert_equal locations[0], locations(:d)
406
- assert_equal locations[1], locations(:a)
407
- end
408
-
409
- def test_find_within_bounds_with_token
410
- #locations = Location.geo_scope(:bounds=>[@sw,@ne])
411
- locations = Location.in_bounds([@sw,@ne])
412
- assert_equal 2, locations.to_a.size
413
- assert_equal 2, locations.count
414
- end
415
-
416
- def test_find_within_bounds_with_string_conditions
417
- #locations = Location.geo_scope(:bounds=>[@sw,@ne]).where("id !=#{locations(:a).id}")
418
- locations = Location.in_bounds([@sw,@ne]).where("id !=#{locations(:a).id}")
419
- assert_equal 1, locations.to_a.size
420
- end
421
-
422
- def test_find_within_bounds_with_array_conditions
423
- #locations = Location.geo_scope(:bounds=>[@sw,@ne]).where(["id != ?", locations(:a).id])
424
- locations = Location.in_bounds([@sw,@ne]).where(["id != ?", locations(:a).id])
425
- assert_equal 1, locations.to_a.size
426
- end
427
-
428
- def test_find_within_bounds_with_hash_conditions
429
- #locations = Location.geo_scope(:bounds=>[@sw,@ne]).where({:id => locations(:a).id})
430
- locations = Location.in_bounds([@sw,@ne]).where({:id => locations(:a).id})
431
- assert_equal 1, locations.to_a.size
432
- end
433
-
434
- def test_auto_geocode
435
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
436
- store=Store.new(:address=>'Irving, TX')
437
- store.save
438
- assert_equal store.lat,@location_a.lat
439
- assert_equal store.lng,@location_a.lng
440
- assert_equal 0, store.errors.size
441
- end
442
-
443
- def test_auto_geocode_failure
444
- Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("BOGUS").returns(Geokit::GeoLoc.new)
445
- store=Store.new(:address=>'BOGUS')
446
- store.save
447
- assert store.new_record?
448
- assert_equal 1, store.errors.size
449
- end
450
-
451
- # Test :through
452
-
453
- #def test_find_with_through
454
- # organizations = MockOrganization.geo_scope(:origin => @location_a).order('distance ASC')
455
- # assert_equal 2, organizations.all.size
456
- # organizations = MockOrganization.geo_scope(:origin => @location_a).where("distance < 3.97")
457
- # assert_equal 1, organizations.count
458
- #end
459
-
460
- def test_find_with_through_with_hash
461
- #people = MockPerson.geo_scope(:origin => @location_a).order('distance ASC')
462
- people = MockPerson.order('distance ASC')
463
- assert_equal 2, people.size
464
- assert_equal 2, people.count
465
- end
466
-
467
- def test_sort_by_distance_from
468
- locations = Location.with_latlng.all
469
- unsorted = [locations(:a), locations(:b), locations(:c), locations(:d), locations(:e), locations(:f)]
470
- sorted = [locations(:a), locations(:b), locations(:c), locations(:f), locations(:d), locations(:e)]
471
- assert_equal sorted, locations.sort_by{|l| l.distance_to(locations(:a))}
472
-
473
- unsorted_collection = DistanceCollection.new(unsorted)
474
- unsorted_collection.set_distance_from(locations(:a))
475
- assert_equal sorted, unsorted_collection.sort_by(&:distance)
476
- end
477
-
478
- end
1
+ require 'test_helper'
2
+
3
+ Geokit::Geocoders::provider_order = [:google, :us]
4
+
5
+ class ActsAsMappableTest < GeokitTestCase
6
+
7
+ LOCATION_A_IP = "217.10.83.5"
8
+
9
+ def setup
10
+ @location_a = Geokit::GeoLoc.new
11
+ @location_a.lat = 32.918593
12
+ @location_a.lng = -96.958444
13
+ @location_a.city = "Irving"
14
+ @location_a.state = "TX"
15
+ @location_a.country_code = "US"
16
+ @location_a.success = true
17
+
18
+ @sw = Geokit::LatLng.new(32.91663,-96.982841)
19
+ @ne = Geokit::LatLng.new(32.96302,-96.919495)
20
+ @bounds_center=Geokit::LatLng.new((@sw.lat+@ne.lat)/2,(@sw.lng+@ne.lng)/2)
21
+
22
+ @starbucks = companies(:starbucks)
23
+ @loc_a = locations(:a)
24
+ @custom_loc_a = custom_locations(:a)
25
+ @loc_e = locations(:e)
26
+ @custom_loc_e = custom_locations(:e)
27
+
28
+ @barnes_and_noble = mock_organizations(:barnes_and_noble)
29
+ @address = mock_addresses(:address_barnes_and_noble)
30
+ end
31
+
32
+ def test_override_default_units_the_hard_way
33
+ Location.default_units = :kms
34
+ #locations = Location.geo_scope(:origin => @loc_a).where("distance < 3.97")
35
+ locations = Location.within(3.9, :origin => @loc_a)
36
+ assert_equal 5, locations.to_a.size
37
+ assert_equal 5, locations.count
38
+ Location.default_units = :miles
39
+ end
40
+
41
+ def test_include
42
+ #locations = Location.geo_scope(:origin => @loc_a).includes(:company).where("company_id = 1").all
43
+ locations = Location.includes(:company).where("company_id = 1").to_a
44
+ assert !locations.empty?
45
+ assert_equal 1, locations[0].company.id
46
+ assert_equal 'Starbucks', locations[0].company.name
47
+ end
48
+
49
+ def test_distance_between_geocoded
50
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
51
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("San Francisco, CA").returns(@location_a)
52
+ assert_equal 0, Location.distance_between("Irving, TX", "San Francisco, CA")
53
+ end
54
+
55
+ def test_distance_to_geocoded
56
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
57
+ assert_equal 0, @custom_loc_a.distance_to("Irving, TX")
58
+ end
59
+
60
+ def test_distance_to_geocoded_error
61
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(Geokit::GeoLoc.new)
62
+ assert_raise(Geokit::Geocoders::GeocodeError) { @custom_loc_a.distance_to("Irving, TX") }
63
+ end
64
+
65
+ def test_custom_attributes_distance_calculations
66
+ assert_equal 0, @custom_loc_a.distance_to(@loc_a)
67
+ assert_equal 0, CustomLocation.distance_between(@custom_loc_a, @loc_a)
68
+ end
69
+
70
+ def test_distance_column_in_select
71
+ #locations = Location.geo_scope(:origin => @loc_a).order("distance ASC")
72
+ locations = Location.by_distance(:origin => @loc_a)
73
+ assert_equal 6, locations.to_a.size
74
+ assert_equal 0, @loc_a.distance_to(locations.first)
75
+ assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
76
+ end
77
+
78
+ def test_find_with_distance_condition
79
+ #locations = Location.geo_scope(:origin => @loc_a, :within => 3.97)
80
+ locations = Location.within(3.97, :origin => @loc_a)
81
+ assert_equal 5, locations.to_a.size
82
+ assert_equal 5, locations.count
83
+ end
84
+
85
+ def test_find_with_distance_condition_with_units_override
86
+ #locations = Location.geo_scope(:origin => @loc_a, :units => :kms, :within => 6.387)
87
+ locations = Location.within(6.387, :origin => @loc_a, :units => :kms)
88
+ assert_equal 5, locations.to_a.size
89
+ assert_equal 5, locations.count
90
+ end
91
+
92
+ def test_find_with_distance_condition_with_formula_override
93
+ #locations = Location.geo_scope(:origin => @loc_a, :formula => :flat, :within => 6.387)
94
+ locations = Location.within(6.387, :origin => @loc_a, :formula => :flat)
95
+ assert_equal 6, locations.to_a.size
96
+ assert_equal 6, locations.count
97
+ end
98
+
99
+ def test_find_within
100
+ locations = Location.within(3.97, :origin => @loc_a)
101
+ assert_equal 5, locations.to_a.size
102
+ assert_equal 5, locations.count
103
+ end
104
+
105
+ def test_find_within_with_coordinates
106
+ locations = Location.within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
107
+ assert_equal 5, locations.to_a.size
108
+ assert_equal 5, locations.count
109
+ end
110
+
111
+ def test_find_within_with_column_as_distance
112
+ locations = Location.joins(:company).within(Company.arel_table[:max_distance], origin: @loc_a)
113
+ assert_equal 4, locations.to_a.size
114
+ assert_equal 4, locations.count
115
+ end
116
+
117
+ def test_find_with_compound_condition
118
+ #locations = Location.geo_scope(:origin => @loc_a).where("distance < 5 and city = 'Coppell'")
119
+ locations = Location.within(5, :origin => @loc_a).where("city = 'Coppell'")
120
+ assert_equal 2, locations.to_a.size
121
+ assert_equal 2, locations.count
122
+ end
123
+
124
+ def test_find_with_secure_compound_condition
125
+ #locations = Location.geo_scope(:origin => @loc_a).where(["distance < ? and city = ?", 5, 'Coppell'])
126
+ locations = Location.within(5, :origin => @loc_a).where(["city = ?", 'Coppell'])
127
+ assert_equal 2, locations.to_a.size
128
+ assert_equal 2, locations.count
129
+ end
130
+
131
+ def test_find_beyond
132
+ locations = Location.beyond(3.95, :origin => @loc_a)
133
+ assert_equal 1, locations.to_a.size
134
+ assert_equal 1, locations.count
135
+ end
136
+
137
+ def test_find_beyond_with_token
138
+ # locations = Location.find(:all, :beyond => 3.95, :origin => @loc_a)
139
+ #locations = Location.geo_scope(:beyond => 3.95, :origin => @loc_a)
140
+ locations = Location.beyond(3.95, :origin => @loc_a)
141
+ assert_equal 1, locations.to_a.size
142
+ assert_equal 1, locations.count
143
+ end
144
+
145
+ def test_find_beyond_with_coordinates
146
+ locations = Location.beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
147
+ assert_equal 1, locations.to_a.size
148
+ assert_equal 1, locations.count
149
+ end
150
+
151
+ def test_find_range_with_token
152
+ locations = Location.in_range(0..10, :origin => @loc_a)
153
+ assert_equal 6, locations.to_a.size
154
+ assert_equal 6, locations.count
155
+ end
156
+
157
+ def test_find_range_with_token_with_conditions
158
+ locations = Location.in_range(0..10, :origin => @loc_a).where(["city = ?", 'Coppell'])
159
+ assert_equal 2, locations.to_a.size
160
+ assert_equal 2, locations.count
161
+ end
162
+
163
+ def test_find_range_with_token_with_hash_conditions
164
+ locations = Location.in_range(0..10, :origin => @loc_a).where(:city => 'Coppell')
165
+ assert_equal 2, locations.to_a.size
166
+ assert_equal 2, locations.count
167
+ end
168
+
169
+ def test_find_range_with_token_excluding_end
170
+ #locations = Location.geo_scope(:range => 0...10, :origin => @loc_a)
171
+ locations = Location.in_range(0...10, :origin => @loc_a)
172
+ assert_equal 6, locations.to_a.size
173
+ assert_equal 6, locations.count
174
+ end
175
+
176
+ def test_find_range_skips_locations_without_coordinates
177
+ nowhere = locations(:g)
178
+ assert_not_includes Location.in_range(0..10, origin: @loc_a), nowhere
179
+ end
180
+
181
+ # MockOrganization has no lat/lng columns of its own; they come from
182
+ # mock_addresses through the join.
183
+ def test_find_range_with_through
184
+ organizations = MockOrganization.joins(:mock_address)
185
+ .in_range(0..3, origin: @loc_a)
186
+ assert_equal [mock_organizations(:barnes_and_noble)], organizations.to_a
187
+ end
188
+
189
+ def test_find_nearest
190
+ assert_equal @loc_a, Location.nearest(:origin => @loc_a).first
191
+ end
192
+
193
+ def test_find_nearest_with_coordinates
194
+ assert_equal @loc_a, Location.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
195
+ end
196
+
197
+ def test_find_nearest_is_scope
198
+ assert Location.nearest(:origin => @loc_a).respond_to? :where
199
+ end
200
+
201
+ def test_find_farthest
202
+ assert_equal @loc_e, Location.farthest(:origin => @loc_a).first
203
+ end
204
+
205
+ def test_find_farthest_with_coordinates
206
+ assert_equal @loc_e, Location.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
207
+ end
208
+
209
+ def test_find_farthest_is_scope
210
+ assert Location.farthest(:origin => @loc_a).respond_to? :where
211
+ end
212
+
213
+ def test_scoped_distance_column_in_select
214
+ #locations = @starbucks.locations.geo_scope(:origin => @loc_a).order("distance ASC")
215
+ locations = @starbucks.locations.by_distance(:origin => @loc_a)
216
+ assert_equal 5, locations.to_a.size
217
+ assert_equal 0, @loc_a.distance_to(locations.first)
218
+ assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
219
+ end
220
+
221
+ def test_scoped_find_with_distance_condition
222
+ #locations = @starbucks.locations.geo_scope(:origin => @loc_a).where("distance < 3.97")
223
+ locations = @starbucks.locations.within(3.97, :origin => @loc_a)
224
+ assert_equal 4, locations.to_a.size
225
+ assert_equal 4, locations.count
226
+ end
227
+
228
+ def test_scoped_find_within
229
+ locations = @starbucks.locations.within(3.97, :origin => @loc_a)
230
+ assert_equal 4, locations.to_a.size
231
+ assert_equal 4, locations.count
232
+ end
233
+
234
+ def test_scoped_find_with_compound_condition
235
+ locations = @starbucks.locations.within(5, :origin => @loc_a).where("city = 'Coppell'")
236
+ assert_equal 2, locations.to_a.size
237
+ assert_equal 2, locations.count
238
+ end
239
+
240
+ def test_scoped_find_beyond
241
+ locations = @starbucks.locations.beyond(3.95, :origin => @loc_a)
242
+ assert_equal 1, locations.to_a.size
243
+ assert_equal 1, locations.count
244
+ end
245
+
246
+ def test_scoped_find_nearest
247
+ assert_equal @loc_a, @starbucks.locations.nearest(:origin => @loc_a).first
248
+ end
249
+
250
+ def test_scoped_find_farthest
251
+ assert_equal @loc_e, @starbucks.locations.farthest(:origin => @loc_a).first
252
+ end
253
+
254
+ def test_ip_geocoded_distance_column_in_select
255
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
256
+ locations = Location.by_distance(:origin => LOCATION_A_IP)
257
+ assert_equal 6, locations.to_a.size
258
+ assert_equal 0, @loc_a.distance_to(locations.first)
259
+ assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
260
+ end
261
+
262
+ def test_ip_geocoded_find_with_distance_condition
263
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
264
+ locations = Location.within(3.97, :origin => LOCATION_A_IP)
265
+ assert_equal 5, locations.to_a.size
266
+ assert_equal 5, locations.count
267
+ end
268
+
269
+ def test_ip_geocoded_find_within
270
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
271
+ locations = Location.within(3.97, :origin => LOCATION_A_IP)
272
+ assert_equal 5, locations.to_a.size
273
+ assert_equal 5, locations.count
274
+ end
275
+
276
+ def test_ip_geocoded_find_with_compound_condition
277
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
278
+ locations = Location.within(5, :origin => LOCATION_A_IP).where("city = 'Coppell'")
279
+ assert_equal 2, locations.to_a.size
280
+ assert_equal 2, locations.count
281
+ end
282
+
283
+ def test_ip_geocoded_find_with_secure_compound_condition
284
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
285
+ locations = Location.within(5, :origin => LOCATION_A_IP).where(["city = ?", 'Coppell'])
286
+ assert_equal 2, locations.to_a.size
287
+ assert_equal 2, locations.count
288
+ end
289
+
290
+ def test_ip_geocoded_find_beyond
291
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
292
+ locations = Location.beyond(3.95, :origin => LOCATION_A_IP)
293
+ assert_equal 1, locations.to_a.size
294
+ assert_equal 1, locations.count
295
+ end
296
+
297
+ def test_ip_geocoded_find_nearest
298
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
299
+ assert_equal @loc_a, Location.nearest(:origin => LOCATION_A_IP).first
300
+ end
301
+
302
+ def test_ip_geocoded_find_farthest
303
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
304
+ assert_equal @loc_e, Location.farthest(:origin => LOCATION_A_IP).first
305
+ end
306
+
307
+ def test_ip_geocoder_exception
308
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with('127.0.0.1').returns(Geokit::GeoLoc.new)
309
+ assert_raises Geokit::Geocoders::GeocodeError do
310
+ Location.farthest(:origin => '127.0.0.1').first
311
+ end
312
+ end
313
+
314
+ def test_address_geocode
315
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with('Irving, TX').twice.returns(@location_a)
316
+ #locations = Location.geo_scope(:origin => 'Irving, TX').where(["distance < ? and city = ?", 5, 'Coppell'])
317
+ locations = Location.within(5, :origin => 'Irving, TX').where(["city = ?", 'Coppell'])
318
+ assert_equal 2, locations.to_a.size
319
+ assert_equal 2, locations.count
320
+ end
321
+
322
+ def test_find_with_custom_distance_condition
323
+ locations = CustomLocation.within(3.97, :origin => @loc_a)
324
+ assert_equal 5, locations.to_a.size
325
+ assert_equal 5, locations.count
326
+ end
327
+
328
+ def test_find_with_custom_distance_condition_using_custom_origin
329
+ #locations = CustomLocation.geo_scope(:origin => @custom_loc_a).where("dist < 3.97")
330
+ locations = CustomLocation.within(3.97, :origin => @custom_loc_a)
331
+ assert_equal 5, locations.to_a.size
332
+ assert_equal 5, locations.count
333
+ end
334
+
335
+ def test_find_within_with_custom
336
+ locations = CustomLocation.within(3.97, :origin => @loc_a)
337
+ assert_equal 5, locations.to_a.size
338
+ assert_equal 5, locations.count
339
+ end
340
+
341
+ def test_find_within_with_coordinates_with_custom
342
+ locations = CustomLocation.within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
343
+ assert_equal 5, locations.to_a.size
344
+ assert_equal 5, locations.count
345
+ end
346
+
347
+ def test_find_with_compound_condition_with_custom
348
+ #locations = CustomLocation.geo_scope(:origin => @loc_a).where("dist < 5 and city = 'Coppell'")
349
+ locations = CustomLocation.within(5, :origin => @loc_a).where("city = 'Coppell'")
350
+ assert_equal 1, locations.to_a.size
351
+ assert_equal 1, locations.count
352
+ end
353
+
354
+ # REMOVED AS SAME AS BELOW
355
+ #def test_find_with_secure_compound_condition_with_custom
356
+ # locations = CustomLocation.geo_scope(:origin => @loc_a).where(["dist < ? and city = ?", 5, 'Coppell'])
357
+ # assert_equal 1, locations.all.size
358
+ # assert_equal 1, locations.count
359
+ #end
360
+
361
+ def test_find_beyond_with_custom
362
+ locations = CustomLocation.beyond(3.95, :origin => @loc_a)
363
+ assert_equal 1, locations.to_a.size
364
+ assert_equal 1, locations.count
365
+ end
366
+
367
+ def test_find_beyond_with_coordinates_with_custom
368
+ locations = CustomLocation.beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
369
+ assert_equal 1, locations.to_a.size
370
+ assert_equal 1, locations.count
371
+ end
372
+
373
+ def test_find_nearest_with_custom
374
+ assert_equal @custom_loc_a, CustomLocation.nearest(:origin => @loc_a).first
375
+ end
376
+
377
+ def test_find_nearest_with_coordinates_with_custom
378
+ assert_equal @custom_loc_a, CustomLocation.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
379
+ end
380
+
381
+ def test_find_farthest_with_custom
382
+ assert_equal @custom_loc_e, CustomLocation.farthest(:origin => @loc_a).first
383
+ end
384
+
385
+ def test_find_farthest_with_coordinates_with_custom
386
+ assert_equal @custom_loc_e, CustomLocation.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
387
+ end
388
+
389
+ def test_find_with_array_origin
390
+ #locations = Location.geo_scope(:origin =>[@loc_a.lat,@loc_a.lng]).where("distance < 3.97")
391
+ locations = Location.within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
392
+ assert_equal 5, locations.to_a.size
393
+ assert_equal 5, locations.count
394
+ end
395
+
396
+
397
+ # Bounding box tests
398
+
399
+ def test_find_within_bounds
400
+ locations = Location.in_bounds([@sw,@ne])
401
+ assert_equal 2, locations.to_a.size
402
+ assert_equal 2, locations.count
403
+ end
404
+
405
+ def test_find_within_bounds_with_inclusive
406
+ sw = Geokit::LatLng.new(@loc_a.lat,@loc_a.lng)
407
+ ne = sw
408
+ bounds = [sw,ne]
409
+ locations = Location.in_bounds(bounds, :inclusive => false)
410
+ assert_equal 0, locations.count
411
+ locations = Location.in_bounds(bounds, :inclusive => true)
412
+ assert_equal 1, locations.count
413
+ end
414
+
415
+ def test_find_within_bounds_ordered_by_distance
416
+ #locations = Location.in_bounds([@sw,@ne], :origin=>@bounds_center).order('distance asc')
417
+ locations = Location.in_bounds([@sw,@ne]).by_distance(:origin => @bounds_center)
418
+ assert_equal locations[0], locations(:d)
419
+ assert_equal locations[1], locations(:a)
420
+ end
421
+
422
+ def test_find_within_bounds_with_token
423
+ #locations = Location.geo_scope(:bounds=>[@sw,@ne])
424
+ locations = Location.in_bounds([@sw,@ne])
425
+ assert_equal 2, locations.to_a.size
426
+ assert_equal 2, locations.count
427
+ end
428
+
429
+ def test_find_within_bounds_with_string_conditions
430
+ #locations = Location.geo_scope(:bounds=>[@sw,@ne]).where("id !=#{locations(:a).id}")
431
+ locations = Location.in_bounds([@sw,@ne]).where("id !=#{locations(:a).id}")
432
+ assert_equal 1, locations.to_a.size
433
+ end
434
+
435
+ def test_find_within_bounds_with_array_conditions
436
+ #locations = Location.geo_scope(:bounds=>[@sw,@ne]).where(["id != ?", locations(:a).id])
437
+ locations = Location.in_bounds([@sw,@ne]).where(["id != ?", locations(:a).id])
438
+ assert_equal 1, locations.to_a.size
439
+ end
440
+
441
+ def test_find_within_bounds_with_hash_conditions
442
+ #locations = Location.geo_scope(:bounds=>[@sw,@ne]).where({:id => locations(:a).id})
443
+ locations = Location.in_bounds([@sw,@ne]).where({:id => locations(:a).id})
444
+ assert_equal 1, locations.to_a.size
445
+ end
446
+
447
+ def test_auto_geocode
448
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
449
+ store=Store.new(:address=>'Irving, TX')
450
+ store.save
451
+ assert_equal store.lat,@location_a.lat
452
+ assert_equal store.lng,@location_a.lng
453
+ assert_equal 0, store.errors.size
454
+ end
455
+
456
+ def test_auto_geocode_failure
457
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("BOGUS").returns(Geokit::GeoLoc.new)
458
+ store=Store.new(:address=>'BOGUS')
459
+ store.save
460
+ assert store.new_record?
461
+ assert_equal 1, store.errors.size
462
+ end
463
+
464
+ # Test :through
465
+
466
+ #def test_find_with_through
467
+ # organizations = MockOrganization.geo_scope(:origin => @location_a).order('distance ASC')
468
+ # assert_equal 2, organizations.all.size
469
+ # organizations = MockOrganization.geo_scope(:origin => @location_a).where("distance < 3.97")
470
+ # assert_equal 1, organizations.count
471
+ #end
472
+
473
+ def test_find_with_through_with_hash
474
+ #people = MockPerson.geo_scope(:origin => @location_a).order('distance ASC')
475
+ people = MockPerson.order('distance ASC')
476
+ assert_equal 2, people.size
477
+ assert_equal 2, people.count
478
+ end
479
+
480
+ def test_sort_by_distance_from
481
+ locations = Location.with_latlng.all
482
+ unsorted = [locations(:a), locations(:b), locations(:c), locations(:d), locations(:e), locations(:f)]
483
+ sorted = [locations(:a), locations(:b), locations(:c), locations(:f), locations(:d), locations(:e)]
484
+ assert_equal sorted, locations.sort_by{|l| l.distance_to(locations(:a))}
485
+
486
+ unsorted_collection = DistanceCollection.new(unsorted)
487
+ unsorted_collection.set_distance_from(locations(:a))
488
+ assert_equal sorted, unsorted_collection.sort_by(&:distance)
489
+ end
490
+
491
+ end