source: trunk/src/allmydata/test/test_abbreviate.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: 5.4 KB
Line 
1"""
2Tests for allmydata.util.abbreviate.
3
4Ported to Python 3.
5"""
6
7from datetime import timedelta
8
9from twisted.trial import unittest
10
11from allmydata.util import abbreviate
12
13
14class Abbreviate(unittest.TestCase):
15    def test_abbrev_time_1s(self):
16        diff = timedelta(seconds=1)
17        s = abbreviate.abbreviate_time(diff)
18        self.assertEqual('1 second ago', s)
19
20    def test_abbrev_time_25s(self):
21        diff = timedelta(seconds=25)
22        s = abbreviate.abbreviate_time(diff)
23        self.assertEqual('25 seconds ago', s)
24
25    def test_abbrev_time_future_5_minutes(self):
26        diff = timedelta(minutes=-5)
27        s = abbreviate.abbreviate_time(diff)
28        self.assertEqual('5 minutes in the future', s)
29
30    def test_abbrev_time_hours(self):
31        diff = timedelta(hours=4)
32        s = abbreviate.abbreviate_time(diff)
33        self.assertEqual('4 hours ago', s)
34
35    def test_abbrev_time_day(self):
36        diff = timedelta(hours=49)  # must be more than 2 days
37        s = abbreviate.abbreviate_time(diff)
38        self.assertEqual('2 days ago', s)
39
40    def test_abbrev_time_month(self):
41        diff = timedelta(days=91)
42        s = abbreviate.abbreviate_time(diff)
43        self.assertEqual('3 months ago', s)
44
45    def test_abbrev_time_year(self):
46        diff = timedelta(weeks=(5 * 52) + 1)
47        s = abbreviate.abbreviate_time(diff)
48        self.assertEqual('5 years ago', s)
49
50    def test_time(self):
51        a = abbreviate.abbreviate_time
52        self.failUnlessEqual(a(None), "unknown")
53        self.failUnlessEqual(a(0), "0 seconds")
54        self.failUnlessEqual(a(1), "1 second")
55        self.failUnlessEqual(a(2), "2 seconds")
56        self.failUnlessEqual(a(119), "119 seconds")
57        MIN = 60
58        self.failUnlessEqual(a(2*MIN), "2 minutes")
59        self.failUnlessEqual(a(60*MIN), "60 minutes")
60        self.failUnlessEqual(a(179*MIN), "179 minutes")
61        HOUR = 60*MIN
62        self.failUnlessEqual(a(180*MIN), "3 hours")
63        self.failUnlessEqual(a(4*HOUR), "4 hours")
64        DAY = 24*HOUR
65        MONTH = 30*DAY
66        self.failUnlessEqual(a(2*DAY), "2 days")
67        self.failUnlessEqual(a(2*MONTH), "2 months")
68        YEAR = 365*DAY
69        self.failUnlessEqual(a(5*YEAR), "5 years")
70
71    def test_space(self):
72        tests_si = [(None, "unknown"),
73                    (0, "0 B"),
74                    (1, "1 B"),
75                    (999, "999 B"),
76                    (1000, "1000 B"),
77                    (1023, "1023 B"),
78                    (1024, "1.02 kB"),
79                    (20*1000, "20.00 kB"),
80                    (1024*1024, "1.05 MB"),
81                    (1000*1000, "1.00 MB"),
82                    (1000*1000*1000, "1.00 GB"),
83                    (1000*1000*1000*1000, "1.00 TB"),
84                    (1000*1000*1000*1000*1000, "1.00 PB"),
85                    (1000*1000*1000*1000*1000*1000, "1.00 EB"),
86                    (1234567890123456789, "1.23 EB"),
87                    ]
88        for (x, expected) in tests_si:
89            got = abbreviate.abbreviate_space(x, SI=True)
90            self.failUnlessEqual(got, expected)
91
92        tests_base1024 = [(None, "unknown"),
93                          (0, "0 B"),
94                          (1, "1 B"),
95                          (999, "999 B"),
96                          (1000, "1000 B"),
97                          (1023, "1023 B"),
98                          (1024, "1.00 kiB"),
99                          (20*1024, "20.00 kiB"),
100                          (1000*1000, "976.56 kiB"),
101                          (1024*1024, "1.00 MiB"),
102                          (1024*1024*1024, "1.00 GiB"),
103                          (1024*1024*1024*1024, "1.00 TiB"),
104                          (1000*1000*1000*1000*1000, "909.49 TiB"),
105                          (1024*1024*1024*1024*1024, "1.00 PiB"),
106                          (1024*1024*1024*1024*1024*1024, "1.00 EiB"),
107                          (1234567890123456789, "1.07 EiB"),
108                    ]
109        for (x, expected) in tests_base1024:
110            got = abbreviate.abbreviate_space(x, SI=False)
111            self.failUnlessEqual(got, expected)
112
113        self.failUnlessEqual(abbreviate.abbreviate_space_both(1234567),
114                             "(1.23 MB, 1.18 MiB)")
115
116    def test_parse_space(self):
117        p = abbreviate.parse_abbreviated_size
118        self.failUnlessEqual(p(""), None)
119        self.failUnlessEqual(p(None), None)
120        self.failUnlessEqual(p("123"), 123)
121        self.failUnlessEqual(p("123B"), 123)
122        self.failUnlessEqual(p("2K"), 2000)
123        self.failUnlessEqual(p("2kb"), 2000)
124        self.failUnlessEqual(p("2KiB"), 2048)
125        self.failUnlessEqual(p("10MB"), 10*1000*1000)
126        self.failUnlessEqual(p("10MiB"), 10*1024*1024)
127        self.failUnlessEqual(p("5G"), 5*1000*1000*1000)
128        self.failUnlessEqual(p("4GiB"), 4*1024*1024*1024)
129        self.failUnlessEqual(p("3TB"), 3*1000*1000*1000*1000)
130        self.failUnlessEqual(p("3TiB"), 3*1024*1024*1024*1024)
131        self.failUnlessEqual(p("6PB"), 6*1000*1000*1000*1000*1000)
132        self.failUnlessEqual(p("6PiB"), 6*1024*1024*1024*1024*1024)
133        self.failUnlessEqual(p("9EB"), 9*1000*1000*1000*1000*1000*1000)
134        self.failUnlessEqual(p("9EiB"), 9*1024*1024*1024*1024*1024*1024)
135
136        e = self.failUnlessRaises(ValueError, p, "12 cubits")
137        self.failUnlessIn("12 cubits", str(e))
138        e = self.failUnlessRaises(ValueError, p, "1 BB")
139        self.failUnlessIn("1 BB", str(e))
140        e = self.failUnlessRaises(ValueError, p, "fhtagn")
141        self.failUnlessIn("fhtagn", str(e))
Note: See TracBrowser for help on using the repository browser.