2008-08-06 02:13:06 +08:00
|
|
|
"""
|
2014-03-02 22:25:53 +08:00
|
|
|
Distance and Area objects to allow for sensible and convenient calculation
|
2008-08-06 02:13:06 +08:00
|
|
|
and conversions. Here are some tests.
|
|
|
|
"""
|
|
|
|
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
|
|
|
|
2016-03-28 23:19:25 +08:00
|
|
|
from django.contrib.gis.measure import A, Area, D, Distance
|
2023-06-24 18:36:38 +08:00
|
|
|
from django.test import SimpleTestCase
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2023-06-24 18:36:38 +08:00
|
|
|
class DistanceTest(SimpleTestCase):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing the Distance object"
|
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_init(self):
|
2014-03-02 22:25:53 +08:00
|
|
|
"Testing initialization from valid units"
|
2008-08-06 02:13:06 +08:00
|
|
|
d = Distance(m=100)
|
|
|
|
self.assertEqual(d.m, 100)
|
|
|
|
|
|
|
|
d1, d2, d3 = D(m=100), D(meter=100), D(metre=100)
|
|
|
|
for d in (d1, d2, d3):
|
|
|
|
self.assertEqual(d.m, 100)
|
|
|
|
|
|
|
|
d = D(nm=100)
|
|
|
|
self.assertEqual(d.m, 185200)
|
|
|
|
|
|
|
|
y1, y2, y3 = D(yd=100), D(yard=100), D(Yard=100)
|
|
|
|
for d in (y1, y2, y3):
|
|
|
|
self.assertEqual(d.yd, 100)
|
|
|
|
|
|
|
|
mm1, mm2 = D(millimeter=1000), D(MiLLiMeTeR=1000)
|
|
|
|
for d in (mm1, mm2):
|
|
|
|
self.assertEqual(d.m, 1.0)
|
|
|
|
self.assertEqual(d.mm, 1000.0)
|
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_init_invalid(self):
|
2014-03-02 22:25:53 +08:00
|
|
|
"Testing initialization from invalid units"
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
D(banana=100)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2024-01-16 06:16:12 +08:00
|
|
|
def test_init_invalid_area_only_units(self):
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
D(ha=100)
|
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_access(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing access in different units"
|
|
|
|
d = D(m=100)
|
|
|
|
self.assertEqual(d.km, 0.1)
|
|
|
|
self.assertAlmostEqual(d.ft, 328.084, 3)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_access_invalid(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing access in invalid units"
|
|
|
|
d = D(m=100)
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertFalse(hasattr(d, "banana"))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_addition(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Test addition & subtraction"
|
|
|
|
d1 = D(m=100)
|
|
|
|
d2 = D(m=200)
|
|
|
|
|
|
|
|
d3 = d1 + d2
|
|
|
|
self.assertEqual(d3.m, 300)
|
|
|
|
d3 += d1
|
|
|
|
self.assertEqual(d3.m, 400)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
d4 = d1 - d2
|
|
|
|
self.assertEqual(d4.m, -100)
|
|
|
|
d4 -= d1
|
|
|
|
self.assertEqual(d4.m, -200)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2013-09-08 23:05:16 +08:00
|
|
|
d1 + 1
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2013-09-08 23:05:16 +08:00
|
|
|
d1 - 1
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2008-08-06 02:13:06 +08:00
|
|
|
d1 += 1
|
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2008-08-06 02:13:06 +08:00
|
|
|
d1 -= 1
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_multiplication(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Test multiplication & division"
|
|
|
|
d1 = D(m=100)
|
|
|
|
|
|
|
|
d3 = d1 * 2
|
|
|
|
self.assertEqual(d3.m, 200)
|
2009-12-18 02:21:30 +08:00
|
|
|
d3 = 2 * d1
|
|
|
|
self.assertEqual(d3.m, 200)
|
2008-08-06 02:13:06 +08:00
|
|
|
d3 *= 5
|
|
|
|
self.assertEqual(d3.m, 1000)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
d4 = d1 / 2
|
|
|
|
self.assertEqual(d4.m, 50)
|
|
|
|
d4 /= 5
|
|
|
|
self.assertEqual(d4.m, 10)
|
2012-06-14 21:24:47 +08:00
|
|
|
d5 = d1 / D(m=2)
|
|
|
|
self.assertEqual(d5, 50)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
a5 = d1 * D(m=10)
|
2014-04-10 04:20:22 +08:00
|
|
|
self.assertIsInstance(a5, Area)
|
2013-11-04 02:08:55 +08:00
|
|
|
self.assertEqual(a5.sq_m, 100 * 10)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2008-08-06 02:13:06 +08:00
|
|
|
d1 *= D(m=1)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2008-08-06 02:13:06 +08:00
|
|
|
d1 /= D(m=1)
|
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_unit_conversions(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing default units during maths"
|
|
|
|
d1 = D(m=100)
|
|
|
|
d2 = D(km=1)
|
|
|
|
|
|
|
|
d3 = d1 + d2
|
|
|
|
self.assertEqual(d3._default_unit, "m")
|
|
|
|
d4 = d2 + d1
|
|
|
|
self.assertEqual(d4._default_unit, "km")
|
|
|
|
d5 = d1 * 2
|
|
|
|
self.assertEqual(d5._default_unit, "m")
|
|
|
|
d6 = d1 / 2
|
|
|
|
self.assertEqual(d6._default_unit, "m")
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_comparisons(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing comparisons"
|
|
|
|
d1 = D(m=100)
|
|
|
|
d2 = D(km=1)
|
|
|
|
d3 = D(km=0)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertGreater(d2, d1)
|
|
|
|
self.assertEqual(d1, d1)
|
|
|
|
self.assertLess(d1, d2)
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertFalse(d3)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_units_str(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing conversion to strings"
|
|
|
|
d1 = D(m=100)
|
|
|
|
d2 = D(km=3.5)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(str(d1), "100.0 m")
|
|
|
|
self.assertEqual(str(d2), "3.5 km")
|
|
|
|
self.assertEqual(repr(d1), "Distance(m=100.0)")
|
|
|
|
self.assertEqual(repr(d2), "Distance(km=3.5)")
|
|
|
|
|
2019-04-09 02:17:05 +08:00
|
|
|
def test_furlong(self):
|
|
|
|
d = D(m=201.168)
|
|
|
|
self.assertEqual(d.furlong, 1)
|
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_unit_att_name(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing the `unit_attname` class method"
|
|
|
|
unit_tuple = [
|
|
|
|
("Yard", "yd"),
|
|
|
|
("Nautical Mile", "nm"),
|
|
|
|
("German legal metre", "german_m"),
|
2019-04-09 02:17:05 +08:00
|
|
|
("Indian yard", "indian_yd"),
|
|
|
|
("Chain (Sears)", "chain_sears"),
|
|
|
|
("Chain", "chain"),
|
|
|
|
("Furrow Long", "furlong"),
|
|
|
|
]
|
2008-08-06 02:13:06 +08:00
|
|
|
for nm, att in unit_tuple:
|
2017-03-08 05:00:43 +08:00
|
|
|
with self.subTest(nm=nm):
|
|
|
|
self.assertEqual(att, D.unit_attname(nm))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2023-06-24 18:36:38 +08:00
|
|
|
def test_unit_att_name_invalid(self):
|
|
|
|
msg = "Unknown unit type: invalid-unit-name"
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
D.unit_attname("invalid-unit-name")
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
A.unit_attname("invalid-unit-name")
|
|
|
|
|
2021-01-16 20:52:11 +08:00
|
|
|
def test_hash(self):
|
|
|
|
d1 = D(m=99)
|
|
|
|
d2 = D(m=100)
|
|
|
|
d3 = D(km=0.1)
|
|
|
|
self.assertEqual(hash(d2), hash(d3))
|
|
|
|
self.assertNotEqual(hash(d1), hash(d2))
|
|
|
|
self.assertNotEqual(hash(d1), hash(d3))
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class AreaTest(unittest.TestCase):
|
|
|
|
"Testing the Area object"
|
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_init(self):
|
2014-03-02 22:25:53 +08:00
|
|
|
"Testing initialization from valid units"
|
2008-08-06 02:13:06 +08:00
|
|
|
a = Area(sq_m=100)
|
|
|
|
self.assertEqual(a.sq_m, 100)
|
|
|
|
|
|
|
|
a = A(sq_m=100)
|
|
|
|
self.assertEqual(a.sq_m, 100)
|
|
|
|
|
|
|
|
a = A(sq_mi=100)
|
|
|
|
self.assertEqual(a.sq_m, 258998811.0336)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_init_invalid_a(self):
|
2014-03-02 22:25:53 +08:00
|
|
|
"Testing initialization from invalid units"
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
A(banana=100)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_access(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing access in different units"
|
|
|
|
a = A(sq_m=100)
|
|
|
|
self.assertEqual(a.sq_km, 0.0001)
|
|
|
|
self.assertAlmostEqual(a.sq_ft, 1076.391, 3)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_access_invalid_a(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing access in invalid units"
|
|
|
|
a = A(sq_m=100)
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertFalse(hasattr(a, "banana"))
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_addition(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Test addition & subtraction"
|
|
|
|
a1 = A(sq_m=100)
|
|
|
|
a2 = A(sq_m=200)
|
|
|
|
|
|
|
|
a3 = a1 + a2
|
|
|
|
self.assertEqual(a3.sq_m, 300)
|
|
|
|
a3 += a1
|
|
|
|
self.assertEqual(a3.sq_m, 400)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
a4 = a1 - a2
|
|
|
|
self.assertEqual(a4.sq_m, -100)
|
|
|
|
a4 -= a1
|
|
|
|
self.assertEqual(a4.sq_m, -200)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2013-09-08 23:05:16 +08:00
|
|
|
a1 + 1
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2013-09-08 23:05:16 +08:00
|
|
|
a1 - 1
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2008-08-06 02:13:06 +08:00
|
|
|
a1 += 1
|
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2008-08-06 02:13:06 +08:00
|
|
|
a1 -= 1
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_multiplication(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Test multiplication & division"
|
|
|
|
a1 = A(sq_m=100)
|
|
|
|
|
|
|
|
a3 = a1 * 2
|
|
|
|
self.assertEqual(a3.sq_m, 200)
|
2009-12-18 02:21:30 +08:00
|
|
|
a3 = 2 * a1
|
|
|
|
self.assertEqual(a3.sq_m, 200)
|
2008-08-06 02:13:06 +08:00
|
|
|
a3 *= 5
|
|
|
|
self.assertEqual(a3.sq_m, 1000)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
a4 = a1 / 2
|
|
|
|
self.assertEqual(a4.sq_m, 50)
|
|
|
|
a4 /= 5
|
|
|
|
self.assertEqual(a4.sq_m, 10)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2013-09-08 23:05:16 +08:00
|
|
|
a1 * A(sq_m=1)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2008-08-06 02:13:06 +08:00
|
|
|
a1 *= A(sq_m=1)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2013-09-08 23:05:16 +08:00
|
|
|
a1 / A(sq_m=1)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-04-29 00:09:37 +08:00
|
|
|
with self.assertRaises(TypeError):
|
2008-08-06 02:13:06 +08:00
|
|
|
a1 /= A(sq_m=1)
|
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_unit_conversions(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing default units during maths"
|
|
|
|
a1 = A(sq_m=100)
|
|
|
|
a2 = A(sq_km=1)
|
|
|
|
|
|
|
|
a3 = a1 + a2
|
|
|
|
self.assertEqual(a3._default_unit, "sq_m")
|
|
|
|
a4 = a2 + a1
|
|
|
|
self.assertEqual(a4._default_unit, "sq_km")
|
|
|
|
a5 = a1 * 2
|
|
|
|
self.assertEqual(a5._default_unit, "sq_m")
|
|
|
|
a6 = a1 / 2
|
|
|
|
self.assertEqual(a6._default_unit, "sq_m")
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_comparisons(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing comparisons"
|
|
|
|
a1 = A(sq_m=100)
|
|
|
|
a2 = A(sq_km=1)
|
|
|
|
a3 = A(sq_km=0)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertGreater(a2, a1)
|
|
|
|
self.assertEqual(a1, a1)
|
|
|
|
self.assertLess(a1, a2)
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertFalse(a3)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_units_str(self):
|
2008-08-06 02:13:06 +08:00
|
|
|
"Testing conversion to strings"
|
|
|
|
a1 = A(sq_m=100)
|
|
|
|
a2 = A(sq_km=3.5)
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
self.assertEqual(str(a1), "100.0 sq_m")
|
|
|
|
self.assertEqual(str(a2), "3.5 sq_km")
|
|
|
|
self.assertEqual(repr(a1), "Area(sq_m=100.0)")
|
|
|
|
self.assertEqual(repr(a2), "Area(sq_km=3.5)")
|
|
|
|
|
2024-01-16 06:16:12 +08:00
|
|
|
def test_hectare(self):
|
|
|
|
a = A(sq_m=10000)
|
|
|
|
self.assertEqual(a.ha, 1)
|
|
|
|
|
|
|
|
def test_hectare_unit_att_name(self):
|
|
|
|
self.assertEqual(A.unit_attname("Hectare"), "ha")
|
|
|
|
|
2021-01-16 20:52:11 +08:00
|
|
|
def test_hash(self):
|
|
|
|
a1 = A(sq_m=100)
|
|
|
|
a2 = A(sq_m=1000000)
|
|
|
|
a3 = A(sq_km=1)
|
|
|
|
self.assertEqual(hash(a2), hash(a3))
|
|
|
|
self.assertNotEqual(hash(a1), hash(a2))
|
|
|
|
self.assertNotEqual(hash(a1), hash(a3))
|