def test_valid_timetable_two_users(self): user2 = User.objects.create(username='******') user2.data.hour = 12 result_1 = validate_and_process_timetable_change(self.user, self.data) result_2 = validate_and_process_timetable_change(user2, self.data) self.assertEqual(result_1, (True, 'Timetable successfully saved')) self.assertEqual(result_2, (True, 'Timetable successfully saved')) self.assertEqual(Timetable.objects.filter(action='Sleep').count(), 4)
def test_invalid_timetable_duplicate(self): result = validate_and_process_timetable_change(self.user, [ *self.data, { 'hour': self.now, 'action': 'Sleep' }, ]) self.assertEqual( result, (False, 'Invalid timetable. Each hour can only appear once')) self.assertEqual(Timetable.objects.filter(user=self.user).count(), 0)
def test_invalid_timetable_too_far_into_future(self): time_too_far_into_the_future = self.now + 15 % HOURS_IN_DAY result = validate_and_process_timetable_change(self.user, [ *self.data, { 'hour': time_too_far_into_the_future, 'action': 'Sleep' } ]) self.assertEqual(result, ( False, f'Invalid timetable. ' f'Chosen time: {time_too_far_into_the_future} is too far into the future' )) self.assertEqual(Timetable.objects.filter(user=self.user).count(), 0)
def test_invalid_timetable_finish_semester_cant_be_added(self): result = validate_and_process_timetable_change(self.user, [ *self.data, { 'hour': self.now, 'action': 'Finish Semester' }, ]) self.assertEqual(result, ( False, 'Invalid timetable. Chosen action: Finish Semester cannot be performed at will' )) self.assertEqual(Timetable.objects.filter(user=self.user).count(), 0)
def test_invalid_timetable_wrong_semester(self): result = validate_and_process_timetable_change(self.user, [ *self.data, { 'hour': self.now, 'action': 'Computer Networks' }, ]) self.assertEqual(result, ( False, "Invalid timetable. You can't take summer classes in the winter and vice versa" )) self.assertEqual(Timetable.objects.filter(user=self.user).count(), 0)
def test_invalid_timetable_wrong_time(self): result = validate_and_process_timetable_change(self.user, [ *self.data, { 'hour': self.now, 'action': 'Party' }, ]) self.assertEqual(result, ( False, 'Invalid timetable. Chosen action: Party cannot be performed at 12' )) self.assertEqual(Timetable.objects.filter(user=self.user).count(), 0)
def test_hour_already_passed(self): time_too_far_into_the_future = (self.now - 1) % HOURS_IN_DAY result = validate_and_process_timetable_change(self.user, [ *self.data, { 'hour': time_too_far_into_the_future, 'action': 'Sleep' } ]) self.assertEqual(result, ( True, 'Timetable successfully saved. However, the following hours have already ' f'passed [{(self.now - 1) % HOURS_IN_DAY}], ' f'your actions for them have not been saved')) self.assertEqual(Timetable.objects.filter(user=self.user).count(), 2)
def post(self, request, pk): process_timetable_wrapper(pk) success, message = validate_and_process_timetable_change( User.objects.get(pk=pk), request.data) return Response({'success': success, 'message': message})
def test_valid_timetable(self): result = validate_and_process_timetable_change(self.user, self.data) self.assertEqual(result, (True, 'Timetable successfully saved')) self.assertEqual(Timetable.objects.filter(user=self.user).count(), 2)