From 1bd176848a0fd11222ae7832043e8f77c249895e Mon Sep 17 00:00:00 2001 From: Rob Hallam <0504004h@student.gla.ac.uk> Date: Thu, 22 Aug 2024 12:18:30 +0100 Subject: [PATCH] test [utils] add Interval tests Covers: - to_json - move_end - update_duration - unhappy path exceptions (mostly init) --- test/test_utils.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/test_utils.py b/test/test_utils.py index 185d766..4876366 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -155,6 +155,11 @@ class TestInterval(unittest.TestCase): self.assertTrue(interval3 > interval2) # interval3 start is larger self.assertTrue(interval3 < interval4) # same start, interval3 has smaller end + def test_to_json(self): + """Test to_json method""" + interval = utils.Interval(start=self.start, end=self.end) + self.assertEqual(interval.to_json(), {"start": self.start, "end": self.end, "duration": self.duration}) + def test_move_start(self): """Test the move_start method - changes start time to time specified, keeps end time constant""" interval = utils.Interval(start=self.start, end=self.end) @@ -171,6 +176,44 @@ class TestInterval(unittest.TestCase): self.assertEqual(interval.end, 10) self.assertEqual(interval.duration, 8) + def test_move_end(self): + """Test the move_end method - changes end time to time specified, keeps start time constant""" + interval = utils.Interval(start=self.start, end=self.end) + interval.move_end(15) + self.assertEqual(interval.start, 0) + self.assertEqual(interval.end, 15) + self.assertEqual(interval.duration, 15) + + def test_update_duration(self): + """Test the update_duration method - changes duration to time specified, keeps start time constant""" + interval = utils.Interval(start=self.start, end=self.end) + interval.update_duration(15) + self.assertEqual(interval.start, 0) + self.assertEqual(interval.end, 15) + self.assertEqual(interval.duration, 15) + # test with relative=True + interval.update_duration(5, relative=True) + self.assertEqual(interval.start, 0) + + # Unhappy path tests + + def test_init_no_start_end(self): + with self.assertRaises(ValueError): + utils.Interval() + + def test_init_start_end_duration(self): + with self.assertRaises(ValueError): + utils.Interval(start=self.start, end=self.end, duration=self.duration) + + def test_init_start_after_end(self): + with self.assertRaises(ValueError): + utils.Interval(start=10, end=0) + + def test_init_negative_duration(self): + with self.assertRaises(ValueError): + utils.Interval(start=0, duration=-10) + with self.assertRaises(ValueError): + utils.Interval(end=0, duration=-10) if __name__ == "__main__":