def update(self, instance, validated_data): service = VacationService(user=self.context['request'].user) try: return service.update_vacation( self.instance, self.validated_data.get('state', None), self.validated_data.get('comment_admin', None) ) except ServiceException as e: raise ValidationError({'error': e.args[0]})
def create(self, validated_data): service = VacationService(user=self.context['request'].user) try: return service.add_vacation( date_start=validated_data['date_start'], date_end=validated_data['date_end'], comment_user=validated_data.get('comment_user', None) ) except ServiceException as e: raise ValidationError({'error': e.args[0]})
def test_logic_add_vacation_bad_data(self): user = Employee.objects.all().first() self.assertRaises( ServiceException, lambda: VacationService(user=user).add_vacation( date_start=45, date_end=self.get_date(), comment_user='******')) self.assertRaises( ServiceException, lambda: VacationService(user=user).add_vacation( date_start=self.get_date(), date_end=45, comment_user='******')) self.assertEqual(Vacation.objects.count(), 0)
def test_logic_add_vacation_date_start_bigger_date_end(self): user = Employee.objects.all().first() self.assertRaises( ServiceException, lambda: VacationService(user=user).add_vacation( date_start=self.get_date(bigger=1), date_end=self.get_date(), comment_user='******'))
def test_logic_add_vacation_unknown(self): self.assertRaises( ServiceException, lambda: VacationService().add_vacation( date_start=self.get_date(), date_end=self.get_date(bigger=1), comment_user='******')) self.assertEqual(Vacation.objects.count(), 0)
def test_logic_add_vacation_summary_date_bigger_14_2_year(self): user = Employee.objects.all().first() vacation = VacationService(user=user).add_vacation( date_start=self.get_date(), date_end=self.get_date(bigger=14), comment_user='******') self.assertIsInstance(vacation, Vacation) self.assertEqual(Vacation.objects.count(), 1) vacation = VacationService(user=user).add_vacation( date_start=self.get_date(bigger=365), date_end=self.get_date(bigger=(14 + 365)), comment_user='******') self.assertIsInstance(vacation, Vacation) self.assertEqual(Vacation.objects.count(), 2)
def test_logic_add_vacation_summary_date_bigger_14(self): user = Employee.objects.all().first() vacation = VacationService(user=user).add_vacation( date_start=self.get_date(), date_end=self.get_date(bigger=14), comment_user='******') self.assertIsInstance(vacation, Vacation) self.assertEqual(Vacation.objects.count(), 1) self.assertRaises( ServiceException, lambda: VacationService(user=user).add_vacation( date_start=self.get_date(), date_end=self.get_date(bigger=3), comment_user='******')) self.assertEqual(Vacation.objects.count(), 1)
def test_logic_update_vacation_by_admin(self): user = Employee.objects.filter(group_code=Employee.GUSER).first() admin = Employee.objects.filter(group_code=Employee.GADMIN).first() vacation = VacationService(user=user).add_vacation( date_start=self.get_date(), date_end=self.get_date(bigger=14), comment_user='******') self.assertIsInstance(vacation, Vacation) self.assertEqual(Vacation.objects.count(), 1) VacationService(user=admin).update_vacation( vacation=vacation, state=Vacation.VACATION_REJECTED_BY_ADMIN) self.assertEqual( Vacation.objects.get(pk=vacation.id).state, Vacation.VACATION_REJECTED_BY_ADMIN)
def test_logic_update_vacation_by_user(self): user = Employee.objects.filter(group_code=Employee.GUSER).first() vacation = VacationService(user=user).add_vacation( date_start=self.get_date(), date_end=self.get_date(bigger=14), comment_user='******') self.assertIsInstance(vacation, Vacation) self.assertEqual(Vacation.objects.count(), 1) self.assertRaises( ServiceException, lambda: VacationService(user=user). update_vacation(vacation=vacation, state=Vacation.VACATION_APPROVED_BY_MANAGER)) self.assertRaises( ServiceException, lambda: VacationService(user=user).update_vacation( vacation=vacation, state=Vacation.VACATION_APPROVED_BY_ADMIN))