88from httpx import AsyncClient , ASGITransport
99
1010{% - if cookiecutter .enable_redis == "True" % }
11- from fakeredis import FakeServer
12- from fakeredis .aioredis import FakeConnection
1311from redis .asyncio import ConnectionPool
1412from {{cookiecutter .project_name }}.services .redis .dependency import get_redis_pool
1513
3230
3331{% - endif % }
3432
33+ {% - if cookiecutter .enable_nats == "True" % }
34+ from natsrpy import Nats
35+ from {{cookiecutter .project_name }}.services .nats .dependencies import get_nats
36+ from {{cookiecutter .project_name }}.services .nats .lifespan import (init_nats ,
37+ shutdown_nats )
38+ {% - endif % }
39+
3540from {{cookiecutter .project_name }}.settings import settings
3641from {{cookiecutter .project_name }}.web .application import get_app
3742
@@ -457,17 +462,28 @@ async def test_kafka_producer() -> AsyncGenerator[AIOKafkaProducer, None]:
457462
458463{% - endif % }
459464
465+
466+ {% - if cookiecutter .enable_nats == "True" % }
467+
468+ @pytest .fixture
469+ async def test_nats () -> AsyncGenerator [Nats , None ]:
470+ """Creat test nats client."""
471+ app_mock = Mock ()
472+ await init_nats (app_mock )
473+ yield app_mock .state .nats
474+ await shutdown_nats (app_mock )
475+
476+ {% - endif % }
477+
460478{% if cookiecutter .enable_redis == "True" - % }
461479@pytest .fixture
462- async def fake_redis_pool () -> AsyncGenerator [ConnectionPool , None ]:
480+ async def test_redis_pool () -> AsyncGenerator [ConnectionPool , None ]:
463481 """
464482 Get instance of a fake redis.
465483
466- :yield: FakeRedis instance.
484+ :yield: ConnectionPool instance.
467485 """
468- server = FakeServer ()
469- server .connected = True
470- pool = ConnectionPool (connection_class = FakeConnection , server = server )
486+ pool = ConnectionPool .from_url (str (settings .redis_url ))
471487
472488 yield pool
473489
@@ -483,14 +499,17 @@ def fastapi_app(
483499 dbpool : AsyncConnectionPool [Any ],
484500 {% - endif % }
485501 {% if cookiecutter .enable_redis == "True" - % }
486- fake_redis_pool : ConnectionPool ,
502+ test_redis_pool : ConnectionPool ,
487503 {% - endif % }
488504 {% - if cookiecutter .enable_rmq == 'True' % }
489505 test_rmq_pool : Pool [Channel ],
490506 {% - endif % }
491507 {% - if cookiecutter .enable_kafka == "True" % }
492508 test_kafka_producer : AIOKafkaProducer ,
493509 {% - endif % }
510+ {% - if cookiecutter .enable_nats == "True" % }
511+ test_nats : Nats ,
512+ {% - endif % }
494513) -> FastAPI :
495514 """
496515 Fixture for creating FastAPI app.
@@ -504,14 +523,17 @@ def fastapi_app(
504523 application .dependency_overrides [get_db_pool ] = lambda : dbpool
505524 {% - endif % }
506525 {% - if cookiecutter .enable_redis == "True" % }
507- application .dependency_overrides [get_redis_pool ] = lambda : fake_redis_pool
526+ application .dependency_overrides [get_redis_pool ] = lambda : test_redis_pool
508527 {% - endif % }
509528 {% - if cookiecutter .enable_rmq == 'True' % }
510529 application .dependency_overrides [get_rmq_channel_pool ] = lambda : test_rmq_pool
511530 {% - endif % }
512531 {% - if cookiecutter .enable_kafka == "True" % }
513532 application .dependency_overrides [get_kafka_producer ] = lambda : test_kafka_producer
514533 {% - endif % }
534+ {% - if cookiecutter .enable_nats == "True" % }
535+ application .dependency_overrides [get_nats ] = lambda : test_nats
536+ {% - endif % }
515537 return application # noqa: RET504
516538
517539
0 commit comments