def test_select_cancellation_policy_for_plan_without_policy(
        service: CreateReservation, context: Context, house, rate_plan):
    context.rate_plan = attr.evolve(rate_plan, policy_id=0)
    context.house = house

    result = service.select_cancellation_policy(context)
    assert is_successful(result)
    assert result.unwrap().rate_plan.policy is None
def test_select_cancellation_policy_fail(service: CreateReservation,
                                         context: Context, house, rate_plan):
    service._policies_repo = Mock(get=Mock(return_value=Nothing))
    context.house = house
    context.rate_plan = rate_plan

    result = service.select_cancellation_policy(context)
    assert is_successful(result)
    assert result.unwrap().rate_plan.policy is None
def test_select_cancellation_policy_error(service: CreateReservation,
                                          context: Context, house, rate_plan):
    service._policies_repo = Mock(get=Mock(side_effect=RuntimeError('ERR')))
    context.house = house
    context.rate_plan = rate_plan

    result = service.select_cancellation_policy(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
def test_select_rate_fail(service: CreateReservation, context: Context, house,
                          room_type, rate_plan):
    service._prices_repo = Mock(select_rates=Mock(return_value=[]))
    context.house = house
    context.room_type = room_type
    context.rate_plan = rate_plan

    result = service.select_rate(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.missed_rate
def test_select_rate_ok(service: CreateReservation, context: Context, house,
                        room_type, rate_plan, rate):
    rate_with_3 = attr.evolve(rate, id=401, occupancy=3)
    rate_with_4 = attr.evolve(rate, id=402, occupancy=4)
    service._prices_repo = Mock(select_rates=Mock(
        return_value=[rate_with_4, rate_with_3, rate]))
    context.house = house
    context.room_type = room_type
    context.rate_plan = rate_plan

    result = service.select_rate(context)
    assert is_successful(result)
    assert result.unwrap().rate == rate
def test_save_reservation_fail(service: CreateReservation, context: Context,
                               house, room_type, rate_plan):
    service._reservations_repo = Mock(save=Mock(return_value=(Nothing, False)))
    context.house = house
    context.room_type = room_type
    context.rate_plan = rate_plan
    context.reservation = Reservation(
        id=None,
        house_id=house.id,
        connection_id=None,
        source=ReservationSources.MANUAL,
        channel=None,
        channel_id='',
        checkin=context.request.checkin,
        checkout=context.request.checkout,
        booked_at=timezone.now(),
        status=ReservationStatuses.HOLD,
    )

    result = service.save_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.save
    assert result.failure().error.startswith('Error save Reservation')
def test_save_reservation_ok(service: CreateReservation, context: Context,
                             house, room_type, rate_plan):
    reservation = Reservation(
        id=111,
        house_id=house.id,
        connection_id=None,
        source=ReservationSources.MANUAL,
        channel=None,
        channel_id='',
        checkin=context.request.checkin,
        checkout=context.request.checkout,
        booked_at=timezone.now(),
        status=ReservationStatuses.HOLD,
    )
    service._reservations_repo = Mock(save=Mock(
        return_value=(Some(reservation), True)))
    context.house = house
    context.room_type = room_type
    context.rate_plan = rate_plan
    context.reservation = attr.evolve(reservation, id=None)

    result = service.save_reservation(context)
    assert is_successful(result)
    assert result.unwrap().reservation == reservation
def test_save_reservation_error(service: CreateReservation, context: Context,
                                house, room_type, rate_plan):
    service._reservations_repo = Mock(save=Mock(
        side_effect=RuntimeError('ERR')))
    context.house = house
    context.room_type = room_type
    context.rate_plan = rate_plan
    context.reservation = Reservation(
        id=None,
        house_id=house.id,
        connection_id=None,
        source=ReservationSources.MANUAL,
        channel=None,
        channel_id='',
        checkin=context.request.checkin,
        checkout=context.request.checkout,
        booked_at=timezone.now(),
        status=ReservationStatuses.HOLD,
    )

    result = service.save_reservation(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
def test_make_reservation_from_data_no_prices(service: CreateReservation,
                                              context: Context, house,
                                              room_type, rate_plan, rate):
    context.house = house
    context.room_type = room_type
    context.rate_plan = attr.evolve(rate_plan, policy=None)
    context.rate = rate
    context.request.prices = {}

    result = service.make_reservation_from_date(context)
    assert is_successful(result)

    reservation = result.unwrap().reservation
    assert isinstance(reservation, Reservation)
    assert reservation.id is None
    assert reservation.house_id == house.id
    assert reservation.connection_id is None
    assert reservation.source == ReservationSources.MANUAL
    assert reservation.channel is None
    assert reservation.channel_id == ''
    assert reservation.checkin == context.request.checkin
    assert reservation.checkout == context.request.checkout
    assert reservation.status == ReservationStatuses.HOLD
    assert reservation.room_count == 1
    assert reservation.currency == house.currency.code
    assert reservation.price == Decimal(0)
    assert reservation.tax == Decimal(0)
    assert reservation.fees == Decimal(0)
    assert reservation.netto_price == Decimal(0)
    assert reservation.guest_name == 'John'
    assert reservation.guest_surname == 'Smith'
    assert reservation.guest_email == '*****@*****.**'
    assert reservation.guest_phone == '+371-20202020'

    assert len(reservation.rooms) == 1

    room = reservation.rooms[0]
    assert isinstance(room, ReservationRoom)
    assert room.id is None
    assert room.channel_id == ''
    assert room.channel_rate_id == ''
    assert room.checkin == context.request.checkin
    assert room.checkout == context.request.checkout
    assert room.rate_plan_id == rate_plan.id
    assert room.policy == {}
    assert room.rate_id == rate.id
    assert room.guest_name == 'John Smith'
    assert room.guest_count == 2
    assert room.adults == 2
    assert room.children == 0
    assert room.currency == context.house.currency.code
    assert room.price == Decimal(0)
    assert room.tax == Decimal(0)
    assert room.fees == Decimal(0)
    assert room.netto_price == Decimal(0)
    assert room.notes_info == 'Comment'

    assert len(room.day_prices) == 2

    price = room.day_prices[0]
    assert isinstance(price, ReservationDay)
    assert price.id is None
    assert price.day == context.request.checkin
    assert price.roomtype_id == room_type.id
    assert price.room_id is None
    assert price.price_changed == Decimal(0)
    assert price.price_original == Decimal(0)
    assert price.price_accepted == Decimal(0)
    assert price.tax == Decimal(0)
    assert price.currency == house.currency.code

    price = room.day_prices[1]
    assert isinstance(price, ReservationDay)
    assert price.id is None
    assert price.day == context.request.checkin + datetime.timedelta(days=1)
    assert price.roomtype_id == room_type.id
    assert price.room_id is None
    assert price.price_changed == Decimal(0)
    assert price.price_original == Decimal(0)
    assert price.price_accepted == Decimal(0)
    assert price.tax == Decimal(0)
    assert price.currency == house.currency.code