Skip to content

Commit 99adb97

Browse files
authored
Make human_readable_filesize assertions locale-aware (#564)
* Make human_readable_filesize assertions locale-aware * Use 'self._fmt' directly as suggested by @jonasbardino
1 parent 98a815b commit 99adb97

1 file changed

Lines changed: 29 additions & 11 deletions

File tree

tests/test_mig_lib_accounting.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
"""Unit tests for the migrid module pointed to in the filename"""
2929

30+
import locale
3031
import os
3132
import pickle
3233

@@ -230,12 +231,25 @@ def test_accounting(self):
230231
total_bytes = test_user_accounting.get('total_bytes', 0)
231232
self.assertEqual(total_bytes, TEST_TOTAL_BYTES)
232233

234+
def _fmt(self, value, decimals=3):
235+
"""Return a locale-aware fixed-point string matching human_readable_filesize output.
236+
237+
human_readable_filesize uses locale.format_string("%.*f", ...) internally,
238+
so we use the same call here to guarantee the expected decimal separator
239+
matches regardless of the active locale (e.g. ',' on da_DK/de_DE).
240+
"""
241+
return locale.format_string("%.*f", (decimals, value))
242+
233243
def test_human_readable_filesize_valid(self):
234244
"""Test human-friendly format helper success on valid byte sizes"""
235-
valid = [(0, "0 B"), (1, "1.000 B"), (42, "42.000 B"),
236-
(2**10, "1.000 KiB"), (2**30, "1.000 GiB"),
237-
(2**50, "1.000 PiB"), (2**89, "512.000 YiB"),
238-
(2**90 - 2**70, "1023.999 YiB")]
245+
valid = [(0, "0 B"),
246+
(1, "%s B" % self._fmt(1.0)),
247+
(42, "%s B" % self._fmt(42.0)),
248+
(2**10, "%s KiB" % self._fmt(1.0)),
249+
(2**30, "%s GiB" % self._fmt(1.0)),
250+
(2**50, "%s PiB" % self._fmt(1.0)),
251+
(2**89, "%s YiB" % self._fmt(512.0)),
252+
(2**90 - 2**70, "%s YiB" % self._fmt(1023.999))]
239253
for (size, expect) in valid:
240254
self.assertEqual(human_readable_filesize(size), expect)
241255

@@ -248,26 +262,30 @@ def test_human_readable_filesize_invalid(self):
248262

249263
def test_human_readable_filesize_decimals(self):
250264
"""Test human-friendly format helper with custom decimal count"""
251-
cases = [(2**10, 0, "1 KiB"),
252-
(2**10, 1, "1.0 KiB"),
253-
(2**10, 2, "1.00 KiB"),
254-
(2**10, 5, "1.00000 KiB")]
265+
cases = [(2**10, 0, "%s KiB" % self._fmt(1, decimals=0)),
266+
(2**10, 1, "%s KiB" % self._fmt(1.0, decimals=1)),
267+
(2**10, 2, "%s KiB" % self._fmt(1.0, decimals=2)),
268+
(2**10, 5, "%s KiB" % self._fmt(1.0, decimals=5))]
255269
for (size, decimals, expect) in cases:
256270
self.assertEqual(human_readable_filesize(size, decimals=decimals),
257271
expect)
258272

259273
def test_human_readable_filesize_si_format(self):
260274
"""Test human-friendly format helper with SI byte units (powers of 1000)"""
261275
# SI exponent is still determined by log2, so values are based on 2**N
262-
valid = [(0, "0 B"), (42, "42.000 B"), (2**10, "1.024 KB"),
263-
(2**20, "1.049 MB"), (2**30, "1.074 GB"), (2**50, "1.126 PB")]
276+
valid = [(0, "0 B"),
277+
(42, "%s B" % self._fmt(42.0)),
278+
(2**10, "%s KB" % self._fmt(1.024)),
279+
(2**20, "%s MB" % self._fmt(1.049)),
280+
(2**30, "%s GB" % self._fmt(1.074)),
281+
(2**50, "%s PB" % self._fmt(1.126))]
264282
for (size, expect) in valid:
265283
self.assertEqual(
266284
human_readable_filesize(size, si_byte_format=True), expect)
267285
# decimals and si_byte_format compose correctly
268286
self.assertEqual(
269287
human_readable_filesize(2**10, decimals=1, si_byte_format=True),
270-
"1.0 KB")
288+
"%s KB" % self._fmt(1.0, decimals=1))
271289

272290
def test_get_usage_decimals(self):
273291
"""Test get_usage passes decimals parameter through to report strings"""

0 commit comments

Comments
 (0)