示例#1
0
    def check_for_overlapping_shifts(self, shift_queryset):
        """
        Check the given Queryset for possible overlapping shifts and raise a Validation error
        with pairs of overlapping shifts.
        :param shift_queryset:
        :return:
        """
        e = []

        for early_shift, late_shift in pairwise(
                shift_queryset.order_by("started")):

            if late_shift.started < early_shift.stopped:
                e.append([
                    ShiftSerializer(early_shift).data,
                    ShiftSerializer(late_shift).data,
                ])

        if e:
            raise serializers.ValidationError({
                "message":
                _("An export of the worktime-sheet is not possible due to overlapping shifts."
                  ),
                "shifts":
                e,
            })
示例#2
0
 def test_shift_in_future_was_reviewed_fails(
         self, shift_starting_in_future_was_reviewed_querydict,
         plain_request_object):
     with pytest.raises(serializers.ValidationError):
         ShiftSerializer(
             data=shift_starting_in_future_was_reviewed_querydict,
             context={
                 "request": plain_request_object
             },
         ).is_valid(raise_exception=True)
示例#3
0
 def test_shift_in_past_as_planned_fails(
         self, shift_is_planned_but_started_in_past_json_querydict,
         plain_request_object):
     with pytest.raises(serializers.ValidationError):
         ShiftSerializer(
             data=shift_is_planned_but_started_in_past_json_querydict,
             context={
                 "request": plain_request_object
             },
         ).is_valid(raise_exception=True)
示例#4
0
 def get_shifts_list(self, request, *args, **kwargs):
     """
     Custom endpoint which retrieves all shifts corresponding to the issued Contract object.
     :param request:
     :param args:
     :param kwargs:
     :return:
     """
     instance = self.get_object()
     serializer = ShiftSerializer(instance.shifts, many=True)
     return Response(serializer.data)
示例#5
0
 def test_validate_correct_data(self, valid_shift_querydict,
                                plain_request_object):
     """
     The ShiftSerializer is tested if a valid JSON passes validation.
     :param valid_shift_querydict:
     :param plain_request_object:
     :return:
     """
     ShiftSerializer(data=valid_shift_querydict,
                     context={
                         "request": plain_request_object
                     }).is_valid(raise_exception=True)
示例#6
0
 def test_type_validation(self, wrong_type_querydict, plain_request_object):
     """
     The  ShiftSerializer is tested whether it raises a Validation
     if the provided shift type is incorrect.
     :param wrong_type_querydict:
     :param plain_request_object:
     :return:
     """
     with pytest.raises(serializers.ValidationError) as e_info:
         ShiftSerializer(data=wrong_type_querydict,
                         context={
                             "request": plain_request_object
                         }).is_valid(raise_exception=True)
示例#7
0
    def construct_json_object(self, user):
        user_data = UserSerializer(user).data
        contract_data = ContractSerializer(Contract.objects.filter(user=user),
                                           many=True).data
        shift_data = ShiftSerializer(Shift.objects.filter(user=user),
                                     many=True).data
        report_data = ReportSerializer(Report.objects.filter(user=user),
                                       many=True).data

        return {
            "user_data": user_data,
            "contracts_data": contract_data,
            "shifts_data": shift_data,
            "reports_data": report_data,
        }
示例#8
0
 def test_tags_validation(self, tags_not_string_querydict,
                          plain_request_object):
     """
     The  ShiftSerializer is tested whether it raises a Validation
     if the tags are incorrect.
     :param tags_not_string_querydict:
     :param plain_request_object:
     :return:
     """
     with pytest.raises(serializers.ValidationError) as e_info:
         ShiftSerializer(
             data=tags_not_string_querydict,
             context={
                 "request": plain_request_object
             },
         ).is_valid(raise_exception=True)
示例#9
0
 def test_stopped_before_started_validation(
         self, stopped_before_started_querydict, plain_request_object):
     """
     The  ShiftSerializer is tested whether it raises a Validation
     if the started and ended datetimes are causally incorrect.
     :param stopped_before_started_querydict: 
     :param plain_request_object: 
     :return: 
     """
     with pytest.raises(serializers.ValidationError) as e_info:
         ShiftSerializer(
             data=stopped_before_started_querydict,
             context={
                 "request": plain_request_object
             },
         ).is_valid(raise_exception=True)
