@@ -61,37 +61,44 @@ def pool(postgres_url: str) -> sessionmaker:
6161
6262@pytest .fixture (scope = "session" )
6363def postgres_url (app_config : Config ) -> str :
64- if os .name == "nt" : # windows testcontainers now not working
65- yield app_config .db .uri
66- else :
67- with PostgresContainer ("postgres:11" ) as postgres :
68- postgres_url_ = postgres .get_connection_url ().replace ("psycopg2" , "asyncpg" )
69- logger .info ("postgres url %s" , postgres_url_ )
70- app_config .db = DBConfig (postgres_url_ )
71- yield postgres_url_
64+ postgres = PostgresContainer ("postgres:11" )
65+ if os .name == "nt" : # TODO workaround from testcontainers/testcontainers-python#108
66+ postgres .get_container_host_ip = lambda : 'localhost'
67+ try :
68+ postgres .start ()
69+ postgres_url_ = postgres .get_connection_url ().replace ("psycopg2" , "asyncpg" )
70+ logger .info ("postgres url %s" , postgres_url_ )
71+ app_config .db = DBConfig (postgres_url_ )
72+ yield postgres_url_
73+ finally :
74+ postgres .stop ()
7275
7376
74- @pytest_asyncio .fixture (scope = "session" )
75- async def redis (app_config : Config ) -> Redis :
76- if os .name == "nt" : # windows testcontainers now not working
77- async with create_redis (app_config .redis ) as r :
78- yield r
79- else :
80- with RedisContainer ("redis:latest" ) as redis_container :
81- url = redis_container .get_container_host_ip ()
82- port = redis_container .get_exposed_port (redis_container .port_to_expose )
83- app_config .redis .url = url
84- app_config .redis .port = port
85- r = create_redis (app_config .redis )
86- yield r
77+ @pytest .fixture (scope = "session" )
78+ def redis (app_config : Config ) -> Redis :
79+ redis_container = RedisContainer ("redis:latest" )
80+ if os .name == "nt" : # TODO workaround from testcontainers/testcontainers-python#108
81+ redis_container .get_container_host_ip = lambda : 'localhost'
82+ try :
83+ redis_container .start ()
84+ url = redis_container .get_container_host_ip ()
85+ port = redis_container .get_exposed_port (redis_container .port_to_expose )
86+ app_config .redis .url = url
87+ app_config .redis .port = port
88+ r = create_redis (app_config .redis )
89+ yield r
90+ finally :
91+ redis_container .stop ()
8792
8893
8994@pytest_asyncio .fixture (scope = "session" )
9095async def scheduler (pool : sessionmaker , redis : Redis , bot : Bot , app_config : Config ):
9196 s = create_scheduler (pool = pool , redis = redis , bot = bot , redis_config = app_config .redis )
92- await s .start ()
93- yield s
94- await s .close ()
97+ try :
98+ await s .start ()
99+ yield s
100+ finally :
101+ await s .close ()
95102
96103
97104@pytest .fixture (scope = "session" )
0 commit comments