geokit-rails 2.5.0 → 2.5.1

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 (99) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +80 -0
  3. data/.gitignore +44 -37
  4. data/CHANGELOG.md +106 -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 +34 -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 +299 -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/distance_collection.rb +8 -8
  29. data/lib/geokit-rails/geocoder_control.rb +19 -19
  30. data/lib/geokit-rails/ip_geocode_lookup.rb +44 -45
  31. data/lib/geokit-rails/railtie.rb +37 -37
  32. data/lib/geokit-rails/version.rb +3 -3
  33. data/lib/geokit-rails.rb +11 -11
  34. data/test/acts_as_mappable_test.rb +486 -478
  35. data/test/adapter_resolution_test.rb +46 -0
  36. data/test/boot.rb +40 -32
  37. data/test/database.yml +31 -28
  38. data/test/distance_collection_test.rb +41 -41
  39. data/test/dummy/Rakefile +6 -6
  40. data/test/dummy/app/assets/stylesheets/application.css +1 -1
  41. data/test/dummy/app/channels/application_cable/channel.rb +4 -4
  42. data/test/dummy/app/channels/application_cable/connection.rb +4 -4
  43. data/test/dummy/app/controllers/application_controller.rb +3 -3
  44. data/test/dummy/app/controllers/location_aware_controller.rb +42 -42
  45. data/test/dummy/app/helpers/application_helper.rb +2 -2
  46. data/test/dummy/app/jobs/application_job.rb +7 -7
  47. data/test/dummy/app/mailers/application_mailer.rb +4 -4
  48. data/test/dummy/app/models/application_record.rb +3 -3
  49. data/test/dummy/app/views/layouts/application.html.erb +15 -15
  50. data/test/dummy/app/views/layouts/mailer.html.erb +13 -13
  51. data/test/dummy/app/views/layouts/mailer.text.erb +1 -1
  52. data/test/dummy/bin/rails +4 -4
  53. data/test/dummy/bin/rake +4 -4
  54. data/test/dummy/bin/setup +33 -33
  55. data/test/dummy/config/application.rb +22 -22
  56. data/test/dummy/config/boot.rb +5 -5
  57. data/test/dummy/config/cable.yml +10 -10
  58. data/test/dummy/config/database.yml +25 -25
  59. data/test/dummy/config/environment.rb +5 -5
  60. data/test/dummy/config/environments/development.rb +68 -68
  61. data/test/dummy/config/environments/production.rb +87 -87
  62. data/test/dummy/config/environments/test.rb +60 -60
  63. data/test/dummy/config/initializers/content_security_policy.rb +25 -25
  64. data/test/dummy/config/initializers/filter_parameter_logging.rb +8 -8
  65. data/test/dummy/config/initializers/inflections.rb +16 -16
  66. data/test/dummy/config/initializers/permissions_policy.rb +11 -11
  67. data/test/dummy/config/locales/en.yml +33 -33
  68. data/test/dummy/config/puma.rb +43 -43
  69. data/test/dummy/config/routes.rb +6 -6
  70. data/test/dummy/config/storage.yml +34 -34
  71. data/test/dummy/config.ru +6 -6
  72. data/test/dummy/public/404.html +67 -67
  73. data/test/dummy/public/422.html +67 -67
  74. data/test/dummy/public/500.html +66 -66
  75. data/test/fixtures/companies.yml +9 -9
  76. data/test/fixtures/custom_locations.yml +61 -61
  77. data/test/fixtures/locations.yml +61 -61
  78. data/test/fixtures/mock_addresses.yml +16 -16
  79. data/test/fixtures/mock_families.yml +2 -2
  80. data/test/fixtures/mock_houses.yml +8 -8
  81. data/test/fixtures/mock_organizations.yml +4 -4
  82. data/test/fixtures/mock_people.yml +4 -4
  83. data/test/ip_geocode_lookup_test.rb +52 -48
  84. data/test/models/company.rb +2 -2
  85. data/test/models/custom_location.rb +11 -11
  86. data/test/models/location.rb +3 -3
  87. data/test/models/mock_address.rb +3 -3
  88. data/test/models/mock_family.rb +4 -4
  89. data/test/models/mock_house.rb +2 -2
  90. data/test/models/mock_organization.rb +5 -5
  91. data/test/models/mock_person.rb +6 -6
  92. data/test/models/store.rb +2 -2
  93. data/test/schema.rb +61 -61
  94. data/test/tasks.rake +37 -37
  95. data/test/test_helper.rb +83 -73
  96. metadata +13 -15
  97. data/.travis.yml +0 -55
  98. data/gemfiles/rails3.gemfile +0 -8
  99. data/gemfiles/rails4.gemfile +0 -8
