Skip to content

Commit

Permalink
Merge pull request #108 from SamuelJennings/main
Browse files Browse the repository at this point in the history
convert numeric types to str before calling Decimal
  • Loading branch information
CarliJoy authored Sep 11, 2024
2 parents b94579c + 63887c9 commit 5198ff8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ Contributors
* Alex Bhandari <[email protected]>
* Jonas Haag <[email protected]>
* Igor Kozyrenko <[email protected]>
* Samuel Scott Jennings <[email protected]>
* Samuel Jennings <[email protected]>
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Changelog
=========


Version 0.7.2
=============
- fix conversion of number input to DecimalField (`issue #106 <https://github.com/CarliJoy/django-pint/issues/106>`)

Version 0.7.1
=============
- fix wrong unit display in widget (`issue #43 <https://github.com/CarliJoy/django-pint/issues/43>`_)
Expand Down
8 changes: 6 additions & 2 deletions src/quantityfield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,13 @@ class PositiveIntegerQuantityField(QuantityFieldMixin, models.PositiveIntegerFie


class DecimalQuantityFormField(QuantityFormFieldMixin, forms.DecimalField):
to_number_type = Decimal

def to_number_type(self, x: object) -> Decimal:
return Decimal(str(x))


class DecimalQuantityField(QuantityFieldMixin, models.DecimalField):
form_field_class = DecimalQuantityFormField
to_number_type = Decimal

def __init__(
self,
Expand Down Expand Up @@ -418,6 +419,9 @@ def __init__(
**kwargs,
)

def to_number_type(self, x: object) -> Decimal:
return Decimal(str(x))

def get_db_prep_save(self, value, connection) -> Decimal:
"""
Get Value that shall be saved to database, make sure it is transformed
Expand Down
32 changes: 32 additions & 0 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from quantityfield.fields import (
BigIntegerQuantityField,
DecimalQuantityField,
DecimalQuantityFormField,
IntegerQuantityField,
PositiveIntegerQuantityField,
QuantityField,
Expand Down Expand Up @@ -452,6 +453,16 @@ class TestDecimalFieldSave(FloatLikeFieldSaveTestBase, TestCase):
OUNCES_IN_GRAM = Decimal("283.50")
EXPECTED_TYPE = Decimal

def test_stores_value_in_base_units(self):
field = self.MODEL._meta.get_field("weight")
expected = Decimal("2.1")
func = field.to_number_type
self.assertIsInstance(func(2.1), Decimal)
self.assertEqual(func(2.1), expected) # test float
self.assertEqual(func("2.1"), expected) # test string
self.assertEqual(func(Decimal("2.1")), expected) # test Decimal
self.assertEqual(func(2), Decimal("2")) # test Int


class IntLikeFieldSaveTestBase(FieldSaveTestBase):
DEFAULT_WEIGHT_STR = "100"
Expand Down Expand Up @@ -514,3 +525,24 @@ def test_comparison_with_quantity(self):
def test_comparison_with_quantity_respects_units(self):
qs = self.MODEL.objects.filter(weight__gt=self.COMPARE_QUANTITY)
self.assertNotIn(self.lightest, qs)


class TestDecimalQuantityFormField(TestCase):

def test_to_number_type_returns_decimal(self):
field = DecimalQuantityFormField(base_units="gram")
expected = Decimal("2.1")
func = field.to_number_type
self.assertIsInstance(func(2.1), Decimal)
self.assertEqual(func(2.1), expected) # test float
self.assertEqual(func("2.1"), expected) # test string
self.assertEqual(func(Decimal("2.1")), expected) # test Decimal
self.assertEqual(func(2), Decimal("2")) # test Int

def test_saves_correct_decimal_precision(self):
field = DecimalQuantityFormField(base_units="gram")
expected = Decimal("2.1")
self.assertEqual(field.clean(2.1).magnitude, expected) # test float
self.assertEqual(field.clean("2.1").magnitude, expected) # test string
self.assertEqual(field.clean(expected).magnitude, expected) # test Decimal
self.assertEqual(field.clean(2).magnitude, Decimal("2")) # test Int

0 comments on commit 5198ff8

Please sign in to comment.