from datetime import datetime import pytest from motor.motor_asyncio import AsyncIOMotorClient from redis import asyncio as aioredis from server.core.cache.backends import mongodb, redis from server.core.constants import CollectionName pytest_plugins = ["tests.core.fixtures.documents_data"] async def test_cache_backends_consistency_save_document( redis_session: aioredis.Redis, mongo_session: AsyncIOMotorClient, spotify_search_data ): key, search_data, created_at = spotify_search_data await mongodb.save_document(CollectionName.SPOTIFY_SEARCH, key, search_data, created_at) mongo_documents, mongo_min_date, mongo_max_date = await mongodb.get_documents( CollectionName.SPOTIFY_SEARCH, [key], -1, True ) await redis.save_document(CollectionName.SPOTIFY_SEARCH, key, search_data, created_at) redis_documents, redis_min_date, redis_max_date = await redis.get_documents( CollectionName.SPOTIFY_SEARCH, [key], -1, True ) expected_date = datetime.fromisoformat(datetime.isoformat(created_at, timespec="milliseconds")) assert mongo_documents == redis_documents == {key: search_data} assert mongo_min_date == redis_min_date == expected_date assert mongo_max_date == redis_max_date == expected_date @pytest.mark.parametrize("by_record_id", (True, False)) async def test_cache_backends_consistency_save_documents( redis_session: aioredis.Redis, mongo_session: AsyncIOMotorClient, spotify_track_data, by_record_id ): key, track_id, track_data, created_at = spotify_track_data await mongodb.save_documents(CollectionName.SPOTIFY_TRACK, track_data, created_at) mongo_documents, mongo_min_date, mongo_max_date = await mongodb.get_documents( CollectionName.SPOTIFY_TRACK, [key], -1, by_record_id ) await redis.save_documents(CollectionName.SPOTIFY_TRACK, track_data, created_at) redis_documents, redis_min_date, redis_max_date = await redis.get_documents( CollectionName.SPOTIFY_TRACK, [key], -1, by_record_id ) expected_date = datetime.fromisoformat(datetime.isoformat(created_at, timespec="milliseconds")) assert mongo_documents == redis_documents == {key if by_record_id else track_id: track_data[key][1]} assert mongo_min_date == redis_min_date == expected_date assert mongo_max_date == redis_max_date == expected_date