@@ -1,478 +1,486 @@
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
+ # MockOrganization has no lat/lng columns of its own; they come from
177
+ # mock_addresses through the join.
178
+ def test_find_range_with_through
179
+ organizations = MockOrganization.joins(:mock_address)
180
+ .in_range(0..3, origin: @loc_a)
181
+ assert_equal [mock_organizations(:barnes_and_noble)], organizations.to_a
182
+ end
183
+
184
+ def test_find_nearest
185
+ assert_equal @loc_a, Location.nearest(:origin => @loc_a).first
186
+ end
187
+
188
+ def test_find_nearest_with_coordinates
189
+ assert_equal @loc_a, Location.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
190
+ end
191
+
192
+ def test_find_nearest_is_scope
193
+ assert Location.nearest(:origin => @loc_a).respond_to? :where
194
+ end
195
+
196
+ def test_find_farthest
197
+ assert_equal @loc_e, Location.farthest(:origin => @loc_a).first
198
+ end
199
+
200
+ def test_find_farthest_with_coordinates
201
+ assert_equal @loc_e, Location.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
202
+ end
203
+
204
+ def test_find_farthest_is_scope
205
+ assert Location.farthest(:origin => @loc_a).respond_to? :where
206
+ end
207
+
208
+ def test_scoped_distance_column_in_select
209
+ #locations = @starbucks.locations.geo_scope(:origin => @loc_a).order("distance ASC")
210
+ locations = @starbucks.locations.by_distance(:origin => @loc_a)
211
+ assert_equal 5, locations.to_a.size
212
+ assert_equal 0, @loc_a.distance_to(locations.first)
213
+ assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
214
+ end
215
+
216
+ def test_scoped_find_with_distance_condition
217
+ #locations = @starbucks.locations.geo_scope(:origin => @loc_a).where("distance < 3.97")
218
+ locations = @starbucks.locations.within(3.97, :origin => @loc_a)
219
+ assert_equal 4, locations.to_a.size
220
+ assert_equal 4, locations.count
221
+ end
222
+
223
+ def test_scoped_find_within
224
+ locations = @starbucks.locations.within(3.97, :origin => @loc_a)
225
+ assert_equal 4, locations.to_a.size
226
+ assert_equal 4, locations.count
227
+ end
228
+
229
+ def test_scoped_find_with_compound_condition
230
+ locations = @starbucks.locations.within(5, :origin => @loc_a).where("city = 'Coppell'")
231
+ assert_equal 2, locations.to_a.size
232
+ assert_equal 2, locations.count
233
+ end
234
+
235
+ def test_scoped_find_beyond
236
+ locations = @starbucks.locations.beyond(3.95, :origin => @loc_a)
237
+ assert_equal 1, locations.to_a.size
238
+ assert_equal 1, locations.count
239
+ end
240
+
241
+ def test_scoped_find_nearest
242
+ assert_equal @loc_a, @starbucks.locations.nearest(:origin => @loc_a).first
243
+ end
244
+
245
+ def test_scoped_find_farthest
246
+ assert_equal @loc_e, @starbucks.locations.farthest(:origin => @loc_a).first
247
+ end
248
+
249
+ def test_ip_geocoded_distance_column_in_select
250
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
251
+ locations = Location.by_distance(:origin => LOCATION_A_IP)
252
+ assert_equal 6, locations.to_a.size
253
+ assert_equal 0, @loc_a.distance_to(locations.first)
254
+ assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
255
+ end
256
+
257
+ def test_ip_geocoded_find_with_distance_condition
258
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
259
+ locations = Location.within(3.97, :origin => LOCATION_A_IP)
260
+ assert_equal 5, locations.to_a.size
261
+ assert_equal 5, locations.count
262
+ end
263
+
264
+ def test_ip_geocoded_find_within
265
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
266
+ locations = Location.within(3.97, :origin => LOCATION_A_IP)
267
+ assert_equal 5, locations.to_a.size
268
+ assert_equal 5, locations.count
269
+ end
270
+
271
+ def test_ip_geocoded_find_with_compound_condition
272
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
273
+ locations = Location.within(5, :origin => LOCATION_A_IP).where("city = 'Coppell'")
274
+ assert_equal 2, locations.to_a.size
275
+ assert_equal 2, locations.count
276
+ end
277
+
278
+ def test_ip_geocoded_find_with_secure_compound_condition
279
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).twice.returns(@location_a)
280
+ locations = Location.within(5, :origin => LOCATION_A_IP).where(["city = ?", 'Coppell'])
281
+ assert_equal 2, locations.to_a.size
282
+ assert_equal 2, locations.count
283
+ end
284
+
285
+ def test_ip_geocoded_find_beyond
286
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
287
+ locations = Location.beyond(3.95, :origin => LOCATION_A_IP)
288
+ assert_equal 1, locations.to_a.size
289
+ assert_equal 1, locations.count
290
+ end
291
+
292
+ def test_ip_geocoded_find_nearest
293
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
294
+ assert_equal @loc_a, Location.nearest(:origin => LOCATION_A_IP).first
295
+ end
296
+
297
+ def test_ip_geocoded_find_farthest
298
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
299
+ assert_equal @loc_e, Location.farthest(:origin => LOCATION_A_IP).first
300
+ end
301
+
302
+ def test_ip_geocoder_exception
303
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with('127.0.0.1').returns(Geokit::GeoLoc.new)
304
+ assert_raises Geokit::Geocoders::GeocodeError do
305
+ Location.farthest(:origin => '127.0.0.1').first
306
+ end
307
+ end
308
+
309
+ def test_address_geocode
310
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with('Irving, TX').twice.returns(@location_a)
311
+ #locations = Location.geo_scope(:origin => 'Irving, TX').where(["distance < ? and city = ?", 5, 'Coppell'])
312
+ locations = Location.within(5, :origin => 'Irving, TX').where(["city = ?", 'Coppell'])
313
+ assert_equal 2, locations.to_a.size
314
+ assert_equal 2, locations.count
315
+ end
316
+
317
+ def test_find_with_custom_distance_condition
318
+ locations = CustomLocation.within(3.97, :origin => @loc_a)
319
+ assert_equal 5, locations.to_a.size
320
+ assert_equal 5, locations.count
321
+ end
322
+
323
+ def test_find_with_custom_distance_condition_using_custom_origin
324
+ #locations = CustomLocation.geo_scope(:origin => @custom_loc_a).where("dist < 3.97")
325
+ locations = CustomLocation.within(3.97, :origin => @custom_loc_a)
326
+ assert_equal 5, locations.to_a.size
327
+ assert_equal 5, locations.count
328
+ end
329
+
330
+ def test_find_within_with_custom
331
+ locations = CustomLocation.within(3.97, :origin => @loc_a)
332
+ assert_equal 5, locations.to_a.size
333
+ assert_equal 5, locations.count
334
+ end
335
+
336
+ def test_find_within_with_coordinates_with_custom
337
+ locations = CustomLocation.within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
338
+ assert_equal 5, locations.to_a.size
339
+ assert_equal 5, locations.count
340
+ end
341
+
342
+ def test_find_with_compound_condition_with_custom
343
+ #locations = CustomLocation.geo_scope(:origin => @loc_a).where("dist < 5 and city = 'Coppell'")
344
+ locations = CustomLocation.within(5, :origin => @loc_a).where("city = 'Coppell'")
345
+ assert_equal 1, locations.to_a.size
346
+ assert_equal 1, locations.count
347
+ end
348
+
349
+ # REMOVED AS SAME AS BELOW
350
+ #def test_find_with_secure_compound_condition_with_custom
351
+ # locations = CustomLocation.geo_scope(:origin => @loc_a).where(["dist < ? and city = ?", 5, 'Coppell'])
352
+ # assert_equal 1, locations.all.size
353
+ # assert_equal 1, locations.count
354
+ #end
355
+
356
+ def test_find_beyond_with_custom
357
+ locations = CustomLocation.beyond(3.95, :origin => @loc_a)
358
+ assert_equal 1, locations.to_a.size
359
+ assert_equal 1, locations.count
360
+ end
361
+
362
+ def test_find_beyond_with_coordinates_with_custom
363
+ locations = CustomLocation.beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
364
+ assert_equal 1, locations.to_a.size
365
+ assert_equal 1, locations.count
366
+ end
367
+
368
+ def test_find_nearest_with_custom
369
+ assert_equal @custom_loc_a, CustomLocation.nearest(:origin => @loc_a).first
370
+ end
371
+
372
+ def test_find_nearest_with_coordinates_with_custom
373
+ assert_equal @custom_loc_a, CustomLocation.nearest(:origin =>[@loc_a.lat, @loc_a.lng]).first
374
+ end
375
+
376
+ def test_find_farthest_with_custom
377
+ assert_equal @custom_loc_e, CustomLocation.farthest(:origin => @loc_a).first
378
+ end
379
+
380
+ def test_find_farthest_with_coordinates_with_custom
381
+ assert_equal @custom_loc_e, CustomLocation.farthest(:origin =>[@loc_a.lat, @loc_a.lng]).first
382
+ end
383
+
384
+ def test_find_with_array_origin
385
+ #locations = Location.geo_scope(:origin =>[@loc_a.lat,@loc_a.lng]).where("distance < 3.97")
386
+ locations = Location.within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
387
+ assert_equal 5, locations.to_a.size
388
+ assert_equal 5, locations.count
389
+ end
390
+
391
+
392
+ # Bounding box tests
393
+
394
+ def test_find_within_bounds
395
+ locations = Location.in_bounds([@sw,@ne])
396
+ assert_equal 2, locations.to_a.size
397
+ assert_equal 2, locations.count
398
+ end
399
+
400
+ def test_find_within_bounds_with_inclusive
401
+ sw = Geokit::LatLng.new(@loc_a.lat,@loc_a.lng)
402
+ ne = sw
403
+ bounds = [sw,ne]
404
+ locations = Location.in_bounds(bounds, :inclusive => false)
405
+ assert_equal 0, locations.count
406
+ locations = Location.in_bounds(bounds, :inclusive => true)
407
+ assert_equal 1, locations.count
408
+ end
409
+
410
+ def test_find_within_bounds_ordered_by_distance
411
+ #locations = Location.in_bounds([@sw,@ne], :origin=>@bounds_center).order('distance asc')
412
+ locations = Location.in_bounds([@sw,@ne]).by_distance(:origin => @bounds_center)
413
+ assert_equal locations[0], locations(:d)
414
+ assert_equal locations[1], locations(:a)
415
+ end
416
+
417
+ def test_find_within_bounds_with_token
418
+ #locations = Location.geo_scope(:bounds=>[@sw,@ne])
419
+ locations = Location.in_bounds([@sw,@ne])
420
+ assert_equal 2, locations.to_a.size
421
+ assert_equal 2, locations.count
422
+ end
423
+
424
+ def test_find_within_bounds_with_string_conditions
425
+ #locations = Location.geo_scope(:bounds=>[@sw,@ne]).where("id !=#{locations(:a).id}")
426
+ locations = Location.in_bounds([@sw,@ne]).where("id !=#{locations(:a).id}")
427
+ assert_equal 1, locations.to_a.size
428
+ end
429
+
430
+ def test_find_within_bounds_with_array_conditions
431
+ #locations = Location.geo_scope(:bounds=>[@sw,@ne]).where(["id != ?", locations(:a).id])
432
+ locations = Location.in_bounds([@sw,@ne]).where(["id != ?", locations(:a).id])
433
+ assert_equal 1, locations.to_a.size
434
+ end
435
+
436
+ def test_find_within_bounds_with_hash_conditions
437
+ #locations = Location.geo_scope(:bounds=>[@sw,@ne]).where({:id => locations(:a).id})
438
+ locations = Location.in_bounds([@sw,@ne]).where({:id => locations(:a).id})
439
+ assert_equal 1, locations.to_a.size
440
+ end
441
+
442
+ def test_auto_geocode
443
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("Irving, TX").returns(@location_a)
444
+ store=Store.new(:address=>'Irving, TX')
445
+ store.save
446
+ assert_equal store.lat,@location_a.lat
447
+ assert_equal store.lng,@location_a.lng
448
+ assert_equal 0, store.errors.size
449
+ end
450
+
451
+ def test_auto_geocode_failure
452
+ Geokit::Geocoders::MultiGeocoder.expects(:geocode).with("BOGUS").returns(Geokit::GeoLoc.new)
453
+ store=Store.new(:address=>'BOGUS')
454
+ store.save
455
+ assert store.new_record?
456
+ assert_equal 1, store.errors.size
457
+ end
458
+
459
+ # Test :through
460
+
461
+ #def test_find_with_through
462
+ # organizations = MockOrganization.geo_scope(:origin => @location_a).order('distance ASC')
463
+ # assert_equal 2, organizations.all.size
464
+ # organizations = MockOrganization.geo_scope(:origin => @location_a).where("distance < 3.97")
465
+ # assert_equal 1, organizations.count
466
+ #end
467
+
468
+ def test_find_with_through_with_hash
469
+ #people = MockPerson.geo_scope(:origin => @location_a).order('distance ASC')
470
+ people = MockPerson.order('distance ASC')
471
+ assert_equal 2, people.size
472
+ assert_equal 2, people.count
473
+ end
474
+
475
+ def test_sort_by_distance_from
476
+ locations = Location.with_latlng.all
477
+ unsorted = [locations(:a), locations(:b), locations(:c), locations(:d), locations(:e), locations(:f)]
478
+ sorted = [locations(:a), locations(:b), locations(:c), locations(:f), locations(:d), locations(:e)]
479
+ assert_equal sorted, locations.sort_by{|l| l.distance_to(locations(:a))}
480
+
481
+ unsorted_collection = DistanceCollection.new(unsorted)
482
+ unsorted_collection.set_distance_from(locations(:a))
483
+ assert_equal sorted, unsorted_collection.sort_by(&:distance)
484
+ end
485
+
486
+ end