Skip to content

Commit 3e91374

Browse files
committed
migrate alloydb
1 parent 08e4230 commit 3e91374

File tree

5 files changed

+66
-193
lines changed

5 files changed

+66
-193
lines changed
Lines changed: 23 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,44 @@
11
from __future__ import annotations
22

3-
import os
4-
import sys
5-
from pathlib import Path
63
from typing import TYPE_CHECKING
74

85
import psycopg
96
import pytest
107

8+
from pytest_databases._service import DockerService
119
from pytest_databases.docker import DockerServiceRegistry
12-
from pytest_databases.helpers import simple_string_hash
13-
14-
if TYPE_CHECKING:
15-
from collections.abc import Generator
16-
17-
18-
COMPOSE_PROJECT_NAME: str = f"pytest-databases-alloydb-{simple_string_hash(__file__)}"
19-
20-
21-
def alloydb_omni_responsive(host: str, port: int, user: str, password: str, database: str) -> bool:
22-
try:
23-
conn = psycopg.connect(
24-
host=host,
25-
port=port,
26-
user=user,
27-
database=database,
28-
password=password,
29-
)
30-
except Exception: # noqa: BLE001
31-
return False
32-
33-
try:
34-
db_open = conn.fetchrow("SELECT 1")
35-
return bool(db_open is not None and db_open[0] == 1)
36-
finally:
37-
conn.close()
38-
39-
40-
@pytest.fixture(scope="session")
41-
def alloydb_compose_project_name() -> str:
42-
return os.environ.get("COMPOSE_PROJECT_NAME", COMPOSE_PROJECT_NAME)
43-
44-
45-
@pytest.fixture(autouse=False, scope="session")
46-
def alloydb_docker_services(
47-
alloydb_compose_project_name: str, worker_id: str = "main"
48-
) -> Generator[DockerServiceRegistry, None, None]:
49-
if os.getenv("GITHUB_ACTIONS") == "true" and sys.platform != "linux":
50-
pytest.skip("Docker not available on this platform")
51-
52-
with DockerServiceRegistry(worker_id, compose_project_name=alloydb_compose_project_name) as registry:
53-
yield registry
10+
from pytest_databases.docker.postgres import (
11+
_make_connection_string,
12+
_provide_postgres_service,
13+
PostgresService as AlloyDBService,
14+
)
5415

16+
__all__ = ("AlloyDBService",)
5517

56-
@pytest.fixture(scope="session")
57-
def postgres_user() -> str:
58-
return "postgres"
5918

60-
61-
@pytest.fixture(scope="session")
62-
def postgres_password() -> str:
63-
return "super-secret"
64-
65-
66-
@pytest.fixture(scope="session")
67-
def postgres_database() -> str:
68-
return "postgres"
69-
70-
71-
@pytest.fixture(scope="session")
72-
def alloydb_omni_port() -> int:
73-
return 5420
74-
75-
76-
@pytest.fixture(scope="session")
77-
def default_alloydb_omni_service_name() -> str:
78-
return "alloydb"
79-
80-
81-
@pytest.fixture(scope="session")
82-
def alloydb_docker_compose_files() -> list[Path]:
83-
return [Path(Path(__file__).parent / "docker-compose.alloydb-omni.yml")]
84-
85-
86-
@pytest.fixture(scope="session")
87-
def alloydb_docker_ip(alloydb_docker_services: DockerServiceRegistry) -> str:
88-
return alloydb_docker_services.docker_ip
19+
if TYPE_CHECKING:
20+
from collections.abc import Generator
8921

9022

