def test_stop_timer_atomic(self, mock_reset): timer = TaskTimer(worker=self.worker, start_time=self.time) timer.save() time_entries_count = TimeEntry.objects.count() self.assertIsNone(timer.stop_time) with self.assertRaises(Exception): stop_timer(self.worker) # Verify that transaction rolled back. timer.refresh_from_db() self.assertIsNone(timer.stop_time) self.assertEqual(TimeEntry.objects.count(), time_entries_count)
def stop_timer(request): worker = Worker.objects.get(user=request.user) try: if request.method == 'POST': time_entry = time_tracking.stop_timer(worker) serializer = TimeEntrySerializer(time_entry) return serializer.data except TimerError as e: raise BadRequest(e) except Exception as e: logger.exception(e) raise e
def test_stop_timer(self): timer = TaskTimer(worker=self.worker, assignment=self.assignment, start_time=self.time) timer.save() start_time = self.time self.time = self.time + datetime.timedelta(hours=1) time_entry = stop_timer(self.worker) # Time entry object is created correctly. self.assertEqual(time_entry.worker, self.worker) self.assertEqual(time_entry.date, start_time.date()) self.assertEqual(time_entry.time_worked, datetime.timedelta(hours=1)) self.assertEqual(time_entry.assignment, self.assignment) self.assertEqual(time_entry.timer_start_time, start_time) self.assertEqual(time_entry.timer_stop_time, self.time) # Timer object should be reset. timer.refresh_from_db() self.assertIsNone(timer.start_time) self.assertIsNone(timer.stop_time)
def test_stop_timer_not_running(self): timer = TaskTimer(worker=self.worker) timer.save() with self.assertRaisesRegex(TimerError, 'Timer not started'): stop_timer(self.worker)