forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_test.py
More file actions
155 lines (143 loc) · 4.61 KB
/
e2e_test.py
File metadata and controls
155 lines (143 loc) · 4.61 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
# Copyright 2022 Google LLC.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Maintainer Note: this sample presumes data exists in
# ALLOYDB_TABLE_NAME within the ALLOYDB_(cluster/instance/database)
import asyncpg # type: ignore
import conftest as conftest # python-docs-samples/alloydb/conftest.py
from google.cloud.alloydb.connector import AsyncConnector, IPTypes
import pytest
import sqlalchemy
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
def preprocess(source: str) -> str:
# Skip the cells which add data to table
if "df" in source:
return ""
# Skip the colab auth cell
if "colab" in source:
return ""
return source
async def _init_connection_pool(
connector: AsyncConnector,
db_name: str,
project_id: str,
cluster_name: str,
instance_name: str,
region: str,
password: str,
) -> AsyncEngine:
connection_string = (
f"projects/{project_id}/locations/"
f"{region}/clusters/{cluster_name}/"
f"instances/{instance_name}"
)
async def getconn() -> asyncpg.Connection:
conn: asyncpg.Connection = await connector.connect(
connection_string,
"asyncpg",
user="postgres",
password=password,
db=db_name,
ip_type=IPTypes.PUBLIC,
)
return conn
pool = create_async_engine(
"postgresql+asyncpg://",
async_creator=getconn,
max_overflow=0,
)
return pool
@pytest.mark.asyncio
async def test_embeddings_batch_processing(
project_id: str,
cluster_name: str,
instance_name: str,
region: str,
database_name: str,
password: str,
table_name: str,
) -> None:
# TODO: Create new table
# Populate the table with embeddings by running the notebook
conftest.run_notebook(
"embeddings_batch_processing.ipynb",
variables={
"project_id": project_id,
"cluster_name": cluster_name,
"database_name": database_name,
"region": region,
"instance_name": instance_name,
"table_name": table_name,
},
preprocess=preprocess,
skip_shell_commands=True,
replace={
(
"password = input(\"Please provide "
"a password to be used for 'postgres' "
"database user: \")"
): f"password = '{password}'",
(
"await create_db("
"database_name=database_name, "
"connector=connector)"
): "",
},
until_end=True,
)
# Connect to the populated table for validation and clean up
async with AsyncConnector() as connector:
pool = await _init_connection_pool(
connector,
database_name,
project_id,
cluster_name,
instance_name,
region,
password,
)
async with pool.connect() as conn:
# Validate that embeddings are non-empty for all rows
result = await conn.execute(
sqlalchemy.text(
f"SELECT COUNT(*) FROM "
f"{table_name} WHERE "
f"analysis_embedding IS NULL"
)
)
row = result.fetchone()
assert row[0] == 0
result = await conn.execute(
sqlalchemy.text(
f"SELECT COUNT(*) FROM "
f"{table_name} WHERE "
f"overview_embedding IS NULL"
)
)
row = result.fetchone()
assert row[0] == 0
# Get the table back to the original state
await conn.execute(
sqlalchemy.text(
f"UPDATE {table_name} set "
f"analysis_embedding = NULL"
)
)
await conn.execute(
sqlalchemy.text(
f"UPDATE {table_name} set "
f"overview_embedding = NULL"
)
)
await conn.commit()
await pool.dispose()