-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathconfig.py
More file actions
68 lines (56 loc) · 2.16 KB
/
config.py
File metadata and controls
68 lines (56 loc) · 2.16 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
"""Plugin's configuration."""
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from _pytest._py.path import LocalPath
from pytest import FixtureRequest
@dataclass(frozen=True)
class PostgreSQLConfig:
"""PostgreSQL Config."""
exec: str
host: str
port: str | None
port_search_count: int
user: str
password: str
options: str
startparams: str
unixsocketdir: str
dbname: str
load: list[Path | str]
postgres_options: str
drop_test_database: bool
def get_config(request: FixtureRequest) -> PostgreSQLConfig:
"""Return a PostgreSQLConfig instance with configuration options."""
def get_postgresql_option(option: str) -> Any:
name = "postgresql_" + option
return request.config.getoption(name) or request.config.getini(name)
load_paths: list[Path | str] = detect_paths(get_postgresql_option("load"))
cfg = PostgreSQLConfig(
exec=get_postgresql_option("exec"),
host=get_postgresql_option("host"),
port=get_postgresql_option("port"),
# Parse as int, because if it's defined in an INI file then it'll always be a string
port_search_count=int(get_postgresql_option("port_search_count")),
user=get_postgresql_option("user"),
password=get_postgresql_option("password"),
options=get_postgresql_option("options"),
startparams=get_postgresql_option("startparams"),
unixsocketdir=get_postgresql_option("unixsocketdir"),
dbname=get_postgresql_option("dbname"),
load=load_paths,
postgres_options=get_postgresql_option("postgres_options"),
drop_test_database=request.config.getoption("postgresql_drop_test_database"),
)
return cfg
def detect_paths(load_paths: list[LocalPath | str]) -> list[Path | str]:
"""Convert path to sql files to Path instances."""
converted_load_paths: list[Path | str] = []
for path in load_paths:
if isinstance(path, LocalPath):
path = str(path)
if path.endswith(".sql"):
converted_load_paths.append(Path(path))
else:
converted_load_paths.append(path)
return converted_load_paths