Merge lp:~ralsina/ubuntuone-control-panel/fix_804271 into lp:ubuntuone-control-panel

Proposed by Roberto Alsina
Status: Merged
Approved by: Roberto Alsina
Approved revision: 182
Merged at revision: 175
Proposed branch: lp:~ralsina/ubuntuone-control-panel/fix_804271
Merge into: lp:ubuntuone-control-panel
Diff against target: 67 lines (+52/-2)
2 files modified
ubuntuone/controlpanel/gui/__init__.py (+5/-2)
ubuntuone/controlpanel/gui/qt/tests/test_humanize.py (+47/-0)
To merge this branch: bzr merge lp:~ralsina/ubuntuone-control-panel/fix_804271
Reviewer Review Type Date Requested Status
John Lenton (community) Approve
Review via email: mp+66595@code.launchpad.net

Commit message

Fix corner cases for humanize, add tests.

Description of the change

Fix corner cases for humanize:

humanize(0) was "0.0 bytes" should be "0 bytes"

Added some minimal tests.

To post a comment you must log in.
180. By Roberto Alsina

Chipaca's needsfixing

181. By Roberto Alsina

Use floating point division

182. By Roberto Alsina

Improve tests

Revision history for this message
John Lenton (chipaca) wrote :

A small nit in that the docstring of test_decimal doesn't have much relation to what it's testing (what it's testing is unclear to me, however).

review: Approve
Revision history for this message
Roberto Alsina (ralsina) wrote :

It's testing that we keep one decimal when we should (in this case, 12.1 KiB instead of 12 KiB).
This is a test that failed with the original code that did integer divisions.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntuone/controlpanel/gui/__init__.py'
2--- ubuntuone/controlpanel/gui/__init__.py 2011-06-28 22:07:59 +0000
3+++ ubuntuone/controlpanel/gui/__init__.py 2011-07-04 11:10:00 +0000
4@@ -166,6 +166,9 @@
5 5: 'PiB', 6: 'Eib', 7: 'ZiB', 8: 'YiB'}
6 unit = 0
7 while int_bytes >= KILOBYTES:
8- int_bytes = int_bytes / KILOBYTES
9+ int_bytes = int_bytes / float(KILOBYTES)
10 unit += 1
11- return '%.1f %s' % (int_bytes, units[unit])
12+ str_bytes = "%.1f" % int_bytes
13+ str_bytes = str_bytes.rstrip('0')
14+ str_bytes = str_bytes.rstrip('.')
15+ return '%s %s' % (str_bytes, units[unit])
16
17=== added file 'ubuntuone/controlpanel/gui/qt/tests/test_humanize.py'
18--- ubuntuone/controlpanel/gui/qt/tests/test_humanize.py 1970-01-01 00:00:00 +0000
19+++ ubuntuone/controlpanel/gui/qt/tests/test_humanize.py 2011-07-04 11:10:00 +0000
20@@ -0,0 +1,47 @@
21+# -*- coding: utf-8 -*-
22+
23+# Authors: Roberto Alsina <alecu@canonical.com>
24+#
25+# Copyright 2011 Canonical Ltd.
26+#
27+# This program is free software: you can redistribute it and/or modify it
28+# under the terms of the GNU General Public License version 3, as published
29+# by the Free Software Foundation.
30+#
31+# This program is distributed in the hope that it will be useful, but
32+# WITHOUT ANY WARRANTY; without even the implied warranties of
33+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
34+# PURPOSE. See the GNU General Public License for more details.
35+#
36+# You should have received a copy of the GNU General Public License along
37+# with this program. If not, see <http://www.gnu.org/licenses/>.
38+
39+"""Tests for the humanize function."""
40+
41+from ubuntuone.controlpanel.tests import TestCase
42+from ubuntuone.controlpanel.gui import humanize
43+
44+
45+class HumanizeTestCase(TestCase):
46+
47+ """Test cases for the humanize function."""
48+
49+ def test_zero(self):
50+ """Humanize zero bytes."""
51+ self.assertEquals(humanize(0), "0 bytes")
52+
53+ def test_exact(self):
54+ """Humanize should not show unnecesary digits."""
55+ self.assertEquals(humanize(1024), "1 KiB")
56+
57+ def test_huge(self):
58+ """Humanize should work for big numbers."""
59+ self.assertEquals(humanize(2 ** 40), "1 TiB")
60+
61+ def test_decimal(self):
62+ """Humanize should work for numbers ending in 0."""
63+ self.assertEquals(humanize(12400), "12.1 KiB")
64+
65+ def test_trailing_zero(self):
66+ """Humanize should work for numbers ending in 0."""
67+ self.assertEquals(humanize(102400), "100 KiB")

Subscribers

People subscribed via source and target branches