forked from dbfixtures/pytest-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
65 lines (53 loc) · 2.18 KB
/
config.py
File metadata and controls
65 lines (53 loc) · 2.18 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
"""Plugin's configuration."""
from pathlib import Path
from typing import Any, List, Optional, TypedDict, Union
from _pytest._py.path import LocalPath
from pytest import FixtureRequest
class PostgresqlConfigDict(TypedDict):
"""Typed Config dictionary."""
exec: str
host: str
port: Optional[str]
port_search_count: int
user: str
password: str
options: str
startparams: str
unixsocketdir: str
dbname: str
load: List[Union[Path, str]]
postgres_options: str
drop_test_database: bool
def get_config(request: FixtureRequest) -> PostgresqlConfigDict:
"""Return a dictionary with config options."""
def get_postgresql_option(option: str, default: Any = None) -> Any:
name = "postgresql_" + option
return request.config.getoption(name) or request.config.getini(name) or default
load_paths = detect_paths(get_postgresql_option("load"))
return PostgresqlConfigDict(
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", default=5)),
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"),
)
def detect_paths(load_paths: List[Union[LocalPath, str]]) -> List[Union[Path, str]]:
"""Convert path to sql files to Path instances."""
converted_load_paths: List[Union[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