Skip to content

Commit

Permalink
fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Dec 11, 2023
1 parent fa71058 commit df3d7dc
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def wrapper(*args, **kwargs):
# NOTE: we do not return internal exceptions over RPC
raise RPCServerError(
method_name=func.__name__,
exc_type=f"{type(exc)}",
exc_type=f"{exc.__class__.__module__}.{exc.__class__.__name__}",
msg=f"{exc}",
) from None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def app_environment(
)
async def test_successful_one_time_payment_workflow(
is_pdb_enabled: bool,
mock_payments_gateway_service_or_none: MockRouter | None,
app: FastAPI,
client: httpx.AsyncClient,
faker: Faker,
rpc_client: RabbitMQRPCClient,
mock_payments_gateway_service_or_none: MockRouter | None,
wallet_id: WalletID,
wallet_name: IDStr,
user_id: UserID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ def app_environment(
)
async def test_successful_create_payment_method_workflow(
is_pdb_enabled: bool,
mock_payments_gateway_service_or_none: MockRouter | None,
app: FastAPI,
client: httpx.AsyncClient,
faker: Faker,
rpc_client: RabbitMQRPCClient,
mock_payments_gateway_service_or_none: MockRouter | None,
wallet_id: WalletID,
wallet_name: IDStr,
user_id: UserID,
Expand Down
32 changes: 25 additions & 7 deletions services/payments/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,40 @@
def disable_rabbitmq_and_rpc_setup(mocker: MockerFixture) -> Callable:
def _():
# The following services are affected if rabbitmq is not in place
mocker.patch("simcore_service_payments.core.application.setup_notifier")
mocker.patch("simcore_service_payments.core.application.setup_socketio")
mocker.patch("simcore_service_payments.core.application.setup_rabbitmq")
mocker.patch("simcore_service_payments.core.application.setup_rpc_api_routes")
mocker.patch(
"simcore_service_payments.core.application.setup_auto_recharge_listener"
"simcore_service_payments.core.application.setup_notifier", autospec=True
)
mocker.patch(
"simcore_service_payments.core.application.setup_socketio", autospec=True
)
mocker.patch(
"simcore_service_payments.core.application.setup_rabbitmq", autospec=True
)
mocker.patch(
"simcore_service_payments.core.application.setup_rpc_api_routes",
autospec=True,
)
mocker.patch(
"simcore_service_payments.core.application.setup_auto_recharge_listener",
autospec=True,
)

return _


@pytest.fixture
def with_disabled_rabbitmq_and_rpc(disable_rabbitmq_and_rpc_setup: Callable):
def with_disabled_rabbitmq_and_rpc(disable_rabbitmq_and_rpc_setup: Callable) -> None:
disable_rabbitmq_and_rpc_setup()


@pytest.fixture
def with_disabled_gateway(mocker: MockerFixture) -> None:
mocker.patch(
"simcore_service_payments.core.application.setup_payments_gateway",
autospec=True,
)


@pytest.fixture
async def rpc_client(
faker: Faker, rabbitmq_rpc_client: Callable[[str], Awaitable[RabbitMQRPCClient]]
Expand Down Expand Up @@ -470,7 +488,7 @@ def mock_resource_usage_tracker_service_api_base(


@pytest.fixture
def mock_resoruce_usage_tracker_service_api(
def mock_resource_usage_tracker_service_api(
faker: Faker,
mock_resource_usage_tracker_service_api_base: MockRouter,
rut_service_openapi_specs: dict[str, Any],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def app_environment(
app_environment: EnvVarsDict,
postgres_env_vars_dict: EnvVarsDict,
with_disabled_rabbitmq_and_rpc: None,
with_disabled_gateway: None,
wait_for_postgres_ready_and_db_migrated: None,
):
# set environs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def app_environment(
app_environment: EnvVarsDict,
postgres_env_vars_dict: EnvVarsDict,
with_disabled_rabbitmq_and_rpc: None,
with_disabled_gateway: None,
wait_for_postgres_ready_and_db_migrated: None,
):
# set environs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def app_environment(
app_environment: EnvVarsDict,
postgres_env_vars_dict: EnvVarsDict,
with_disabled_rabbitmq_and_rpc: None,
with_disabled_gateway: None,
wait_for_postgres_ready_and_db_migrated: None,
):
# set environs
Expand Down
22 changes: 18 additions & 4 deletions services/payments/tests/unit/test_rpc_payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from models_library.api_schemas_webserver.wallets import WalletPaymentInitiated
from models_library.rabbitmq_basic_types import RPCMethodName
from pydantic import parse_obj_as
from pytest_mock import MockerFixture
from pytest_simcore.helpers.typing_env import EnvVarsDict
from pytest_simcore.helpers.utils_envs import setenvs_from_dict
from respx import MockRouter
Expand Down Expand Up @@ -68,7 +69,17 @@ def init_payment_kwargs(faker: Faker) -> dict[str, Any]:
}


@pytest.fixture
def _disable_startup(mocker: MockerFixture):
mocker.patch(
"simcore_service_payments.services.payments_gateway._create_start_policy",
return_value=lambda: print("on-startup"),
)


async def test_rpc_init_payment_fail(
is_pdb_enabled: bool,
_disable_startup,
app: FastAPI,
rpc_client: RabbitMQRPCClient,
init_payment_kwargs: dict[str, Any],
Expand All @@ -81,18 +92,20 @@ async def test_rpc_init_payment_fail(
PAYMENTS_RPC_NAMESPACE,
parse_obj_as(RPCMethodName, "init_payment"),
**init_payment_kwargs,
timeout_s=None if is_pdb_enabled else 5,
)

exc = exc_info.value
exc: RPCServerError = exc_info.value
assert exc.exc_type == f"{httpx.ConnectError}"
assert exc.method_name == "init_payment"
assert exc.msg


async def test_webserver_one_time_payment_workflow(
is_pdb_enabled: bool,
mock_payments_gateway_service_or_none: MockRouter | None,
app: FastAPI,
rpc_client: RabbitMQRPCClient,
mock_payments_gateway_service_or_none: MockRouter | None,
init_payment_kwargs: dict[str, Any],
payments_clean_db: None,
):
Expand All @@ -115,7 +128,7 @@ async def test_webserver_one_time_payment_workflow(
payment_id=result.payment_id,
user_id=init_payment_kwargs["user_id"],
wallet_id=init_payment_kwargs["wallet_id"],
timeout_s=20, # for tests
timeout_s=None if is_pdb_enabled else 5,
)

assert result is None
Expand All @@ -125,6 +138,7 @@ async def test_webserver_one_time_payment_workflow(


async def test_cancel_invalid_payment_id(
is_pdb_enabled: bool,
rpc_client: RabbitMQRPCClient,
mock_payments_gateway_service_or_none: MockRouter | None,
init_payment_kwargs: dict[str, Any],
Expand All @@ -140,7 +154,7 @@ async def test_cancel_invalid_payment_id(
payment_id=invalid_payment_id,
user_id=init_payment_kwargs["user_id"],
wallet_id=init_payment_kwargs["wallet_id"],
timeout_s=20, # for tests
timeout_s=None if is_pdb_enabled else 5,
)
error = exc_info.value

Expand Down
6 changes: 3 additions & 3 deletions services/payments/tests/unit/test_rpc_payments_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ async def test_webserver_init_and_cancel_payment_method_workflow(

async def test_webserver_crud_payment_method_workflow(
is_pdb_enabled: bool,
mock_payments_gateway_service_or_none: MockRouter | None,
app: FastAPI,
rpc_client: RabbitMQRPCClient,
mock_payments_gateway_service_or_none: MockRouter | None,
user_id: UserID,
user_name: IDStr,
user_email: EmailStr,
Expand Down Expand Up @@ -200,10 +200,10 @@ async def test_webserver_crud_payment_method_workflow(

async def test_webserver_pay_with_payment_method_workflow(
is_pdb_enabled: bool,
mock_resource_usage_tracker_service_api: None,
mock_payments_gateway_service_or_none: MockRouter | None,
app: FastAPI,
rpc_client: RabbitMQRPCClient,
mock_resoruce_usage_tracker_service_api: None,
mock_payments_gateway_service_or_none: MockRouter | None,
faker: Faker,
product_name: ProductName,
user_id: UserID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ async def test_process_message__whole_autorecharge_flow_success(
mocked_pay_with_payment_method: mock.AsyncMock,
mock_rpc_server: RabbitMQRPCClient,
mock_rpc_client: RabbitMQRPCClient,
mock_resoruce_usage_tracker_service_api: MockRouter,
mock_resource_usage_tracker_service_api: MockRouter,
postgres_db: sa.engine.Engine,
):
publisher = create_rabbitmq_client("publisher")
Expand All @@ -241,7 +241,7 @@ async def test_process_message__whole_autorecharge_flow_success(
assert row.wallet_id == wallet_id
assert row.state == PaymentTransactionState.SUCCESS
assert row.comment == "Payment generated by auto recharge"
assert len(mock_resoruce_usage_tracker_service_api.calls) == 1
assert len(mock_resource_usage_tracker_service_api.calls) == 1


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion services/payments/tests/unit/test_services_payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ def app_environment(


async def test_fails_to_pay_with_payment_method_without_funds(
mock_payments_gateway_service_or_none: MockRouter | None,
app: FastAPI,
create_fake_payment_method_in_db: Callable[
[PaymentMethodID, WalletID, UserID], Awaitable[PaymentsMethodsDB]
],
no_funds_payment_method_id: PaymentMethodID,
mock_payments_gateway_service_or_none: MockRouter | None,
wallet_id: WalletID,
wallet_name: IDStr,
user_id: UserID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ def amount_dollars(request: pytest.FixtureRequest) -> float:
"https://github.com/ITISFoundation/osparc-simcore/pull/4715"
)
async def test_one_time_payment_workflow(
mock_payments_gateway_service_or_none: MockRouter | None,
app: FastAPI,
faker: Faker,
mock_payments_gateway_service_or_none: MockRouter | None,
amount_dollars: float,
):
payment_gateway_api = PaymentsGatewayApi.get_from_app_state(app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def app(


async def test_add_credits_to_wallet(
app: FastAPI, faker: Faker, mock_resoruce_usage_tracker_service_api: MockRouter
app: FastAPI, faker: Faker, mock_resource_usage_tracker_service_api: MockRouter
):
# test
rut_api = ResourceUsageTrackerApi.get_from_app_state(app)
Expand Down

0 comments on commit df3d7dc

Please sign in to comment.