diff --git a/src/barril/units/_abstractvaluewithquantity.py b/src/barril/units/_abstractvaluewithquantity.py index 0c4c17f..97ea1e8 100644 --- a/src/barril/units/_abstractvaluewithquantity.py +++ b/src/barril/units/_abstractvaluewithquantity.py @@ -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"] @@ -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) diff --git a/src/barril/units/_quantity.py b/src/barril/units/_quantity.py index 1d24f98..ecfb060 100644 --- a/src/barril/units/_quantity.py +++ b/src/barril/units/_quantity.py @@ -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): diff --git a/src/barril/units/_tests/test_empty_scalar.py b/src/barril/units/_tests/test_empty_scalar.py index 03ec48e..8550b2e 100644 --- a/src/barril/units/_tests/test_empty_scalar.py +++ b/src/barril/units/_tests/test_empty_scalar.py @@ -1,4 +1,6 @@ from barril.units import Scalar +from barril.units.unit_database import InvalidQuantityTypeError +import pytest def testEmptyScalar(): @@ -11,3 +13,15 @@ def testEmptyScalar(): assert scalar.GetValue() == 0.0 assert scalar == scalar.Copy() + + +def testEmptyScalarWithInitialValue(): + # 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() == "" diff --git a/src/barril/units/_tests/test_scalar.py b/src/barril/units/_tests/test_scalar.py index 5a0e587..aaaf305 100644 --- a/src/barril/units/_tests/test_scalar.py +++ b/src/barril/units/_tests/test_scalar.py @@ -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. @@ -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