source: trunk/src/allmydata/test/web/test_util.py

Last change on this file was 1cfe843d, checked in by Alexandre Detiste <alexandre.detiste@…>, at 2024-02-22T23:40:25Z

more python2 removal

  • Property mode set to 100644
File size: 4.4 KB
Line 
1"""
2Ported to Python 3.
3"""
4
5from twisted.trial import unittest
6from allmydata.web import status, common
7from allmydata.dirnode import ONLY_FILES
8from ..common import ShouldFailMixin
9from .. import common_util as testutil
10
11class Util(ShouldFailMixin, testutil.ReallyEqualMixin, unittest.TestCase):
12
13    def test_parse_replace_arg(self):
14        self.failUnlessReallyEqual(common.parse_replace_arg(b"true"), True)
15        self.failUnlessReallyEqual(common.parse_replace_arg(b"false"), False)
16        self.failUnlessReallyEqual(common.parse_replace_arg(b"only-files"),
17                                   ONLY_FILES)
18        self.failUnlessRaises(common.WebError, common.parse_replace_arg, b"only_fles")
19
20    def test_abbreviate_time(self):
21        self.failUnlessReallyEqual(common.abbreviate_time(None), "")
22        self.failUnlessReallyEqual(common.abbreviate_time(1.234), "1.23s")
23        self.failUnlessReallyEqual(common.abbreviate_time(0.123), "123ms")
24        self.failUnlessReallyEqual(common.abbreviate_time(0.00123), "1.2ms")
25        self.failUnlessReallyEqual(common.abbreviate_time(0.000123), "123us")
26        self.failUnlessReallyEqual(common.abbreviate_time(-123000), "-123000000000us")
27        self.failUnlessReallyEqual(common.abbreviate_time(2.5), "2.50s")
28        self.failUnlessReallyEqual(common.abbreviate_time(0.25), "250ms")
29        self.failUnlessReallyEqual(common.abbreviate_time(0.0021), "2.1ms")
30
31        self.failUnlessReallyEqual(common.abbreviate_time(None), "")
32        self.failUnlessReallyEqual(common.abbreviate_time(2.5), "2.50s")
33        self.failUnlessReallyEqual(common.abbreviate_time(0.25), "250ms")
34        self.failUnlessReallyEqual(common.abbreviate_time(0.0021), "2.1ms")
35        self.failUnlessReallyEqual(common.abbreviate_time(0.000123), "123us")
36        self.failUnlessReallyEqual(common.abbreviate_rate(None), "")
37        self.failUnlessReallyEqual(common.abbreviate_rate(2500000), "2.50MBps")
38        self.failUnlessReallyEqual(common.abbreviate_rate(30100), "30.1kBps")
39        self.failUnlessReallyEqual(common.abbreviate_rate(123), "123Bps")
40
41    def test_compute_rate(self):
42        self.failUnlessReallyEqual(common.compute_rate(None, None), None)
43        self.failUnlessReallyEqual(common.compute_rate(None, 1), None)
44        self.failUnlessReallyEqual(common.compute_rate(250000, None), None)
45        self.failUnlessReallyEqual(common.compute_rate(250000, 0), None)
46        self.failUnlessReallyEqual(common.compute_rate(250000, 10), 25000.0)
47        self.failUnlessReallyEqual(common.compute_rate(0, 10), 0.0)
48        self.shouldFail(AssertionError, "test_compute_rate", "",
49                        common.compute_rate, -100, 10)
50        self.shouldFail(AssertionError, "test_compute_rate", "",
51                        common.compute_rate, 100, -10)
52
53        # Sanity check
54        rate = common.compute_rate(10*1000*1000, 1)
55        self.failUnlessReallyEqual(common.abbreviate_rate(rate), "10.00MBps")
56
57    def test_abbreviate_rate(self):
58        self.failUnlessReallyEqual(common.abbreviate_rate(None), "")
59        self.failUnlessReallyEqual(common.abbreviate_rate(1234000), "1.23MBps")
60        self.failUnlessReallyEqual(common.abbreviate_rate(12340), "12.3kBps")
61        self.failUnlessReallyEqual(common.abbreviate_rate(123), "123Bps")
62        self.failUnlessReallyEqual(common.abbreviate_rate(2500000), "2.50MBps")
63        self.failUnlessReallyEqual(common.abbreviate_rate(30100), "30.1kBps")
64        self.failUnlessReallyEqual(common.abbreviate_rate(123), "123Bps")
65
66    def test_abbreviate_size(self):
67        self.failUnlessReallyEqual(common.abbreviate_size(None), "")
68        self.failUnlessReallyEqual(common.abbreviate_size(1.23*1000*1000*1000), "1.23GB")
69        self.failUnlessReallyEqual(common.abbreviate_size(1.23*1000*1000), "1.23MB")
70        self.failUnlessReallyEqual(common.abbreviate_size(1230), "1.2kB")
71        self.failUnlessReallyEqual(common.abbreviate_size(123), "123B")
72
73    def test_plural(self):
74        def convert(s):
75            return "%d second%s" % (s, status.plural(s))
76        self.failUnlessReallyEqual(convert(0), "0 seconds")
77        self.failUnlessReallyEqual(convert(1), "1 second")
78        self.failUnlessReallyEqual(convert(2), "2 seconds")
79        def convert2(s):
80            return "has share%s: %s" % (status.plural(s), ",".join(s))
81        self.failUnlessReallyEqual(convert2([]), "has shares: ")
82        self.failUnlessReallyEqual(convert2(["1"]), "has share: 1")
83        self.failUnlessReallyEqual(convert2(["1","2"]), "has shares: 1,2")
Note: See TracBrowser for help on using the repository browser.