[master 1/2] Extend Size.roundToNearest to allow Size units (#1200812)

mulkieran installerbot-noreply at redhat.com
Tue Mar 17 12:41:23 UTC 2015


From: mulhern <amulhern at redhat.com>

Related: fed#1200812

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/size.py     | 21 +++++++++++++++++----
 tests/size_test.py | 11 +++++++++++
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/blivet/size.py b/blivet/size.py
index b70405c..1300ce7 100644
--- a/blivet/size.py
+++ b/blivet/size.py
@@ -371,15 +371,28 @@ def humanReadable(self, max_places=2, strip=True, min_value=1, xlate=True):
         return retval_str + " " + _makeSpec(unit.abbr, _BYTES_SYMBOL, xlate, lowercase=False)
 
     def roundToNearest(self, unit, rounding=ROUND_DEFAULT):
-        """
-            :param unit: a unit specifier, a named constant like KiB
+        """ Rounds to nearest unit specified as a named constant or a Size.
+
+            :param unit: a unit specifier
+            :type unit: a named constant like KiB, or any non-negative Size
             :keyword rounding: which direction to round
             :type rounding: one of ROUND_UP, ROUND_DOWN, or ROUND_DEFAULT
             :returns: Size rounded to nearest whole specified unit
             :rtype: :class:`Size`
+
+            If unit is Size(0), returns Size(0).
         """
         if rounding not in (ROUND_UP, ROUND_DOWN, ROUND_DEFAULT):
             raise ValueError("invalid rounding specifier")
 
-        rounded = self.convertTo(unit).to_integral_value(rounding=rounding)
-        return Size(rounded * unit.factor)
+        factor = getattr(unit, "factor", unit)
+
+        if factor == Size(0):
+            return Size(0)
+
+        factor = Decimal(factor)
+        if factor < 0:
+            raise ValueError("invalid rounding unit: %s" % factor)
+
+        rounded = (Decimal(self) / factor).to_integral_value(rounding=rounding)
+        return Size(rounded * factor)
diff --git a/tests/size_test.py b/tests/size_test.py
index adeb1ce..885ae8e 100644
--- a/tests/size_test.py
+++ b/tests/size_test.py
@@ -342,6 +342,17 @@ def testRoundToNearest(self):
         self.assertEqual(s.roundToNearest(TiB, rounding=size.ROUND_DOWN),
                          Size(0))
 
+        # test Size parameters
+        self.assertEqual(s.roundToNearest(Size("128 GiB")), Size("512 GiB"))
+        self.assertEqual(s.roundToNearest(Size("1 KiB")), Size("513 GiB"))
+        self.assertEqual(s.roundToNearest(Size("1 TiB")), Size("1 TiB"))
+        self.assertEqual(s.roundToNearest(Size("1 TiB"), rounding=size.ROUND_DOWN), Size(0))
+        self.assertEqual(s.roundToNearest(Size(0)), Size(0))
+        self.assertEqual(s.roundToNearest(Size("13 GiB")), Size("507 GiB"))
+
+        with self.assertRaises(ValueError):
+            s.roundToNearest(Size("-1 B"))
+
 class UtilityMethodsTestCase(unittest.TestCase):
 
     def testLowerASCII(self):


-- 
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/de7efc681b04ffffba126da89fdc0eb1b2f0f36d


More information about the anaconda-patches mailing list