47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
"""
|
|
Tests para el servicio de sesión Redis.
|
|
Usa fakeredis para no necesitar un servidor Redis real.
|
|
"""
|
|
import json
|
|
|
|
import fakeredis.aioredis
|
|
import pytest
|
|
|
|
from src.services import session
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _patch_redis(monkeypatch):
|
|
"""Reemplaza el cliente Redis real con fakeredis."""
|
|
fake = fakeredis.aioredis.FakeRedis(decode_responses=True)
|
|
monkeypatch.setattr(session, "_client", fake)
|
|
monkeypatch.setattr(session, "get_client", lambda: fake)
|
|
yield
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_guardar_y_obtener():
|
|
datos = {"marca": "CADILLAC", "tipo": "ESCALADE", "precio_referencia": 2000000}
|
|
await session.guardar_ultima_cotizacion("5212345678", datos)
|
|
|
|
resultado = await session.obtener_ultima_cotizacion("5212345678")
|
|
assert resultado == datos
|
|
assert resultado["marca"] == "CADILLAC"
|
|
assert resultado["precio_referencia"] == 2000000
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_expiracion():
|
|
datos = {"marca": "BMW", "tipo": "M5"}
|
|
await session.guardar_ultima_cotizacion("5299999999", datos)
|
|
|
|
r = session.get_client()
|
|
ttl = await r.ttl("sesion:5299999999")
|
|
assert 86399 <= ttl <= 86400
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_sesion_inexistente():
|
|
resultado = await session.obtener_ultima_cotizacion("0000000000")
|
|
assert resultado is None
|