Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try unit conversion instead of literal dimensionality check #99 #101

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/quantityfield/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ def check_matching_unit_dimension(
If not
:raise DimensionalityError
"""

base_unit = getattr(ureg, base_units)
# create a pint quantity by multiplying unit with magnitude of 1
base_quant = 1 * base_unit

for unit_string in units_to_check:
unit = getattr(ureg, unit_string)
if unit.dimensionality != base_unit.dimensionality:
raise DimensionalityError(base_unit, unit)
# try to convert base qunatity to new unit, this also work for ureg.context
try:
base_quant.to(unit)
except DimensionalityError as e:
raise DimensionalityError(base_unit, unit) from e
54 changes: 53 additions & 1 deletion tests/test_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.test import TestCase

from pint import DimensionalityError
from pint import Context, DimensionalityError

import quantityfield.fields as fields
import quantityfield.helper as helper
Expand Down Expand Up @@ -28,3 +28,55 @@ def test_get_prep_value(self):
field = fields.IntegerQuantityField("meter")
with self.assertRaises(ValueError):
field.get_prep_value("foobar")


class TestContextHandling(TestCase):
def setUp(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the other lack it as well but could you add some doc string explaining why the test are needed?
May also refer to the isseue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

# Define a context where weight is equated to force
self.context = Context("earth")
self.context.add_transformation(
"[mass]", "[force]", lambda ureg, x: x * ureg.gravity
)
self.context.add_transformation(
"[force]", "[mass]", lambda ureg, x: x / ureg.gravity
)
ureg.add_context(self.context)

def test_context_global(self):
# Activate context globally and test
ureg.enable_contexts("earth")
helper.check_matching_unit_dimension(ureg, "kg", ["newton", "kN", "ton"])
ureg.disable_contexts()

def test_context_with_block(self):
# Use context with the 'with' statement
with ureg.context("earth"):
helper.check_matching_unit_dimension(ureg, "kg", ["newton", "kN", "ton"])

def test_invalid_context(self):
with self.assertRaises(DimensionalityError):
helper.check_matching_unit_dimension(ureg, "kg", ["newton", "kN", "ton"])

def test_field_w_context_global(self):
ureg.enable_contexts("earth")
self.field = fields.IntegerQuantityField(
base_units="kg", unit_choices=["newton", "kN", "ton"]
)
ureg.disable_contexts()

def test_field_w_context_block(self):
with ureg.context("earth"):
self.field = fields.IntegerQuantityField(
base_units="kg", unit_choices=["newton", "kN", "ton"]
)

def test_invalid_field_wo_context(self):
with self.assertRaises(DimensionalityError):
self.field = fields.IntegerQuantityField(
base_units="kg", unit_choices=["newton", "kN", "ton"]
)

def tearDown(self):
# Clean up by disabling and removing the context
ureg.disable_contexts()
ureg.remove_context("earth")
Loading