from datetime import timedelta import pytest from django.utils import timezone from apps.notifications import services pytestmark = pytest.mark.integration def test_fake_redis_store_add_filter_mark_and_delete(fake_notification_redis, monkeypatch): monkeypatch.setattr(services, "redis_client", fake_notification_redis) first = services.RedisNotificationStore.add("user-1", {"type": "news", "title": "One", "body_markdown": "# Long body"}) second = services.RedisNotificationStore.add("user-1", {"type": "event", "title": "Two"}) assert services.RedisNotificationStore.unread_count("user-1") == 2 assert services.RedisNotificationStore.list("user-1", type_filter="news")[1] == 1 assert services.RedisNotificationStore.get("user-2", first["id"]) is None marked = services.RedisNotificationStore.mark_seen("user-1", first["id"]) assert marked["unread_count"] == 1 assert services.RedisNotificationStore.list("user-1", read_filter="read")[1] == 1 assert services.RedisNotificationStore.mark_all_seen("user-1") == 2 assert services.RedisNotificationStore.unread_count("user-1") == 0 assert services.RedisNotificationStore.delete("user-1", second["id"]) assert not services.RedisNotificationStore.delete("user-1", second["id"]) assert services.RedisNotificationStore.get("user-1", second["id"]) is None def test_fake_redis_store_cleans_expired_and_corrupt_records(fake_notification_redis, monkeypatch): monkeypatch.setattr(services, "redis_client", fake_notification_redis) old = (timezone.now() - timedelta(days=90)).isoformat() expired = services.RedisNotificationStore.add("user-1", {"created_at": old, "title": "Old"}) recent = services.RedisNotificationStore.add("user-1", {"title": "Recent"}) assert services.RedisNotificationStore.cleanup_expired(retention_days=30) == 1 assert services.RedisNotificationStore.get("user-1", expired["id"]) is None fake_notification_redis.hset(services.RedisNotificationStore._data_key("user-1"), recent["id"], "broken json") assert services.RedisNotificationStore.list("user-1") == ([], 0) assert services.RedisNotificationStore.get("user-1", recent["id"]) is None