Skip to content

Commit 79e1398

Browse files
authored
samples: bit reverse sequence (#937)
* samples: bit reverse sequence * fix: sequence * fix: review comments * fix: lint E721 * fix: new database for sequence test * fix: lint and blacken * fix: doc * fix: database name
1 parent ba5ff0b commit 79e1398

19 files changed

+839
-534
lines changed

google/cloud/spanner_dbapi/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __repr__(self):
5252
return self.__str__()
5353

5454
def __eq__(self, other):
55-
if type(self) != type(other):
55+
if type(self) is not type(other):
5656
return False
5757
if self.name != other.name:
5858
return False
@@ -95,7 +95,7 @@ def __len__(self):
9595
return len(self.argv)
9696

9797
def __eq__(self, other):
98-
if type(self) != type(other):
98+
if type(self) is not type(other):
9999
return False
100100

101101
if len(self) != len(other):

google/cloud/spanner_v1/_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ def _merge_query_options(base, merge):
8181
If the resultant object only has empty fields, returns None.
8282
"""
8383
combined = base or ExecuteSqlRequest.QueryOptions()
84-
if type(combined) == dict:
84+
if type(combined) is dict:
8585
combined = ExecuteSqlRequest.QueryOptions(
8686
optimizer_version=combined.get("optimizer_version", ""),
8787
optimizer_statistics_package=combined.get(
8888
"optimizer_statistics_package", ""
8989
),
9090
)
9191
merge = merge or ExecuteSqlRequest.QueryOptions()
92-
if type(merge) == dict:
92+
if type(merge) is dict:
9393
merge = ExecuteSqlRequest.QueryOptions(
9494
optimizer_version=merge.get("optimizer_version", ""),
9595
optimizer_statistics_package=merge.get("optimizer_statistics_package", ""),

google/cloud/spanner_v1/backup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(
9595
self._max_expire_time = None
9696
self._referencing_backups = None
9797
self._database_dialect = None
98-
if type(encryption_config) == dict:
98+
if type(encryption_config) is dict:
9999
if source_backup:
100100
self._encryption_config = CopyBackupEncryptionConfig(
101101
**encryption_config

google/cloud/spanner_v1/batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def commit(self, return_commit_stats=False, request_options=None):
175175

176176
if request_options is None:
177177
request_options = RequestOptions()
178-
elif type(request_options) == dict:
178+
elif type(request_options) is dict:
179179
request_options = RequestOptions(request_options)
180180
request_options.transaction_tag = self.transaction_tag
181181

google/cloud/spanner_v1/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def __init__(
143143
):
144144
self._emulator_host = _get_spanner_emulator_host()
145145

146-
if client_options and type(client_options) == dict:
146+
if client_options and type(client_options) is dict:
147147
self._client_options = google.api_core.client_options.from_dict(
148148
client_options
149149
)

google/cloud/spanner_v1/database.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ def create(self):
436436
db_name = f'"{db_name}"'
437437
else:
438438
db_name = f"`{db_name}`"
439-
if type(self._encryption_config) == dict:
439+
if type(self._encryption_config) is dict:
440440
self._encryption_config = EncryptionConfig(**self._encryption_config)
441441

442442
request = CreateDatabaseRequest(
@@ -621,7 +621,7 @@ def execute_partitioned_dml(
621621
)
622622
if request_options is None:
623623
request_options = RequestOptions()
624-
elif type(request_options) == dict:
624+
elif type(request_options) is dict:
625625
request_options = RequestOptions(request_options)
626626
request_options.transaction_tag = None
627627

@@ -806,7 +806,7 @@ def restore(self, source):
806806
"""
807807
if source is None:
808808
raise ValueError("Restore source not specified")
809-
if type(self._encryption_config) == dict:
809+
if type(self._encryption_config) is dict:
810810
self._encryption_config = RestoreDatabaseEncryptionConfig(
811811
**self._encryption_config
812812
)
@@ -1011,7 +1011,7 @@ def __init__(self, database, request_options=None):
10111011
self._session = self._batch = None
10121012
if request_options is None:
10131013
self._request_options = RequestOptions()
1014-
elif type(request_options) == dict:
1014+
elif type(request_options) is dict:
10151015
self._request_options = RequestOptions(request_options)
10161016
else:
10171017
self._request_options = request_options

google/cloud/spanner_v1/snapshot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def read(
247247

248248
if request_options is None:
249249
request_options = RequestOptions()
250-
elif type(request_options) == dict:
250+
elif type(request_options) is dict:
251251
request_options = RequestOptions(request_options)
252252

253253
if self._read_only:
@@ -414,7 +414,7 @@ def execute_sql(
414414

415415
if request_options is None:
416416
request_options = RequestOptions()
417-
elif type(request_options) == dict:
417+
elif type(request_options) is dict:
418418
request_options = RequestOptions(request_options)
419419
if self._read_only:
420420
# Transaction tags are not supported for read only transactions.

google/cloud/spanner_v1/transaction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def commit(self, return_commit_stats=False, request_options=None):
215215

216216
if request_options is None:
217217
request_options = RequestOptions()
218-
elif type(request_options) == dict:
218+
elif type(request_options) is dict:
219219
request_options = RequestOptions(request_options)
220220
if self.transaction_tag is not None:
221221
request_options.transaction_tag = self.transaction_tag
@@ -352,7 +352,7 @@ def execute_update(
352352

353353
if request_options is None:
354354
request_options = RequestOptions()
355-
elif type(request_options) == dict:
355+
elif type(request_options) is dict:
356356
request_options = RequestOptions(request_options)
357357
request_options.transaction_tag = self.transaction_tag
358358

@@ -463,7 +463,7 @@ def batch_update(self, statements, request_options=None):
463463

464464
if request_options is None:
465465
request_options = RequestOptions()
466-
elif type(request_options) == dict:
466+
elif type(request_options) is dict:
467467
request_options = RequestOptions(request_options)
468468
request_options.transaction_tag = self.transaction_tag
469469

samples/samples/conftest.py

Lines changed: 90 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,19 @@
3838
def sample_name():
3939
"""Sample testcase modules must define this fixture.
4040
41-
The name is used to label the instance created by the sample, to
42-
aid in debugging leaked instances.
43-
"""
44-
raise NotImplementedError(
45-
"Define 'sample_name' fixture in sample test driver")
41+
The name is used to label the instance created by the sample, to
42+
aid in debugging leaked instances.
43+
"""
44+
raise NotImplementedError("Define 'sample_name' fixture in sample test driver")
4645

4746

4847
@pytest.fixture(scope="module")
4948
def database_dialect():
5049
"""Database dialect to be used for this sample.
5150
52-
The dialect is used to initialize the dialect for the database.
53-
It can either be GoogleStandardSql or PostgreSql.
54-
"""
51+
The dialect is used to initialize the dialect for the database.
52+
It can either be GoogleStandardSql or PostgreSql.
53+
"""
5554
# By default, we consider GOOGLE_STANDARD_SQL dialect. Other specific tests
5655
# can override this if required.
5756
return DatabaseDialect.GOOGLE_STANDARD_SQL
@@ -105,7 +104,7 @@ def multi_region_instance_id():
105104
@pytest.fixture(scope="module")
106105
def instance_config(spanner_client):
107106
return "{}/instanceConfigs/{}".format(
108-
spanner_client.project_name, "regional-us-central1"
107+
spanner_client.project_name, "regional-us-central1"
109108
)
110109

111110

@@ -116,20 +115,20 @@ def multi_region_instance_config(spanner_client):
116115

117116
@pytest.fixture(scope="module")
118117
def sample_instance(
119-
spanner_client,
120-
cleanup_old_instances,
121-
instance_id,
122-
instance_config,
123-
sample_name,
118+
spanner_client,
119+
cleanup_old_instances,
120+
instance_id,
121+
instance_config,
122+
sample_name,
124123
):
125124
sample_instance = spanner_client.instance(
126-
instance_id,
127-
instance_config,
128-
labels={
129-
"cloud_spanner_samples": "true",
130-
"sample_name": sample_name,
131-
"created": str(int(time.time())),
132-
},
125+
instance_id,
126+
instance_config,
127+
labels={
128+
"cloud_spanner_samples": "true",
129+
"sample_name": sample_name,
130+
"created": str(int(time.time())),
131+
},
133132
)
134133
op = retry_429(sample_instance.create)()
135134
op.result(INSTANCE_CREATION_TIMEOUT) # block until completion
@@ -151,20 +150,20 @@ def sample_instance(
151150

152151
@pytest.fixture(scope="module")
153152
def multi_region_instance(
154-
spanner_client,
155-
cleanup_old_instances,
156-
multi_region_instance_id,
157-
multi_region_instance_config,
158-
sample_name,
153+
spanner_client,
154+
cleanup_old_instances,
155+
multi_region_instance_id,
156+
multi_region_instance_config,
157+
sample_name,
159158
):
160159
multi_region_instance = spanner_client.instance(
161-
multi_region_instance_id,
162-
multi_region_instance_config,
163-
labels={
164-
"cloud_spanner_samples": "true",
165-
"sample_name": sample_name,
166-
"created": str(int(time.time())),
167-
},
160+
multi_region_instance_id,
161+
multi_region_instance_config,
162+
labels={
163+
"cloud_spanner_samples": "true",
164+
"sample_name": sample_name,
165+
"created": str(int(time.time())),
166+
},
168167
)
169168
op = retry_429(multi_region_instance.create)()
170169
op.result(INSTANCE_CREATION_TIMEOUT) # block until completion
@@ -188,44 +187,49 @@ def multi_region_instance(
188187
def database_id():
189188
"""Id for the database used in samples.
190189
191-
Sample testcase modules can override as needed.
192-
"""
190+
Sample testcase modules can override as needed.
191+
"""
193192
return "my-database-id"
194193

195194

195+
@pytest.fixture(scope="module")
196+
def bit_reverse_sequence_database_id():
197+
"""Id for the database used in bit reverse sequence samples.
198+
199+
Sample testcase modules can override as needed.
200+
"""
201+
return "sequence-database-id"
202+
203+
196204
@pytest.fixture(scope="module")
197205
def database_ddl():
198206
"""Sequence of DDL statements used to set up the database.
199207
200-
Sample testcase modules can override as needed.
201-
"""
208+
Sample testcase modules can override as needed.
209+
"""
202210
return []
203211

204212

205213
@pytest.fixture(scope="module")
206214
def sample_database(
207-
spanner_client,
208-
sample_instance,
209-
database_id,
210-
database_ddl,
211-
database_dialect):
215+
spanner_client, sample_instance, database_id, database_ddl, database_dialect
216+
):
212217
if database_dialect == DatabaseDialect.POSTGRESQL:
213218
sample_database = sample_instance.database(
214-
database_id,
215-
database_dialect=DatabaseDialect.POSTGRESQL,
219+
database_id,
220+
database_dialect=DatabaseDialect.POSTGRESQL,
216221
)
217222

218223
if not sample_database.exists():
219224
operation = sample_database.create()
220225
operation.result(OPERATION_TIMEOUT_SECONDS)
221226

222227
request = spanner_admin_database_v1.UpdateDatabaseDdlRequest(
223-
database=sample_database.name,
224-
statements=database_ddl,
228+
database=sample_database.name,
229+
statements=database_ddl,
225230
)
226231

227-
operation =\
228-
spanner_client.database_admin_api.update_database_ddl(request)
232+
operation = spanner_client.database_admin_api.update_database_ddl(request)
229233
operation.result(OPERATION_TIMEOUT_SECONDS)
230234

231235
yield sample_database
@@ -234,8 +238,8 @@ def sample_database(
234238
return
235239

236240
sample_database = sample_instance.database(
237-
database_id,
238-
ddl_statements=database_ddl,
241+
database_id,
242+
ddl_statements=database_ddl,
239243
)
240244

241245
if not sample_database.exists():
@@ -247,11 +251,43 @@ def sample_database(
247251
sample_database.drop()
248252

249253

254+
@pytest.fixture(scope="module")
255+
def bit_reverse_sequence_database(
256+
spanner_client, sample_instance, bit_reverse_sequence_database_id, database_dialect
257+
):
258+
if database_dialect == DatabaseDialect.POSTGRESQL:
259+
bit_reverse_sequence_database = sample_instance.database(
260+
bit_reverse_sequence_database_id,
261+
database_dialect=DatabaseDialect.POSTGRESQL,
262+
)
263+
264+
if not bit_reverse_sequence_database.exists():
265+
operation = bit_reverse_sequence_database.create()
266+
operation.result(OPERATION_TIMEOUT_SECONDS)
267+
268+
yield bit_reverse_sequence_database
269+
270+
bit_reverse_sequence_database.drop()
271+
return
272+
273+
bit_reverse_sequence_database = sample_instance.database(
274+
bit_reverse_sequence_database_id
275+
)
276+
277+
if not bit_reverse_sequence_database.exists():
278+
operation = bit_reverse_sequence_database.create()
279+
operation.result(OPERATION_TIMEOUT_SECONDS)
280+
281+
yield bit_reverse_sequence_database
282+
283+
bit_reverse_sequence_database.drop()
284+
285+
250286
@pytest.fixture(scope="module")
251287
def kms_key_name(spanner_client):
252288
return "projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}".format(
253-
spanner_client.project,
254-
"us-central1",
255-
"spanner-test-keyring",
256-
"spanner-test-cmek",
289+
spanner_client.project,
290+
"us-central1",
291+
"spanner-test-keyring",
292+
"spanner-test-cmek",
257293
)

0 commit comments

Comments
 (0)