Skip to content

Commit a26d82c

Browse files
tests: add test
1 parent accab18 commit a26d82c

File tree

10 files changed

+59
-119
lines changed

10 files changed

+59
-119
lines changed

packages/models-library/src/models_library/rpc/notifications/messages.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC
2-
from typing import Annotated, Any, Literal, TypeAlias
2+
from typing import Annotated, Any, TypeAlias
33

44
from pydantic import BaseModel, Field
55

@@ -9,12 +9,12 @@ class BaseRecipient(BaseModel, ABC):
99

1010

1111
class SMSRecipient(BaseRecipient):
12-
type: Literal["sms"]
12+
type: Annotated[str, Field(frozen=True)] = "sms"
1313
phone_number: str
1414

1515

1616
class EmailRecipient(BaseRecipient):
17-
type: Literal["email"]
17+
type: Annotated[str, Field(frozen=True)] = "email"
1818
address: str
1919

2020

packages/service-library/src/servicelib/rabbitmq/rpc_interfaces/notifications/messages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from models_library.api_schemas_notifications import NOTIFICATIONS_RPC_NAMESPACE
44
from models_library.rabbitmq_basic_types import RPCMethodName
5-
from models_library.rpc.notifications.messages import NotificationMessage, Recipient
5+
from models_library.rpc.notifications.messages import BaseRecipient, NotificationMessage
66
from pydantic import NonNegativeInt, TypeAdapter
77

88
from ... import RabbitMQRPCClient
@@ -14,7 +14,7 @@ async def send_notification_message(
1414
rabbitmq_rpc_client: RabbitMQRPCClient,
1515
*,
1616
message: NotificationMessage,
17-
recipients: list[Recipient],
17+
recipients: list[BaseRecipient],
1818
) -> None:
1919
await rabbitmq_rpc_client.request(
2020
NOTIFICATIONS_RPC_NAMESPACE,

services/notifications/src/simcore_service_notifications/api/rest/_health.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
from fastapi import APIRouter, Depends
55
from models_library.api_schemas__common.health import HealthCheckGet
66
from models_library.errors import (
7-
POSRGRES_DATABASE_UNHEALTHY_MSG,
87
RABBITMQ_CLIENT_UNHEALTHY_MSG,
98
)
109
from servicelib.rabbitmq import RabbitMQClient
1110

12-
from ...clients.postgres import PostgresLiveness
13-
from .dependencies import get_postgres_liveness, get_rabbitmq_client
11+
from .dependencies import get_rabbitmq_client
1412

1513
router = APIRouter()
1614

@@ -22,12 +20,8 @@ class HealthCheckError(RuntimeError):
2220
@router.get("/", response_model=HealthCheckGet)
2321
async def check_service_health(
2422
rabbitmq_client: Annotated[RabbitMQClient, Depends(get_rabbitmq_client)],
25-
postgres_liveness: Annotated[PostgresLiveness, Depends(get_postgres_liveness)],
2623
):
2724
if not rabbitmq_client.healthy:
2825
raise HealthCheckError(RABBITMQ_CLIENT_UNHEALTHY_MSG)
2926

30-
if not postgres_liveness.is_responsive:
31-
raise HealthCheckError(POSRGRES_DATABASE_UNHEALTHY_MSG)
32-
3327
return HealthCheckGet(timestamp=f"{__name__}@{arrow.utcnow().datetime.isoformat()}")

services/notifications/src/simcore_service_notifications/api/rest/dependencies.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
from fastapi import Depends, FastAPI, Request
66
from servicelib.rabbitmq._client_rpc import RabbitMQRPCClient
77

8-
from ...clients.postgres import PostgresLiveness
9-
from ...clients.postgres import get_postgres_liveness as get_postgress_db_liveness
10-
118

129
def get_application(request: Request) -> FastAPI:
1310
return cast(FastAPI, request.app)
@@ -18,9 +15,3 @@ def get_rabbitmq_client(
1815
) -> RabbitMQRPCClient:
1916
assert isinstance(app.state.rabbitmq_rpc_server, RabbitMQRPCClient) # nosec
2017
return app.state.rabbitmq_rpc_server
21-
22-
23-
def get_postgres_liveness(
24-
app: Annotated[FastAPI, Depends(get_application)],
25-
) -> PostgresLiveness:
26-
return get_postgress_db_liveness(app)

services/notifications/src/simcore_service_notifications/clients/postgres/__init__.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

services/notifications/src/simcore_service_notifications/clients/postgres/_liveness.py

Lines changed: 0 additions & 43 deletions
This file was deleted.

services/notifications/src/simcore_service_notifications/core/events.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88
)
99
from servicelib.fastapi.postgres_lifespan import (
1010
create_postgres_database_input_state,
11-
postgres_database_lifespan,
1211
)
1312

1413
from .._meta import APP_FINISHED_BANNER_MSG, APP_STARTED_BANNER_MSG
1514
from ..api.rpc.routes import rpc_api_routes_lifespan
1615
from ..clients.celery import celery_lifespan
17-
from ..clients.postgres import postgres_lifespan
1816
from ..clients.rabbitmq import rabbitmq_lifespan
1917
from .settings import ApplicationSettings
2018

@@ -41,10 +39,6 @@ def create_app_lifespan(settings: ApplicationSettings) -> LifespanManager:
4139
app_lifespan = LifespanManager()
4240
app_lifespan.add(_settings_lifespan)
4341

44-
# - postgres
45-
app_lifespan.add(postgres_database_lifespan)
46-
app_lifespan.add(postgres_lifespan)
47-
4842
if settings.NOTIFICATIONS_CELERY and not settings.NOTIFICATIONS_WORKER_MODE:
4943
# - rabbitmq
5044
app_lifespan.add(rabbitmq_lifespan)

