1 | from __future__ import absolute_import |
||
2 | from __future__ import division |
||
3 | from builtins import range |
||
4 | from nose.plugins.skip import SkipTest |
||
5 | from past.utils import old_div |
||
6 | # Project imports |
||
7 | import mock |
||
8 | import os |
||
9 | import random |
||
10 | import re |
||
11 | import sys |
||
12 | from mock import patch |
||
13 | from tempfile import gettempdir |
||
14 | |||
15 | try: |
||
16 | from StringIO import StringIO |
||
17 | except ImportError: |
||
18 | from io import StringIO |
||
19 | |||
20 | sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) |
||
21 | |||
22 | from . import helper |
||
23 | from elodie import geolocation |
||
24 | |||
25 | os.environ['TZ'] = 'GMT' |
||
26 | |||
27 | |||
28 | def test_decimal_to_dms(): |
||
29 | |||
30 | for x in range(0, 1000): |
||
31 | target_decimal_value = random.uniform(0.0, 180.0) |
||
32 | if(x % 2 == 1): |
||
33 | target_decimal_value = target_decimal_value * -1 |
||
34 | |||
35 | dms = geolocation.decimal_to_dms(target_decimal_value) |
||
36 | |||
37 | check_value = (dms[0] + dms[1] / 60 + dms[2] / 3600) * dms[3] |
||
38 | |||
39 | target_decimal_value = round(target_decimal_value, 8) |
||
40 | check_value = round(check_value, 8) |
||
41 | |||
42 | assert target_decimal_value == check_value, '%s does not match %s' % (check_value, target_decimal_value) |
||
43 | |||
44 | def test_dms_to_decimal_positive_sign(): |
||
45 | decimal = geolocation.dms_to_decimal(10, 20, 100, 'NE') |
||
46 | assert helper.isclose(decimal, 10.3611111111) |
||
47 | |||
48 | decimal = geolocation.dms_to_decimal(10, 20, 100, 'ne') |
||
49 | assert helper.isclose(decimal, 10.3611111111) |
||
50 | |||
51 | def test_dms_to_decimal_negative_sign(): |
||
52 | decimal = geolocation.dms_to_decimal(10, 20, 100, 'SW') |
||
53 | assert helper.isclose(decimal, -10.3611111111) |
||
54 | |||
55 | decimal = geolocation.dms_to_decimal(10, 20, 100, 'sw') |
||
56 | assert helper.isclose(decimal, -10.3611111111) |
||
57 | |||
58 | View Code Duplication | def test_dms_string_latitude(): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
59 | |||
60 | for x in range(0, 5): |
||
61 | target_decimal_value = random.uniform(0.0, 180.0) |
||
62 | if(x % 2 == 1): |
||
63 | target_decimal_value = target_decimal_value * -1 |
||
64 | |||
65 | dms = geolocation.decimal_to_dms(target_decimal_value) |
||
66 | dms_string = geolocation.dms_string(target_decimal_value, 'latitude') |
||
67 | |||
68 | check_value = 'N' if target_decimal_value >= 0 else 'S' |
||
69 | |||
70 | assert check_value in dms_string, '%s not in %s' % (check_value, dms_string) |
||
71 | assert str(dms[0]) in dms_string, '%s not in %s' % (dms[0], dms_string) |
||
72 | |||
73 | View Code Duplication | def test_dms_string_longitude(): |
|
0 ignored issues
–
show
|
|||
74 | |||
75 | for x in range(0, 5): |
||
76 | target_decimal_value = random.uniform(0.0, 180.0) |
||
77 | if(x % 2 == 1): |
||
78 | target_decimal_value = target_decimal_value * -1 |
||
79 | |||
80 | dms = geolocation.decimal_to_dms(target_decimal_value) |
||
81 | dms_string = geolocation.dms_string(target_decimal_value, 'longitude') |
||
82 | |||
83 | check_value = 'E' if target_decimal_value >= 0 else 'W' |
||
84 | |||
85 | assert check_value in dms_string, '%s not in %s' % (check_value, dms_string) |
||
86 | assert str(dms[0]) in dms_string, '%s not in %s' % (dms[0], dms_string) |
||
87 | |||
88 | def test_reverse_lookup_with_valid_key(): |
||
89 | res = geolocation.lookup(lat=37.368, lon=-122.03) |
||
90 | assert res['address']['city'] == 'Sunnyvale', res |
||
91 | |||
92 | def test_reverse_lookup_with_invalid_lat_lon(): |
||
93 | res = geolocation.lookup(lat=999, lon=999) |
||
94 | assert res is None, res |
||
95 | |||
96 | @mock.patch('elodie.geolocation.__KEY__', 'invalid_key') |
||
97 | def test_reverse_lookup_with_invalid_key(): |
||
98 | res = geolocation.lookup(lat=37.368, lon=-122.03) |
||
99 | assert res is None, res |
||
100 | |||
101 | def test_lookup_with_valid_key(): |
||
102 | res = geolocation.lookup(location='Sunnyvale, CA') |
||
103 | latLng = res['results'][0]['locations'][0]['latLng'] |
||
104 | assert latLng['lat'] == 37.37187, latLng |
||
105 | assert latLng['lng'] == -122.03749, latLng |
||
106 | |||
107 | def test_lookup_with_invalid_location(): |
||
108 | res = geolocation.lookup(location='foobar dne') |
||
109 | assert res is None, res |
||
110 | |||
111 | @mock.patch('elodie.geolocation.__PREFER_ENGLISH_NAMES__', True) |
||
112 | def test_lookup_with_prefer_english_names_true(): |
||
113 | raise SkipTest("gh-425 MapQuest API no longer supports prefer_english_names.") |
||
114 | res = geolocation.lookup(lat=55.66333, lon=37.61583) |
||
115 | assert res['address']['city'] == 'Nagorny District', res |
||
116 | |||
117 | @mock.patch('elodie.geolocation.__PREFER_ENGLISH_NAMES__', False) |
||
118 | def test_lookup_with_prefer_english_names_false(): |
||
119 | raise SkipTest("gh-425 MapQuest API no longer supports prefer_english_names.") |
||
120 | res = geolocation.lookup(lat=55.66333, lon=37.61583) |
||
121 | assert res['address']['city'] == u'\u041d\u0430\u0433\u043e\u0440\u043d\u044b\u0439 \u0440\u0430\u0439\u043e\u043d', res |
||
122 | |||
123 | @mock.patch('elodie.constants.debug', True) |
||
124 | def test_lookup_debug_mapquest_url(): |
||
125 | out = StringIO() |
||
126 | sys.stdout = out |
||
127 | res = geolocation.lookup(location='Sunnyvale, CA') |
||
128 | output = out.getvalue() |
||
129 | assert 'MapQuest url:' in output, output |
||
130 | |||
131 | View Code Duplication | @mock.patch('elodie.constants.location_db', '%s/location.json-cached' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
132 | def test_place_name_deprecated_string_cached(): |
||
133 | # See gh-160 for backwards compatability needed when a string is stored instead of a dict |
||
134 | helper.reset_dbs() |
||
135 | with open('%s/location.json-cached' % gettempdir(), 'w') as f: |
||
136 | f.write(""" |
||
137 | [{"lat": 37.3667027222222, "long": -122.033383611111, "name": "OLDVALUE"}] |
||
138 | """ |
||
139 | ) |
||
140 | place_name = geolocation.place_name(37.3667027222222, -122.033383611111) |
||
141 | helper.restore_dbs() |
||
142 | |||
143 | assert place_name['city'] == 'Sunnyvale', place_name |
||
144 | |||
145 | View Code Duplication | @mock.patch('elodie.constants.location_db', '%s/location.json-cached' % gettempdir()) |
|
0 ignored issues
–
show
|
|||
146 | def test_place_name_cached(): |
||
147 | helper.reset_dbs() |
||
148 | with open('%s/location.json-cached' % gettempdir(), 'w') as f: |
||
149 | f.write(""" |
||
150 | [{"lat": 37.3667027222222, "long": -122.033383611111, "name": {"city": "UNITTEST"}}] |
||
151 | """ |
||
152 | ) |
||
153 | place_name = geolocation.place_name(37.3667027222222, -122.033383611111) |
||
154 | helper.restore_dbs() |
||
155 | |||
156 | assert place_name['city'] == 'UNITTEST', place_name |
||
157 | |||
158 | def test_place_name_no_default(): |
||
159 | # See gh-160 for backwards compatability needed when a string is stored instead of a dict |
||
160 | helper.reset_dbs() |
||
161 | place_name = geolocation.place_name(123456.000, 123456.000) |
||
162 | helper.restore_dbs() |
||
163 | |||
164 | assert place_name['default'] == 'Unknown Location', place_name |
||
165 | |||
166 | @mock.patch('elodie.geolocation.__KEY__', 'invalid_key') |
||
167 | def test_lookup_with_invalid_key(): |
||
168 | res = geolocation.lookup(location='Sunnyvale, CA') |
||
169 | assert res is None, res |
||
170 | |||
171 | @mock.patch('elodie.geolocation.__KEY__', '') |
||
172 | def test_lookup_with_no_key(): |
||
173 | res = geolocation.lookup(location='Sunnyvale, CA') |
||
174 | assert res is None, res |
||
175 | |||
176 | def test_parse_result_with_error(): |
||
177 | res = geolocation.parse_result({'error': 'foo'}) |
||
178 | assert res is None, res |
||
179 | |||
180 | def test_parse_result_with_city(): |
||
181 | # https://www.mapquestapi.com/geocoding/v1/reverse?location=37.37187,-122.03749&key=key_goes_here&format=json |
||
182 | results = {"info":{"statuscode":0,"copyright":{"text":"© 2022 MapQuest, Inc.","imageUrl":"http://api.mqcdn.com/res/mqlogo.gif","imageAltText":"© 2022 MapQuest, Inc."},"messages":[]},"options":{"maxResults":1,"ignoreLatLngInput":False},"results":[{"providedLocation":{"latLng":{"lat":37.368,"lng":-122.03}},"locations":[{"street":"312 Old San Francisco Rd","adminArea6":"Heritage District","adminArea6Type":"Neighborhood","adminArea5":"Sunnyvale","adminArea5Type":"City","adminArea4":"Santa Clara","adminArea4Type":"County","adminArea3":"CA","adminArea3Type":"State","adminArea1":"US","adminArea1Type":"Country","postalCode":"94086","geocodeQualityCode":"P1AAA","geocodeQuality":"POINT","dragPoint":False,"sideOfStreet":"R","linkId":"0","unknownInput":"","type":"s","latLng":{"lat":37.36798,"lng":-122.03018},"displayLatLng":{"lat":37.36785,"lng":-122.03021},"mapUrl":""}]}]} |
||
183 | |||
184 | res = geolocation.parse_result(results) |
||
185 | assert res == results, res |
||
186 | |||
187 | def test_parse_result_with_lat_lon(): |
||
188 | # https://www.mapquestapi.com/geocoding/v1/address?format=json&key=key_goes_here&locale=en_US&location=Sunnyvale,CA |
||
189 | results = {"info":{"statuscode":0,"copyright":{"text":"© 2022 MapQuest, Inc.","imageUrl":"http://api.mqcdn.com/res/mqlogo.gif","imageAltText":"© 2022 MapQuest, Inc."},"messages":[]},"options":{"maxResults":-1,"ignoreLatLngInput":False},"results":[{"providedLocation":{"location":"Sunnyvale,CA"},"locations":[{"street":"","adminArea6":"","adminArea6Type":"Neighborhood","adminArea5":"Sunnyvale","adminArea5Type":"City","adminArea4":"Santa Clara","adminArea4Type":"County","adminArea3":"CA","adminArea3Type":"State","adminArea1":"US","adminArea1Type":"Country","postalCode":"","geocodeQualityCode":"A5XAX","geocodeQuality":"CITY","dragPoint":False,"sideOfStreet":"N","linkId":"0","unknownInput":"","type":"s","latLng":{"lat":37.37187,"lng":-122.03749},"displayLatLng":{"lat":37.37187,"lng":-122.03749},"mapUrl":""}]}]} |
||
190 | |||
191 | res = geolocation.parse_result(results) |
||
192 | assert res == results, res |
||
193 | |||
194 | def test_parse_result_with_unknown_lat_lon(): |
||
195 | # https://www.mapquestapi.com/geocoding/v1/address?format=json&key=key_goes_here&locale=en_US&location=ABCDEFGHIJKLMNOPQRSTUVWXYZ |
||
196 | results = {"info":{"statuscode":0,"copyright":{"text":"© 2022 MapQuest, Inc.","imageUrl":"http://api.mqcdn.com/res/mqlogo.gif","imageAltText":"© 2022 MapQuest, Inc."},"messages":[]},"options":{"maxResults":-1,"ignoreLatLngInput":False},"results":[{"providedLocation":{"location":"ABCDEFGHIJKLMNOPQRSTUVWXYZ"},"locations":[{"street":"","adminArea6":"","adminArea5":"","adminArea4":"","adminArea3":"","adminArea1":"US","postalCode":"","geocodeQualityCode":"A1XAX","geocodeQuality":"COUNTRY","dragPoint":False,"sideOfStreet":"N","linkId":"0","unknownInput":"","type":"s","latLng":{"lat":38.89037,"lng":-77.03196},"displayLatLng":{"lat":38.89037,"lng":-77.03196},"mapUrl":""}]}]} |
||
197 | |||
198 | res = geolocation.parse_result(results) |
||
199 | assert res is None, res |
||
200 |