From e4657c536e4f69331d2c365c958778d573d59478 Mon Sep 17 00:00:00 2001 From: Arthur Besen Soprana Date: Thu, 10 Oct 2019 18:23:05 -0300 Subject: [PATCH] Add new tests --- src/barril/units/_quantity.py | 15 ++++++++++++--- src/barril/units/_tests/test_scalar.py | 7 +++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/barril/units/_quantity.py b/src/barril/units/_quantity.py index ecfb060..0faab7c 100644 --- a/src/barril/units/_quantity.py +++ b/src/barril/units/_quantity.py @@ -9,6 +9,7 @@ UnitDatabase, UnitsError, FixUnitIfIsLegacy, + ComposedUnitError, ) from oop_ext.interface._interface import ImplementsInterface @@ -677,9 +678,17 @@ def Convert(self, value, to_unit): :returns: An object with values to the passed unit. """ - return self._unit_database.Convert( - self._category, self._CreateUnitsWithJoinedExponentsString(), to_unit, value - ) + try: + return self._unit_database.Convert( + self._composing_categories, self._composing_units, to_unit, value + ) + except ComposedUnitError: + 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_scalar.py b/src/barril/units/_tests/test_scalar.py index aaaf305..0a9810a 100644 --- a/src/barril/units/_tests/test_scalar.py +++ b/src/barril/units/_tests/test_scalar.py @@ -383,6 +383,13 @@ class MyScalar(units.Scalar): scalar = Scalar("length", 1.18, "m") assert scalar.GetFormatted() == "1.18 [m]" + # Check that squared works + scalar2 = scalar * scalar + assert scalar2.GetFormatted("m2") == "1.3924 [m2]" + assert scalar2.GetFormattedValue("m2") == "1.3924" + assert scalar2.GetFormatted((("m", 2),)) == "1.3924 [('m', 2)]" + assert scalar2.GetFormattedValue((("m", 2),)) == "1.3924" + def testCopyProperties(unit_database_well_length): """