artofmission-Geokit 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ require 'test/unit'
2
+ require 'net/http'
3
+ require 'rubygems'
4
+ require 'mocha'
5
+ require File.join(File.dirname(__FILE__), '../../../../config/environment')
6
+
7
+
8
+ class MockSuccess < Net::HTTPSuccess #:nodoc: all
9
+ def initialize
10
+ end
11
+ end
12
+
13
+ class MockFailure < Net::HTTPServiceUnavailable #:nodoc: all
14
+ def initialize
15
+ end
16
+ end
17
+
18
+ # Base class for testing geocoders.
19
+ class BaseGeocoderTest < Test::Unit::TestCase #:nodoc: all
20
+
21
+ # Defines common test fixtures.
22
+ def setup
23
+ @address = 'San Francisco, CA'
24
+ @full_address = '100 Spear St, San Francisco, CA, 94105-1522, US'
25
+ @full_address_short_zip = '100 Spear St, San Francisco, CA, 94105, US'
26
+
27
+ @success = GeoKit::GeoLoc.new({:city=>"SAN FRANCISCO", :state=>"CA", :country_code=>"US", :lat=>37.7742, :lng=>-122.417068})
28
+ @success.success = true
29
+ end
30
+
31
+ def test_timeout_call_web_service
32
+ GeoKit::Geocoders::Geocoder.class_eval do
33
+ def self.do_get(url)
34
+ sleep(2)
35
+ end
36
+ end
37
+ url = "http://www.anything.com"
38
+ GeoKit::Geocoders::timeout = 1
39
+ assert_nil GeoKit::Geocoders::Geocoder.call_geocoder_service(url)
40
+ end
41
+
42
+ def test_successful_call_web_service
43
+ url = "http://www.anything.com"
44
+ GeoKit::Geocoders::Geocoder.expects(:do_get).with(url).returns("SUCCESS")
45
+ assert_equal "SUCCESS", GeoKit::Geocoders::Geocoder.call_geocoder_service(url)
46
+ end
47
+
48
+ def test_find_geocoder_methods
49
+ public_methods = GeoKit::Geocoders::Geocoder.public_methods
50
+ assert public_methods.include?("yahoo_geocoder")
51
+ assert public_methods.include?("google_geocoder")
52
+ assert public_methods.include?("ca_geocoder")
53
+ assert public_methods.include?("us_geocoder")
54
+ assert public_methods.include?("multi_geocoder")
55
+ assert public_methods.include?("ip_geocoder")
56
+ end
57
+ end
@@ -0,0 +1,75 @@
1
+ $LOAD_PATH.unshift File.join('..', 'lib')
2
+ require 'geo_kit/mappable'
3
+ require 'test/unit'
4
+
5
+ class BoundsTest < Test::Unit::TestCase #:nodoc: all
6
+
7
+ def setup
8
+ # This is the area in Texas
9
+ @sw = GeoKit::LatLng.new(32.91663,-96.982841)
10
+ @ne = GeoKit::LatLng.new(32.96302,-96.919495)
11
+ @bounds=GeoKit::Bounds.new(@sw,@ne)
12
+ @loc_a=GeoKit::LatLng.new(32.918593,-96.958444) # inside bounds
13
+ @loc_b=GeoKit::LatLng.new(32.914144,-96.958444) # outside bouds
14
+
15
+ # this is a cross-meridan area
16
+ @cross_meridian=GeoKit::Bounds.normalize([30,170],[40,-170])
17
+ @inside_cm=GeoKit::LatLng.new(35,175)
18
+ @inside_cm_2=GeoKit::LatLng.new(35,-175)
19
+ @east_of_cm=GeoKit::LatLng.new(35,-165)
20
+ @west_of_cm=GeoKit::LatLng.new(35,165)
21
+
22
+ end
23
+
24
+ def test_equality
25
+ assert_equal GeoKit::Bounds.new(@sw,@ne), GeoKit::Bounds.new(@sw,@ne)
26
+ end
27
+
28
+ def test_normalize
29
+ res=GeoKit::Bounds.normalize(@sw,@ne)
30
+ assert_equal res,GeoKit::Bounds.new(@sw,@ne)
31
+ res=GeoKit::Bounds.normalize([@sw,@ne])
32
+ assert_equal res,GeoKit::Bounds.new(@sw,@ne)
33
+ res=GeoKit::Bounds.normalize([@sw.lat,@sw.lng],[@ne.lat,@ne.lng])
34
+ assert_equal res,GeoKit::Bounds.new(@sw,@ne)
35
+ res=GeoKit::Bounds.normalize([[@sw.lat,@sw.lng],[@ne.lat,@ne.lng]])
36
+ assert_equal res,GeoKit::Bounds.new(@sw,@ne)
37
+ end
38
+
39
+ def test_point_inside_bounds
40
+ assert @bounds.contains?(@loc_a)
41
+ end
42
+
43
+ def test_point_outside_bounds
44
+ assert !@bounds.contains?(@loc_b)
45
+ end
46
+
47
+ def test_point_inside_bounds_cross_meridian
48
+ assert @cross_meridian.contains?(@inside_cm)
49
+ assert @cross_meridian.contains?(@inside_cm_2)
50
+ end
51
+
52
+ def test_point_outside_bounds_cross_meridian
53
+ assert !@cross_meridian.contains?(@east_of_cm)
54
+ assert !@cross_meridian.contains?(@west_of_cm)
55
+ end
56
+
57
+ def test_center
58
+ assert_in_delta 32.939828,@bounds.center.lat,0.00005
59
+ assert_in_delta -96.9511763,@bounds.center.lng,0.00005
60
+ end
61
+
62
+ def test_center_cross_meridian
63
+ assert_in_delta 35.41160, @cross_meridian.center.lat,0.00005
64
+ assert_in_delta 179.38112, @cross_meridian.center.lng,0.00005
65
+ end
66
+
67
+ def test_creation_from_circle
68
+ bounds=GeoKit::Bounds.from_point_and_radius([32.939829, -96.951176],2.5)
69
+ inside=GeoKit::LatLng.new 32.9695270000,-96.9901590000
70
+ outside=GeoKit::LatLng.new 32.8951550000,-96.9584440000
71
+ assert bounds.contains?(inside)
72
+ assert !bounds.contains?(outside)
73
+ end
74
+
75
+ end
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), 'base_geocoder_test')
2
+
3
+ GeoKit::Geocoders::geocoder_ca = "SOMEKEYVALUE"
4
+
5
+ class CaGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ CA_SUCCESS=<<-EOF
8
+ <?xml version="1.0" encoding="UTF-8" ?>
9
+ <geodata><latt>49.243086</latt><longt>-123.153684</longt></geodata>
10
+ EOF
11
+
12
+ def setup
13
+ @ca_full_hash = {:street_address=>"2105 West 32nd Avenue",:city=>"Vancouver", :state=>"BC"}
14
+ @ca_full_loc = GeoKit::GeoLoc.new(@ca_full_hash)
15
+ end
16
+
17
+ def test_geocoder_with_geo_loc_with_account
18
+ response = MockSuccess.new
19
+ response.expects(:body).returns(CA_SUCCESS)
20
+ url = "http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml"
21
+ GeoKit::Geocoders::CaGeocoder.expects(:call_geocoder_service).with(url).returns(response)
22
+ verify(GeoKit::Geocoders::CaGeocoder.geocode(@ca_full_loc))
23
+ end
24
+
25
+ def test_service_unavailable
26
+ response = MockFailure.new
27
+ #Net::HTTP.expects(:get_response).with(URI.parse("http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml")).returns(response)
28
+ url = "http://geocoder.ca/?stno=2105&addresst=West+32nd+Avenue&city=Vancouver&prov=BC&auth=SOMEKEYVALUE&geoit=xml"
29
+ GeoKit::Geocoders::CaGeocoder.expects(:call_geocoder_service).with(url).returns(response)
30
+ assert !GeoKit::Geocoders::CaGeocoder.geocode(@ca_full_loc).success
31
+ end
32
+
33
+ private
34
+
35
+ def verify(location)
36
+ assert_equal "BC", location.state
37
+ assert_equal "Vancouver", location.city
38
+ assert_equal "49.243086,-123.153684", location.ll
39
+ assert !location.is_us?
40
+ end
41
+ end
@@ -0,0 +1,12 @@
1
+ mysql:
2
+ adapter: mysql
3
+ host: localhost
4
+ username: root
5
+ password:
6
+ database: geokit_plugin_test
7
+ postgresql:
8
+ adapter: postgresql
9
+ host: localhost
10
+ username: root
11
+ password:
12
+ database: geokit_plugin_test
@@ -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
File without changes
@@ -0,0 +1,49 @@
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__), '../../../../config/environment')
3
+
4
+ class GeoLocTest < Test::Unit::TestCase #:nodoc: all
5
+
6
+ def setup
7
+ @loc = GeoKit::GeoLoc.new
8
+ end
9
+
10
+ def test_is_us
11
+ assert !@loc.is_us?
12
+ @loc.country_code = 'US'
13
+ assert @loc.is_us?
14
+ end
15
+
16
+ def test_street_number
17
+ @loc.street_address = '123 Spear St.'
18
+ assert_equal '123', @loc.street_number
19
+ end
20
+
21
+ def test_street_name
22
+ @loc.street_address = '123 Spear St.'
23
+ assert_equal 'Spear St.', @loc.street_name
24
+ end
25
+
26
+ def test_city
27
+ @loc.city = "san francisco"
28
+ assert_equal 'San Francisco', @loc.city
29
+ end
30
+
31
+ def test_full_address
32
+ @loc.city = 'San Francisco'
33
+ @loc.state = 'CA'
34
+ @loc.zip = '94105'
35
+ @loc.country_code = 'US'
36
+ assert_equal 'San Francisco, CA, 94105, US', @loc.full_address
37
+ @loc.full_address = 'Irving, TX, 75063, US'
38
+ assert_equal 'Irving, TX, 75063, US', @loc.full_address
39
+ end
40
+
41
+ def test_hash
42
+ @loc.city = 'San Francisco'
43
+ @loc.state = 'CA'
44
+ @loc.zip = '94105'
45
+ @loc.country_code = 'US'
46
+ @another = GeoKit::GeoLoc.new @loc.to_hash
47
+ assert_equal @loc, @another
48
+ end
49
+ end
@@ -0,0 +1,88 @@
1
+ require File.join(File.dirname(__FILE__), 'base_geocoder_test')
2
+
3
+ GeoKit::Geocoders::google = 'Google'
4
+
5
+ class GoogleGeocoderTest < BaseGeocoderTest #:nodoc: all
6
+
7
+ GOOGLE_FULL=<<-EOF.strip
8
+ <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>100 spear st, san francisco, ca</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>100 Spear St, San Francisco, CA 94105, USA</address><AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>San Francisco</SubAdministrativeAreaName><Locality><LocalityName>San Francisco</LocalityName><Thoroughfare><ThoroughfareName>100 Spear St</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>94105</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails><Point><coordinates>-122.393985,37.792501,0</coordinates></Point></Placemark></Response></kml>
9
+ EOF
10
+
11
+ GOOGLE_CITY=<<-EOF.strip
12
+ <?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name>San Francisco</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>San Francisco, CA, USA</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><AdministrativeArea><AdministrativeAreaName>CA</AdministrativeAreaName><Locality><LocalityName>San Francisco</LocalityName></Locality></AdministrativeArea></Country></AddressDetails><Point><coordinates>-122.418333,37.775000,0</coordinates></Point></Placemark></Response></kml>
13
+ EOF
14
+
15
+ def setup
16
+ super
17
+ @google_full_hash = {:street_address=>"100 Spear St", :city=>"San Francisco", :state=>"CA", :zip=>"94105", :country_code=>"US"}
18
+ @google_city_hash = {:city=>"San Francisco", :state=>"CA"}
19
+
20
+ @google_full_loc = GeoKit::GeoLoc.new(@google_full_hash)
21
+ @google_city_loc = GeoKit::GeoLoc.new(@google_city_hash)
22
+ end
23
+
24
+ def test_google_full_address
25
+ response = MockSuccess.new
26
+ response.expects(:body).returns(GOOGLE_FULL)
27
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
28
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
29
+ res=GeoKit::Geocoders::GoogleGeocoder.geocode(@address)
30
+ assert_equal "CA", res.state
31
+ assert_equal "San Francisco", res.city
32
+ assert_equal "37.792501,-122.393985", res.ll # slightly dif from yahoo
33
+ assert res.is_us?
34
+ assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
35
+ assert_equal "google", res.provider
36
+ end
37
+
38
+ def test_google_full_address_with_geo_loc
39
+ response = MockSuccess.new
40
+ response.expects(:body).returns(GOOGLE_FULL)
41
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@full_address_short_zip)}&output=xml&key=Google&oe=utf-8"
42
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
43
+ res=GeoKit::Geocoders::GoogleGeocoder.geocode(@google_full_loc)
44
+ assert_equal "CA", res.state
45
+ assert_equal "San Francisco", res.city
46
+ assert_equal "37.792501,-122.393985", res.ll # slightly dif from yahoo
47
+ assert res.is_us?
48
+ assert_equal "100 Spear St, San Francisco, CA 94105, USA", res.full_address #slightly different from yahoo
49
+ assert_equal "google", res.provider
50
+ end
51
+
52
+ def test_google_city
53
+ response = MockSuccess.new
54
+ response.expects(:body).returns(GOOGLE_CITY)
55
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
56
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
57
+ res=GeoKit::Geocoders::GoogleGeocoder.geocode(@address)
58
+ assert_equal "CA", res.state
59
+ assert_equal "San Francisco", res.city
60
+ assert_equal "37.775,-122.418333", res.ll
61
+ assert res.is_us?
62
+ assert_equal "San Francisco, CA, USA", res.full_address
63
+ assert_nil res.street_address
64
+ assert_equal "google", res.provider
65
+ end
66
+
67
+ def test_google_city_with_geo_loc
68
+ response = MockSuccess.new
69
+ response.expects(:body).returns(GOOGLE_CITY)
70
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
71
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
72
+ res=GeoKit::Geocoders::GoogleGeocoder.geocode(@google_city_loc)
73
+ assert_equal "CA", res.state
74
+ assert_equal "San Francisco", res.city
75
+ assert_equal "37.775,-122.418333", res.ll
76
+ assert res.is_us?
77
+ assert_equal "San Francisco, CA, USA", res.full_address
78
+ assert_nil res.street_address
79
+ assert_equal "google", res.provider
80
+ end
81
+
82
+ def test_service_unavailable
83
+ response = MockFailure.new
84
+ url = "http://maps.google.com/maps/geo?q=#{CGI.escape(@address)}&output=xml&key=Google&oe=utf-8"
85
+ GeoKit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
86
+ assert !GeoKit::Geocoders::GoogleGeocoder.geocode(@google_city_loc).success
87
+ end
88
+ end