Files
guilan-ace-backend/apps/notifications/tests/unit/test_stream.py
Amirhossein Khalili ff99130938 test: migrate backend suites to pytest with module coverage gate
Split backend tests into unit and integration suites, add isolated fixtures and coverage enforcement, and update CI and test documentation. Cover current authentication, content, notification, payment, media, and admin flows; fix behaviors revealed by the new tests. Validate both suites and the per-module gate against local PostgreSQL.
2026-09-26 16:50:29 +03:30

29 lines
1.3 KiB
Python

import json
import pytest
from apps.notifications.api.views import NotificationStreamView
from apps.notifications.services import RedisNotificationStore
pytestmark = pytest.mark.unit
def test_sse_stream_sends_snapshot_update_and_closes_pubsub(mocker, settings):
settings.NOTIFICATION_SSE_HEARTBEAT_SECONDS = 60
pubsub = mocker.Mock()
pubsub.get_message.side_effect = [{"type": "message", "data": json.dumps({"event": "notification", "data": {"id": "one"}})}]
mocker.patch("apps.notifications.api.views.RedisNotificationStore.get_pubsub", return_value=pubsub)
mocker.patch("apps.notifications.api.views.RedisNotificationStore.list", return_value=([{"id": "one"}], 1))
mocker.patch("apps.notifications.api.views.RedisNotificationStore.unread_count", return_value=1)
mocker.patch("apps.notifications.api.views.RedisNotificationStore.summary", return_value={"id": "one"})
stream = NotificationStreamView()._build_stream("42")
assert next(stream).startswith("retry:")
assert "connected" in next(stream)
assert "unread_count" in next(stream)
assert '"id": "one"' in next(stream)
stream.close()
pubsub.subscribe.assert_called_once_with(RedisNotificationStore._channel_key("42"))
pubsub.unsubscribe.assert_called_once()
pubsub.close.assert_called_once()