sniff 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. data/README.markdown +73 -0
  2. data/lib/sniff/active_record_ext.rb +12 -0
  3. data/lib/sniff/conversions_ext.rb +45 -0
  4. data/lib/sniff/database.rb +157 -0
  5. data/lib/sniff/emitter.rb +65 -0
  6. data/lib/sniff/tasks.rb +14 -0
  7. data/lib/sniff/timeframe.rb +251 -0
  8. data/lib/sniff.rb +31 -0
  9. data/lib/test_support/data_models/census_division.rb +9 -0
  10. data/lib/test_support/data_models/census_region.rb +9 -0
  11. data/lib/test_support/data_models/climate_division.rb +8 -0
  12. data/lib/test_support/data_models/country.rb +9 -0
  13. data/lib/test_support/data_models/egrid_region.rb +7 -0
  14. data/lib/test_support/data_models/egrid_subregion.rb +8 -0
  15. data/lib/test_support/data_models/gender.rb +6 -0
  16. data/lib/test_support/data_models/petroleum_administration_for_defense_district.rb +9 -0
  17. data/lib/test_support/data_models/state.rb +13 -0
  18. data/lib/test_support/data_models/urbanity.rb +6 -0
  19. data/lib/test_support/data_models/zip_code.rb +14 -0
  20. data/lib/test_support/db/fixtures/census_divisions.csv +10 -0
  21. data/lib/test_support/db/fixtures/census_regions.csv +5 -0
  22. data/lib/test_support/db/fixtures/climate_divisions.csv +360 -0
  23. data/lib/test_support/db/fixtures/countries.csv +247 -0
  24. data/lib/test_support/db/fixtures/egrid_regions.csv +6 -0
  25. data/lib/test_support/db/fixtures/egrid_subregions.csv +27 -0
  26. data/lib/test_support/db/fixtures/genders.csv +3 -0
  27. data/lib/test_support/db/fixtures/petroleum_administration_for_defense_districts.csv +8 -0
  28. data/lib/test_support/db/fixtures/states.csv +58 -0
  29. data/lib/test_support/db/fixtures/urbanities.csv +5 -0
  30. data/lib/test_support/db/fixtures/yearly_anonymous_emissions.csv +0 -0
  31. data/lib/test_support/db/fixtures/yearly_typical_emissions.csv +0 -0
  32. data/lib/test_support/db/fixtures/zip_codes.csv +3390 -0
  33. data/lib/test_support/db/schema.rb +117 -0
  34. data/spec/lib/sniff/database_spec.rb +22 -0
  35. data/spec/spec.opts +2 -0
  36. data/spec/spec_helper.rb +5 -0
  37. data/vendor/geokit-rails/CHANGELOG.rdoc +46 -0
  38. data/vendor/geokit-rails/MIT-LICENSE +20 -0
  39. data/vendor/geokit-rails/README.markdown +561 -0
  40. data/vendor/geokit-rails/Rakefile +18 -0
  41. data/vendor/geokit-rails/about.yml +9 -0
  42. data/vendor/geokit-rails/assets/api_keys_template +61 -0
  43. data/vendor/geokit-rails/init.rb +1 -0
  44. data/vendor/geokit-rails/install.rb +14 -0
  45. data/vendor/geokit-rails/lib/geokit-rails/acts_as_mappable.rb +456 -0
  46. data/vendor/geokit-rails/lib/geokit-rails/adapters/abstract.rb +31 -0
  47. data/vendor/geokit-rails/lib/geokit-rails/adapters/mysql.rb +22 -0
  48. data/vendor/geokit-rails/lib/geokit-rails/adapters/postgresql.rb +22 -0
  49. data/vendor/geokit-rails/lib/geokit-rails/adapters/sqlserver.rb +43 -0
  50. data/vendor/geokit-rails/lib/geokit-rails/defaults.rb +22 -0
  51. data/vendor/geokit-rails/lib/geokit-rails/geocoder_control.rb +16 -0
  52. data/vendor/geokit-rails/lib/geokit-rails/ip_geocode_lookup.rb +46 -0
  53. data/vendor/geokit-rails/lib/geokit-rails.rb +24 -0
  54. data/vendor/geokit-rails/test/acts_as_mappable_test.rb +474 -0
  55. data/vendor/geokit-rails/test/boot.rb +25 -0
  56. data/vendor/geokit-rails/test/database.yml +20 -0
  57. data/vendor/geokit-rails/test/fixtures/companies.yml +7 -0
  58. data/vendor/geokit-rails/test/fixtures/custom_locations.yml +54 -0
  59. data/vendor/geokit-rails/test/fixtures/locations.yml +54 -0
  60. data/vendor/geokit-rails/test/fixtures/mock_addresses.yml +17 -0
  61. data/vendor/geokit-rails/test/fixtures/mock_families.yml +2 -0
  62. data/vendor/geokit-rails/test/fixtures/mock_houses.yml +9 -0
  63. data/vendor/geokit-rails/test/fixtures/mock_organizations.yml +5 -0
  64. data/vendor/geokit-rails/test/fixtures/mock_people.yml +5 -0
  65. data/vendor/geokit-rails/test/fixtures/stores.yml +0 -0
  66. data/vendor/geokit-rails/test/ip_geocode_lookup_test.rb +77 -0
  67. data/vendor/geokit-rails/test/models/company.rb +3 -0
  68. data/vendor/geokit-rails/test/models/custom_location.rb +12 -0
  69. data/vendor/geokit-rails/test/models/location.rb +4 -0
  70. data/vendor/geokit-rails/test/models/mock_address.rb +4 -0
  71. data/vendor/geokit-rails/test/models/mock_family.rb +3 -0
  72. data/vendor/geokit-rails/test/models/mock_house.rb +3 -0
  73. data/vendor/geokit-rails/test/models/mock_organization.rb +4 -0
  74. data/vendor/geokit-rails/test/models/mock_person.rb +4 -0
  75. data/vendor/geokit-rails/test/models/store.rb +3 -0
  76. data/vendor/geokit-rails/test/schema.rb +60 -0
  77. data/vendor/geokit-rails/test/tasks.rake +31 -0
  78. data/vendor/geokit-rails/test/test_helper.rb +23 -0
  79. metadata +492 -0
