Skip to content

Commit

Permalink
Refactored Convert to using combined unit
Browse files Browse the repository at this point in the history
  • Loading branch information
arthursoprana committed Oct 10, 2019
1 parent 7dbe280 commit d9114e1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
8 changes: 6 additions & 2 deletions src/barril/units/_abstractvaluewithquantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from barril.units.unit_database import UnitDatabase
from oop_ext.interface._interface import ImplementsInterface

from .interfaces import IObjectWithQuantity, IQuantity
from ._quantity import ObtainQuantity
from .interfaces import IObjectWithQuantity, IQuantity

__all__ = ["AbstractValueWithQuantityObject"]

Expand Down Expand Up @@ -201,8 +201,12 @@ def CreateCopy(self, value=None, unit=None, category=None, **kwargs):
Returns a new scalar that's a copy of this scalar.
"""
try:

if value is None:
value = self.GetAbstractValue(unit)
if not self.HasCategory():
value = self.GetAbstractValue()
else:
value = self.GetAbstractValue(unit)

if unit is None and category is None:
return self.CreateWithQuantity(self._quantity, value=value, **kwargs)
Expand Down
14 changes: 3 additions & 11 deletions src/barril/units/_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,17 +677,9 @@ def Convert(self, value, to_unit):
:returns:
An object with values to the passed unit.
"""
try:
return self._unit_database.Convert(
self._composing_categories, self._composing_units, to_unit, value
)
except:
return self._unit_database.Convert(
self._category,
self._CreateUnitsWithJoinedExponentsString(),
to_unit,
value,
)
return self._unit_database.Convert(
self._category, self._CreateUnitsWithJoinedExponentsString(), to_unit, value
)

@classmethod
def _GetComparison(cls, operator, use_literals=False):
Expand Down
18 changes: 18 additions & 0 deletions src/barril/units/_tests/test_empty_scalar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from barril.units import Scalar
from barril.units.unit_database import InvalidQuantityTypeError
import pytest


def testEmptyScalar():
Expand All @@ -11,3 +13,19 @@ def testEmptyScalar():
assert scalar.GetValue() == 0.0

assert scalar == scalar.Copy()


# Should we support this?
def testEmptyScalarWithInitialValue():
"""
ScalarMultiData exception when some of its scalars don't have a quantity_type
"""
# An empty scalar doesn't have a category defined
scalar_1 = Scalar.CreateEmptyScalar(20.0)

# When try to retrieve an empty scalar value using any unit a exception
# is being raised
with pytest.raises(InvalidQuantityTypeError):
_ = scalar_1.GetValue("m")

assert scalar_1.GetUnit() == ""
17 changes: 5 additions & 12 deletions src/barril/units/_tests/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,18 +384,6 @@ class MyScalar(units.Scalar):
assert scalar.GetFormatted() == "1.18 [m]"


def testEmptyScalar():
"""
ScalarMultiData exception when some of its scalars don't have a quantity_type
"""
# An empty scalar doesn't have a category defined
scalar_1 = Scalar.CreateEmptyScalar(20.0)

# When try to retrieve scalar value or unit a exception was being raised
assert scalar_1.GetValue("m") == 20.0
assert scalar_1.GetUnit() == ""


def testCopyProperties(unit_database_well_length):
"""
Test if the mehod SetValue is not called when copying the Scalar's properties.
Expand All @@ -411,6 +399,11 @@ def testCopyProperties(unit_database_well_length):
assert scalar_dest.GetUnit() == unit
assert scalar_dest.GetValue() == value

scalar_dest = scalar_source.CreateCopy(category=category, value=None, unit=unit)
assert scalar_dest.GetCategory() == category
assert scalar_dest.GetUnit() == unit
assert scalar_dest.GetValue() == value


def testCopyPropertiesAndValidation(unit_database_well_length):
category = "well-length-with-min-and-max" # the minimum value is zero
Expand Down

0 comments on commit d9114e1

Please sign in to comment.