def test_select_bot_user_ok(service: UpdateReservationCache, context: Context,
                            house, reservation, user):
    service._members_repo = Mock(get_bot_user=Mock(return_value=Some(user)))
    context.house = house
    context.reservations = [reservation]

    result = service.select_bot_user(context)
    assert is_successful(result), result.failure()
    assert result.unwrap().user == user
def test_select_bot_user_fail(service: UpdateReservationCache,
                              context: Context, house, reservation):
    service._members_repo = Mock(get_bot_user=Mock(return_value=Nothing))
    context.house = house
    context.reservations = [reservation]

    result = service.select_bot_user(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.missed_user
def test_select_payed_amounts_empty(service: UpdateReservationCache,
                                    context: Context, house, user):
    context.house = house
    context.reservations = []
    context.user = user
    context.api = Mock(select_invoices_for_order=Mock(
        side_effect=RuntimeError('ERR')))

    result = service.select_payed_amounts(context)
    assert is_successful(result)
    assert result.unwrap().payed_amounts == {}
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_bot_user_error(service: UpdateReservationCache,
                               context: Context, house, reservation):
    service._members_repo = Mock(get_bot_user=Mock(
        side_effect=RuntimeError('ERR')))
    context.house = house
    context.reservations = [reservation]

    result = service.select_bot_user(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
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_process_reservations(service: UpdateReservationCache,
                              context: Context, house, reservation):
    context.house = house
    context.reservations = [reservation]

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

    assert len(result.unwrap().cached_reservations) == 1
    assert all([
        isinstance(x, CachedReservation)
        for x in result.unwrap().cached_reservations
    ])
def test_select_payed_amounts_error(service: UpdateReservationCache,
                                    context: Context, house, reservation,
                                    user):
    context.house = house
    context.reservations = [reservation]
    context.user = user
    context.api = Mock(select_invoices_for_order=Mock(
        side_effect=RuntimeError('ERR')))

    result = service.select_payed_amounts(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
def test_select_reservations_ok(service: UpdateReservationCache,
                                context: Context, house, reservation):
    service._reservations_repo = Mock(select=Mock(return_value=[reservation]))
    context.house = house

    result = service.select_reservations(context)
    assert is_successful(result)
    assert result.unwrap().reservations == [reservation]
def test_save_cache_ok(service: UpdateReservationCache, context: Context,
                       house, room_type):
    service._cache_repo = Mock(save=Mock(return_value=True))
    context.house = house
    context.cached_reservations = [
        CachedReservation(
            pk='111-1',
            reservation_id=111,
            grid='roomtype',
            grid_id=room_type.id,
            checkin=datetime.datetime.combine(datetime.date.today(),
                                              datetime.time(14)),
            checkout=datetime.datetime.combine(
                datetime.date.today() + datetime.timedelta(days=1),
                datetime.time(11)),
        )
    ]

    result = service.save_cache(context)
    assert is_successful(result)
def test_select_payed_amounts_ok(service: UpdateReservationCache,
                                 context: Context, house, reservation, user):
    context.house = house
    context.reservations = [
        attr.evolve(reservation, quotation_id=321),
        attr.evolve(reservation, id=123, quotation_id=322),
        attr.evolve(reservation, id=124, quotation_id=None),
    ]
    context.user = user
    context.api = Mock(select_invoices_for_order=Mock(
        return_value=[Mock(id=987, amount_total=50.0, amount_residual=20.0)]))

    result = service.select_payed_amounts(context)
    assert is_successful(result)
    assert result.unwrap().payed_amounts == {
        321: Decimal(30),
        322: Decimal(30)
    }

    context.api.select_invoices_for_order.assert_any_call(321)
    context.api.select_invoices_for_order.assert_any_call(322)
def test_save_cache_error(service: UpdateReservationCache, context: Context,
                          house, room_type):
    service._cache_repo = Mock(save=Mock(side_effect=RuntimeError('ERR')))
    context.house = house
    context.cached_reservations = [
        CachedReservation(
            pk='111-1',
            reservation_id=111,
            grid='roomtype',
            grid_id=room_type.id,
            checkin=datetime.datetime.combine(datetime.date.today(),
                                              datetime.time(14)),
            checkout=datetime.datetime.combine(
                datetime.date.today() + datetime.timedelta(days=1),
                datetime.time(11)),
        )
    ]

    result = service.save_cache(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.error
    assert str(result.failure().exc) == 'ERR'
def test_missed_house_id(service: UpdateReservationCache, context: Context):
    context.house_id = None

    result = service.select_house(context)
    assert not is_successful(result)
    assert result.failure().failure == ReservationErrors.missed_house
def context(house):
    return Context(house_id=house.id)