def test_context_nested(self): p = Progress(num_steps=2) self.assertFalse(p.is_started()) self.assertFalse(p.is_completed()) self.assertEqual(len(p.progress_history), 1) self.assertEqual(p.completion(), 0.0) with p: p.update(msg="do a thing") self.assertEqual(len(p.progress_history), 2) for i in range(5): with p: p.update( msg="do a thing in a loop nested in the other thing") self.assertEqual(len(p.progress_history), 3 + i) self.assertFalse(p.is_completed()) self.assertEqual(len(p.progress_history), 7) self.assertFalse(p.is_completed()) self.assertTrue(p.is_completed()) self.assertEqual(len(p.progress_history), 8) self.assertEqual(p.completion(), 1.0)
def test_context(self): p = Progress(num_steps=2) self.assertFalse(p.is_started()) self.assertFalse(p.is_completed()) self.assertEqual(len(p.progress_history), 1) self.assertEqual(p.completion(), 0.0) with p: p.update(msg="do a thing") self.assertTrue(p.is_started()) self.assertEqual(len(p.progress_history), 2) self.assertEqual(p.completion(), 0.5) self.assertTrue(p.is_completed()) self.assertEqual(len(p.progress_history), 3) self.assertEqual(p.completion(), 1.0)
def test_basic_single_step(self): p = Progress(num_steps=2) self.assertFalse(p.is_started()) self.assertFalse(p.is_completed()) self.assertEqual(len(p.progress_history), 1) self.assertEqual(p.completion(), 0.0) p.update(msg="do a thing") self.assertTrue(p.is_started()) self.assertEqual(len(p.progress_history), 2) self.assertEqual(p.completion(), 0.5) p.mark_complete() self.assertTrue(p.is_completed()) self.assertEqual(len(p.progress_history), 3) self.assertEqual(p.completion(), 1.0)
def test_cancellation(self): p = Progress() p.add_estimated_steps(10) with p: for i in range(10): if i < 6: p.update() if i == 5: p.cancel("nope") self.assertTrue(p.should_cancel) else: with self.assertRaises(RuntimeError): p.update() self.assertFalse(p.is_completed()) self.assertTrue(p.should_cancel)