-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtest_progress.py
More file actions
119 lines (94 loc) · 4 KB
/
test_progress.py
File metadata and controls
119 lines (94 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""Module progress tests"""
import unittest
from xblock.progress import Progress
class ProgressTest(unittest.TestCase):
"""Test that basic Progress objects work. A Progress represents a
fraction between 0 and 1.
"""
not_started = Progress(0, 17)
part_done = Progress(2, 6)
half_done = Progress(3, 6)
also_half_done = Progress(1, 2)
done = Progress(7, 7)
def test_create_object(self):
"""Test creating Progress objects with valid and invalid inputs."""
# These should work:
prg1 = Progress(0, 2) # pylint: disable=unused-variable
prg2 = Progress(1, 2) # pylint: disable=unused-variable
prg3 = Progress(2, 2) # pylint: disable=unused-variable
prg4 = Progress(2.5, 5.0) # pylint: disable=unused-variable
prg5 = Progress(3.7, 12.3333) # pylint: disable=unused-variable
# These shouldn't
self.assertRaises(ValueError, Progress, 0, 0)
self.assertRaises(ValueError, Progress, 2, 0)
self.assertRaises(ValueError, Progress, 1, -2)
self.assertRaises(TypeError, Progress, 0, "all")
# check complex numbers just for the heck of it :)
self.assertRaises(TypeError, Progress, 2j, 3)
def test_clamp(self):
"""Test that Progress clamps values to the valid range."""
assert (2, 2) == Progress(3, 2).frac()
assert (0, 2) == Progress((-2), 2).frac()
def test_frac(self):
"""Test that `frac()` returns the numerator and denominator correctly."""
prg = Progress(1, 2)
(a_mem, b_mem) = prg.frac()
assert a_mem == 1
assert b_mem == 2
def test_percent(self):
"""Test that `percent()` returns the correct completion percentage."""
assert self.not_started.percent() == 0
assert round(self.part_done.percent() - 33.33333333333333, 7) >= 0
assert self.half_done.percent() == 50
assert self.done.percent() == 100
assert self.half_done.percent() == self.also_half_done.percent()
def test_started(self):
"""Test that `started()` correctly identifies if progress has begun."""
assert not self.not_started.started()
assert self.part_done.started()
assert self.half_done.started()
assert self.done.started()
def test_inprogress(self):
"""Test that `inprogress()` correctly identifies ongoing progress."""
# only true if working on it
assert not self.done.inprogress()
assert not self.not_started.inprogress()
assert self.part_done.inprogress()
assert self.half_done.inprogress()
def test_done(self):
"""Test that `done()` correctly identifies completed progress."""
assert self.done.done()
assert not self.half_done.done()
assert not self.not_started.done()
def test_str(self):
"""Test that `__str__()` formats progress as 'numerator/denominator' correctly."""
assert str(self.not_started) == "0/17"
assert str(self.part_done) == "2/6"
assert str(self.done) == "7/7"
assert str(Progress(2.1234, 7)) == "2.12/7"
assert str(Progress(2.0034, 7)) == "2/7"
assert str(Progress(0.999, 7)) == "1/7"
def test_add(self):
"""Test the Progress.add_counts() method"""
prg1 = Progress(0, 2)
prg2 = Progress(1, 3)
prg3 = Progress(2, 5)
prg_none = None
def add(a, b):
return Progress.add_counts(a, b).frac()
assert add(prg1, prg1) == (0, 4)
assert add(prg1, prg2) == (1, 5)
assert add(prg2, prg3) == (3, 8)
assert add(prg2, prg_none) == prg2.frac()
assert add(prg_none, prg2) == prg2.frac()
def test_equality(self):
"""Test that comparing Progress objects for equality
works correctly."""
prg1 = Progress(1, 2)
prg2 = Progress(2, 4)
prg3 = Progress(1, 2)
assert prg1 == prg3
assert prg1 != prg2
# Check != while we're at it
assert prg1 != prg2
assert prg1 == prg3