caellion-python-commons: tests/test_stringutil_formatters.py Source File

caellion-python-commons

test_stringutil_formatters.py
Go to the documentation of this file.
1 # test framework
2 import unittest
3 import pytest
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:
12 # SI formatter
13 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "0"),
14 (0, 3, "0.000"),
15 (1000, 0, "1k"),
16 (1000000, 0, "1M"),
17 (123.456, 3, "123.456"),
18 (1234.56, 3, "1.235k"),
19 (123456, 3, "123.456k"),
20 (123456789, 3, "123.457M"),
21 (999999, 3, "999.999k"),
22 (12345678901234567890123456789, 0, "12346Y"),
23 (12345678901234567890123456789000, 0, "12345679Y"),
24 ],
25 )
26 def test_fmt_si_plus(self, value, decimals, expected):
27 assert fmt.formatSI(value, decimals) == expected
28
29 @pytest.mark.parametrize( "value,expected", [ (0, "0"),
30 (1000, "1k"),
31 (1000000, "1M"),
32 (12345678901234567890123456789, "12346Y"),
33 ],
34 )
35 def test_fmt_si_plus_no_decimals(self, value, expected):
36 assert fmt.formatSI(value) == expected
37
38 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "0"),
39 (0, 3, "0.000"),
40 (-1000, 0, "-1k"),
41 (-1000000, 0, "-1M"),
42 (-123.456, 3, "-123.456"),
43 (-1234.56, 3, "-1.235k"),
44 (-123456, 3, "-123.456k"),
45 (-123456789, 3, "-123.457M"),
46 (-999999, 3, "-999.999k"),
47 (-12345678901234567890123456789, 0, "-12346Y"),
48 ],
49 )
50 def test_fmt_si_minus(self, value, decimals, expected):
51 assert fmt.formatSI(value, decimals) == expected
52
53 # binary SI
54 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "0"),
55 (1024, 0, "1ki"),
56 (1024 * 1024, 0, "1Mi"),
57 (512 * 1024, 3, "512.000ki"),
58 (256520, 0, "251ki"),
59 (256520, 3, "250.508ki"),
60 (10240002, 3, "9.766Mi"),
61 (12345678901234567890123456789, 0, "10212Yi"),
62 ],
63 )
64 def test_fmt_si_bin_plus(self, value, decimals, expected):
65 assert fmt.formatBinarySI(value, decimals) == expected
66
67 @pytest.mark.parametrize( "value,expected", [ (0, "0"),
68 (1024, "1ki"),
69 (1024 * 1024, "1Mi"),
70 (256520, "251ki"),
71 (12345678901234567890123456789, "10212Yi"),
72 ],
73 )
74 def test_fmt_si_bin_plus_no_decimals(self, value, expected):
75 assert fmt.formatBinarySI(value) == expected
76
77 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "0"),
78 (-1024, 0, "-1ki"),
79 (-1024 * 1024, 0, "-1Mi"),
80 (-512 * 1024, 3, "-512.000ki"),
81 (-256520, 0, "-251ki"),
82 (-256520, 3, "-250.508ki"),
83 (-10240002, 3, "-9.766Mi"),
84 (-12345678901234567890123456789, 0, "-10212Yi"),
85 ],
86 )
87 def test_fmt_si_bin_minus(self, value, decimals, expected):
88 assert fmt.formatBinarySI(value, decimals) == expected
89
90 # subvalues
91 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "0"),
92 (0, 3, "0.000"),
93 (0.100, 0, "100m"),
94 (0.000100, 0, "100μ"),
95 (0.000000100, 0, "100n"),
96 (0.0001231, 1, "123.1μ"),
97 (0.135100, 0, "135m"),
98 (1e-50, 0, "0y"),
99 (1e-24, 0, "1y"),
100 (1e-25, 0, "0y"),
101 (9.999e-25, 0, "0y"),
102 ],
103 )
104 def test_fmt_si_sub_plus(self, value, decimals, expected):
105 assert fmt.formatSISubValue(value, decimals) == expected
106
107 @pytest.mark.parametrize( "value,expected", [ (0, "0"),
108 (0.100, "100m"),
109 (0.000100, "100μ"),
110 (0.000000100, "100n"),
111 (0.135100, "135m"),
112 (1e-50, "0y"),
113 ],
114 )
115 def test_fmt_si_sub_plus_no_decimals(self, value, expected):
116 assert fmt.formatSISubValue(value) == expected
117
118 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "0"),
119 (0, 3, "0.000"),
120 (-0.100, 0, "-100m"),
121 (-0.000100, 0, "-100μ"),
122 (-0.000000100, 0, "-100n"),
123 (-0.0001231, 1, "-123.1μ"),
124 (-0.135100, 0, "-135m"),
125 (-1e-50, 0, "-0y"),
126 ],
127 )
128 def test_fmt_si_sub_minus(self, value, decimals, expected):
129 assert fmt.formatSISubValue(value, decimals) == expected
130
131 # subvalues
132 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "BinSubValueError"),
133 (0, 3, "BinSubValueError"),
134 (0.100, 0, "BinSubValueError"),
135 (0.000100, 0, "BinSubValueError"),
136 (0.000000100, 0, "BinSubValueError"),
137 (0.0001231, 1, "BinSubValueError"),
138 (0.135100, 0, "BinSubValueError"),
139 (1e-50, 0, "BinSubValueError"),
140 ],
141 )
142 def test_fmt_si_binary_sub_plus(self, value, decimals, expected):
143 assert fmt.formatSIBinarySubValue(value, decimals) == expected
144
145 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "BinSubValueError"),
146 (0, 3, "BinSubValueError"),
147 (-0.100, 0, "BinSubValueError"),
148 (-0.000100, 0, "BinSubValueError"),
149 (-0.000000100, 0, "BinSubValueError"),
150 (-0.0001231, 1, "BinSubValueError"),
151 (-0.135100, 0, "BinSubValueError"),
152 (-1e-50, 0, "BinSubValueError"),
153 ],
154 )
155 def test_fmt_si_binary_sub_minus(self, value, decimals, expected):
156 assert fmt.formatSIBinarySubValue(value, decimals) == expected
157
158 # full-range formatters
159 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "0"),
160 (0, 3, "0.000"),
161 (1000, 0, "1k"),
162 (1000000, 0, "1M"),
163 (123.456, 3, "123.456"),
164 (1234.56, 3, "1.235k"),
165 (123456, 3, "123.456k"),
166 (123456789, 3, "123.457M"),
167 (999999, 3, "999.999k"),
168 (12345678901234567890123456789, 0, "12346Y"),
169 (0.100, 0, "100m"),
170 (0.000100, 0, "100μ"),
171 (0.000000100, 0, "100n"),
172 (0.0001231, 1, "123.1μ"),
173 (0.135100, 0, "135m"),
174 (1e-50, 0, "0y"),
175 (1, 0, "1"),
176 (1 - 1e-24, 0, "1"),
177 ],
178 )
179 def test_fmt_si_plus_fullrange(self, value, decimals, expected):
180 assert fmt.formatSIFullRange(value, decimals) == expected
181
182 @pytest.mark.parametrize( "value,decimals,expected", [ (0, 0, "0"),
183 (0, 3, "0.000"),
184 (-1000, 0, "-1k"),
185 (-1000000, 0, "-1M"),
186 (-123.456, 3, "-123.456"),
187 (-1234.56, 3, "-1.235k"),
188 (-123456, 3, "-123.456k"),
189 (-123456789, 3, "-123.457M"),
190 (-999999, 3, "-999.999k"),
191 (-12345678901234567890123456789, 0, "-12346Y"),
192 (0.100, 0, "100m"),
193 (0.000100, 0, "100μ"),
194 (0.000000100, 0, "100n"),
195 (0.0001231, 1, "123.1μ"),
196 (0.135100, 0, "135m"),
197 (-1e-50, 0, "-0y"),
198 (-1, 0, "-1"),
199 (-1 + 1e-24, 0, "-1"),
200 ],
201 )
202 def test_fmt_si_minus_fullrange(self, value, decimals, expected):
203 assert fmt.formatSIFullRange(value, decimals) == expected
204
205 @pytest.mark.parametrize( "units,seconds,unit,expected", [ (9.99, 365000 * 86400, "unit", "9.99unit/millennium"),
206 (1.0, 365000 * 86400, "unit", "1.00unit/millennium"),
207 (9.99, 36500 * 86400, "unit", "9.99unit/century"),
208 (1.0, 36500 * 86400, "unit", "1.00unit/century"),
209 (9.99, 365 * 86400, "unit", "9.99unit/year"),
210 (1.0, 365 * 86400, "unit", "1.00unit/year"),
211 (6.99, 7 * 86400.0, "unit", "6.99unit/week"),
212 (1.0, 7 * 86400.0, "unit", "1.00unit/week"),
213 (9.99, 86400.0, "unit", "9.99unit/day"),
214 (1.0, 86400.0, "unit", "1.00unit/day"),
215 (9.99, 3600.0, "unit", "9.99unit/hour"),
216 (1.0, 3600.0, "unit", "1.00unit/hour"),
217 (1.0, 60.0, "unit", "1.00unit/minute"),
218 (1.0, 1.0, "unit", "1.00unit/second"),
219 (0.0, 1.0, "unit", "0.00unit/millennium"),
220 (1.0, 0.001, "unit", "1.00unit/millisecond"),
221 (1.0, 0.000001, "unit", "1.00unit/microsecond"),
222 (-1.0, 0.000001, "unit", "-1.00unit/microsecond"),
223 ],
224 )
225 def test_fmt_units_per_interval(self, units, seconds, unit, expected):
226 assert fmt.formatUnitsPerIntervalDynamic(
227 units, seconds, unit) == expected
228
229 @pytest.mark.parametrize( "units,seconds,unit", [(1.0, -0.000001, "unit"), (1.0, 0, "unit")]
230 )
231 def test_fmt_units_per_interval_negative_duration_exception(
232 self, units, seconds, unit
233 ):
234 with pytest.raises(InvalidDurationException):
235 fmt.formatUnitsPerIntervalDynamic(units, seconds, unit)
236
237
238 if __name__ == "__main__":
239 unittest.main()
240
tests.test_stringutil_formatters.TestStringUtilFormatters
Definition: test_stringutil_formatters.py:11
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_units_per_interval
def test_fmt_units_per_interval(self, units, seconds, unit, expected)
Definition: test_stringutil_formatters.py:225
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_bin_plus
def test_fmt_si_bin_plus(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:64
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_minus_fullrange
def test_fmt_si_minus_fullrange(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:202
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_plus_fullrange
def test_fmt_si_plus_fullrange(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:179
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_sub_plus_no_decimals
def test_fmt_si_sub_plus_no_decimals(self, value, expected)
Definition: test_stringutil_formatters.py:115
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_binary_sub_plus
def test_fmt_si_binary_sub_plus(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:142
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_plus
def test_fmt_si_plus(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:26
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_sub_plus
def test_fmt_si_sub_plus(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:104
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_plus_no_decimals
def test_fmt_si_plus_no_decimals(self, value, expected)
Definition: test_stringutil_formatters.py:35
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_minus
def test_fmt_si_minus(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:50
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_bin_minus
def test_fmt_si_bin_minus(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:87
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_units_per_interval_negative_duration_exception
def test_fmt_units_per_interval_negative_duration_exception(self, units, seconds, unit)
Definition: test_stringutil_formatters.py:233
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_bin_plus_no_decimals
def test_fmt_si_bin_plus_no_decimals(self, value, expected)
Definition: test_stringutil_formatters.py:74
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_sub_minus
def test_fmt_si_sub_minus(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:128
tests.test_stringutil_formatters.TestStringUtilFormatters.test_fmt_si_binary_sub_minus
def test_fmt_si_binary_sub_minus(self, value, decimals, expected)
Definition: test_stringutil_formatters.py:155
caellion.pycommons.stringutil.formatters
This module provides various string formatters.
Definition: formatters.py:1