From efbb2665fe4bd47519d63c16a07eee2d078a670c Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Mon, 26 Aug 2019 17:08:00 -0400 Subject: [PATCH] Improve test coverage --- nilmdb/utils/diskusage.py | 9 ++++----- tests/test_misc.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/nilmdb/utils/diskusage.py b/nilmdb/utils/diskusage.py index 90d42fb..b6049e3 100644 --- a/nilmdb/utils/diskusage.py +++ b/nilmdb/utils/diskusage.py @@ -11,10 +11,9 @@ def human_size(num): unit, num_decimals = unit_list[exponent] format_string = '{:.%sf} {}' % (num_decimals) return format_string.format(quotient, unit) - if num == 0: # pragma: no cover - return '0 bytes' - if num == 1: # pragma: no cover + elif num == 1: return '1 byte' + return '0 bytes' def du(path): """Like du -sb, returns total size of path in bytes. Ignore @@ -28,7 +27,7 @@ def du(path): filepath = os.path.join(path, thisfile) size += du(filepath) return size - except OSError as e: # pragma: no cover - if e.errno != errno.ENOENT: + except OSError as e: + if e.errno != errno.ENOENT: # pragma: no cover raise return 0 diff --git a/tests/test_misc.py b/tests/test_misc.py index 5e2098c..1e7b818 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -70,3 +70,26 @@ class TestMisc(object): nilmdb.utils.fallocate.punch_hole("/", 1, 1, False) # no exception because we ignore errors by default nilmdb.utils.fallocate.punch_hole(fn, 1, -1) + + def test_diskusage(self): + hs = nilmdb.utils.diskusage.human_size + eq_(hs(0), "0 bytes") + eq_(hs(1), "1 byte") + eq_(hs(1023), "1023 bytes") + + eq_(hs(1024), "1 kiB") + + eq_(hs(1048575), "1024 kiB") + eq_(hs(1048576), "1.0 MiB") + + eq_(hs(1073741823), "1024.0 MiB") + eq_(hs(1073741824), "1.00 GiB") + + eq_(hs(1099511627775), "1024.00 GiB") + eq_(hs(1099511627776), "1.00 TiB") + + eq_(hs(1099511627776 * 5000.1234), "5000.12 TiB") + + nilmdb.utils.diskusage.du("/dev") + with assert_raises(OSError): + nilmdb.utils.diskusage.du("/dev/null/bogus")