Skip to content

Commit 181bbd1

Browse files
chore(docs): add missing samples for async deletes (googleapis#973)
1 parent e46f037 commit 181bbd1

2 files changed

Lines changed: 404 additions & 0 deletions

File tree

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
# Copyright 2024, Google LLC
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import datetime
17+
import os
18+
from typing import AsyncGenerator
19+
20+
from google.cloud._helpers import _microseconds_from_datetime
21+
import pytest, pytest_asyncio
22+
23+
import deletes_snippets_async
24+
25+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
26+
BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"]
27+
TABLE_ID_PREFIX = "mobile-time-series-{}"
28+
29+
30+
@pytest_asyncio.fixture
31+
async def table_id() -> AsyncGenerator[str, None]:
32+
table_id = _create_table()
33+
await _populate_table(table_id)
34+
yield table_id
35+
_delete_table(table_id)
36+
37+
38+
def _create_table():
39+
from google.cloud import bigtable
40+
import uuid
41+
42+
client = bigtable.Client(project=PROJECT, admin=True)
43+
instance = client.instance(BIGTABLE_INSTANCE)
44+
45+
table_id = TABLE_ID_PREFIX.format(str(uuid.uuid4())[:16])
46+
table = instance.table(table_id)
47+
if table.exists():
48+
table.delete()
49+
50+
table.create(column_families={"stats_summary": None, "cell_plan": None})
51+
client.close()
52+
return table_id
53+
54+
55+
def _delete_table(table_id: str):
56+
from google.cloud import bigtable
57+
58+
client = bigtable.Client(project=PROJECT, admin=True)
59+
instance = client.instance(BIGTABLE_INSTANCE)
60+
table = instance.table(table_id)
61+
table.delete()
62+
client.close()
63+
64+
65+
async def _populate_table(table_id):
66+
from google.cloud.bigtable.data import (
67+
BigtableDataClientAsync,
68+
RowMutationEntry,
69+
SetCell,
70+
)
71+
72+
timestamp = datetime.datetime(2019, 5, 1)
73+
timestamp_minus_hr = timestamp - datetime.timedelta(hours=1)
74+
75+
async with (BigtableDataClientAsync(project=PROJECT) as client):
76+
async with client.get_table(BIGTABLE_INSTANCE, table_id) as table:
77+
async with table.mutations_batcher() as batcher:
78+
await batcher.append(
79+
RowMutationEntry(
80+
"phone#4c410523#20190501",
81+
[
82+
SetCell(
83+
"stats_summary",
84+
"connected_cell",
85+
1,
86+
_microseconds_from_datetime(timestamp),
87+
),
88+
SetCell(
89+
"stats_summary",
90+
"connected_cell",
91+
1,
92+
_microseconds_from_datetime(timestamp),
93+
),
94+
SetCell(
95+
"stats_summary",
96+
"connected_wifi",
97+
1,
98+
_microseconds_from_datetime(timestamp),
99+
),
100+
SetCell(
101+
"stats_summary",
102+
"os_build",
103+
"PQ2A.190405.003",
104+
_microseconds_from_datetime(timestamp),
105+
),
106+
SetCell(
107+
"cell_plan",
108+
"data_plan_01gb",
109+
"true",
110+
_microseconds_from_datetime(timestamp_minus_hr),
111+
),
112+
SetCell(
113+
"cell_plan",
114+
"data_plan_01gb",
115+
"false",
116+
_microseconds_from_datetime(timestamp),
117+
),
118+
SetCell(
119+
"cell_plan",
120+
"data_plan_05gb",
121+
"true",
122+
_microseconds_from_datetime(timestamp),
123+
),
124+
],
125+
)
126+
)
127+
await batcher.append(
128+
RowMutationEntry(
129+
"phone#4c410523#20190502",
130+
[
131+
SetCell(
132+
"stats_summary",
133+
"connected_cell",
134+
1,
135+
_microseconds_from_datetime(timestamp),
136+
),
137+
SetCell(
138+
"stats_summary",
139+
"connected_wifi",
140+
1,
141+
_microseconds_from_datetime(timestamp),
142+
),
143+
SetCell(
144+
"stats_summary",
145+
"os_build",
146+
"PQ2A.190405.004",
147+
_microseconds_from_datetime(timestamp),
148+
),
149+
SetCell(
150+
"cell_plan",
151+
"data_plan_05gb",
152+
"true",
153+
_microseconds_from_datetime(timestamp),
154+
),
155+
],
156+
)
157+
)
158+
await batcher.append(
159+
RowMutationEntry(
160+
"phone#4c410523#20190505",
161+
[
162+
SetCell(
163+
"stats_summary",
164+
"connected_cell",
165+
0,
166+
_microseconds_from_datetime(timestamp),
167+
),
168+
SetCell(
169+
"stats_summary",
170+
"connected_wifi",
171+
1,
172+
_microseconds_from_datetime(timestamp),
173+
),
174+
SetCell(
175+
"stats_summary",
176+
"os_build",
177+
"PQ2A.190406.000",
178+
_microseconds_from_datetime(timestamp),
179+
),
180+
SetCell(
181+
"cell_plan",
182+
"data_plan_05gb",
183+
"true",
184+
_microseconds_from_datetime(timestamp),
185+
),
186+
],
187+
)
188+
)
189+
await batcher.append(
190+
RowMutationEntry(
191+
"phone#5c10102#20190501",
192+
[
193+
SetCell(
194+
"stats_summary",
195+
"connected_cell",
196+
1,
197+
_microseconds_from_datetime(timestamp),
198+
),
199+
SetCell(
200+
"stats_summary",
201+
"connected_wifi",
202+
1,
203+
_microseconds_from_datetime(timestamp),
204+
),
205+
SetCell(
206+
"stats_summary",
207+
"os_build",
208+
"PQ2A.190401.002",
209+
_microseconds_from_datetime(timestamp),
210+
),
211+
SetCell(
212+
"cell_plan",
213+
"data_plan_10gb",
214+
"true",
215+
_microseconds_from_datetime(timestamp),
216+
),
217+
],
218+
)
219+
)
220+
await batcher.append(
221+
RowMutationEntry(
222+
"phone#5c10102#20190502",
223+
[
224+
SetCell(
225+
"stats_summary",
226+
"connected_cell",
227+
1,
228+
_microseconds_from_datetime(timestamp),
229+
),
230+
SetCell(
231+
"stats_summary",
232+
"connected_wifi",
233+
0,
234+
_microseconds_from_datetime(timestamp),
235+
),
236+
SetCell(
237+
"stats_summary",
238+
"os_build",
239+
"PQ2A.190406.000",
240+
_microseconds_from_datetime(timestamp),
241+
),
242+
SetCell(
243+
"cell_plan",
244+
"data_plan_10gb",
245+
"true",
246+
_microseconds_from_datetime(timestamp),
247+
),
248+
],
249+
)
250+
)
251+
252+
253+
def assert_output_match(capsys, expected):
254+
out, _ = capsys.readouterr()
255+
assert out == expected
256+
257+
258+
@pytest.mark.asyncio
259+
async def test_delete_from_column(capsys, table_id):
260+
await deletes_snippets_async.delete_from_column(
261+
PROJECT, BIGTABLE_INSTANCE, table_id
262+
)
263+
assert_output_match(capsys, "")
264+
265+
266+
@pytest.mark.asyncio
267+
async def test_delete_from_column_family(capsys, table_id):
268+
await deletes_snippets_async.delete_from_column_family(
269+
PROJECT, BIGTABLE_INSTANCE, table_id
270+
)
271+
assert_output_match(capsys, "")
272+
273+
274+
@pytest.mark.asyncio
275+
async def test_delete_from_row(capsys, table_id):
276+
await deletes_snippets_async.delete_from_row(PROJECT, BIGTABLE_INSTANCE, table_id)
277+
assert_output_match(capsys, "")
278+
279+
280+
@pytest.mark.asyncio
281+
async def test_streaming_and_batching(capsys, table_id):
282+
await deletes_snippets_async.streaming_and_batching(
283+
PROJECT, BIGTABLE_INSTANCE, table_id
284+
)
285+
assert_output_match(capsys, "")
286+
287+
288+
@pytest.mark.asyncio
289+
async def test_check_and_mutate(capsys, table_id):
290+
await deletes_snippets_async.check_and_mutate(PROJECT, BIGTABLE_INSTANCE, table_id)
291+
assert_output_match(capsys, "")

0 commit comments

Comments
 (0)