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_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_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)