1+ import os
2+ import random
3+ from typing import AsyncGenerator
4+
15import pytest
26
7+ from psqlpy import PSQLPool
8+
39
4- @pytest .fixture (scope = "session" )
10+ @pytest .fixture ()
511def anyio_backend () -> str :
612 """
713 Anyio backend.
@@ -10,3 +16,83 @@ def anyio_backend() -> str:
1016 :return: backend name.
1117 """
1218 return "asyncio"
19+
20+
21+ def random_string (length : int = 10 ) -> str :
22+ return "" .join (random .choice ("AbCdEfG" ) for _ in range (length ))
23+
24+
25+ @pytest .fixture ()
26+ def postgres_host () -> str :
27+ return os .environ .get ("POSTGRES_HOST" , "localhost" )
28+
29+
30+ @pytest .fixture ()
31+ def postgres_user () -> str :
32+ return os .environ .get ("POSTGRES_USER" , "postgres" )
33+
34+
35+ @pytest .fixture ()
36+ def postgres_password () -> str :
37+ return os .environ .get ("POSTGRES_PASSWORD" , "postgres" )
38+
39+
40+ @pytest .fixture ()
41+ def postgres_port () -> int :
42+ return int (os .environ .get ("POSTGRES_PORT" , 5432 ))
43+
44+
45+ @pytest .fixture ()
46+ def postgres_dbname () -> str :
47+ return os .environ .get ("POSTGRES_DBNAME" , "psqlpy_test" )
48+
49+
50+ @pytest .fixture ()
51+ def table_name () -> str :
52+ return random_string ()
53+
54+
55+ @pytest .fixture ()
56+ def number_database_records () -> int :
57+ return random .randint (10 , 35 )
58+
59+
60+ @pytest .fixture ()
61+ async def psql_pool (
62+ postgres_host : str ,
63+ postgres_user : str ,
64+ postgres_password : str ,
65+ postgres_port : int ,
66+ postgres_dbname : str ,
67+ ) -> AsyncGenerator [PSQLPool , None ]:
68+ pg_pool = PSQLPool (
69+ username = postgres_user ,
70+ password = postgres_password ,
71+ host = postgres_host ,
72+ port = postgres_port ,
73+ db_name = postgres_dbname ,
74+ )
75+ await pg_pool .startup ()
76+ yield pg_pool
77+
78+
79+ @pytest .fixture (autouse = True )
80+ async def create_deafult_data_for_tests (
81+ psql_pool : PSQLPool ,
82+ table_name : str ,
83+ number_database_records : int ,
84+ ) -> AsyncGenerator [None , None ]:
85+ await psql_pool .execute (
86+ f"CREATE TABLE { table_name } (id SERIAL, name VARCHAR(255))" ,
87+ )
88+
89+ for table_id in range (1 , number_database_records + 1 ):
90+ new_name = random_string ()
91+ await psql_pool .execute (
92+ querystring = f"INSERT INTO { table_name } VALUES ($1, $2)" ,
93+ parameters = [table_id , new_name ],
94+ )
95+ yield
96+ await psql_pool .execute (
97+ f"DROP TABLE { table_name } " ,
98+ )
0 commit comments