def test_make_reservation_from_data_with_new_plan(
    service: UpdateReservationPrices,
    context: Context,
    house,
    room_type,
    room,
    rate_plan,
    reservation,
    cancellation_policy,
):
    context.house = house
    context.source = reservation
    context.reservation_room = reservation.rooms[0]
    context.room_types = {room_type.id: room_type}
    context.rooms = {room.id: room}
    context.rate_plan = attr.evolve(rate_plan, policy=cancellation_policy)

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

    _reservation = result.unwrap().reservation
    assert len(_reservation.rooms[0].day_prices) == len(context.prices)
    assert [x.day for x in _reservation.rooms[0].day_prices
            ] == list(context.prices.keys())
    for price in _reservation.rooms[0].day_prices:
        assert price.price_accepted == Decimal(1200)
        assert price.room_id == room.id
        assert price.roomtype_id == room_type.id
        assert price.tax == Decimal(120)

    assert _reservation.rooms[0].netto_price == Decimal(1120)
    assert _reservation.rooms[0].netto_price_accepted == Decimal(6000)
    assert _reservation.rooms[0].price == Decimal(1120)
    assert _reservation.rooms[0].price_accepted == Decimal(6600)
    assert _reservation.rooms[0].tax == Decimal(600)
    assert _reservation.rooms[0].rate_plan_id == rate_plan.id
    assert _reservation.rooms[0].policy == {
        'name': 'Charge for 2 nights in case of cancelation',
        'policy_items': [{
            'days': None,
            'charge': 2,
            'charge_type': 'NIGHT'
        }]
    }
    assert _reservation.rooms[0].checkin == datetime.date.today()
    assert _reservation.rooms[0].checkout == datetime.date.today(
    ) + datetime.timedelta(days=5)

    assert _reservation.netto_price == Decimal(1120)
    assert _reservation.netto_price_accepted == Decimal(6000)
    assert _reservation.price == Decimal(1120)
    assert _reservation.price_accepted == Decimal(6600)
    assert _reservation.tax == Decimal(600)
    assert _reservation.checkin == datetime.date.today()
    assert _reservation.checkout == datetime.date.today() + datetime.timedelta(
        days=5)
def test_select_cancellation_policy_fail(service: UpdateReservationPrices,
                                         context: Context, house, rate_plan,
                                         reservation):
    service._policies_repo = Mock(get=Mock(return_value=Nothing))
    context.house = house
    context.source = reservation
    context.reservation_room = reservation.rooms[0]
    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_for_plan_without_it(
        service: UpdateReservationPrices, context: Context, house, rate_plan,
        reservation):
    service._policies_repo = Mock(get=Mock(side_effect=RuntimeError('ERR')))
    context.house = house
    context.source = reservation
    context.reservation_room = reservation.rooms[0]
    context.rate_plan = attr.evolve(rate_plan, policy_id=0)

    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: UpdateReservationPrices,
                                          context: Context, house, rate_plan,
                                          reservation):
    service._policies_repo = Mock(get=Mock(side_effect=RuntimeError('ERR')))
    context.house = house
    context.source = reservation
    context.reservation_room = reservation.rooms[0]
    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_make_reservation_from_data_with_same_plan(
        service: UpdateReservationPrices, context: Context, house, room_type,
        room, rate_plan, reservation):
    context.house = house
    context.source = reservation
    context.reservation_room = reservation.rooms[0]
    context.room_types = {room_type.id: room_type}
    context.rooms = {room.id: room}
    context.rate_plan = attr.evolve(rate_plan,
                                    id=reservation.rooms[0].rate_plan_id)

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

    _reservation = result.unwrap().reservation

    assert len(_reservation.rooms[0].day_prices) == len(context.prices)
    assert [x.day for x in _reservation.rooms[0].day_prices
            ] == list(context.prices.keys())
    for price in _reservation.rooms[0].day_prices:
        assert price.price_accepted == Decimal(1200)
        assert price.room_id == room.id
        assert price.roomtype_id == room_type.id
        assert price.tax == Decimal(120)

    assert _reservation.rooms[0].netto_price == Decimal(1120)
    assert _reservation.rooms[0].netto_price_accepted == Decimal(6000)
    assert _reservation.rooms[0].price == Decimal(1120)
    assert _reservation.rooms[0].price_accepted == Decimal(6600)
    assert _reservation.rooms[0].tax == Decimal(600)
    assert _reservation.rooms[0].rate_plan_id == 499
    assert _reservation.rooms[0].policy == {}
    assert _reservation.rooms[0].checkin == datetime.date.today()
    assert _reservation.rooms[0].checkout == datetime.date.today(
    ) + datetime.timedelta(days=5)

    assert _reservation.netto_price == Decimal(1120)
    assert _reservation.netto_price_accepted == Decimal(6000)
    assert _reservation.price == Decimal(1120)
    assert _reservation.price_accepted == Decimal(6600)
    assert _reservation.tax == Decimal(600)
    assert _reservation.checkin == datetime.date.today()
    assert _reservation.checkout == datetime.date.today() + datetime.timedelta(
        days=5)