Full coverage on coordinates

This commit is contained in:
Lucidiot 2019-01-30 20:09:30 +01:00
parent 0e4c24046b
commit 736ec62d3b
No known key found for this signature in database
GPG Key ID: AE3F7205692FA205
2 changed files with 28 additions and 3 deletions

View File

@ -1,5 +1,5 @@
from unittest import TestCase
from math import trunc, floor, ceil
from math import trunc, floor, ceil, radians
from urbantz.utils import Coordinates
@ -59,5 +59,30 @@ class TestCoordinates(TestCase):
def test_ceil(self):
self.assertTupleEqual(tuple(ceil(self.coords)), (13, -13))
def test_add(self):
other = Coordinates(lat=7.43, lng=-6.51)
self.assertEqual(self.coords + other, Coordinates(lat=20, lng=-20))
with self.assertRaises(TypeError):
self.coords + 4
def test_sub(self):
other = Coordinates(lat=2.57, lng=-3.49)
self.assertEqual(self.coords - other, Coordinates(lat=10, lng=-10))
with self.assertRaises(TypeError):
self.coords - "lol"
def test_toJSON(self):
self.assertListEqual(self.coords.toJSON(), [-13.49, 12.57])
def test_fromJSON(self):
self.assertEqual(Coordinates.fromJSON([-13.49, 12.57]), self.coords)
def test_to_radians(self):
self.assertTupleEqual(self.coords.to_radians(), (radians(12.57), radians(-13.49)))
def test_distance(self):
other = Coordinates(lat=2.57, lng=-3.49)
self.assertEqual(self.coords.distance(other), other.distance(self.coords))
self.assertEqual(self.coords.distance(other), 1564640.3229974532)
with self.assertRaises(NotImplementedError):
self.coords.distance("lol")

View File

@ -42,7 +42,7 @@ class Coordinates(object):
tuple(self) == tuple(other)
def __add__(self, other):
if not hasattr(other, 'lat') or hasattr(other, 'lng'):
if not hasattr(other, 'lat') or not hasattr(other, 'lng'):
return NotImplemented
return self.__class__(
lat=self.lat + other.lat,
@ -50,7 +50,7 @@ class Coordinates(object):
)
def __sub__(self, other):
if not hasattr(other, 'lat') or hasattr(other, 'lng'):
if not hasattr(other, 'lat') or not hasattr(other, 'lng'):
return NotImplemented
return self.__class__(
lat=self.lat - other.lat,