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