def test_select_rate_plans_ok(service: UpdateReservationCache,
                              context: Context, house, reservation, rate_plan,
                              user):
    service._prices_repo = Mock(select_plans=Mock(return_value=[rate_plan]))
    context.house = house
    context.reservations = [reservation]
    context.user = user

    result = service.select_rate_plans(context)
    assert is_successful(result), result.failure()
    assert result.unwrap().rate_plans == {rate_plan.id: rate_plan}
def test_select_rate_plans_error(service: UpdateReservationCache,
                                 context: Context, house, reservation, user):
    service._prices_repo = Mock(select_plans=Mock(
        side_effect=RuntimeError('ERR')))
    context.house = house
    context.reservations = [reservation]
    context.user = user

    result = service.select_rate_plans(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
def test_success(service: UpdateReservationCache, house, reservation,
                 rate_plan, user):
    service._houses_repo = Mock(get=Mock(return_value=Some(house)))
    service._reservations_repo = Mock(select=Mock(return_value=[reservation]))
    service._cache_repo = Mock(save=Mock(return_value=True),
                               delete=Mock(return_value=None))
    service._prices_repo = Mock(select_plans=Mock(return_value=[rate_plan]))
    service._members_repo = Mock(get_bot_user=Mock(return_value=Some(user)))
    service.get_rpc_api = Mock(return_value=Mock(
        select_invoices_for_order=Mock(return_value=[
            Mock(id=987, amount_total=50.0, amount_residual=20.0)
        ])))

    result = service.execute(house_id=house.id)
    assert is_successful(result)