-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_postgres.py
More file actions
173 lines (156 loc) · 5.42 KB
/
test_postgres.py
File metadata and controls
173 lines (156 loc) · 5.42 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
from __future__ import annotations
from typing import TYPE_CHECKING
from hypothesis import assume, given
from hypothesis.strategies import DrawFn, booleans, composite, lists, none, sampled_from
from pytest import raises
from sqlalchemy import URL, Column, Integer, MetaData, Table
from utilities.hypothesis import integers, temp_paths, text_ascii, urls
from utilities.postgres import (
_build_pg_dump,
_build_pg_restore_or_psql,
_path_pg_dump,
_PGDumpFormat,
_resolve_data_only_and_clean,
_ResolveDataOnlyAndCleanError,
pg_dump,
restore,
)
from utilities.typing import get_literal_elements
if TYPE_CHECKING:
from pathlib import Path
@composite
def tables(draw: DrawFn, /) -> list[Table | str]:
metadata = MetaData()
names = draw(lists(text_ascii(min_size=1), max_size=5, unique=True))
tables = [
Table(n, metadata, Column("id", Integer, primary_key=True)) for n in names
]
return [draw(sampled_from([n, t])) for n, t in zip(names, tables, strict=True)]
class TestPGDump:
@given(
url=urls(all_=True), path=temp_paths(), logger=text_ascii(min_size=1) | none()
)
async def test_main(self, *, url: URL, path: Path, logger: str | None) -> None:
_ = await pg_dump(url, path, dry_run=True, logger=logger)
@given(
url=urls(all_=True),
path=temp_paths(),
format_=sampled_from(get_literal_elements(_PGDumpFormat)),
jobs=integers(min_value=0) | none(),
create=booleans(),
extension=lists(text_ascii(min_size=1)) | none(),
extension_exc=lists(text_ascii(min_size=1)) | none(),
schema=lists(text_ascii(min_size=1)) | none(),
schema_exc=lists(text_ascii(min_size=1)) | none(),
table=tables() | none(),
table_exc=tables() | none(),
inserts=booleans(),
on_conflict_do_nothing=booleans(),
role=text_ascii(min_size=1) | none(),
docker_container=text_ascii(min_size=1) | none(),
)
def test_build(
self,
*,
url: URL,
path: Path,
format_: _PGDumpFormat,
jobs: int | None,
create: bool,
extension: list[str] | None,
extension_exc: list[str] | None,
schema: list[str] | None,
schema_exc: list[str] | None,
table: list[Table | str] | None,
table_exc: list[Table | str] | None,
inserts: bool,
on_conflict_do_nothing: bool,
role: str | None,
docker_container: str | None,
) -> None:
_ = _build_pg_dump(
url,
path,
format_=format_,
jobs=jobs,
create=create,
extension=extension,
extension_exc=extension_exc,
schema=schema,
schema_exc=schema_exc,
table=table,
table_exc=table_exc,
inserts=inserts,
on_conflict_do_nothing=on_conflict_do_nothing,
role=role,
docker_container=docker_container,
)
@given(
url=urls(all_=True),
path=temp_paths(),
format_=sampled_from(get_literal_elements(_PGDumpFormat)),
jobs=integers(min_value=0),
)
def test_jobs(
self, *, url: URL, path: Path, format_: _PGDumpFormat, jobs: int
) -> None:
_ = _build_pg_dump(url, path, format_=format_, jobs=jobs)
@given(path=temp_paths(), format_=sampled_from(get_literal_elements(_PGDumpFormat)))
def test_path(self, *, path: Path, format_: _PGDumpFormat) -> None:
path = _path_pg_dump(path, format_=format_)
assert path.suffix in [".sql", ".pgdump", "", ".tar"]
class TestResolveDataOnlyAndClean:
@given(data_only=booleans(), clean=booleans())
def test_main(self, *, data_only: bool, clean: bool) -> None:
_ = assume(not (data_only and clean))
_ = _resolve_data_only_and_clean(data_only=data_only, clean=clean)
def test_erorr(self) -> None:
with raises(
_ResolveDataOnlyAndCleanError,
match=r"Cannot use '--data-only' and '--clean' together",
):
_ = _resolve_data_only_and_clean(data_only=True, clean=True)
class TestRestore:
@given(
url=urls(all_=True), path=temp_paths(), logger=text_ascii(min_size=1) | none()
)
async def test_main(self, *, url: URL, path: Path, logger: str | None) -> None:
_ = await restore(url, path, dry_run=True, logger=logger)
@given(
url=urls(all_=True),
path=temp_paths(),
psql=booleans(),
create=booleans(),
jobs=integers(min_value=0) | none(),
schema=lists(text_ascii(min_size=1)) | none(),
schema_exc=lists(text_ascii(min_size=1)) | none(),
table=tables() | none(),
role=text_ascii(min_size=1) | none(),
docker_container=text_ascii(min_size=1) | none(),
)
def test_build(
self,
*,
url: URL,
path: Path,
psql: bool,
create: bool,
jobs: int | None,
schema: list[str] | None,
schema_exc: list[str] | None,
table: list[Table | str] | None,
role: str | None,
docker_container: str | None,
) -> None:
_ = _build_pg_restore_or_psql(
url,
path,
psql=psql,
create=create,
jobs=jobs,
schema=schema,
schema_exc=schema_exc,
table=table,
role=role,
docker_container=docker_container,
)