91-
# alias to the latest
9223
@pytest.fixture(autouse=False, scope="session")
9324
def alloydb_omni_service(
94-
alloydb_docker_services: DockerServiceRegistry,
95-
default_alloydb_omni_service_name: str,
96-
alloydb_docker_compose_files: list[Path],
97-
alloydb_omni_port: int,
98-
postgres_database: str,
99-
postgres_user: str,
100-
postgres_password: str,
101-
) -> Generator[None, None, None]:
102-
os.environ["POSTGRES_PASSWORD"] = postgres_password
103-
os.environ["POSTGRES_USER"] = postgres_user
104-
os.environ["POSTGRES_DATABASE"] = postgres_database
105-
os.environ[f"{default_alloydb_omni_service_name.upper()}_PORT"] = str(alloydb_omni_port)
106-
alloydb_docker_services.start(
107-
name=default_alloydb_omni_service_name,
108-
docker_compose_files=alloydb_docker_compose_files,
109-
timeout=45,
110-
pause=1,
111-
check=alloydb_omni_responsive,
112-
port=alloydb_omni_port,
113-
database=postgres_database,
114-
user=postgres_user,
115-
password=postgres_password,
116-
)
117-
yield
25+
docker_service: DockerService,
26+
) -> Generator[DockerServiceRegistry, None, None]:
27+
with _provide_postgres_service(
28+
docker_service=docker_service, image="google/alloydbomni", name="alloydb-omni"
29+
) as service:
30+
yield service
11831

11932

12033
@pytest.fixture(autouse=False, scope="session")
121-
def alloydb_omni_startup_connection(
122-
alloydb_omni_service: DockerServiceRegistry,
123-
alloydb_docker_ip: str,
124-
alloydb_omni_port: int,
125-
postgres_database: str,
126-
postgres_user: str,
127-
postgres_password: str,
128-
) -> Generator[psycopg.Connection, None, None]:
34+
def alloydb_omni_startup_connection(alloydb_omni_service: AlloyDBService) -> Generator[psycopg.Connection, None, None]:
12935
with psycopg.connect(
130-
host=alloydb_docker_ip,
131-
port=alloydb_omni_port,
132-
user=postgres_user,
133-
database=postgres_database,
134-
password=postgres_password,
36+
_make_connection_string(
37+
host=alloydb_omni_service.host,
38+
port=alloydb_omni_service.port,
39+
user=alloydb_omni_service.user,
40+
database=alloydb_omni_service.database,
41+
password=alloydb_omni_service.password,
42+
)
13543
) as conn:
13644
yield conn

src/pytest_databases/docker/docker-compose.alloydb-omni.yml

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/pytest_databases/docker/postgres.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def _make_connection_string(host: str, port: int, user: str, password: str, data
2121

2222
@dataclasses.dataclass
2323
class PostgresService(ServiceContainer):
24-
db: str
24+
database: str
2525
password: str
2626
user: str
2727

@@ -61,7 +61,7 @@ def check(_service: ServiceContainer) -> bool:
6161
exec_after_start=f"psql -U postgres -d postgres -c 'CREATE DATABASE {db_name};'",
6262
) as service:
6363
yield PostgresService(
64-
db=db_name,
64+
database=db_name,
6565
host=service.host,
6666
port=service.port,
6767
user="postgres",
@@ -140,7 +140,7 @@ def postgres_startup_connection(
140140
port=postgres_service.port,
141141
user=postgres_service.user,
142142
password=postgres_service.password,
143-
database=postgres_service.db,
143+
database=postgres_service.database,
144144
),
145145
) as conn:
146146
yield conn
@@ -156,7 +156,7 @@ def postgres11_startup_connection(
156156
port=postgres_11_service.port,
157157
user=postgres_11_service.user,
158158
password=postgres_11_service.password,
159-
database=postgres_11_service.db,
159+
database=postgres_11_service.database,
160160
),
161161
) as conn:
162162
yield conn
@@ -172,7 +172,7 @@ def postgres12_startup_connection(
172172
port=postgres_12_service.port,
173173
user=postgres_12_service.user,
174174
password=postgres_12_service.password,
175-
database=postgres_12_service.db,
175+
database=postgres_12_service.database,
176176
),
177177
) as conn:
178178
yield conn
@@ -188,7 +188,7 @@ def postgres13_startup_connection(
188188
port=postgres_13_service.port,
189189
user=postgres_13_service.user,
190190
password=postgres_13_service.password,
191-
database=postgres_13_service.db,
191+
database=postgres_13_service.database,
192192
),
193193
) as conn:
194194
yield conn
@@ -204,7 +204,7 @@ def postgres14_startup_connection(
204204
port=postgres_14_service.port,
205205
user=postgres_14_service.user,
206206
password=postgres_14_service.password,
207-
database=postgres_14_service.db,
207+
database=postgres_14_service.database,
208208
),
209209
) as conn:
210210
yield conn
@@ -220,7 +220,7 @@ def postgres15_startup_connection(
220220
port=postgres_15_service.port,
221221
user=postgres_15_service.user,
222222
password=postgres_15_service.password,
223-
database=postgres_15_service.db,
223+
database=postgres_15_service.database,
224224
),
225225
) as conn:
226226
yield conn
@@ -236,7 +236,7 @@ def postgres16_startup_connection(
236236
port=postgres_16_service.port,
237237
user=postgres_16_service.user,
238238
password=postgres_16_service.password,
239-
database=postgres_16_service.db,
239+
database=postgres_16_service.database,
240240
),
241241
) as conn:
242242
yield conn
@@ -252,7 +252,7 @@ def postgres17_startup_connection(
252252
port=postgres_17_service.port,
253253
user=postgres_17_service.user,
254254
password=postgres_17_service.password,
255-
database=postgres_17_service.db,
255+
database=postgres_17_service.database,
256256
),
257257
) as conn:
258258
yield conn