示例#10
0
 def test_contract_not_belonging_to_user_validation(
         self, clockedinshift_invalid_contract_json, plain_request_object):
     """
     The  ClockedInShiftSerializer is tested whether it raises a Validation
     if the provided contract does not belong to the provided user.
     :param clockedinshift_invalid_contract_json:
     :param plain_request_object:
     :return:
     """
     with pytest.raises(serializers.ValidationError):
         ShiftSerializer(
             data=clockedinshift_invalid_contract_json,
             context={
                 "request": plain_request_object
             },
         ).is_valid(raise_exception=True)
示例#11
0
 def test_contract_not_belonging_to_user_validation(
         self, contract_not_belonging_to_user_querydict,
         plain_request_object):
     """
     The  ShiftSerializer is tested whether it raises a Validation
     if the provided contract does not belong to the provided user.
     :param contract_not_belonging_to_user_querydict:
     :param plain_request_object:
     :return:
     """
     with pytest.raises(serializers.ValidationError) as e_info:
         ShiftSerializer(
             data=contract_not_belonging_to_user_querydict,
             context={
                 "request": plain_request_object
             },
         ).is_valid(raise_exception=True)
示例#12
0
 def test_shift_starts_ends_after_contract_validation(
         self, shift_starts_ends_after_contract_json_querydict,
         plain_request_object):
     """
     The  ShiftSerializer is tested whether it raises a Validation
     if the shift ends after the contract ends.
     :param shift_starts_ends_after_contract_json_querydict:
     :param plain_request_object:
     :return:
     """
     with pytest.raises(serializers.ValidationError) as e_info:
         ShiftSerializer(
             data=shift_starts_ends_after_contract_json_querydict,
             context={
                 "request": plain_request_object
             },
         ).is_valid(raise_exception=True)
示例#13
0
 def test_stopped_on_next_day_validation(self,
                                         stopped_on_next_day_querydict,
                                         plain_request_object):
     """
     The  ShiftSerializer is tested whether it raises a Validation
     if the shift ends on a different day.
     :param stopped_on_next_day_querydict:
     :param plain_request_object:
     :return:
     """
     with pytest.raises(serializers.ValidationError) as e_info:
         ShiftSerializer(
             data=stopped_on_next_day_querydict,
             context={
                 "request": plain_request_object
             },
         ).is_valid(raise_exception=True)
示例#14
0
 def test_shift_started_before_contract_validation(
         self, shift_starts_before_contract_querydict,
         plain_request_object):
     """
     The  ShiftSerializer is tested whether it raises a Validation
     if the shift starts before the contract started.
     :param shift_starts_before_contract_querydict:
     :param plain_request_object:
     :return:
     """
     with pytest.raises(serializers.ValidationError):
         ShiftSerializer(
             data=shift_starts_before_contract_querydict,
             context={
                 "request": plain_request_object
             },
         ).is_valid(raise_exception=True)
示例#15
0
 def test_shift_not_createable_if_allready_exported_exist(
     self,
     valid_shift_querydict,
     plain_request_object,
     test_shift_creation_if_allready_exported,
 ):
     """
     Test that we can not create a Shift if allready exported Shifts exist for this month.
     :param valid_shift_querydict:
     :param plain_request_object:
     :param test_shift_creation_if_allready_exported:
     :return:
     """
     with pytest.raises(exceptions.PermissionDenied):
         ShiftSerializer(data=valid_shift_querydict,
                         context={
                             "request": plain_request_object
                         }).is_valid(raise_exception=True)
示例#16
0
    def test_report_update_called_on_update(self, plain_request_object,
                                            test_contract_change):
        """
        Test if we change the contract of a shift wether the reports of both contracts are updated.

        We start with fresh 00:00 Reports
        :param test_contract_change:
        :return:
        """

        shift = Shift.objects.get(contract=test_contract_change[0])
        ShiftSerializer().update(shift, {"contract": test_contract_change[1]})

        assert Report.objects.get(
            contract=test_contract_change[0],
            month_year=datetime.date(2019, 1,
                                     1)).worktime == datetime.timedelta(0)
        assert Report.objects.get(
            contract=test_contract_change[1],
            month_year=datetime.date(
                2019, 1, 1)).worktime == datetime.timedelta(hours=2)