Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New tests for human readable values #1623

Merged
merged 8 commits into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions beets/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,13 @@ def input_yn(prompt, require=False):

def human_bytes(size):
"""Formats size, a number of bytes, in a human-readable way."""
suffices = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'HB']
for suffix in suffices:
powers = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'H']
unit = 'B'
for power in powers:
if size < 1024:
return "%3.1f %s" % (size, suffix)
return "%3.1f %s%s" % (size, power, unit)
size /= 1024.0
unit = 'iB'
return "big"


Expand Down
6 changes: 3 additions & 3 deletions test/test_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,14 +1012,14 @@ def test_summarize_items(self):

i2 = deepcopy(self.item)
summary = commands.summarize_items([self.item, i2], False)
self.assertEqual(summary, "2 items, F, 4kbps, 21:48, 1.9 KB")
self.assertEqual(summary, "2 items, F, 4kbps, 21:48, 1.9 KiB")

i2.format = "G"
summary = commands.summarize_items([self.item, i2], False)
self.assertEqual(summary, "2 items, F 1, G 1, 4kbps, 21:48, 1.9 KB")
self.assertEqual(summary, "2 items, F 1, G 1, 4kbps, 21:48, 1.9 KiB")

summary = commands.summarize_items([self.item, i2, i2], False)
self.assertEqual(summary, "3 items, G 2, F 1, 4kbps, 32:42, 2.9 KB")
self.assertEqual(summary, "3 items, G 2, F 1, 4kbps, 32:42, 2.9 KiB")


class PathFormatTest(_common.TestCase):
Expand Down
67 changes: 67 additions & 0 deletions test/test_ui_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf8 -*-
# This file is part of beets.
# Copyright 2015, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

"""Test module for file ui/__init__.py
"""
from test import _common
from test._common import unittest

from beets import ui


class InitTest(_common.LibTestCase):
def setUp(self):
super(InitTest, self).setUp()

def test_human_bytes(self):
tests = [
(0, '0.0 B'),
(30, '30.0 B'),
(pow(2, 10), '1.0 KiB'),
(pow(2, 20), '1.0 MiB'),
(pow(2, 30), '1.0 GiB'),
(pow(2, 40), '1.0 TiB'),
(pow(2, 50), '1.0 PiB'),
(pow(2, 60), '1.0 EiB'),
(pow(2, 70), '1.0 ZiB'),
(pow(2, 80), '1.0 YiB'),
(pow(2, 90), '1.0 HiB'),
(pow(2, 100), 'big'),
]
for i, h in tests:
self.assertEqual(h, ui.human_bytes(i))

def test_human_seconds(self):
tests = [
(0, '0.0 seconds'),
(30, '30.0 seconds'),
(60, '1.0 minutes'),
(90, '1.5 minutes'),
(125, '2.1 minutes'),
(3600, '1.0 hours'),
(86400, '1.0 days'),
(604800, '1.0 weeks'),
(31449600, '1.0 years'),
(314496000, '1.0 decades'),
]
for i, h in tests:
self.assertEqual(h, ui.human_seconds(i))


def suite():
return unittest.TestLoader().loadTestsFromName(__name__)

if __name__ == b'__main__':
unittest.main(defaultTest='suite')