def test_availability_request_year_processor( professionals: QuerySet, services: QuerySet, ): """Should set dates and a professional.""" service = services.first() professional = professionals.exclude(pk=service.professional.pk).first() request = Request() request.service = service request.professional = professional processor = RequestYearProcessor() new_request = processor.get(request) assert new_request.professional == service.professional assert new_request.start_datetime == arrow.utcnow().replace( hour=0, minute=0, second=0, microsecond=0, ) assert new_request.end_datetime == arrow.utcnow().replace( hour=0, minute=0, second=0, microsecond=0, ).shift(years=1)
def test_delete_saver_save(professionals: QuerySet): """Should save the provided slots.""" start = arrow.utcnow() end = start.shift(hours=3) professional = professionals.first() slot = AvailabilitySlot() slot.professional = professional slot.start_datetime = start.datetime slot.end_datetime = end.datetime slot.save() pk = slot.pk slot.pk = None request = Request() request.professional = professionals.first() request = RequestYearProcessor().get(request) saver = DeleteSaver() saver.set_request(request).set_slots([slot]) saver.save() slots = AvailabilitySlot.objects.all() assert slots.count() == 1 assert slot.pk != pk assert saver.set_slots([]).save() is None # type: ignore
def test_abstract_request_processor_reset_date_to_midnight(): """Should reset a dates to midnight.""" processor = RequestYearProcessor() request = Request() request.start_datetime = arrow.utcnow().replace(hour=1) processor._request = request assert processor._reset_date_to_midnight("invalid") is None processor._reset_date_to_midnight("start_datetime") assert processor._request.start_datetime == arrow.utcnow().replace( hour=0, minute=0, second=0, microsecond=0, )
def test_availability_request_append_processor( professionals: QuerySet, services: QuerySet, ): """Should set dates and a professional.""" service = services.first() professional = professionals.exclude(pk=service.professional.pk).first() request = Request() request.service = service request.professional = professional processor = RequestAppendProcessor() new_request = processor.get(request) assert new_request.professional == service.professional assert new_request.start_datetime == arrow.utcnow().replace( hour=0, minute=0, second=0, microsecond=0, ).shift(years=1) assert new_request.end_datetime == new_request.start_datetime.shift( days=settings.AVAILABILITY_DAYS_TO_APPEND)
def test_availability_request_validator( professionals: QuerySet, services: QuerySet, ): """Should validate a request.""" service = services.first() professional = professionals.exclude(pk=service.professional.pk).first() request = Request() now = arrow.utcnow() validator = RequestValidator() with pytest.raises(AvailabilityValueError) as error: validator.validate(request) assert "datetime is invalid" in str(error) request.start_datetime = now with pytest.raises(AvailabilityValueError) as error: validator.validate(request) assert "datetime is invalid" in str(error) request.end_datetime = request.start_datetime.shift(hours=-1) with pytest.raises(AvailabilityValueError) as error: validator.validate(request) assert "be less" in str(error) request.end_datetime = request.end_datetime.replace( tzinfo="America/Toronto") with pytest.raises(AvailabilityValueError) as error: validator.validate(request) assert "datetime is invalid" in str(error) request.end_datetime = request.start_datetime.shift(months=1) with pytest.raises(AvailabilityValueError) as error: validator.validate(request) assert "professional is empty" in str(error) request.professional = professional request.service = service with pytest.raises(AvailabilityValueError) as error: validator.validate(request) assert "service is incorrect" in str(error) request.professional = service.professional validator.validate(request)
def test_abstract_saver_set_request_slots( professionals: QuerySet, availability_slots: QuerySet, ): """Should set the request slots.""" request = Request() request.professional = professionals.first() saver = DeleteSaver() with pytest.raises(AvailabilityValueError) as error: saver._check_request() assert "request is not set" in str(error) saver.set_request(request) with pytest.raises(AvailabilityValueError) as error: saver._check_request() assert "slots are not set" in str(error) saver.set_slots(list(availability_slots)) saver._check_request() assert saver._request == request assert saver._slots == list(availability_slots)
def test_closed_periods_restriction_apply( professional_closed_periods: QuerySet): """Should apply the closed periods restriction.""" professional = professional_closed_periods.first().professional restriction = ClosedPeriodsRestriction() start = arrow.utcnow().replace( hour=0, minute=0, second=0, microsecond=0, ) request = Request() request.start_datetime = start request.end_datetime = start.shift(days=20) slot1 = AvailabilitySlot() slot1.professional = professional slot1.start_datetime = start.datetime slot1.end_datetime = start.shift(days=1).datetime slot2 = AvailabilitySlot() slot2.professional = professional slot2.start_datetime = start.shift(days=6).datetime slot2.end_datetime = start.shift(days=7).datetime with pytest.raises(AvailabilityValueError) as error: restriction.apply() assert "request is not set." in str(error) restriction.set_request(request) with pytest.raises(AvailabilityValueError) as error: restriction.apply() assert "slots are not set." in str(error) restriction.set_slots([slot1, slot2]) result = restriction.apply() assert result == [slot1]
def test_availability_request_processor_custom_dates(professionals: QuerySet): """Should reset dates to midnight.""" professional = professionals.first() request = Request() request.professional = professional start = arrow.utcnow() end = arrow.utcnow().shift() request.start_datetime = start request.end_datetime = end processor = RequestYearProcessor() new_request = processor.get(request) assert new_request.start_datetime == start.replace( hour=0, minute=0, second=0, microsecond=0, ) assert new_request.end_datetime == end.replace( hour=0, minute=0, second=0, microsecond=0, )
def test_closed_periods_restriction_get_closed_periods_for_slot( professional_closed_periods: QuerySet, service_closed_periods: QuerySet, ): """Should lazy load closed periods for the slot.""" professional = professional_closed_periods.first().professional service = service_closed_periods.first().service service.is_base_schedule = False slot = AvailabilitySlot() slot.professional = professional request = Request() start = arrow.utcnow().replace( hour=0, minute=0, second=0, microsecond=0, ) request.start_datetime = start request.end_datetime = start.shift(days=20) restriction = ClosedPeriodsRestriction() restriction.set_request(request) assert restriction.professionals_periods is None assert restriction.service_periods is None result = restriction._get_closed_periods_for_slot(slot) assert len(result) == 2 assert isinstance(result[0], ProfessionalClosedPeriod) assert result == restriction.professionals_periods assert restriction.service_periods is None slot.service = service assert len(result) == 2 result = restriction._get_closed_periods_for_slot(slot) assert isinstance(result[0], ServiceClosedPeriod) assert result == restriction.service_periods
def test_order_restriction_apply(orders: QuerySet): """Should apply the orders restriction.""" order = orders.first() professional = order.service.professional restriction = OrderRestriction() start = arrow.utcnow() request = Request() request.start_datetime = start.shift(days=-1) request.end_datetime = start.shift(days=20) request.professional = professional slot1 = AvailabilitySlot() slot1.professional = professional slot1.start_datetime = start.shift(hours=3).datetime slot1.end_datetime = start.shift(hours=3, minutes=30).datetime slot2 = AvailabilitySlot() slot2.professional = professional slot2.start_datetime = start.shift(days=2).datetime slot2.end_datetime = start.shift(days=3).datetime with pytest.raises(AvailabilityValueError) as error: restriction.apply() assert "request is not set." in str(error) restriction.set_request(request) with pytest.raises(AvailabilityValueError) as error: restriction.apply() assert "slots are not set." in str(error) restriction.set_slots([slot1, slot2]) result = restriction.apply() assert result == [slot2]
def test_order_restriction_get_orders(orders: QuerySet): """Should return the orders.""" order = orders.first() restriction = OrderRestriction() request = Request() request.start_datetime = arrow.utcnow() request.end_datetime = arrow.utcnow().shift(days=2) request.professional = order.service.professional request.service = order.service result = restriction.set_request(request)._get_orders() assert len(result) == 1 request.service = None restriction.orders = None result = restriction.set_request(request)._get_orders() assert len(result) == 2