Skip to content

Commit

Permalink
rpc interface
Browse files Browse the repository at this point in the history
  • Loading branch information
matusdrobuliak66 committed Dec 10, 2024
1 parent 78ef2cc commit a6f49cd
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import logging
from typing import Final

from models_library.api_schemas_resource_usage_tracker import (
RESOURCE_USAGE_TRACKER_RPC_NAMESPACE,
)
from models_library.api_schemas_resource_usage_tracker.licensed_items_purchases import (
LicensedItemPurchaseGet,
)
from models_library.api_schemas_resource_usage_tracker.service_runs import (
ServiceRunPage,
)
from models_library.products import ProductName
from models_library.rabbitmq_basic_types import RPCMethodName
from models_library.resource_tracker_licensed_items_purchases import (
LicensedItemsPurchasesCreate,
)
from models_library.rest_ordering import OrderBy
from models_library.wallets import WalletID
from pydantic import AnyUrl, NonNegativeInt, TypeAdapter

from ....logging_utils import log_decorator
from ....rabbitmq import RabbitMQRPCClient

_logger = logging.getLogger(__name__)


_DEFAULT_TIMEOUT_S: Final[NonNegativeInt] = 30

_RPC_METHOD_NAME_ADAPTER: TypeAdapter[RPCMethodName] = TypeAdapter(RPCMethodName)


@log_decorator(_logger, level=logging.DEBUG)
async def get_licensed_items_purchases_page(
rabbitmq_rpc_client: RabbitMQRPCClient,
*,
product_name: ProductName,
wallet_id: WalletID,
offset: int = 0,
limit: int = 20,
order_by: OrderBy = OrderBy(field="purchased_at"),
) -> ServiceRunPage:
result = await rabbitmq_rpc_client.request(
RESOURCE_USAGE_TRACKER_RPC_NAMESPACE,
_RPC_METHOD_NAME_ADAPTER.validate_python("get_licensed_items_purchases_page"),
product_name=product_name,
wallet_id=wallet_id,
limit=limit,
offset=offset,
order_by=order_by,
timeout_s=_DEFAULT_TIMEOUT_S,
)
assert isinstance(result, ServiceRunPage) # nosec
return result


@log_decorator(_logger, level=logging.DEBUG)
async def get_licensed_item_purchase(
rabbitmq_rpc_client: RabbitMQRPCClient,
*,
product_name: ProductName,
wallet_id: WalletID,
) -> LicensedItemPurchaseGet:
result = await rabbitmq_rpc_client.request(
RESOURCE_USAGE_TRACKER_RPC_NAMESPACE,
_RPC_METHOD_NAME_ADAPTER.validate_python("get_licensed_item_purchase"),
product_name=product_name,
wallet_id=wallet_id,
timeout_s=_DEFAULT_TIMEOUT_S,
)
assert isinstance(result, LicensedItemPurchaseGet) # nosec
return result


@log_decorator(_logger, level=logging.DEBUG)
async def create_licensed_item_purchase(
rabbitmq_rpc_client: RabbitMQRPCClient, *, data: LicensedItemsPurchasesCreate
) -> LicensedItemPurchaseGet:
result: AnyUrl = await rabbitmq_rpc_client.request(
RESOURCE_USAGE_TRACKER_RPC_NAMESPACE,
_RPC_METHOD_NAME_ADAPTER.validate_python("create_licensed_item_purchase"),
data=data,
timeout_s=_DEFAULT_TIMEOUT_S,
)
assert isinstance(result, LicensedItemPurchaseGet) # nosec
return result
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# pylint:disable=unused-variable
# pylint:disable=unused-argument
# pylint:disable=redefined-outer-name
# pylint:disable=too-many-arguments

import os
from unittest.mock import AsyncMock, Mock

import pytest
import sqlalchemy as sa
from moto.server import ThreadedMotoServer
from pydantic import AnyUrl, TypeAdapter
from pytest_mock import MockerFixture
from pytest_simcore.helpers.typing_env import EnvVarsDict
from servicelib.rabbitmq import RabbitMQRPCClient
from servicelib.rabbitmq.rpc_interfaces.resource_usage_tracker import service_runs
from settings_library.s3 import S3Settings
from types_aiobotocore_s3 import S3Client

pytest_simcore_core_services_selection = [
"postgres",
"rabbit",
]
pytest_simcore_ops_services_selection = [
"adminer",
]

_USER_ID = 1


@pytest.fixture
async def mocked_export(mocker: MockerFixture) -> AsyncMock:
return mocker.patch(
"simcore_service_resource_usage_tracker.services.service_runs.service_runs_db.export_service_runs_table_to_s3",
autospec=True,
)


@pytest.fixture
async def mocked_presigned_link(mocker: MockerFixture) -> AsyncMock:
return mocker.patch(
"simcore_service_resource_usage_tracker.services.service_runs.SimcoreS3API.create_single_presigned_download_link",
return_value=TypeAdapter(AnyUrl).validate_python("https://www.testing.com/"),
)


@pytest.fixture
async def enable_resource_usage_tracker_s3(
mock_env: EnvVarsDict,
mocked_aws_server: ThreadedMotoServer,
mocked_s3_server_envs: EnvVarsDict,
mocked_s3_server_settings: S3Settings,
s3_client: S3Client,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# Create bucket
await s3_client.create_bucket(Bucket=mocked_s3_server_settings.S3_BUCKET_NAME)

# Remove the environment variable
if "RESOURCE_USAGE_TRACKER_S3" in os.environ:
monkeypatch.delenv("RESOURCE_USAGE_TRACKER_S3")


@pytest.mark.rpc_test()
async def test_rpc_list_service_runs_which_was_billed(
enable_resource_usage_tracker_s3: None,
mocked_redis_server: None,
postgres_db: sa.engine.Engine,
rpc_client: RabbitMQRPCClient,
mocked_export: Mock,
mocked_presigned_link: Mock,
):
download_url = await service_runs.export_service_runs(
rpc_client,
user_id=_USER_ID,
product_name="osparc",
)
assert isinstance(download_url, AnyUrl) # nosec
assert mocked_export.called
assert mocked_presigned_link.called

0 comments on commit a6f49cd

Please sign in to comment.