@@ -0,0 +1,474 @@
1
+ require 'test_helper'
2
+
3
+
4
+ Geokit::Geocoders::provider_order = [:google, :us]
5
+
6
+ class ActsAsMappableTest < GeokitTestCase
7
+
8
+ LOCATION_A_IP = "217.10.83.5"
9
+
10
+ def setup
11
+ @location_a = GeoKit::GeoLoc.new
12
+ @location_a.lat = 32.918593
13
+ @location_a.lng = -96.958444
14
+ @location_a.city = "Irving"
15
+ @location_a.state = "TX"
16
+ @location_a.country_code = "US"
17
+ @location_a.success = true
18
+
19
+ @sw = GeoKit::LatLng.new(32.91663,-96.982841)
20
+ @ne = GeoKit::LatLng.new(32.96302,-96.919495)
21
+ @bounds_center=GeoKit::LatLng.new((@sw.lat+@ne.lat)/2,(@sw.lng+@ne.lng)/2)
22
+
23
+ @starbucks = companies(:starbucks)
24
+ @loc_a = locations(:a)
25
+ @custom_loc_a = custom_locations(:a)
26
+ @loc_e = locations(:e)
27
+ @custom_loc_e = custom_locations(:e)
28
+
29
+ @barnes_and_noble = mock_organizations(:barnes_and_noble)
30
+ @address = mock_addresses(:address_barnes_and_noble)
31
+ end
32
+
33
+ def test_override_default_units_the_hard_way
34
+ Location.default_units = :kms
35
+ locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
36
+ assert_equal 5, locations.size
37
+ locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
38
+ assert_equal 5, locations
39
+ Location.default_units = :miles
40
+ end
41
+
42
+ def test_include
43
+ locations = Location.find(:all, :origin => @loc_a, :include => :company, :conditions => "company_id = 1")
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.find(:all, :origin => @loc_a, :order => "distance ASC")
72
+ assert_equal 6, locations.size
73
+ assert_equal 0, @loc_a.distance_to(locations.first)
74
+ assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
75
+ end
76
+
77
+ def test_find_with_distance_condition
78
+ locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
79
+ assert_equal 5, locations.size
80
+ locations = Location.count(:origin => @loc_a, :conditions => "distance < 3.97")
81
+ assert_equal 5, locations
82
+ end
83
+
84
+ def test_find_with_distance_condition_with_units_override
85
+ locations = Location.find(:all, :origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
86
+ assert_equal 5, locations.size
87
+ locations = Location.count(:origin => @loc_a, :units => :kms, :conditions => "distance < 6.387")
88
+ assert_equal 5, locations
89
+ end
90
+
91
+ def test_find_with_distance_condition_with_formula_override
92
+ locations = Location.find(:all, :origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
93
+ assert_equal 6, locations.size
94
+ locations = Location.count(:origin => @loc_a, :formula => :flat, :conditions => "distance < 6.387")
95
+ assert_equal 6, locations
96
+ end
97
+
98
+ def test_find_within
99
+ locations = Location.find_within(3.97, :origin => @loc_a)
100
+ assert_equal 5, locations.size
101
+ locations = Location.count_within(3.97, :origin => @loc_a)
102
+ assert_equal 5, locations
103
+ end
104
+
105
+ def test_find_within_with_token
106
+ locations = Location.find(:all, :within => 3.97, :origin => @loc_a)
107
+ assert_equal 5, locations.size
108
+ locations = Location.count(:within => 3.97, :origin => @loc_a)
109
+ assert_equal 5, locations
110
+ end
111
+
112
+ def test_find_within_with_coordinates
113
+ locations = Location.find_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
114
+ assert_equal 5, locations.size
115
+ locations = Location.count_within(3.97, :origin =>[@loc_a.lat,@loc_a.lng])
116
+ assert_equal 5, locations
117
+ end
118
+
119
+ def test_find_with_compound_condition
120
+ locations = Location.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
121
+ assert_equal 2, locations.size
122
+ locations = Location.count(:origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
123
+ assert_equal 2, locations
124
+ end
125
+
126
+ def test_find_with_secure_compound_condition
127
+ locations = Location.find(:all, :origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
128
+ assert_equal 2, locations.size
129
+ locations = Location.count(:origin => @loc_a, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
130
+ assert_equal 2, locations
131
+ end
132
+
133
+ def test_find_beyond
134
+ locations = Location.find_beyond(3.95, :origin => @loc_a)
135
+ assert_equal 1, locations.size
136
+ locations = Location.count_beyond(3.95, :origin => @loc_a)
137
+ assert_equal 1, locations
138
+ end
139
+
140
+ def test_find_beyond_with_token
141
+ locations = Location.find(:all, :beyond => 3.95, :origin => @loc_a)
142
+ assert_equal 1, locations.size
143
+ locations = Location.count(:beyond => 3.95, :origin => @loc_a)
144
+ assert_equal 1, locations
145
+ end
146
+
147
+ def test_find_beyond_with_coordinates
148
+ locations = Location.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
149
+ assert_equal 1, locations.size
150
+ locations = Location.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
151
+ assert_equal 1, locations
152
+ end
153
+
154
+ def test_find_range_with_token
155
+ locations = Location.find(:all, :range => 0..10, :origin => @loc_a)
156
+ assert_equal 6, locations.size
157
+ locations = Location.count(:range => 0..10, :origin => @loc_a)
158
+ assert_equal 6, locations
159
+ end
160
+
161
+ def test_find_range_with_token_with_conditions
162
+ locations = Location.find(:all, :origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
163
+ assert_equal 2, locations.size
164
+ locations = Location.count(:origin => @loc_a, :range => 0..10, :conditions => ["city = ?", 'Coppell'])
165
+ assert_equal 2, locations
166
+ end
167
+
168
+ def test_find_range_with_token_with_hash_conditions
169
+ locations = Location.find(:all, :origin => @loc_a, :range => 0..10, :conditions => {:city => 'Coppell'})
170
+ assert_equal 2, locations.size
171
+ locations = Location.count(:origin => @loc_a, :range => 0..10, :conditions => {:city => 'Coppell'})
172
+ assert_equal 2, locations
173
+ end
174
+
175
+ def test_find_range_with_token_excluding_end
176
+ locations = Location.find(:all, :range => 0...10, :origin => @loc_a)
177
+ assert_equal 6, locations.size
178
+ locations = Location.count(:range => 0...10, :origin => @loc_a)
179
+ assert_equal 6, locations
180
+ end
181
+
182
+ def test_find_nearest
183
+ assert_equal @loc_a, Location.find_nearest(:origin => @loc_a)
184
+ end
185
+
186
+ def test_find_nearest_through_find
187
+ assert_equal @loc_a, Location.find(:nearest, :origin => @loc_a)
188
+ end
189
+
190
+ def test_find_nearest_with_coordinates
191
+ assert_equal @loc_a, Location.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
192
+ end
193
+
194
+ def test_find_farthest
195
+ assert_equal @loc_e, Location.find_farthest(:origin => @loc_a)
196
+ end
197
+
198
+ def test_find_farthest_through_find
199
+ assert_equal @loc_e, Location.find(:farthest, :origin => @loc_a)
200
+ end
201
+
202
+ def test_find_farthest_with_coordinates
203
+ assert_equal @loc_e, Location.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
204
+ end
205
+
206
+ def test_scoped_distance_column_in_select
207
+ locations = @starbucks.locations.find(:all, :origin => @loc_a, :order => "distance ASC")
208
+ assert_equal 5, locations.size
209
+ assert_equal 0, @loc_a.distance_to(locations.first)
210
+ assert_in_delta 3.97, @loc_a.distance_to(locations.last, :units => :miles, :formula => :sphere), 0.01
211
+ end
212
+
213
+ def test_scoped_find_with_distance_condition
214
+ locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 3.97")
215
+ assert_equal 4, locations.size
216
+ locations = @starbucks.locations.count(:origin => @loc_a, :conditions => "distance < 3.97")
217
+ assert_equal 4, locations
218
+ end
219
+
220
+ def test_scoped_find_within
221
+ locations = @starbucks.locations.find_within(3.97, :origin => @loc_a)
222
+ assert_equal 4, locations.size
223
+ locations = @starbucks.locations.count_within(3.97, :origin => @loc_a)
224
+ assert_equal 4, locations
225
+ end
226
+
227
+ def test_scoped_find_with_compound_condition
228
+ locations = @starbucks.locations.find(:all, :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
229
+ assert_equal 2, locations.size
230
+ locations = @starbucks.locations.count( :origin => @loc_a, :conditions => "distance < 5 and city = 'Coppell'")
231
+ assert_equal 2, locations
232
+ end
233
+
234
+ def test_scoped_find_beyond
235
+ locations = @starbucks.locations.find_beyond(3.95, :origin => @loc_a)
236
+ assert_equal 1, locations.size
237
+ locations = @starbucks.locations.count_beyond(3.95, :origin => @loc_a)
238
+ assert_equal 1, locations
239
+ end
240
+
241
+ def test_scoped_find_nearest
242
+ assert_equal @loc_a, @starbucks.locations.find_nearest(:origin => @loc_a)
243
+ end
244
+
245
+ def test_scoped_find_farthest
246
+ assert_equal @loc_e, @starbucks.locations.find_farthest(:origin => @loc_a)
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.find(:all, :origin => LOCATION_A_IP, :order => "distance ASC")
252
+ assert_equal 6, locations.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).returns(@location_a)
259
+ locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 3.97")
260
+ assert_equal 5, locations.size
261
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
262
+ locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 3.97")
263
+ assert_equal 5, locations
264
+ end
265
+
266
+ def test_ip_geocoded_find_within
267
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
268
+ locations = Location.find_within(3.97, :origin => LOCATION_A_IP)
269
+ assert_equal 5, locations.size
270
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
271
+ locations = Location.count_within(3.97, :origin => LOCATION_A_IP)
272
+ assert_equal 5, locations
273
+ end
274
+
275
+ def test_ip_geocoded_find_with_compound_condition
276
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
277
+ locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
278
+ assert_equal 2, locations.size
279
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
280
+ locations = Location.count(:origin => LOCATION_A_IP, :conditions => "distance < 5 and city = 'Coppell'")
281
+ assert_equal 2, locations
282
+ end
283
+
284
+ def test_ip_geocoded_find_with_secure_compound_condition
285
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
286
+ locations = Location.find(:all, :origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
287
+ assert_equal 2, locations.size
288
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
289
+ locations = Location.count(:origin => LOCATION_A_IP, :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
290
+ assert_equal 2, locations
291
+ end
292
+
293
+ def test_ip_geocoded_find_beyond
294
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
295
+ locations = Location.find_beyond(3.95, :origin => LOCATION_A_IP)
296
+ assert_equal 1, locations.size
297
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
298
+ locations = Location.count_beyond(3.95, :origin => LOCATION_A_IP)
299
+ assert_equal 1, locations
300
+ end
301
+
302
+ def test_ip_geocoded_find_nearest
303
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
304
+ assert_equal @loc_a, Location.find_nearest(:origin => LOCATION_A_IP)
305
+ end
306
+
307
+ def test_ip_geocoded_find_farthest
308
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with(LOCATION_A_IP).returns(@location_a)
309
+ assert_equal @loc_e, Location.find_farthest(:origin => LOCATION_A_IP)
310
+ end
311
+
312
+ def test_ip_geocoder_exception
313
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with('127.0.0.1').returns(GeoKit::GeoLoc.new)
314
+ assert_raises GeoKit::Geocoders::GeocodeError do
315
+ Location.find_farthest(:origin => '127.0.0.1')
316
+ end
317
+ end
318
+
319
+ def test_address_geocode
320
+ GeoKit::Geocoders::MultiGeocoder.expects(:geocode).with('Irving, TX').returns(@location_a)
321
+ locations = Location.find(:all, :origin => 'Irving, TX', :conditions => ["distance < ? and city = ?", 5, 'Coppell'])
322
+ assert_equal 2, locations.size
323
+ end
324
+
325
+ def test_find_with_custom_distance_condition
326
+ locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 3.97")
327
+ assert_equal 5, locations.size
328
+ locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 3.97")
329
+ assert_equal 5, locations
330
+ end
331
+
332
+ def test_find_with_custom_distance_condition_using_custom_origin
333
+ locations = CustomLocation.find(:all, :origin => @custom_loc_a, :conditions => "dist < 3.97")
334
+ assert_equal 5, locations.size
335
+ locations = CustomLocation.count(:origin => @custom_loc_a, :conditions => "dist < 3.97")
336
+ assert_equal 5, locations
337
+ end
338
+
339
+ def test_find_within_with_custom
340
+ locations = CustomLocation.find_within(3.97, :origin => @loc_a)
341
+ assert_equal 5, locations.size
342
+ locations = CustomLocation.count_within(3.97, :origin => @loc_a)
343
+ assert_equal 5, locations
344
+ end
345
+
346
+ def test_find_within_with_coordinates_with_custom
347
+ locations = CustomLocation.find_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
348
+ assert_equal 5, locations.size
349
+ locations = CustomLocation.count_within(3.97, :origin =>[@loc_a.lat, @loc_a.lng])
350
+ assert_equal 5, locations
351
+ end
352
+
353
+ def test_find_with_compound_condition_with_custom
354
+ locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
355
+ assert_equal 1, locations.size
356
+ locations = CustomLocation.count(:origin => @loc_a, :conditions => "dist < 5 and city = 'Coppell'")
357
+ assert_equal 1, locations
358
+ end
359
+
360
+ def test_find_with_secure_compound_condition_with_custom
361
+ locations = CustomLocation.find(:all, :origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
362
+ assert_equal 1, locations.size
363
+ locations = CustomLocation.count(:origin => @loc_a, :conditions => ["dist < ? and city = ?", 5, 'Coppell'])
364
+ assert_equal 1, locations
365
+ end
366
+
367
+ def test_find_beyond_with_custom
368
+ locations = CustomLocation.find_beyond(3.95, :origin => @loc_a)
369
+ assert_equal 1, locations.size
370
+ locations = CustomLocation.count_beyond(3.95, :origin => @loc_a)
371
+ assert_equal 1, locations
372
+ end
373
+
374
+ def test_find_beyond_with_coordinates_with_custom
375
+ locations = CustomLocation.find_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
376
+ assert_equal 1, locations.size
377
+ locations = CustomLocation.count_beyond(3.95, :origin =>[@loc_a.lat, @loc_a.lng])
378
+ assert_equal 1, locations
379
+ end
380
+
381
+ def test_find_nearest_with_custom
382
+ assert_equal @custom_loc_a, CustomLocation.find_nearest(:origin => @loc_a)
383
+ end
384
+
385
+ def test_find_nearest_with_coordinates_with_custom
386
+ assert_equal @custom_loc_a, CustomLocation.find_nearest(:origin =>[@loc_a.lat, @loc_a.lng])
387
+ end
388
+
389
+ def test_find_farthest_with_custom
390
+ assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin => @loc_a)
391
+ end
392
+
393
+ def test_find_farthest_with_coordinates_with_custom
394
+ assert_equal @custom_loc_e, CustomLocation.find_farthest(:origin =>[@loc_a.lat, @loc_a.lng])
395
+ end
396
+
397
+ def test_find_with_array_origin
398
+ locations = Location.find(:all, :origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
399
+ assert_equal 5, locations.size
400
+ locations = Location.count(:origin =>[@loc_a.lat,@loc_a.lng], :conditions => "distance < 3.97")
401
+ assert_equal 5, locations
402
+ end
403
+
404
+
405
+ # Bounding box tests
406
+
407
+ def test_find_within_bounds
408
+ locations = Location.find_within_bounds([@sw,@ne])
409
+ assert_equal 2, locations.size
410
+ locations = Location.count_within_bounds([@sw,@ne])
411
+ assert_equal 2, locations
412
+ end
413
+
414
+ def test_find_within_bounds_ordered_by_distance
415
+ locations = Location.find_within_bounds([@sw,@ne], :origin=>@bounds_center, :order=>'distance asc')
416
+ assert_equal locations[0], locations(:d)
417
+ assert_equal locations[1], locations(:a)
418
+ end
419
+
420
+ def test_find_within_bounds_with_token
421
+ locations = Location.find(:all, :bounds=>[@sw,@ne])
422
+ assert_equal 2, locations.size
423
+ locations = Location.count(:bounds=>[@sw,@ne])
424
+ assert_equal 2, locations
425
+ end
426
+
427
+ def test_find_within_bounds_with_string_conditions
428
+ locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>"id !=#{locations(:a).id}")
429
+ assert_equal 1, locations.size
430
+ end
431
+
432
+ def test_find_within_bounds_with_array_conditions
433
+ locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>["id != ?", locations(:a).id])
434
+ assert_equal 1, locations.size
435
+ end
436
+
437
+ def test_find_within_bounds_with_hash_conditions
438
+ locations = Location.find(:all, :bounds=>[@sw,@ne], :conditions=>{:id => locations(:a).id})
439
+ assert_equal 1, locations.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.find(:all, :origin => @location_a, :order => 'distance ASC')
463
+ assert_equal 2, organizations.size
464
+ organizations = MockOrganization.count(:origin => @location_a, :conditions => "distance < 3.97")
465
+ assert_equal 1, organizations
466
+ end
467
+
468
+ def test_find_with_through_with_hash
469
+ people = MockPerson.find(:all, :origin => @location_a, :order => 'distance ASC')
470
+ assert_equal 2, people.size
471
+ people = MockPerson.count(:origin => @location_a, :conditions => "distance < 3.97")
472
+ assert_equal 2, people
473
+ end
474
+ end
@@ -0,0 +1,25 @@
1
+ require 'active_support'
2
+ require 'active_support/test_case'
3
+
4
+ require 'test/unit'
5
+ require 'test/unit/testcase'
6
+ require 'active_support/testing/setup_and_teardown'
7
+
8
+ require 'active_record'
9
+ require 'active_record/fixtures'
10
+
11
+ require 'action_controller'
12
+ require 'action_controller/test_process'
13
+
14
+ PLUGIN_ROOT = File.join(File.dirname(__FILE__), '..')
15
+ ADAPTER = ENV['DB'] || 'mysql'
16
+
17
+ $LOAD_PATH << File.join(PLUGIN_ROOT, 'lib') << File.join(PLUGIN_ROOT, 'test', 'models')
18
+
19
+ FIXTURES_PATH = File.join(PLUGIN_ROOT, 'test', 'fixtures')
20
+ ActiveRecord::Base.configurations = config = YAML::load(IO.read(File.join(PLUGIN_ROOT, 'test', 'database.yml')))
21
+ ActiveRecord::Base.logger = Logger.new(File.join(PLUGIN_ROOT, 'test', "#{ADAPTER}-debug.log"))
22
+ ActiveRecord::Base.establish_connection(config[ADAPTER])
23
+
24
+ ActiveRecord::Migration.verbose = false
25
+ load File.join(PLUGIN_ROOT, 'test', 'schema.rb')
@@ -0,0 +1,20 @@
1
+ baes: &base
2
+ host: localhost
3
+ username: root
4
+ password:
5
+
6
+ mysql:
7
+ adapter: mysql
8
+ database: geokit_plugin_test
9
+ <<: *base
10
+
11
+ postgresql:
12
+ adapter: postgresql
13
+ database: geokit_plugin_test
14
+ <<: *base
15
+
16
+ sqlserver:
17
+ adapter: sqlserver
18
+ mode: ODBC
19
+ dsn: geokit_tests
20
+ username: ruby
@@ -0,0 +1,7 @@
1
+ starbucks:
2
+ id: 1
3
+ name: Starbucks
4
+
5
+ barnes_and_noble:
6
+ id: 2
7
+ name: Barnes & Noble
@@ -0,0 +1,54 @@
1
+ a:
2
+ id: 1
3
+ company_id: 1
4
+ street: 7979 N MacArthur Blvd
5
+ city: Irving
6
+ state: TX
7
+ postal_code: 75063
8
+ latitude: 32.918593
9
+ longitude: -96.958444
10
+ b:
11
+ id: 2
12
+ company_id: 1
13
+ street: 7750 N Macarthur Blvd # 160
14
+ city: Irving
15
+ state: TX
16
+ postal_code: 75063
17
+ latitude: 32.914144
18
+ longitude: -96.958444
19
+ c:
20
+ id: 3
21
+ company_id: 1
22
+ street: 5904 N Macarthur Blvd # 160
23
+ city: Irving
24
+ state: TX
25
+ postal_code: 75039
26
+ latitude: 32.895155
27
+ longitude: -96.958444
28
+ d:
29
+ id: 4
30
+ company_id: 1
31
+ street: 817 S Macarthur Blvd # 145
32
+ city: Coppell
33
+ state: TX
34
+ postal_code: 75019
35
+ latitude: 32.951613
36
+ longitude: -96.958444
37
+ e:
38
+ id: 5
39
+ company_id: 1
40
+ street: 106 N Denton Tap Rd # 350
41
+ city: Coppell
42
+ state: TX
43
+ postal_code: 75019
44
+ latitude: 32.969527
45
+ longitude: -96.990159
46
+ f:
47
+ id: 6
48
+ company_id: 2
49
+ street: 5904 N Macarthur Blvd # 160
50
+ city: Irving
51
+ state: TX
52
+ postal_code: 75039
53
+ latitude: 32.895155
54
+ longitude: -96.958444
@@ -0,0 +1,54 @@
1
+ a:
2
+ id: 1
3
+ company_id: 1
4
+ street: 7979 N MacArthur Blvd
5
+ city: Irving
6
+ state: TX
7
+ postal_code: 75063
8
+ lat: 32.918593
9
+ lng: -96.958444
10
+ b:
11
+ id: 2
12
+ company_id: 1
13
+ street: 7750 N Macarthur Blvd # 160
14
+ city: Irving
15
+ state: TX
16
+ postal_code: 75063
17
+ lat: 32.914144
18
+ lng: -96.958444
19
+ c:
20
+ id: 3
21
+ company_id: 1
22
+ street: 5904 N Macarthur Blvd # 160
23
+ city: Irving
24
+ state: TX
25
+ postal_code: 75039
26
+ lat: 32.895155
27
+ lng: -96.958444
28
+ d:
29
+ id: 4
30
+ company_id: 1
31
+ street: 817 S Macarthur Blvd # 145
32
+ city: Coppell
33
+ state: TX
34
+ postal_code: 75019
35
+ lat: 32.951613
36
+ lng: -96.958444
37
+ e:
38
+ id: 5
39
+ company_id: 1
40
+ street: 106 N Denton Tap Rd # 350
41
+ city: Coppell
42
+ state: TX
43
+ postal_code: 75019
44
+ lat: 32.969527
45
+ lng: -96.990159
46
+ f:
47
+ id: 6
48
+ company_id: 2
49
+ street: 5904 N Macarthur Blvd # 160
50
+ city: Irving
51
+ state: TX
52
+ postal_code: 75039
53
+ lat: 32.895155
54
+ lng: -96.958444
@@ -0,0 +1,17 @@
1
+ address_starbucks:
2
+ addressable: starbucks (MockOrganization)
3
+ street: 106 N Denton Tap Rd # 350
4
+ city: Coppell
5
+ state: TX
6
+ postal_code: 75019
7
+ lat: 32.969527
8
+ lng: -96.990159
9
+
10
+ address_barnes_and_noble:
11
+ addressable: barnes_and_noble (MockOrganization)
12
+ street: 5904 N Macarthur Blvd # 160
13
+ city: Irving
14
+ state: TX
15
+ postal_code: 75039
16
+ lat: 32.895155
17
+ lng: -96.958444
@@ -0,0 +1,2 @@
1
+ my_family:
2
+ mock_house: my_house
@@ -0,0 +1,9 @@
1
+ another_house:
2
+ address: 106 N Denton Tap Rd # 350
3
+ lat: 32.969527
4
+ lng: -96.990159
5
+
6
+ my_house:
7
+ address: 5904 N Macarthur Blvd # 160
8
+ lat: 32.895155
9
+ lng: -96.958444
@@ -0,0 +1,5 @@
1
+ starbucks:
2
+ name: Starbucks
3
+
4
+ barnes_and_noble:
5
+ name: Barnes & Noble
@@ -0,0 +1,5 @@
1
+ father:
2
+ mock_family: my_family
3
+
4
+ mother:
5
+ mock_family: my_family
File without changes