Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit e46f037

Browse files
chore(tests): fix async filter sample tests (#976)
1 parent f02b36f commit e46f037

6 files changed

Lines changed: 107 additions & 53 deletions

File tree

samples/snippets/filters/filter_snippets_async.py

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
# See the License for the specific language governing permissions and
1212
# limitations under the License.
1313

14-
import datetime
15-
from google.cloud.bigtable.data import Row
1614
from google.cloud._helpers import _datetime_from_microseconds
15+
from google.cloud.bigtable.data import Row
1716

1817

1918
# [START bigtable_filters_limit_row_sample_asyncio]
2019
async def filter_limit_row_sample(project_id, instance_id, table_id):
21-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
22-
from google.cloud.bigtable.data import row_filters
20+
from google.cloud.bigtable.data import (
21+
BigtableDataClientAsync,
22+
ReadRowsQuery,
23+
row_filters,
24+
)
2325

2426
query = ReadRowsQuery(row_filter=row_filters.RowSampleFilter(0.75))
2527

@@ -32,8 +34,11 @@ async def filter_limit_row_sample(project_id, instance_id, table_id):
3234
# [END bigtable_filters_limit_row_sample_asyncio]
3335
# [START bigtable_filters_limit_row_regex_asyncio]
3436
async def filter_limit_row_regex(project_id, instance_id, table_id):
35-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
36-
from google.cloud.bigtable.data import row_filters
37+
from google.cloud.bigtable.data import (
38+
BigtableDataClientAsync,
39+
ReadRowsQuery,
40+
row_filters,
41+
)
3742

3843
query = ReadRowsQuery(
3944
row_filter=row_filters.RowKeyRegexFilter(".*#20190501$".encode("utf-8"))
@@ -48,8 +53,11 @@ async def filter_limit_row_regex(project_id, instance_id, table_id):
4853
# [END bigtable_filters_limit_row_regex_asyncio]
4954
# [START bigtable_filters_limit_cells_per_col_asyncio]
5055
async def filter_limit_cells_per_col(project_id, instance_id, table_id):
51-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
52-
from google.cloud.bigtable.data import row_filters
56+
from google.cloud.bigtable.data import (
57+
BigtableDataClientAsync,
58+
ReadRowsQuery,
59+
row_filters,
60+
)
5361

5462
query = ReadRowsQuery(row_filter=row_filters.CellsColumnLimitFilter(2))
5563

@@ -62,8 +70,11 @@ async def filter_limit_cells_per_col(project_id, instance_id, table_id):
6270
# [END bigtable_filters_limit_cells_per_col_asyncio]
6371
# [START bigtable_filters_limit_cells_per_row_asyncio]
6472
async def filter_limit_cells_per_row(project_id, instance_id, table_id):
65-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
66-
from google.cloud.bigtable.data import row_filters
73+
from google.cloud.bigtable.data import (
74+
BigtableDataClientAsync,
75+
ReadRowsQuery,
76+
row_filters,
77+
)
6778

6879
query = ReadRowsQuery(row_filter=row_filters.CellsRowLimitFilter(2))
6980

@@ -76,8 +87,11 @@ async def filter_limit_cells_per_row(project_id, instance_id, table_id):
7687
# [END bigtable_filters_limit_cells_per_row_asyncio]
7788
# [START bigtable_filters_limit_cells_per_row_offset_asyncio]
7889
async def filter_limit_cells_per_row_offset(project_id, instance_id, table_id):
79-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
80-
from google.cloud.bigtable.data import row_filters
90+
from google.cloud.bigtable.data import (
91+
BigtableDataClientAsync,
92+
ReadRowsQuery,
93+
row_filters,
94+
)
8195

8296
query = ReadRowsQuery(row_filter=row_filters.CellsRowOffsetFilter(2))
8397

@@ -90,8 +104,11 @@ async def filter_limit_cells_per_row_offset(project_id, instance_id, table_id):
90104
# [END bigtable_filters_limit_cells_per_row_offset_asyncio]
91105
# [START bigtable_filters_limit_col_family_regex_asyncio]
92106
async def filter_limit_col_family_regex(project_id, instance_id, table_id):
93-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
94-
from google.cloud.bigtable.data import row_filters
107+
from google.cloud.bigtable.data import (
108+
BigtableDataClientAsync,
109+
ReadRowsQuery,
110+
row_filters,
111+
)
95112

96113
query = ReadRowsQuery(
97114
row_filter=row_filters.FamilyNameRegexFilter("stats_.*$".encode("utf-8"))
@@ -106,8 +123,11 @@ async def filter_limit_col_family_regex(project_id, instance_id, table_id):
106123
# [END bigtable_filters_limit_col_family_regex_asyncio]
107124
# [START bigtable_filters_limit_col_qualifier_regex_asyncio]
108125
async def filter_limit_col_qualifier_regex(project_id, instance_id, table_id):
109-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
110-
from google.cloud.bigtable.data import row_filters
126+
from google.cloud.bigtable.data import (
127+
BigtableDataClientAsync,
128+
ReadRowsQuery,
129+
row_filters,
130+
)
111131

112132
query = ReadRowsQuery(
113133
row_filter=row_filters.ColumnQualifierRegexFilter(
@@ -124,8 +144,11 @@ async def filter_limit_col_qualifier_regex(project_id, instance_id, table_id):
124144
# [END bigtable_filters_limit_col_qualifier_regex_asyncio]
125145
# [START bigtable_filters_limit_col_range_asyncio]
126146
async def filter_limit_col_range(project_id, instance_id, table_id):
127-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
128-
from google.cloud.bigtable.data import row_filters
147+
from google.cloud.bigtable.data import (
148+
BigtableDataClientAsync,
149+
ReadRowsQuery,
150+
row_filters,
151+
)
129152

130153
query = ReadRowsQuery(
131154
row_filter=row_filters.ColumnRangeFilter(
@@ -142,8 +165,11 @@ async def filter_limit_col_range(project_id, instance_id, table_id):
142165
# [END bigtable_filters_limit_col_range_asyncio]
143166
# [START bigtable_filters_limit_value_range_asyncio]
144167
async def filter_limit_value_range(project_id, instance_id, table_id):
145-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
146-
from google.cloud.bigtable.data import row_filters
168+
from google.cloud.bigtable.data import (
169+
BigtableDataClientAsync,
170+
ReadRowsQuery,
171+
row_filters,
172+
)
147173

148174
query = ReadRowsQuery(
149175
row_filter=row_filters.ValueRangeFilter(b"PQ2A.190405", b"PQ2A.190406")
@@ -160,8 +186,11 @@ async def filter_limit_value_range(project_id, instance_id, table_id):
160186

161187

162188
async def filter_limit_value_regex(project_id, instance_id, table_id):
163-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
164-
from google.cloud.bigtable.data import row_filters
189+
from google.cloud.bigtable.data import (
190+
BigtableDataClientAsync,
191+
ReadRowsQuery,
192+
row_filters,
193+
)
165194

166195
query = ReadRowsQuery(
167196
row_filter=row_filters.ValueRegexFilter("PQ2A.*$".encode("utf-8"))
@@ -177,8 +206,12 @@ async def filter_limit_value_regex(project_id, instance_id, table_id):
177206
# [START bigtable_filters_limit_timestamp_range_asyncio]
178207
async def filter_limit_timestamp_range(project_id, instance_id, table_id):
179208
import datetime
180-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
181-
from google.cloud.bigtable.data import row_filters
209+
210+
from google.cloud.bigtable.data import (
211+
BigtableDataClientAsync,
212+
ReadRowsQuery,
213+
row_filters,
214+
)
182215

183216
end = datetime.datetime(2019, 5, 1)
184217

@@ -193,8 +226,11 @@ async def filter_limit_timestamp_range(project_id, instance_id, table_id):
193226
# [END bigtable_filters_limit_timestamp_range_asyncio]
194227
# [START bigtable_filters_limit_block_all_asyncio]
195228
async def filter_limit_block_all(project_id, instance_id, table_id):
196-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
197-
from google.cloud.bigtable.data import row_filters
229+
from google.cloud.bigtable.data import (
230+
BigtableDataClientAsync,
231+
ReadRowsQuery,
232+
row_filters,
233+
)
198234

199235
query = ReadRowsQuery(row_filter=row_filters.BlockAllFilter(True))
200236

@@ -207,8 +243,11 @@ async def filter_limit_block_all(project_id, instance_id, table_id):
207243
# [END bigtable_filters_limit_block_all_asyncio]
208244
# [START bigtable_filters_limit_pass_all_asyncio]
209245
async def filter_limit_pass_all(project_id, instance_id, table_id):
210-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
211-
from google.cloud.bigtable.data import row_filters
246+
from google.cloud.bigtable.data import (
247+
BigtableDataClientAsync,
248+
ReadRowsQuery,
249+
row_filters,
250+
)
212251

213252
query = ReadRowsQuery(row_filter=row_filters.PassAllFilter(True))
214253

@@ -221,8 +260,11 @@ async def filter_limit_pass_all(project_id, instance_id, table_id):
221260
# [END bigtable_filters_limit_pass_all_asyncio]
222261
# [START bigtable_filters_modify_strip_value_asyncio]
223262
async def filter_modify_strip_value(project_id, instance_id, table_id):
224-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
225-
from google.cloud.bigtable.data import row_filters
263+
from google.cloud.bigtable.data import (
264+
BigtableDataClientAsync,
265+
ReadRowsQuery,
266+
row_filters,
267+
)
226268

227269
query = ReadRowsQuery(row_filter=row_filters.StripValueTransformerFilter(True))
228270

@@ -235,8 +277,11 @@ async def filter_modify_strip_value(project_id, instance_id, table_id):
235277
# [END bigtable_filters_modify_strip_value_asyncio]
236278
# [START bigtable_filters_modify_apply_label_asyncio]
237279
async def filter_modify_apply_label(project_id, instance_id, table_id):
238-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
239-
from google.cloud.bigtable.data import row_filters
280+
from google.cloud.bigtable.data import (
281+
BigtableDataClientAsync,
282+
ReadRowsQuery,
283+
row_filters,
284+
)
240285

241286
query = ReadRowsQuery(row_filter=row_filters.ApplyLabelFilter(label="labelled"))
242287

@@ -249,8 +294,11 @@ async def filter_modify_apply_label(project_id, instance_id, table_id):
249294
# [END bigtable_filters_modify_apply_label_asyncio]
250295
# [START bigtable_filters_composing_chain_asyncio]
251296
async def filter_composing_chain(project_id, instance_id, table_id):
252-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
253-
from google.cloud.bigtable.data import row_filters
297+
from google.cloud.bigtable.data import (
298+
BigtableDataClientAsync,
299+
ReadRowsQuery,
300+
row_filters,
301+
)
254302

255303
query = ReadRowsQuery(
256304
row_filter=row_filters.RowFilterChain(
@@ -270,8 +318,11 @@ async def filter_composing_chain(project_id, instance_id, table_id):
270318
# [END bigtable_filters_composing_chain_asyncio]
271319
# [START bigtable_filters_composing_interleave_asyncio]
272320
async def filter_composing_interleave(project_id, instance_id, table_id):
273-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
274-
from google.cloud.bigtable.data import row_filters
321+
from google.cloud.bigtable.data import (
322+
BigtableDataClientAsync,
323+
ReadRowsQuery,
324+
row_filters,
325+
)
275326

276327
query = ReadRowsQuery(
277328
row_filter=row_filters.RowFilterUnion(
@@ -291,8 +342,11 @@ async def filter_composing_interleave(project_id, instance_id, table_id):
291342
# [END bigtable_filters_composing_interleave_asyncio]
292343
# [START bigtable_filters_composing_condition_asyncio]
293344
async def filter_composing_condition(project_id, instance_id, table_id):
294-
from google.cloud.bigtable.data import BigtableDataClientAsync, ReadRowsQuery
295-
from google.cloud.bigtable.data import row_filters
345+
from google.cloud.bigtable.data import (
346+
BigtableDataClientAsync,
347+
ReadRowsQuery,
348+
row_filters,
349+
)
296350

297351
query = ReadRowsQuery(
298352
row_filter=row_filters.ConditionalRowFilter(

samples/snippets/filters/filter_snippets_async_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import datetime
1616
import os
17-
import time
1817

1918
import inspect
2019
from typing import AsyncGenerator
@@ -26,7 +25,6 @@
2625
from . import filter_snippets_async
2726
from google.cloud._helpers import (
2827
_microseconds_from_datetime,
29-
_datetime_from_microseconds,
3028
)
3129

3230
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
@@ -77,7 +75,7 @@ async def _populate_table(table_id):
7775
timestamp = datetime.datetime(2019, 5, 1)
7876
timestamp_minus_hr = timestamp - datetime.timedelta(hours=1)
7977

80-
async with (BigtableDataClientAsync(project=PROJECT) as client):
78+
async with BigtableDataClientAsync(project=PROJECT) as client:
8179
async with client.get_table(BIGTABLE_INSTANCE, table_id) as table:
8280
async with table.mutations_batcher() as batcher:
8381
await batcher.append(
@@ -257,6 +255,8 @@ async def _populate_table(table_id):
257255

258256
def _datetime_to_micros(value: datetime.datetime) -> int:
259257
"""Uses the same conversion rules as the old client in"""
258+
import calendar
259+
import datetime as dt
260260
if not value.tzinfo:
261261
value = value.replace(tzinfo=datetime.timezone.utc)
262262
# Regardless of what timezone is on the value, convert it to UTC.

samples/snippets/filters/filters_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313

1414

1515
import datetime
16+
import inspect
1617
import os
1718
import time
1819
import uuid
19-
import inspect
2020

2121
from google.cloud import bigtable
2222
import pytest
23-
from .snapshots.snap_filters_test import snapshots
2423

2524
from . import filter_snippets
26-
25+
from .snapshots.snap_filters_test import snapshots
2726

2827
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
2928
BIGTABLE_INSTANCE = os.environ["BIGTABLE_INSTANCE"]

samples/snippets/filters/noxfile.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import nox
2424

25-
2625
# WARNING - WARNING - WARNING - WARNING - WARNING
2726
# WARNING - WARNING - WARNING - WARNING - WARNING
2827
# DO NOT EDIT THIS FILE EVER!
@@ -160,6 +159,7 @@ def blacken(session: nox.sessions.Session) -> None:
160159
# format = isort + black
161160
#
162161

162+
163163
@nox.session
164164
def format(session: nox.sessions.Session) -> None:
165165
"""
@@ -187,7 +187,9 @@ def _session_tests(
187187
session: nox.sessions.Session, post_install: Callable = None
188188
) -> None:
189189
# check for presence of tests
190-
test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob("**/test_*.py", recursive=True)
190+
test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob(
191+
"**/test_*.py", recursive=True
192+
)
191193
test_list.extend(glob.glob("**/tests", recursive=True))
192194

193195
if len(test_list) == 0:
@@ -209,9 +211,7 @@ def _session_tests(
209211

210212
if os.path.exists("requirements-test.txt"):
211213
if os.path.exists("constraints-test.txt"):
212-
session.install(
213-
"-r", "requirements-test.txt", "-c", "constraints-test.txt"
214-
)
214+
session.install("-r", "requirements-test.txt", "-c", "constraints-test.txt")
215215
else:
216216
session.install("-r", "requirements-test.txt")
217217
with open("requirements-test.txt") as rtfile:
@@ -224,9 +224,9 @@ def _session_tests(
224224
post_install(session)
225225

226226
if "pytest-parallel" in packages:
227-
concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto'])
227+
concurrent_args.extend(["--workers", "auto", "--tests-per-worker", "auto"])
228228
elif "pytest-xdist" in packages:
229-
concurrent_args.extend(['-n', 'auto'])
229+
concurrent_args.extend(["-n", "auto"])
230230

231231
session.run(
232232
"pytest",
@@ -256,7 +256,7 @@ def py(session: nox.sessions.Session) -> None:
256256

257257

258258
def _get_repo_root() -> Optional[str]:
259-
""" Returns the root folder of the project. """
259+
"""Returns the root folder of the project."""
260260
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
261261
p = Path(os.getcwd())
262262
for i in range(10):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pytest==7.4.4
2+
pytest-asyncio
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-cloud-bigtable==2.22.0
1+
google-cloud-bigtable==2.23.0

0 commit comments

Comments
 (0)