Passed
Pull Request — master (#357)
by Thomas
02:02
created

test_lookup_with_prefer_language_danish()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
This code seems to be duplicated in your project.
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
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
@mock.patch('elodie.geolocation.__PREFER_LANGUAGE__', 'da-DK')
126
def test_lookup_with_prefer_language_danish():
127
    res = geolocation.lookup(lat=55.6775055555556, lon=12.5686222222222)
128
    assert res['address']['city'] == 'K\u00f8benhavn', res
129
130 View Code Duplication
@mock.patch('elodie.constants.location_db', '%s/location.json-cached' % gettempdir())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
131
def test_place_name_deprecated_string_cached():
132
    # See gh-160 for backwards compatability needed when a string is stored instead of a dict
133
    helper.reset_dbs()
134
    with open('%s/location.json-cached' % gettempdir(), 'w') as f:
135
        f.write("""
136
[{"lat": 37.3667027222222, "long": -122.033383611111, "name": "OLDVALUE"}]
137
"""
138
    )
139
    place_name = geolocation.place_name(37.3667027222222, -122.033383611111)
140
    helper.restore_dbs()
141
142
    assert place_name['city'] == 'Sunnyvale', place_name
143
144 View Code Duplication
@mock.patch('elodie.constants.location_db', '%s/location.json-cached' % gettempdir())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
145
def test_place_name_cached():
146
    helper.reset_dbs()
147
    with open('%s/location.json-cached' % gettempdir(), 'w') as f:
148
        f.write("""
149
[{"lat": 37.3667027222222, "long": -122.033383611111, "name": {"city": "UNITTEST"}}]
150
"""
151
    )
152
    place_name = geolocation.place_name(37.3667027222222, -122.033383611111)
153
    helper.restore_dbs()
154
155
    assert place_name['city'] == 'UNITTEST', place_name
156
157
def test_place_name_no_default():
158
    # See gh-160 for backwards compatability needed when a string is stored instead of a dict
159
    helper.reset_dbs()
160
    place_name = geolocation.place_name(123456.000, 123456.000)
161
    helper.restore_dbs()
162
163
    assert place_name['default'] == 'Unknown Location', place_name
164
165
@mock.patch('elodie.geolocation.__KEY__', 'invalid_key')
166
def test_lookup_with_invalid_key():
167
    res = geolocation.lookup(location='Sunnyvale, CA')
168
    assert res is None, res
169
170
@mock.patch('elodie.geolocation.__KEY__', '')
171
def test_lookup_with_no_key():
172
    res = geolocation.lookup(location='Sunnyvale, CA')
173
    assert res is None, res
174
175
def test_parse_result_with_error():
176
    res = geolocation.parse_result({'error': 'foo'})
177
    assert res is None, res
178
179
def test_parse_result_with_city():
180
    # http://open.mapquestapi.com/nominatim/v1/reverse.php?lat=37.368&lon=-122.03&key=key_goes_here&format=json
181
    results = {
182
        "place_id": "60197412",
183
        "osm_type": "way",
184
        "osm_id": "30907961",
185
        "lat": "37.36746105",
186
        "lon": "-122.030237558742",
187
        "display_name": "111, East El Camino Real, Sunnyvale, Santa Clara County, California, 94087, United States of America",
188
        "address": {
189
            "house_number": "111",
190
            "road": "East El Camino Real",
191
            "city": "Sunnyvale",
192
            "county": "Santa Clara County",
193
            "state": "California",
194
            "postcode": "94087",
195
            "country": "United States of America",
196
            "country_code": "us"
197
        }
198
    }
199
200
    res = geolocation.parse_result(results)
201
    assert res == results, res
202
203
def test_parse_result_with_lat_lon():
204
    # http://open.mapquestapi.com/geocoding/v1/address?location=abcdefghijklmnopqrstuvwxyz&key=key_goes_here&format=json
205
    results = {
206
        "results": [
207
            {
208
               "locations": [
209
                    {
210
                        "latLng": {
211
                            "lat": 123.00,
212
                            "lng": -142.99
213
                        }
214
                    }
215
                ]
216
            }
217
        ]
218
    }
219
220
    res = geolocation.parse_result(results)
221
    assert res == results, res
222
223
def test_parse_result_with_unknown_lat_lon():
224
    # http://open.mapquestapi.com/geocoding/v1/address?location=abcdefghijklmnopqrstuvwxyz&key=key_goes_here&format=json
225
    results = {
226
        "results": [
227
            {
228
               "locations": [
229
                    {
230
                        "latLng": {
231
                            "lat": 39.78373,
232
                            "lng": -100.445882
233
                        }
234
                    }
235
                ]
236
            }
237
        ]
238
    }
239
240
    res = geolocation.parse_result(results)
241
    assert res is None, res
242