services/notifications/tests/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
start_worker,
1616
)
1717
from celery.signals import worker_init, worker_shutdown # type: ignore[import-untyped]
18-
from celery.worker.worker import WorkController # type: ignore[import-untyped]
18+
from celery.worker.worker import WorkController
1919
from celery_library.signals import on_worker_init, on_worker_shutdown
2020
from models_library.basic_types import BootModeEnum
2121
from pytest_simcore.helpers.monkeypatch_envs import EnvVarsDict, setenvs_from_dict
@@ -32,7 +32,6 @@
3232
"pytest_simcore.docker_compose",
3333
"pytest_simcore.docker_swarm",
3434
"pytest_simcore.environment_configs",
35-
"pytest_simcore.postgres_service",
3635
"pytest_simcore.rabbit_service",
3736
"pytest_simcore.redis_service",
3837
"pytest_simcore.repository_paths",
@@ -81,7 +80,7 @@ def celery_config() -> dict[str, Any]:
8180

8281
@pytest.fixture
8382
async def with_celery_worker(
84-
mock_environment: EnvVarsDict,
83+
app_environment: EnvVarsDict,
8584
celery_app: Celery,
8685
monkeypatch: pytest.MonkeyPatch,
8786
) -> AsyncIterator[TestWorkController]:

services/notifications/tests/unit/conftest.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,62 @@
22
# pylint: disable=unused-argument
33

44
from collections.abc import AsyncIterator
5+
from typing import Final
56

67
import pytest
7-
import sqlalchemy as sa
88
from asgi_lifespan import LifespanManager
99
from fastapi import FastAPI
1010
from fastapi.testclient import TestClient
1111
from pytest_simcore.helpers.monkeypatch_envs import EnvVarsDict, setenvs_from_dict
1212
from settings_library.rabbit import RabbitSettings
13+
from settings_library.redis import RedisSettings
1314
from simcore_service_notifications.core.application import create_app
1415
from simcore_service_notifications.core.settings import ApplicationSettings
1516

17+
_LIFESPAN_TIMEOUT: Final[int] = 30
18+
1619

1720
@pytest.fixture
1821
def app_environment(
1922
monkeypatch: pytest.MonkeyPatch,
2023
mock_environment: EnvVarsDict,
21-
rabbit_service: RabbitSettings,
22-
postgres_db: sa.engine.Engine, # waiting for postgres service to start
23-
postgres_env_vars_dict: EnvVarsDict,
2424
) -> EnvVarsDict:
2525
return setenvs_from_dict(
2626
monkeypatch,
2727
{
2828
**mock_environment,
29-
"RABBIT_HOST": rabbit_service.RABBIT_HOST,
30-
"RABBIT_PASSWORD": rabbit_service.RABBIT_PASSWORD.get_secret_value(),
31-
"RABBIT_PORT": f"{rabbit_service.RABBIT_PORT}",
32-
"RABBIT_SECURE": f"{rabbit_service.RABBIT_SECURE}",
33-
"RABBIT_USER": rabbit_service.RABBIT_USER,
34-
**postgres_env_vars_dict,
3529
},
3630
)
3731

3832

3933
@pytest.fixture
40-
async def initialized_app(app_environment: EnvVarsDict) -> AsyncIterator[FastAPI]:
41-
app: FastAPI = create_app(ApplicationSettings.create_from_envs())
34+
def enabled_rabbitmq(
35+
app_environment: EnvVarsDict, rabbit_service: RabbitSettings
36+
) -> RabbitSettings:
37+
return rabbit_service
38+
39+
40+
@pytest.fixture
41+
def enabled_redis(
42+
app_environment: EnvVarsDict, redis_service: RedisSettings
43+
) -> RedisSettings:
44+
return redis_service
45+
46+
47+
@pytest.fixture
48+
def app_settings(
49+
app_environment: EnvVarsDict,
50+
enabled_rabbitmq: RabbitSettings,
51+
enabled_redis: RedisSettings,
52+
) -> ApplicationSettings:
53+
settings = ApplicationSettings.create_from_envs()
54+
print(f"{settings.model_dump_json(indent=2)=}")
55+
return settings
56+
57+
58+
@pytest.fixture
59+
async def initialized_app(app_settings: ApplicationSettings) -> AsyncIterator[FastAPI]:
60+
app: FastAPI = create_app(app_settings)
4261

4362
async with LifespanManager(app, startup_timeout=30, shutdown_timeout=30):
4463
yield app
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
from celery.contrib.testing.worker import TestWorkController
2+
from fastapi import FastAPI
3+
from models_library.rpc.notifications.messages import NotificationMessage
24
from servicelib.rabbitmq import RabbitMQRPCClient
5+
from servicelib.rabbitmq.rpc_interfaces.notifications.messages import (
6+
send_notification_message,
7+
)
8+
from simcore_service_notifications.clients.celery import EmailRecipient
9+
10+
pytest_simcore_core_services_selection = [
11+
"rabbit",
12+
"redis",
13+
]
314

415

516
async def test_send_email(
17+
initialized_app: FastAPI,
618
notifications_rabbitmq_rpc_client: RabbitMQRPCClient,
719
with_celery_worker: TestWorkController,
820
):
9-
pass
21+
await send_notification_message(
22+
notifications_rabbitmq_rpc_client,
23+
message=NotificationMessage(
24+
event="test_event",
25+
context={"key": "value"},
26+
),
27+
recipients=[EmailRecipient(address="[email protected]")],
28+
)

0 commit comments

Comments
 (0)