Ejemplo n.º 1
0
 def test_string_conversions(self):
     working_examples = {
         '1024KB': 1048576,
         '1024TB': 1125899906842624,
         '1024K': 1048576,
         '1024T': 1125899906842624,
         '1TB': 1099511627776,
         '1T': 1099511627776,
         '1KB': 1024,
         '1K': 1024,
         '1B': 1,
         '1': 1,
         '1MB': 1048576,
         '7MB': 7340032,
         '0MB': 0,
         '0KB': 0,
         '0TB': 0,
         '': 0,
     }
     for (in_value, expected_value) in working_examples.items():
         b_value = strutils.to_bytes(in_value)
         self.assertEqual(expected_value, b_value)
         if in_value:
             in_value = "-" + in_value
             b_value = strutils.to_bytes(in_value)
             self.assertEqual(expected_value * -1, b_value)
     breaking_examples = [
         'junk1KB', '1023BBBB',
     ]
     for v in breaking_examples:
         self.assertRaises(TypeError, strutils.to_bytes, v)
Ejemplo n.º 2
0
 def test_string_conversions(self):
     working_examples = {
         '1024KB': 1048576,
         '1024TB': 1125899906842624,
         '1024K': 1048576,
         '1024T': 1125899906842624,
         '1TB': 1099511627776,
         '1T': 1099511627776,
         '1KB': 1024,
         '1K': 1024,
         '1B': 1,
         '1': 1,
         '1MB': 1048576,
         '7MB': 7340032,
         '0MB': 0,
         '0KB': 0,
         '0TB': 0,
         '': 0,
     }
     for (in_value, expected_value) in working_examples.items():
         b_value = strutils.to_bytes(in_value)
         self.assertEqual(expected_value, b_value)
         if in_value:
             in_value = "-" + in_value
             b_value = strutils.to_bytes(in_value)
             self.assertEqual(expected_value * -1, b_value)
     breaking_examples = [
         'junk1KB',
         '1023BBBB',
     ]
     for v in breaking_examples:
         self.assertRaises(TypeError, strutils.to_bytes, v)
Ejemplo n.º 3
0
 def _extract_bytes(self, details):
     # Replace it with the byte amount
     real_size = self.SIZE_RE.search(details)
     if real_size:
         details = real_size.group(1)
     try:
         details = strutils.to_bytes(details)
     except TypeError:
         pass
     return details
Ejemplo n.º 4
0
 def _extract_bytes(self, details):
     # Replace it with the byte amount
     real_size = self.SIZE_RE.search(details)
     if not real_size:
         raise ValueError(_('Invalid input value "%s".') % details)
     magnitude = real_size.group(1)
     unit_of_measure = real_size.group(2)
     bytes_info = real_size.group(3)
     if bytes_info:
         return int(real_size.group(4))
     elif not unit_of_measure:
         return int(magnitude)
     return strutils.to_bytes('%s%s' % (magnitude, unit_of_measure))
Ejemplo n.º 5
0
 def _extract_bytes(self, details):
     # Replace it with the byte amount
     real_size = self.SIZE_RE.search(details)
     if not real_size:
         raise ValueError(_('Invalid input value "%s".') % details)
     magnitude = real_size.group(1)
     unit_of_measure = real_size.group(2)
     bytes_info = real_size.group(3)
     if bytes_info:
         return int(real_size.group(4))
     elif not unit_of_measure:
         return int(magnitude)
     return strutils.to_bytes('%s%s' % (magnitude, unit_of_measure))