def test_success(service: CreateReservation, context: Context, house,
                 room_type, rate_plan, rate, cancellation_policy):
    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,
        is_verified=True,
    )
    service._houses_repo = Mock(get=Mock(return_value=Some(house)))
    service._roomtypes_repo = Mock(get=Mock(return_value=Some(room_type)))
    service._prices_repo = Mock(get_plan=Mock(return_value=Some(rate_plan)),
                                select_rates=Mock(return_value=[rate]))
    service._reservations_repo = Mock(
        save=Mock(
            return_value=(Some(attr.evolve(reservation, is_verified=False)),
                          True)),
        accept=Mock(return_value=Some(reservation)),
    )
    service._policies_repo = Mock(get=Mock(
        return_value=Some(cancellation_policy)))

    result = service.execute(house.id, context.request, context.user)
    assert is_successful(result), result.failure()
    assert result.unwrap() == attr.evolve(reservation, is_verified=True)
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'