1
0
mirror of https://github.com/django/django.git synced 2024-12-22 09:05:43 +00:00

Fixed #34676 -- Normalized Distance()/Area() exceptions for nonexistent units.

This commit is contained in:
Andrew Northall 2023-06-24 11:36:38 +01:00 committed by Mariusz Felisiak
parent 650ce96782
commit 38cde27a89
2 changed files with 11 additions and 5 deletions

View File

@ -232,7 +232,7 @@ class MeasureBase:
""" """
Retrieve the unit attribute name for the given unit string. Retrieve the unit attribute name for the given unit string.
For example, if the given unit string is 'metre', return 'm'. For example, if the given unit string is 'metre', return 'm'.
Raise an exception if an attribute cannot be found. Raise an AttributeError if an attribute cannot be found.
""" """
lower = unit_str.lower() lower = unit_str.lower()
if unit_str in cls.UNITS: if unit_str in cls.UNITS:
@ -242,9 +242,7 @@ class MeasureBase:
elif lower in cls.LALIAS: elif lower in cls.LALIAS:
return cls.LALIAS[lower] return cls.LALIAS[lower]
else: else:
raise Exception( raise AttributeError(f"Unknown unit type: {unit_str}")
'Could not find a unit keyword associated with "%s"' % unit_str
)
class Distance(MeasureBase): class Distance(MeasureBase):

View File

@ -6,9 +6,10 @@ and conversions. Here are some tests.
import unittest import unittest
from django.contrib.gis.measure import A, Area, D, Distance from django.contrib.gis.measure import A, Area, D, Distance
from django.test import SimpleTestCase
class DistanceTest(unittest.TestCase): class DistanceTest(SimpleTestCase):
"Testing the Distance object" "Testing the Distance object"
def test_init(self): def test_init(self):
@ -157,6 +158,13 @@ class DistanceTest(unittest.TestCase):
with self.subTest(nm=nm): with self.subTest(nm=nm):
self.assertEqual(att, D.unit_attname(nm)) self.assertEqual(att, D.unit_attname(nm))
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")
def test_hash(self): def test_hash(self):
d1 = D(m=99) d1 = D(m=99)
d2 = D(m=100) d2 = D(m=100)