Skip to content

Commit

Permalink
Fixed #34676 -- Normalized Distance()/Area() exceptions for nonexiste…
Browse files Browse the repository at this point in the history
…nt units.
  • Loading branch information
anorthall authored and felixxm committed Jun 25, 2023
1 parent 650ce96 commit 38cde27
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 2 additions & 4 deletions django/contrib/gis/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def unit_attname(cls, unit_str):
"""
Retrieve the unit attribute name for the given unit string.
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()
if unit_str in cls.UNITS:
Expand All @@ -242,9 +242,7 @@ def unit_attname(cls, unit_str):
elif lower in cls.LALIAS:
return cls.LALIAS[lower]
else:
raise Exception(
'Could not find a unit keyword associated with "%s"' % unit_str
)
raise AttributeError(f"Unknown unit type: {unit_str}")


class Distance(MeasureBase):
Expand Down
10 changes: 9 additions & 1 deletion tests/gis_tests/test_measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import unittest

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"

def test_init(self):
Expand Down Expand Up @@ -157,6 +158,13 @@ def test_unit_att_name(self):
with self.subTest(nm=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):
d1 = D(m=99)
d2 = D(m=100)
Expand Down

0 comments on commit 38cde27

Please sign in to comment.