Created
July 10, 2010 09:58
-
-
Save mumoshu/470604 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby -Ku | |
require "test/unit" | |
require "src/reverse_geocoder.rb" | |
require "pp" | |
require "json" | |
module TestData | |
@@http_geocoder_response = <<JSON | |
{ | |
"status": "OK", | |
"results": [ { | |
"types": [ "sublocality_level_4", "sublocality", "political" ], | |
"formatted_address": "日本, 東京都江東区白河1丁目6−12", | |
"address_components": [ { | |
"long_name": "12", | |
"short_name": "12", | |
"types": [ "sublocality_level_4", "sublocality", "political" ] | |
}, { | |
"long_name": "6", | |
"short_name": "6", | |
"types": [ "sublocality_level_3", "sublocality", "political" ] | |
}, { | |
"long_name": "1丁目", | |
"short_name": "1丁目", | |
"types": [ "sublocality_level_2", "sublocality", "political" ] | |
}, { | |
"long_name": "白河", | |
"short_name": "白河", | |
"types": [ "sublocality_level_1", "sublocality", "political" ] | |
}, { | |
"long_name": "江東区", | |
"short_name": "江東区", | |
"types": [ "locality", "political" ] | |
}, { | |
"long_name": "東京都", | |
"short_name": "東京都", | |
"types": [ "administrative_area_level_1", "political" ] | |
}, { | |
"long_name": "日本", | |
"short_name": "JP", | |
"types": [ "country", "political" ] | |
} ], | |
"geometry": { | |
"location": { | |
"lat": 35.6822885, | |
"lng": 139.7996116 | |
}, | |
"location_type": "APPROXIMATE", | |
"viewport": { | |
"southwest": { | |
"lat": 35.6791409, | |
"lng": 139.7964640 | |
}, | |
"northeast": { | |
"lat": 35.6854361, | |
"lng": 139.8027592 | |
} | |
} | |
} | |
}, { | |
"types": [ "sublocality_level_3", "sublocality", "political" ], | |
"formatted_address": "日本, 東京都江東区白河1丁目6", | |
"address_components": [ { | |
"long_name": "6", | |
"short_name": "6", | |
"types": [ "sublocality_level_3", "sublocality", "political" ] | |
}, { | |
"long_name": "1丁目", | |
"short_name": "1丁目", | |
"types": [ "sublocality_level_2", "sublocality", "political" ] | |
}, { | |
"long_name": "白河", | |
"short_name": "白河", | |
"types": [ "sublocality_level_1", "sublocality", "political" ] | |
}, { | |
"long_name": "江東区", | |
"short_name": "江東区", | |
"types": [ "locality", "political" ] | |
}, { | |
"long_name": "東京都", | |
"short_name": "東京都", | |
"types": [ "administrative_area_level_1", "political" ] | |
}, { | |
"long_name": "日本", | |
"short_name": "JP", | |
"types": [ "country", "political" ] | |
} ], | |
"geometry": { | |
"location": { | |
"lat": 35.6820996, | |
"lng": 139.7991867 | |
}, | |
"location_type": "APPROXIMATE", | |
"viewport": { | |
"southwest": { | |
"lat": 35.6791231, | |
"lng": 139.7961848 | |
}, | |
"northeast": { | |
"lat": 35.6854184, | |
"lng": 139.8024801 | |
} | |
}, | |
"bounds": { | |
"southwest": { | |
"lat": 35.6820416, | |
"lng": 139.7987303 | |
}, | |
"northeast": { | |
"lat": 35.6824999, | |
"lng": 139.7999346 | |
} | |
} | |
} | |
}, { | |
"types": [ "sublocality_level_2", "sublocality", "political" ], | |
"formatted_address": "日本, 東京都江東区白河1丁目", | |
"address_components": [ { | |
"long_name": "1丁目", | |
"short_name": "1丁目", | |
"types": [ "sublocality_level_2", "sublocality", "political" ] | |
}, { | |
"long_name": "白河", | |
"short_name": "白河", | |
"types": [ "sublocality_level_1", "sublocality", "political" ] | |
}, { | |
"long_name": "江東区", | |
"short_name": "江東区", | |
"types": [ "locality", "political" ] | |
}, { | |
"long_name": "東京都", | |
"short_name": "東京都", | |
"types": [ "administrative_area_level_1", "political" ] | |
}, { | |
"long_name": "日本", | |
"short_name": "JP", | |
"types": [ "country", "political" ] | |
} ], | |
"geometry": { | |
"location": { | |
"lat": 35.6813525, | |
"lng": 139.7998088 | |
}, | |
"location_type": "APPROXIMATE", | |
"viewport": { | |
"southwest": { | |
"lat": 35.6791174, | |
"lng": 139.7967261 | |
}, | |
"northeast": { | |
"lat": 35.6854126, | |
"lng": 139.8030213 | |
} | |
}, | |
"bounds": { | |
"southwest": { | |
"lat": 35.6806506, | |
"lng": 139.7985684 | |
}, | |
"northeast": { | |
"lat": 35.6838794, | |
"lng": 139.8011790 | |
} | |
} | |
} | |
}, { | |
"types": [ "sublocality_level_1", "sublocality", "political" ], | |
"formatted_address": "日本, 東京都江東区白河", | |
"address_components": [ { | |
"long_name": "白河", | |
"short_name": "白河", | |
"types": [ "sublocality_level_1", "sublocality", "political" ] | |
}, { | |
"long_name": "江東区", | |
"short_name": "江東区", | |
"types": [ "locality", "political" ] | |
}, { | |
"long_name": "東京都", | |
"short_name": "東京都", | |
"types": [ "administrative_area_level_1", "political" ] | |
}, { | |
"long_name": "日本", | |
"short_name": "JP", | |
"types": [ "country", "political" ] | |
} ], | |
"geometry": { | |
"location": { | |
"lat": 35.6824746, | |
"lng": 139.8032584 | |
}, | |
"location_type": "APPROXIMATE", | |
"viewport": { | |
"southwest": { | |
"lat": 35.6793686, | |
"lng": 139.7985711 | |
}, | |
"northeast": { | |
"lat": 35.6856639, | |
"lng": 139.8101835 | |
} | |
}, | |
"bounds": { | |
"southwest": { | |
"lat": 35.6806554, | |
"lng": 139.7985711 | |
}, | |
"northeast": { | |
"lat": 35.6843771, | |
"lng": 139.8101835 | |
} | |
} | |
} | |
}, { | |
"types": [ "postal_code" ], | |
"formatted_address": "〒135-0021", | |
"address_components": [ { | |
"long_name": "135-0021", | |
"short_name": "135-0021", | |
"types": [ "postal_code" ] | |
}, { | |
"long_name": "日本", | |
"short_name": "JP", | |
"types": [ "country", "political" ] | |
} ], | |
"geometry": { | |
"location": { | |
"lat": 35.6813551, | |
"lng": 139.8035569 | |
}, | |
"location_type": "APPROXIMATE", | |
"viewport": { | |
"southwest": { | |
"lat": 35.6780348, | |
"lng": 139.7984421 | |
}, | |
"northeast": { | |
"lat": 35.6846754, | |
"lng": 139.8086717 | |
} | |
}, | |
"bounds": { | |
"southwest": { | |
"lat": 35.6780348, | |
"lng": 139.7984421 | |
}, | |
"northeast": { | |
"lat": 35.6846754, | |
"lng": 139.8086717 | |
} | |
} | |
} | |
}, { | |
"types": [ "locality", "political" ], | |
"formatted_address": "日本, 東京都江東区", | |
"address_components": [ { | |
"long_name": "江東区", | |
"short_name": "江東区", | |
"types": [ "locality", "political" ] | |
}, { | |
"long_name": "東京都", | |
"short_name": "東京都", | |
"types": [ "administrative_area_level_1", "political" ] | |
}, { | |
"long_name": "日本", | |
"short_name": "JP", | |
"types": [ "country", "political" ] | |
} ], | |
"geometry": { | |
"location": { | |
"lat": 35.6728535, | |
"lng": 139.8174097 | |
}, | |
"location_type": "APPROXIMATE", | |
"viewport": { | |
"southwest": { | |
"lat": 35.5784725, | |
"lng": 139.7711750 | |
}, | |
"northeast": { | |
"lat": 35.7080596, | |
"lng": 139.8490087 | |
} | |
}, | |
"bounds": { | |
"southwest": { | |
"lat": 35.5784725, | |
"lng": 139.7711750 | |
}, | |
"northeast": { | |
"lat": 35.7080596, | |
"lng": 139.8490087 | |
} | |
} | |
} | |
}, { | |
"types": [ "administrative_area_level_1", "political" ], | |
"formatted_address": "日本, 東京都", | |
"address_components": [ { | |
"long_name": "東京都", | |
"short_name": "東京都", | |
"types": [ "administrative_area_level_1", "political" ] | |
}, { | |
"long_name": "日本", | |
"short_name": "JP", | |
"types": [ "country", "political" ] | |
} ], | |
"geometry": { | |
"location": { | |
"lat": 35.6894875, | |
"lng": 139.6917064 | |
}, | |
"location_type": "APPROXIMATE", | |
"viewport": { | |
"southwest": { | |
"lat": 35.5011900, | |
"lng": 138.9427580 | |
}, | |
"northeast": { | |
"lat": 35.8986440, | |
"lng": 139.9198530 | |
} | |
}, | |
"bounds": { | |
"southwest": { | |
"lat": 35.5011900, | |
"lng": 138.9427580 | |
}, | |
"northeast": { | |
"lat": 35.8986440, | |
"lng": 139.9198530 | |
} | |
} | |
} | |
}, { | |
"types": [ "country", "political" ], | |
"formatted_address": "日本", | |
"address_components": [ { | |
"long_name": "日本", | |
"short_name": "JP", | |
"types": [ "country", "political" ] | |
} ], | |
"geometry": { | |
"location": { | |
"lat": 36.2048240, | |
"lng": 138.2529240 | |
}, | |
"location_type": "APPROXIMATE", | |
"viewport": { | |
"southwest": { | |
"lat": 23.9207972, | |
"lng": 122.9100000 | |
}, | |
"northeast": { | |
"lat": 45.6691047, | |
"lng": 149.1209620 | |
} | |
}, | |
"bounds": { | |
"southwest": { | |
"lat": 23.9207972, | |
"lng": 122.9100000 | |
}, | |
"northeast": { | |
"lat": 45.6691047, | |
"lng": 149.1209620 | |
} | |
} | |
} | |
} ] | |
} | |
JSON | |
@@http_geocoder_stub = HttpGeocoderStub.new(@@http_geocoder_response) | |
@@route_json = <<JSON | |
{ | |
"types": [ "route" ], | |
"formatted_address": "日本, 〒040-0001 道道571号線", | |
"address_components": [ { | |
"long_name": "道道571号線", | |
"short_name": "道道571号線", | |
"types": [ "route" ] | |
}, { | |
"long_name": "日本", | |
"short_name": "JP", | |
"types": [ "country", "political" ] | |
}, { | |
"long_name": "040-0001", | |
"short_name": "040-0001", | |
"types": [ "postal_code" ] | |
} ], | |
"geometry": { | |
"location": { | |
"lat": 41.7933121, | |
"lng": 140.7526284 | |
}, | |
"location_type": "APPROXIMATE", | |
"viewport": { | |
"southwest": { | |
"lat": 41.7901644, | |
"lng": 140.7494808 | |
}, | |
"northeast": { | |
"lat": 41.7964597, | |
"lng": 140.7557761 | |
} | |
}, | |
"bounds": { | |
"southwest": { | |
"lat": 41.7930668, | |
"lng": 140.7524522 | |
}, | |
"northeast": { | |
"lat": 41.7935573, | |
"lng": 140.7528047 | |
} | |
} | |
} | |
} | |
JSON | |
@@route = JSON.parse(@@route_json) | |
end | |
class ReverseGeocoderTest < Test::Unit::TestCase | |
include TestData | |
def setup | |
@obj = ReverseGeocoder.new(@@http_geocoder_stub) | |
@lat = 41.9765913065423 | |
@lng = 140.14085292468 | |
end | |
# def teardown | |
# end | |
def test_get | |
res = @obj.get(@lat, @lng) | |
assert(res.ok?, "status is OK.") | |
assert_match(/東京/, res.results.first.formatted_address) | |
assert_equal(8, res.results.size, "results.size") | |
assert_equal(7, res.results.reject {|x| x.postal_code? }.size, "number of results excluding postal codes") | |
assert(!res.results[0].postal_code?, "first item is not postal code.") | |
end | |
def test_get_finest_address | |
res = @obj.get(42.8305128097694,141.646680213468) | |
finest = res.results.finest | |
puts | |
pp finest | |
assert_match(/東京/, finest.formatted_address) | |
end | |
def test_proxies | |
# ReverseGeocoder.new(HttpGeocoder.new(DefaultProxies.proxy1)) | |
end | |
def test_result | |
r = Result.new(@@route) | |
assert(r.route?, "result is route.") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment