From bb540b29cda071c761e0b6a14a13788df0bfc6df Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sat, 28 Aug 2021 20:43:57 -0700 Subject: [PATCH 1/2] Add tests for ScheduleItem.get_duration() --- wafer/schedule/tests/test_models.py | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/wafer/schedule/tests/test_models.py b/wafer/schedule/tests/test_models.py index 56bfa34d..c63a3a3d 100644 --- a/wafer/schedule/tests/test_models.py +++ b/wafer/schedule/tests/test_models.py @@ -377,6 +377,42 @@ def test_invalid_slot_block(self): self.assertRaises(ValidationError, slot4.clean) +class ScheduleItemTests(TestCase): + def setUp(self): + utc = timezone.utc + self.blocks = [ + ScheduleBlock.objects.create( + start_time=D.datetime(2013, 9, 22, 9, 0, 0, tzinfo=utc), + end_time=D.datetime(2013, 9, 22, 19, 0, 0, tzinfo=utc)), + ] + self.slots = [ + Slot.objects.create( + start_time=D.datetime(2013, 9, 22, 9, 0, 0, tzinfo=utc), + end_time=D.datetime(2013, 9, 22, 9, 20, 0, tzinfo=utc)), + Slot.objects.create( + start_time=D.datetime(2013, 9, 22, 9, 30, 0, tzinfo=utc), + end_time=D.datetime(2013, 9, 22, 9, 50, 0, tzinfo=utc)), + ] + self.venues = [ + Venue.objects.create(order=1, name='Venue 1'), + ] + self.pages = make_pages(1) + + def test_duration_minutes(self): + """Test events spanning multiple slots with a gap in between""" + item = make_items(self.venues, self.pages)[0] + # Single Slot + item.slots.add(self.slots[0]) + self.assertEqual(item.get_duration_minutes(), 20) + + def test_get_duration(self): + """Test ScheduleItem.get_duration()""" + item = make_items(self.venues, self.pages)[0] + # Single Slot + item.slots.add(self.slots[0]) + self.assertEqual(item.get_duration(), {'minutes': 20, 'hours': 0}) + + class LastUpdateTests(TestCase): def setUp(self): From 20f43eead01e71d3c89d409c91091f910cd871d8 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Sat, 28 Aug 2021 20:47:28 -0700 Subject: [PATCH 2/2] Count gaps between Slots in ScheduleItem.get_duration() --- wafer/schedule/models.py | 32 ++++++++++++++--------------- wafer/schedule/tests/test_models.py | 3 +++ 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/wafer/schedule/models.py b/wafer/schedule/models.py index da40dc46..b2c6b3e9 100644 --- a/wafer/schedule/models.py +++ b/wafer/schedule/models.py @@ -333,26 +333,24 @@ def __str__(self): def get_duration(self): """Return the total duration of the item. - This is the sum of all the slot durations.""" - # This is intended for the pentabarf xml file - # It will do the wrong thing if the slots aren't - # contigious, which we should address sometime. - slots = list(self.slots.all()) - result = {'hours': 0, 'minutes': 0} - if slots: - for slot in slots: - dur = slot.get_duration() - result['hours'] += dur['hours'] - result['minutes'] += dur['minutes'] - # Normalise again - hours, result['minutes'] = divmod(result['minutes'], 60) - result['hours'] += hours - return result + This is the time from start to end, including any discontinuities + between slots.""" + minutes = self.get_duration_minutes() + hours, minutes = divmod(minutes, 60) + return { + 'hours': hours, + 'minutes': minutes, + } def get_duration_minutes(self): """Return the duration in total number of minutes.""" - duration = self.get_duration() - return int(duration['hours'] * 60 + duration['minutes']) + slots = list(self.slots.all()) + if not slots: + return 0 + start = slots[0].get_start_time() + end = slots[-1].end_time + duration = end - start + return duration.total_seconds() // 60 @property def guid(self): diff --git a/wafer/schedule/tests/test_models.py b/wafer/schedule/tests/test_models.py index c63a3a3d..2d3375b8 100644 --- a/wafer/schedule/tests/test_models.py +++ b/wafer/schedule/tests/test_models.py @@ -404,6 +404,9 @@ def test_duration_minutes(self): # Single Slot item.slots.add(self.slots[0]) self.assertEqual(item.get_duration_minutes(), 20) + # Spanning 2 slots with a gap in-between + item.slots.add(self.slots[1]) + self.assertEqual(item.get_duration_minutes(), 50) def test_get_duration(self): """Test ScheduleItem.get_duration()"""