Ejemplo n.º 1
0
    def testMinValue(self):
        """ Test behavior on min_value parameter. """
        s = Size(9, MiB)
        self.assertEqual(s.components(), (Fraction(9, 1), MiB))
        self.assertEqual(
           s.components(StrConfig(min_value=10)),
           (Fraction(9216, 1), KiB)
        )

        s = Size("0.5", GiB)
        self.assertEqual(
           s.components(StrConfig(min_value=1)),
           (Fraction(512, 1), MiB)
        )
        self.assertEqual(
           s.components(StrConfig(min_value=Decimal("0.1"))),
           (Fraction(1, 2), GiB)
        )
        self.assertEqual(
           s.components(StrConfig(min_value=Decimal(1))),
           (Fraction(512, 1), MiB)
        )

        # when min_value is 10 and single digit on left of decimal, display
        # with smaller unit.
        s = Size('7.18', KiB)
        self.assertEqual(s.components(StrConfig(min_value=10))[1], B)
        s = Size('9.68', TiB)
        self.assertEqual(s.components(StrConfig(min_value=10))[1], GiB)
        s = Size('4.29', MiB)
        self.assertEqual(s.components(StrConfig(min_value=10))[1], KiB)

        # when min value is 100 and two digits on left of decimal
        s = Size('14', MiB)
        self.assertEqual(
           s.components(StrConfig(min_value=100)),
           (Fraction(14 * 1024, 1), KiB)
        )
Ejemplo n.º 2
0
 def testNegative(self):
     """ Test construction of negative sizes. """
     s = Size(-500, MiB)
     self.assertEqual(s.components(), (Fraction(-500, 1), MiB))
     self.assertEqual(s.convertTo(B), -524288000)
Ejemplo n.º 3
0
 def testRoundingToBytes(self):
     """ Test that second part is B when rounding to bytes. """
     s = Size(500)
     self.assertEqual(s.components()[1], B)
Ejemplo n.º 4
0
 def testSIUnits(self):
     """ Test binary_units param. """
     s = Size(1000)
     self.assertEqual(s.components(StrConfig(binary_units=False)), (1, KB))
Ejemplo n.º 5
0
 def testExceptionValues(self):
     """ Test that exceptions are properly raised on bad params. """
     s = Size(500)
     with self.assertRaises(SizeValueError):
         s.components(StrConfig(min_value=-1))