-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconftest.py
More file actions
202 lines (155 loc) · 4.58 KB
/
conftest.py
File metadata and controls
202 lines (155 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import os
import random
from typing import AsyncGenerator
import pytest
from psqlpy import ConnectionPool, Cursor
from psqlpy._internal import SslMode
from pydantic import BaseModel
class DefaultPydanticModel(BaseModel):
"""Validation model for test data based on Pydantic."""
id: int
name: str
class DefaultPythonModelClass:
"""Validation model for test data based on default Python class."""
def __init__(self, id: int, name: str) -> None:
self.id = id
self.name = name
@pytest.fixture
def anyio_backend() -> str:
"""
Anyio backend.
Backend for anyio pytest plugin.
:return: backend name.
"""
return "asyncio"
def random_string(length: int = 10) -> str:
return "".join(random.choice("AbCdEfG") for _ in range(length))
@pytest.fixture
def postgres_host() -> str:
return os.environ.get("POSTGRES_HOST", "localhost")
@pytest.fixture
def postgres_user() -> str:
return os.environ.get("POSTGRES_USER", "postgres")
@pytest.fixture
def postgres_password() -> str:
return os.environ.get("POSTGRES_PASSWORD", "postgres")
@pytest.fixture
def postgres_port() -> int:
return int(os.environ.get("POSTGRES_PORT", 5432))
@pytest.fixture
def postgres_dbname() -> str:
return os.environ.get("POSTGRES_DBNAME", "psqlpy_test")
@pytest.fixture
def table_name() -> str:
return random_string()
@pytest.fixture
def listener_table_name() -> str:
return random_string()
@pytest.fixture
def map_parameters_table_name() -> str:
return random_string()
@pytest.fixture
def number_database_records() -> int:
return random.randint(10, 35)
@pytest.fixture
def ssl_cert_file() -> str:
return os.environ.get(
"POSTGRES_CERT_FILE",
"/home/runner/work/_temp/pgdata/server.crt",
)
@pytest.fixture
async def psql_pool(
postgres_host: str,
postgres_user: str,
postgres_password: str,
postgres_port: int,
postgres_dbname: str,
) -> ConnectionPool:
return ConnectionPool(
username=postgres_user,
password=postgres_password,
host=postgres_host,
port=postgres_port,
db_name=postgres_dbname,
)
@pytest.fixture
async def psql_pool_with_cert_file(
postgres_host: str,
postgres_user: str,
postgres_password: str,
postgres_port: int,
postgres_dbname: str,
ssl_cert_file: str,
) -> ConnectionPool:
return ConnectionPool(
username=postgres_user,
password=postgres_password,
host=postgres_host,
port=postgres_port,
db_name=postgres_dbname,
ssl_mode=SslMode.VerifyFull,
ca_file=ssl_cert_file,
)
@pytest.fixture(autouse=True)
async def create_default_data_for_tests(
psql_pool: ConnectionPool,
table_name: str,
number_database_records: int,
) -> AsyncGenerator[None, None]:
connection = await psql_pool.connection()
await connection.execute(
f"CREATE TABLE {table_name} (id SERIAL, name VARCHAR(255))",
)
for table_id in range(1, number_database_records + 1):
new_name = random_string()
await connection.execute(
querystring=f"INSERT INTO {table_name} VALUES ($1, $2)",
parameters=[table_id, new_name],
)
yield
await connection.execute(
f"DROP TABLE {table_name}",
)
@pytest.fixture
async def create_table_for_listener_tests(
psql_pool: ConnectionPool,
listener_table_name: str,
) -> AsyncGenerator[None, None]:
connection = await psql_pool.connection()
await connection.execute(
f"CREATE TABLE {listener_table_name}"
f"(id SERIAL, payload VARCHAR(255),"
f"channel VARCHAR(255), process_id INT)",
)
yield
await connection.execute(
f"DROP TABLE {listener_table_name}",
)
@pytest.fixture
async def create_table_for_map_parameters_test(
psql_pool: ConnectionPool,
map_parameters_table_name: str,
) -> AsyncGenerator[None, None]:
connection = await psql_pool.connection()
await connection.execute(
f"CREATE TABLE {map_parameters_table_name}"
"(id SERIAL, name VARCHAR(255),surname VARCHAR(255), age INT)",
)
yield
await connection.execute(
f"DROP TABLE {map_parameters_table_name}",
)
@pytest.fixture
async def test_cursor(
psql_pool: ConnectionPool,
table_name: str,
) -> AsyncGenerator[Cursor, None]:
connection = await psql_pool.connection()
transaction = connection.transaction()
await transaction.begin()
cursor = transaction.cursor(
querystring=f"SELECT * FROM {table_name}",
)
await cursor.start()
yield cursor
await transaction.commit()