Enable duplicate code detection for Python code
1 | from __future__ import absolute_import |
||
2 | from __future__ import division |
||
3 | from builtins import range |
||
4 | from past.utils import old_div |
||
5 | # Project imports |
||
6 | import mock |
||
7 | import os |
||
8 | import random |
||
9 | import re |
||
10 | import sys |
||
11 | from mock import patch |
||
12 | from tempfile import gettempdir |
||
13 | |||
14 | sys.path.insert(0, os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))) |
||
15 | |||
16 | from . import helper |
||
17 | from elodie import geolocation |
||
18 | |||
19 | os.environ['TZ'] = 'GMT' |
||
20 | |||
21 | |||
22 | def test_decimal_to_dms(): |
||
23 | |||
24 | for x in range(0, 1000): |
||
25 | target_decimal_value = random.uniform(0.0, 180.0) |
||
26 | if(x % 2 == 1): |
||
27 | target_decimal_value = target_decimal_value * -1 |
||
28 | |||
29 | dms = geolocation.decimal_to_dms(target_decimal_value) |
||
30 | |||
31 | check_value = (dms[0] + dms[1] / 60 + dms[2] / 3600) * dms[3] |
||
32 | |||
33 | target_decimal_value = round(target_decimal_value, 8) |
||
34 | check_value = round(check_value, 8) |
||
35 | |||
36 | assert target_decimal_value == check_value, '%s does not match %s' % (check_value, target_decimal_value) |
||
37 | |||
38 | def test_dms_to_decimal_positive_sign(): |
||
39 | decimal = geolocation.dms_to_decimal(10, 20, 100, 'NE') |
||
40 | assert helper.isclose(decimal, 10.3611111111) |
||
41 | |||
42 | decimal = geolocation.dms_to_decimal(10, 20, 100, 'ne') |
||
43 | assert helper.isclose(decimal, 10.3611111111) |
||
44 | |||
45 | def test_dms_to_decimal_negative_sign(): |
||
46 | decimal = geolocation.dms_to_decimal(10, 20, 100, 'SW') |
||
47 | assert helper.isclose(decimal, -10.3611111111) |
||
48 | |||
49 | decimal = geolocation.dms_to_decimal(10, 20, 100, 'sw') |
||
50 | assert helper.isclose(decimal, -10.3611111111) |
||
51 | |||
52 | View Code Duplication | def test_dms_string_latitude(): |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
53 | |||
54 | for x in range(0, 5): |
||
55 | target_decimal_value = random.uniform(0.0, 180.0) |
||
56 | if(x % 2 == 1): |
||
57 | target_decimal_value = target_decimal_value * -1 |
||
58 | |||
59 | dms = geolocation.decimal_to_dms(target_decimal_value) |
||
60 | dms_string = geolocation.dms_string(target_decimal_value, 'latitude') |
||
61 | |||
62 | check_value = 'N' if target_decimal_value >= 0 else 'S' |
||
63 | |||
64 | assert check_value in dms_string, '%s not in %s' % (check_value, dms_string) |
||
65 | assert str(dms[0]) in dms_string, '%s not in %s' % (dms[0], dms_string) |
||
66 | |||
67 | View Code Duplication | def test_dms_string_longitude(): |
|
0 ignored issues
–
show
|
|||
68 | |||
69 | for x in range(0, 5): |
||
70 | target_decimal_value = random.uniform(0.0, 180.0) |
||
71 | if(x % 2 == 1): |
||
72 | target_decimal_value = target_decimal_value * -1 |
||
73 | |||
74 | dms = geolocation.decimal_to_dms(target_decimal_value) |
||
75 | dms_string = geolocation.dms_string(target_decimal_value, 'longitude') |
||
76 | |||
77 | check_value = 'E' if target_decimal_value >= 0 else 'W' |
||
78 | |||
79 | assert check_value in dms_string, '%s not in %s' % (check_value, dms_string) |
||
80 | assert str(dms[0]) in dms_string, '%s not in %s' % (dms[0], dms_string) |
||
81 | |||
82 | def test_reverse_lookup_with_valid_key(): |
||
83 | res = geolocation.lookup(lat=37.368, lon=-122.03) |
||
84 | assert res['address']['city'] == 'Sunnyvale', res |
||
85 | |||
86 | def test_reverse_lookup_with_invalid_lat_lon(): |
||
87 | res = geolocation.lookup(lat=999, lon=999) |
||
88 | assert res is None, res |
||
89 | |||
90 | @mock.patch('elodie.geolocation.__KEY__', 'invalid_key') |
||
91 | def test_reverse_lookup_with_invalid_key(): |
||
92 | res = geolocation.lookup(lat=37.368, lon=-122.03) |
||
93 | assert res is None, res |
||
94 | |||
95 | def test_lookup_with_valid_key(): |
||
96 | res = geolocation.lookup(location='Sunnyvale, CA') |
||
97 | latLng = res['results'][0]['locations'][0]['latLng'] |
||
98 | assert latLng['lat'] == 37.36883, latLng |
||
99 | assert latLng['lng'] == -122.03635, latLng |
||
100 | |||
101 | def test_lookup_with_invalid_location(): |
||
102 | res = geolocation.lookup(location='foobar dne') |
||
103 | assert res is None, res |
||
104 | |||
105 | def test_lookup_with_invalid_location(): |
||
106 | res = geolocation.lookup(location='foobar dne') |
||
107 | assert res is None, res |
||
108 | |||
109 | def test_lookup_with_valid_key(): |
||
110 | res = geolocation.lookup(location='Sunnyvale, CA') |
||
111 | latLng = res['results'][0]['locations'][0]['latLng'] |
||
112 | assert latLng['lat'] == 37.36883, latLng |
||
113 | assert latLng['lng'] == -122.03635, latLng |
||
114 | |||
115 | @mock.patch('elodie.geolocation.__PREFER_ENGLISH_NAMES__', True) |
||
116 | def test_lookup_with_prefer_english_names_true(): |
||
117 | res = geolocation.lookup(lat=55.66333, lon=37.61583) |
||
118 | assert res['address']['city'] == 'Nagorny District', res |
||
119 | |||
120 | @mock.patch('elodie.geolocation.__PREFER_ENGLISH_NAMES__', False) |
||
121 | def test_lookup_with_prefer_english_names_false(): |
||
122 | res = geolocation.lookup(lat=55.66333, lon=37.61583) |
||
123 | assert res['address']['city'] == u'\u041d\u0430\u0433\u043e\u0440\u043d\u044b\u0439 \u0440\u0430\u0439\u043e\u043d', res |
||
124 | |||
125 | View Code Duplication | @mock.patch('elodie.constants.location_db', '%s/location.json-cached' % gettempdir()) |
|
126 | def test_place_name_deprecated_string_cached(): |
||
127 | # See gh-160 for backwards compatability needed when a string is stored instead of a dict |
||
128 | helper.reset_dbs() |
||
129 | with open('%s/location.json-cached' % gettempdir(), 'w') as f: |
||
130 | f.write(""" |
||
131 | [{"lat": 37.3667027222222, "long": -122.033383611111, "name": "OLDVALUE"}] |
||
132 | """ |
||
133 | ) |
||
134 | place_name = geolocation.place_name(37.3667027222222, -122.033383611111) |
||
135 | helper.restore_dbs() |
||
136 | |||
137 | assert place_name['city'] == 'Sunnyvale', place_name |
||
138 | |||
139 | View Code Duplication | @mock.patch('elodie.constants.location_db', '%s/location.json-cached' % gettempdir()) |
|
140 | def test_place_name_cached(): |
||
141 | helper.reset_dbs() |
||
142 | with open('%s/location.json-cached' % gettempdir(), 'w') as f: |
||
143 | f.write(""" |
||
144 | [{"lat": 37.3667027222222, "long": -122.033383611111, "name": {"city": "UNITTEST"}}] |
||
145 | """ |
||
146 | ) |
||
147 | place_name = geolocation.place_name(37.3667027222222, -122.033383611111) |
||
148 | helper.restore_dbs() |
||
149 | |||
150 | assert place_name['city'] == 'UNITTEST', place_name |
||
151 | |||
152 | def test_place_name_no_default(): |
||
153 | # See gh-160 for backwards compatability needed when a string is stored instead of a dict |
||
154 | helper.reset_dbs() |
||
155 | place_name = geolocation.place_name(123456.000, 123456.000) |
||
156 | helper.restore_dbs() |
||
157 | |||
158 | assert place_name['default'] == 'Unknown Location', place_name |
||
159 | |||
160 | @mock.patch('elodie.geolocation.__KEY__', 'invalid_key') |
||
161 | def test_lookup_with_invalid_key(): |
||
162 | res = geolocation.lookup(location='Sunnyvale, CA') |
||
163 | assert res is None, res |
||
164 | |||
165 | @mock.patch('elodie.geolocation.__KEY__', '') |
||
166 | def test_lookup_with_no_key(): |
||
167 | res = geolocation.lookup(location='Sunnyvale, CA') |
||
168 | assert res is None, res |
||
169 | |||
170 | def test_parse_result_with_error(): |
||
171 | res = geolocation.parse_result({'error': 'foo'}) |
||
172 | assert res is None, res |
||
173 | |||
174 | def test_parse_result_with_city(): |
||
175 | # http://open.mapquestapi.com/nominatim/v1/reverse.php?lat=37.368&lon=-122.03&key=key_goes_here&format=json |
||
176 | results = { |
||
177 | "place_id": "60197412", |
||
178 | "osm_type": "way", |
||
179 | "osm_id": "30907961", |
||
180 | "lat": "37.36746105", |
||
181 | "lon": "-122.030237558742", |
||
182 | "display_name": "111, East El Camino Real, Sunnyvale, Santa Clara County, California, 94087, United States of America", |
||
183 | "address": { |
||
184 | "house_number": "111", |
||
185 | "road": "East El Camino Real", |
||
186 | "city": "Sunnyvale", |
||
187 | "county": "Santa Clara County", |
||
188 | "state": "California", |
||
189 | "postcode": "94087", |
||
190 | "country": "United States of America", |
||
191 | "country_code": "us" |
||
192 | } |
||
193 | } |
||
194 | |||
195 | res = geolocation.parse_result(results) |
||
196 | assert res == results, res |
||
197 | |||
198 | def test_parse_result_with_lat_lon(): |
||
199 | # http://open.mapquestapi.com/geocoding/v1/address?location=abcdefghijklmnopqrstuvwxyz&key=key_goes_here&format=json |
||
200 | results = { |
||
201 | "results": [ |
||
202 | { |
||
203 | "locations": [ |
||
204 | { |
||
205 | "latLng": { |
||
206 | "lat": 123.00, |
||
207 | "lng": -142.99 |
||
208 | } |
||
209 | } |
||
210 | ] |
||
211 | } |
||
212 | ] |
||
213 | } |
||
214 | |||
215 | res = geolocation.parse_result(results) |
||
216 | assert res == results, res |
||
217 | |||
218 | def test_parse_result_with_unknown_lat_lon(): |
||
219 | # http://open.mapquestapi.com/geocoding/v1/address?location=abcdefghijklmnopqrstuvwxyz&key=key_goes_here&format=json |
||
220 | results = { |
||
221 | "results": [ |
||
222 | { |
||
223 | "locations": [ |
||
224 | { |
||
225 | "latLng": { |
||
226 | "lat": 39.78373, |
||
227 | "lng": -100.445882 |
||
228 | } |
||
229 | } |
||
230 | ] |
||
231 | } |
||
232 | ] |
||
233 | } |
||
234 | |||
235 | res = geolocation.parse_result(results) |
||
236 | assert res is None, res |
||
237 |