tests/docker/test_alloydb_omni.py

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,41 @@
22

33
from typing import TYPE_CHECKING
44

5-
import pytest
5+
import psycopg
66

7-
from pytest_databases.docker.alloydb_omni import alloydb_omni_responsive
7+
from pytest_databases.docker.alloydb_omni import AlloyDBService
8+
from pytest_databases.docker.postgres import _make_connection_string
89

910
if TYPE_CHECKING:
10-
import psycopg
11+
pass
1112

12-
from pytest_databases.docker import DockerServiceRegistry
1313

14-
15-
pytestmark = pytest.mark.skip
14+
# pytestmark = pytest.mark.skip
1615

1716
pytest_plugins = [
1817
"pytest_databases.docker.alloydb_omni",
1918
]
2019

2120

22-
def test_alloydb_omni_default_config(
23-
default_alloydb_omni_service_name: str,
24-
alloydb_omni_port: int,
25-
postgres_database: str,
26-
postgres_user: str,
27-
postgres_password: str,
28-
) -> None:
29-
assert default_alloydb_omni_service_name == "alloydb"
30-
assert alloydb_omni_port == 5420
31-
assert postgres_database == "postgres"
32-
assert postgres_user == "postgres"
33-
assert postgres_password == "super-secret"
34-
35-
36-
def test_alloydb_omni_services(
37-
alloydb_docker_ip: str,
38-
alloydb_omni_service: DockerServiceRegistry,
39-
alloydb_omni_port: int,
40-
postgres_database: str,
41-
postgres_user: str,
42-
postgres_password: str,
43-
) -> None:
44-
ping = alloydb_omni_responsive(
45-
alloydb_docker_ip,
46-
port=alloydb_omni_port,
47-
database=postgres_database,
48-
user=postgres_user,
49-
password=postgres_password,
50-
)
51-
assert ping
52-
53-
54-
def test_alloydb_omni_service_after_start(
55-
postgres_startup_connection: psycopg.Connection,
56-
) -> None:
57-
postgres_startup_connection.execute("CREATE TABLE if not exists simple_table as SELECT 1")
58-
result = postgres_startup_connection.execute("select * from simple_table").fetchone()
21+
def check(service: AlloyDBService) -> bool:
22+
with psycopg.connect(
23+
_make_connection_string(
24+
host=service.host,
25+
port=service.port,
26+
user=service.user,
27+
database=service.database,
28+
password=service.password,
29+
)
30+
) as conn:
31+
db_open = conn.execute("SELECT 1").fetchone()
32+
return bool(db_open is not None and db_open[0] == 1)
33+
34+
35+
def test_alloydb_omni_services(alloydb_omni_service: AlloyDBService) -> None:
36+
assert check(alloydb_omni_service)
37+
38+
39+
def test_alloydb_omni_service_after_start(alloydb_omni_startup_connection: psycopg.Connection) -> None:
40+
alloydb_omni_startup_connection.execute("CREATE TABLE if not exists simple_table as SELECT 1")
41+
result = alloydb_omni_startup_connection.execute("select * from simple_table").fetchone()
5942
assert bool(result is not None and result[0] == 1)

0 commit comments

Comments
 (0)