caellion-python-commons
test_stringutil_formatters.py
Go to the documentation of this file.
1 # test framework
2 import unittest
3 from nose2.tools import params
4 
5 # package
6 from caellion.pycommons.stringutil.formatters import NumberFormatting as fmt
7 from caellion.pycommons.stringutil.formatters import InvalidDurationException
8 
9 
10 # test cases
11 class TestStringUtilFormatters(unittest.TestCase):
12  # SI formatter
13  @params((0, 0, "0"), (0, 3, "0.000"), (1000, 0, "1k"), (1000000, 0, "1M"), (123.456, 3, "123.456"), (1234.56, 3, "1.235k"), (123456, 3, "123.456k"), (123456789, 3, "123.457M"), (999999, 3, "999.999k"), (12345678901234567890123456789, 0, "12346Y"), (12345678901234567890123456789000, 0, "12345679Y"))
14  def test_fmt_si_plus(self, value, decimals, expected):
15  self.assertEqual(fmt.formatSI(value, decimals), expected)
16 
17  @params((0, "0"), (1000, "1k"), (1000000, "1M"), (12345678901234567890123456789, "12346Y"))
18  def test_fmt_si_plus_no_decimals(self, value, expected):
19  self.assertEqual(fmt.formatSI(value), expected)
20 
21  @params((0, 0, "0"), (0, 3, "0.000"), (-1000, 0, "-1k"), (-1000000, 0, "-1M"), (-123.456, 3, "-123.456"), (-1234.56, 3, "-1.235k"), (-123456, 3, "-123.456k"), (-123456789, 3, "-123.457M"), (-999999, 3, "-999.999k"), (-12345678901234567890123456789, 0, "-12346Y"))
22  def test_fmt_si_minus(self, value, decimals, expected):
23  self.assertEqual(fmt.formatSI(value, decimals), expected)
24 
25  # binary SI
26  @params((0, 0, "0"), (1024, 0, "1ki"), (1024 * 1024, 0, "1Mi"), (512 * 1024, 3, "512.000ki"), (256520, 0, "251ki"), (256520, 3, "250.508ki"), (10240002, 3, "9.766Mi"), (12345678901234567890123456789, 0, "10212Yi"))
27  def test_fmt_si_bin_plus(self, value, decimals, expected):
28  self.assertEqual(fmt.formatBinarySI(value, decimals), expected)
29 
30  @params((0, "0"), (1024, "1ki"), (1024 * 1024, "1Mi"), (256520, "251ki"), (12345678901234567890123456789, "10212Yi"))
31  def test_fmt_si_bin_plus_no_decimals(self, value, expected):
32  self.assertEqual(fmt.formatBinarySI(value), expected)
33 
34  @params((0, 0, "0"), (-1024, 0, "-1ki"), (-1024 * 1024, 0, "-1Mi"), (-512 * 1024, 3, "-512.000ki"), (-256520, 0, "-251ki"), (-256520, 3, "-250.508ki"), (-10240002, 3, "-9.766Mi"), (-12345678901234567890123456789, 0, "-10212Yi"))
35  def test_fmt_si_bin_minus(self, value, decimals, expected):
36  self.assertEqual(fmt.formatBinarySI(value, decimals), expected)
37 
38  # subvalues
39  @params((0, 0, "0"), (0, 3, "0.000"), (0.100, 0, "100m"), (0.000100, 0, "100μ"), (0.000000100, 0, "100n"), (0.0001231, 1, "123.1μ"), (0.135100, 0, "135m"), (1e-50, 0, "0y"), (1e-24, 0, "1y"), (1e-25, 0, "0y"), (9.999e-25, 0, "0y"))
40  def test_fmt_si_sub_plus(self, value, decimals, expected):
41  self.assertEqual(fmt.formatSISubValue(value, decimals), expected)
42 
43  @params((0, "0"), (0.100, "100m"), (0.000100, "100μ"), (0.000000100, "100n"), (0.135100, "135m"), (1e-50, "0y"))
44  def test_fmt_si_sub_plus_no_decimals(self, value, expected):
45  self.assertEqual(fmt.formatSISubValue(value), expected)
46 
47  @params((0, 0, "0"), (0, 3, "0.000"), (-0.100, 0, "-100m"), (-0.000100, 0, "-100μ"), (-0.000000100, 0, "-100n"), (-0.0001231, 1, "-123.1μ"), (-0.135100, 0, "-135m"), (-1e-50, 0, "-0y"))
48  def test_fmt_si_sub_minus(self, value, decimals, expected):
49  self.assertEqual(fmt.formatSISubValue(value, decimals), expected)
50 
51  # subvalues
52  @params((0, 0, "BinSubValueError"), (0, 3, "BinSubValueError"), (0.100, 0, "BinSubValueError"), (0.000100, 0, "BinSubValueError"), (0.000000100, 0, "BinSubValueError"), (0.0001231, 1, "BinSubValueError"), (0.135100, 0, "BinSubValueError"), (1e-50, 0, "BinSubValueError"))
53  def test_fmt_si_binary_sub_plus(self, value, decimals, expected):
54  self.assertEqual(fmt.formatSIBinarySubValue(value, decimals), expected)
55 
56  @params((0, 0, "BinSubValueError"), (0, 3, "BinSubValueError"), (-0.100, 0, "BinSubValueError"), (-0.000100, 0, "BinSubValueError"), (-0.000000100, 0, "BinSubValueError"), (-0.0001231, 1, "BinSubValueError"), (-0.135100, 0, "BinSubValueError"), (-1e-50, 0, "BinSubValueError"))
57  def test_fmt_si_binary_sub_minus(self, value, decimals, expected):
58  self.assertEqual(fmt.formatSIBinarySubValue(value, decimals), expected)
59 
60  # full-range formatters
61  @params((0, 0, "0"), (0, 3, "0.000"), (1000, 0, "1k"), (1000000, 0, "1M"), (123.456, 3, "123.456"), (1234.56, 3, "1.235k"), (123456, 3, "123.456k"), (123456789, 3, "123.457M"), (999999, 3, "999.999k"), (12345678901234567890123456789, 0, "12346Y"), (0.100, 0, "100m"), (0.000100, 0, "100μ"), (0.000000100, 0, "100n"), (0.0001231, 1, "123.1μ"), (0.135100, 0, "135m"), (1e-50, 0, "0y"), (1, 0, "1"), (1 - 1e-24, 0, "1"))
62  def test_fmt_si_plus_fullrange(self, value, decimals, expected):
63  self.assertEqual(fmt.formatSIFullRange(value, decimals), expected)
64 
65  @params((0, 0, "0"), (0, 3, "0.000"), (-1000, 0, "-1k"), (-1000000, 0, "-1M"), (-123.456, 3, "-123.456"), (-1234.56, 3, "-1.235k"), (-123456, 3, "-123.456k"), (-123456789, 3, "-123.457M"), (-999999, 3, "-999.999k"), (-12345678901234567890123456789, 0, "-12346Y"), (0.100, 0, "100m"), (0.000100, 0, "100μ"), (0.000000100, 0, "100n"), (0.0001231, 1, "123.1μ"), (0.135100, 0, "135m"), (-1e-50, 0, "-0y"), (-1, 0, "-1"), (-1 + 1e-24, 0, "-1"))
66  def test_fmt_si_minus_fullrange(self, value, decimals, expected):
67  self.assertEqual(fmt.formatSIFullRange(value, decimals), expected)
68 
69  @params(
70  (9.99, 365000 * 86400, "unit", "9.99unit/millenium"),
71  (1.0, 365000 * 86400, "unit", "1.00unit/millenium"),
72  (9.99, 36500 * 86400, "unit", "9.99unit/century"),
73  (1.0, 36500 * 86400, "unit", "1.00unit/century"),
74  (9.99, 365 * 86400, "unit", "9.99unit/year"),
75  (1.0, 365 * 86400, "unit", "1.00unit/year"),
76  (6.99, 7 * 86400.0, "unit", "6.99unit/week"),
77  (1.0, 7 * 86400.0, "unit", "1.00unit/week"),
78  (9.99, 86400.0, "unit", "9.99unit/day"),
79  (1.0, 86400.0, "unit", "1.00unit/day"),
80  (9.99, 3600.0, "unit", "9.99unit/hour"),
81  (1.0, 3600.0, "unit", "1.00unit/hour"),
82  (1.0, 60.0, "unit", "1.00unit/minute"),
83  (1.0, 1.0, "unit", "1.00unit/second"),
84  (0.0, 1.0, "unit", "0.00unit/millenium"),
85  (1.0, 0.001, "unit", "1.00unit/millisecond"),
86  (1.0, 0.000001, "unit", "1.00unit/microsecond"),
87  (-1.0, 0.000001, "unit", "-1.00unit/microsecond"),
88  )
89  def test_fmt_units_per_interval(self, units, seconds, unit, expected):
90  self.assertEqual(fmt.formatUnitsPerIntervalDynamic(units, seconds, unit), expected)
91 
92  @params((1.0, -0.000001, "unit"), (1.0, 0, "unit"))
94  with self.assertRaises(InvalidDurationException):
95  try:
96  fmt.formatUnitsPerIntervalDynamic(units, seconds, unit)
97  except InvalidDurationException as e:
98  self.assertEqual(e.args[0], str("{:s} is not a valid, positive duration").format(str(seconds)))
99  raise
100 
101 
102 if __name__ == "__main__":
103  unittest.main()
def test_fmt_si_bin_minus(self, value, decimals, expected)
def test_fmt_si_plus_fullrange(self, value, decimals, expected)
def test_fmt_si_sub_minus(self, value, decimals, expected)
def test_fmt_units_per_interval(self, units, seconds, unit, expected)
def test_fmt_si_binary_sub_minus(self, value, decimals, expected)
def test_fmt_si_minus_fullrange(self, value, decimals, expected)
def test_fmt_si_binary_sub_plus(self, value, decimals, expected)
def test_fmt_units_per_interval_negative_duration_exception(self, units, seconds, unit)