1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#
# Copyright 2014 Google Inc. All rights reserved.
#
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
#
"""Tests for the convert module."""
import datetime
import unittest
import pytest
from googlemaps import convert
class ConvertTest(unittest.TestCase):
def test_latlng(self):
expected = "1,2"
ll = {"lat": 1, "lng": 2}
self.assertEqual(expected, convert.latlng(ll))
ll = [1, 2]
self.assertEqual(expected, convert.latlng(ll))
ll = (1, 2)
self.assertEqual(expected, convert.latlng(ll))
self.assertEqual(expected, convert.latlng(expected))
with self.assertRaises(TypeError):
convert.latlng(1)
def test_location_list(self):
expected = "1,2|1,2"
ll = [{"lat": 1, "lng": 2}, {"lat": 1, "lng": 2}]
self.assertEqual(expected, convert.location_list(ll))
ll = [[1, 2], [1, 2]]
self.assertEqual(expected, convert.location_list(ll))
ll = [(1, 2), [1, 2]]
self.assertEqual(expected, convert.location_list(ll))
self.assertEqual(expected, convert.location_list(expected))
with self.assertRaises(TypeError):
convert.latlng(1)
def test_join_list(self):
self.assertEqual("asdf", convert.join_list("|", "asdf"))
self.assertEqual("1,2,A", convert.join_list(",", ["1", "2", "A"]))
self.assertEqual("", convert.join_list(",", []))
self.assertEqual("a,B", convert.join_list(",", ("a", "B")))
def test_as_list(self):
self.assertEqual([1], convert.as_list(1))
self.assertEqual([1, 2, 3], convert.as_list([1, 2, 3]))
self.assertEqual(["string"], convert.as_list("string"))
self.assertEqual((1, 2), convert.as_list((1, 2)))
a_dict = {"a": 1}
self.assertEqual([a_dict], convert.as_list(a_dict))
def test_time(self):
self.assertEqual("1409810596", convert.time(1409810596))
dt = datetime.datetime.fromtimestamp(1409810596)
self.assertEqual("1409810596", convert.time(dt))
def test_components(self):
c = {"country": "US"}
self.assertEqual("country:US", convert.components(c))
c = {"country": "US", "foo": 1}
self.assertEqual("country:US|foo:1", convert.components(c))
c = {"country": ["US", "AU"], "foo": 1}
self.assertEqual("country:AU|country:US|foo:1", convert.components(c))
with self.assertRaises(TypeError):
convert.components("test")
with self.assertRaises(TypeError):
convert.components(1)
with self.assertRaises(TypeError):
convert.components(("c", "b"))
def test_bounds(self):
ne = {"lat": 1, "lng": 2}
sw = (3, 4)
b = {"northeast": ne, "southwest": sw}
self.assertEqual("3,4|1,2", convert.bounds(b))
with self.assertRaises(TypeError):
convert.bounds("test")
def test_size(self):
self.assertEqual("1x1", convert.size(1))
self.assertEqual("2x3", convert.size((2, 3)))
with self.assertRaises(TypeError):
convert.size("test")
def test_polyline_decode(self):
syd_mel_route = (
"rvumEis{y[`NsfA~tAbF`bEj^h{@{KlfA~eA~`AbmEghAt~D|e@j"
"lRpO~yH_\\v}LjbBh~FdvCxu@`nCplDbcBf_B|wBhIfhCnqEb~D~"
"jCn_EngApdEtoBbfClf@t_CzcCpoEr_Gz_DxmAphDjjBxqCviEf}"
"B|pEvsEzbE~qGfpExjBlqCx}BvmLb`FbrQdpEvkAbjDllD|uDldD"
"j`Ef|AzcEx_Gtm@vuI~xArwD`dArlFnhEzmHjtC~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h"
"C~w@hnB{e@yF}`D`_Ayx@~vGqn@l}CafC"
)
points = convert.decode_polyline(syd_mel_route)
self.assertAlmostEqual(-33.86746, points[0]["lat"])
self.assertAlmostEqual(151.207090, points[0]["lng"])
self.assertAlmostEqual(-37.814130, points[-1]["lat"])
self.assertAlmostEqual(144.963180, points[-1]["lng"])
def test_polyline_round_trip(self):
test_polyline = (
"gcneIpgxzRcDnBoBlEHzKjBbHlG`@`IkDxIi"
"KhKoMaLwTwHeIqHuAyGXeB~Ew@fFjAtIzExF"
)
points = convert.decode_polyline(test_polyline)
actual_polyline = convert.encode_polyline(points)
self.assertEqual(test_polyline, actual_polyline)
@pytest.mark.parametrize(
"value, expected",
[
(40, "40"),
(40.0, "40"),
(40.1, "40.1"),
(40.00000001, "40.00000001"),
(40.000000009, "40.00000001"),
(40.000000001, "40"),
],
)
def test_format_float(value, expected):
assert convert.format_float(value) == expected
|