Skip to content

Commit 19d5083

Browse files
fix comments:
- Use shared instance fixture - Update the shared instance to use ENTERPRISE_PLUS edition - Use pytest skip when Queues not supported
1 parent f32b057 commit 19d5083

7 files changed

Lines changed: 153 additions & 161 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
2+
request = spanner_instance_admin.UpdateInstanceRequest(
3+
instance=spanner_instance_admin.Instance(
4+
name="projects/my-project/instances/my-instance",
5+
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE,
6+
),
7+
field_mask={"paths": ["edition"]},
8+
)
9+
print("SUCCESS")

packages/google-cloud-spanner/tests/system/_async/conftest.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,33 @@ async def shared_instance(
138138
instance_config,
139139
):
140140
spanner_client._instance_admin_api = None
141-
instance = spanner_client.instance(shared_instance_id, instance_config.name)
142141

143142
if _helpers.CREATE_INSTANCE:
144-
op = await instance.create()
145-
await op.result(instance_operation_timeout)
143+
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
144+
import time
145+
146+
create_time = str(int(time.time()))
147+
labels = {"python-spanner-systests": "true", "created": create_time}
148+
149+
request = spanner_instance_admin.CreateInstanceRequest(
150+
parent=spanner_client.project_name,
151+
instance_id=shared_instance_id,
152+
instance=spanner_instance_admin.Instance(
153+
config=instance_config.name,
154+
display_name=shared_instance_id,
155+
node_count=1,
156+
labels=labels,
157+
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE_PLUS,
158+
),
159+
)
160+
created_op = await spanner_client.instance_admin_api.create_instance(request=request)
161+
await created_op.result(instance_operation_timeout)
162+
163+
instance = spanner_client.instance(
164+
shared_instance_id, instance_config.name, labels=labels
165+
)
146166
else:
167+
instance = spanner_client.instance(shared_instance_id, instance_config.name)
147168
await instance.reload()
148169

149170
yield instance
@@ -205,3 +226,10 @@ async def databases_to_delete():
205226
def not_postgres(database_dialect):
206227
if database_dialect == DatabaseDialect.POSTGRESQL:
207228
pytest.skip("Skip for Postgres")
229+
230+
231+
@pytest.fixture(scope="function")
232+
def not_emulator():
233+
if _helpers.USE_EMULATOR:
234+
pytest.skip(f"{_helpers.USE_EMULATOR_ENVVAR} set in environment.")
235+

packages/google-cloud-spanner/tests/system/_async/test_database_api.py

Lines changed: 42 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -88,90 +88,61 @@ async def test_db_batch_insert_then_db_snapshot_read(shared_database):
8888

8989

9090
@pytest.mark.asyncio
91-
async def test_db_batch_send_and_ack(not_emulator, spanner_client, database_dialect, instance_config):
91+
async def test_db_batch_send_and_ack(not_emulator, spanner_client, database_dialect, shared_instance):
9292
import uuid
9393
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
9494
from google.cloud.spanner_admin_database_v1 import DatabaseDialect
9595
from google.api_core.exceptions import MethodNotImplemented, GoogleAPIError
9696

97-
instance_id = f"test-instance-{uuid.uuid4().hex[:8]}"
9897
db_name = f"test-db-{uuid.uuid4().hex[:8]}"
9998
queue_name = f"test_queue_{uuid.uuid4().hex[:8]}"
10099

101-
config_name = instance_config.name
102-
request = spanner_instance_admin.CreateInstanceRequest(
103-
parent=spanner_client.project_name,
104-
instance_id=instance_id,
105-
instance=spanner_instance_admin.Instance(
106-
config=config_name,
107-
display_name=instance_id,
108-
node_count=1,
109-
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE,
110-
),
111-
)
112-
print(f"instance creation request: {request}")
113-
operation = await spanner_client.instance_admin_api.create_instance(request=request)
114-
operation.result(600)
115-
116-
test_instance = spanner_client.instance(instance_id, configuration_name=config_name)
100+
test_database = await shared_instance.database(db_name, database_dialect=database_dialect)
101+
operation = await test_database.create()
102+
operation.result(300)
117103

118104
try:
119-
test_database = await test_instance.database(db_name, database_dialect=database_dialect)
120-
operation = await test_database.create()
121-
operation.result(300)
122-
print("Database created successfully!")
105+
# Create the Queue
106+
if database_dialect == DatabaseDialect.POSTGRESQL:
107+
queue_ddl = f"""CREATE QUEUE {queue_name} (
108+
id bigint NOT NULL,
109+
"Payload" varchar NOT NULL,
110+
PRIMARY KEY (id)
111+
)"""
112+
else:
113+
queue_ddl = f"""CREATE QUEUE {queue_name} (
114+
Id INT64 NOT NULL,
115+
Payload STRING(MAX) NOT NULL
116+
) PRIMARY KEY (Id)"""
123117

124118
try:
125-
# 3. Create the Queue
126-
if database_dialect == DatabaseDialect.POSTGRESQL:
127-
queue_ddl = f"""CREATE QUEUE {queue_name} (
128-
id bigint NOT NULL,
129-
"Payload" varchar NOT NULL,
130-
PRIMARY KEY (id)
131-
)"""
132-
else:
133-
queue_ddl = f"""CREATE QUEUE {queue_name} (
134-
Id INT64 NOT NULL,
135-
Payload STRING(MAX) NOT NULL
136-
) PRIMARY KEY (Id)"""
137-
138-
try:
139-
operation = await test_database.update_ddl([queue_ddl])
140-
await operation.result(600)
141-
except MethodNotImplemented as e:
142-
print(f"MethodNotImplemented. Skipping test because Queues are not implemented yet: {e}")
143-
return
144-
except GoogleAPIError as e:
145-
if getattr(e, 'code', None) == 501 or (getattr(e, 'grpc_status_code', None) and e.grpc_status_code.name == 'UNIMPLEMENTED') or "UNIMPLEMENTED" in str(e):
146-
print(f"Skipping test because Queues are not implemented yet: {e}")
147-
return
148-
raise
149-
print("Queue created successfully.")
150-
151-
# 4. Run mutations
152-
print("Sending message to queue...")
153-
async with test_database.batch() as batch:
154-
batch.send(
155-
queue=queue_name,
156-
key=(2,),
157-
payload="Hello, Queues!",
158-
)
159-
print("Send successful.")
160-
161-
print("Acking message in queue...")
162-
async with test_database.batch() as batch:
163-
batch.ack(
164-
queue=queue_name,
165-
key=(2,),
166-
)
167-
print("Ack successful.")
168-
169-
finally:
170-
print("Dropping database...")
171-
await test_database.drop()
119+
operation = await test_database.update_ddl([queue_ddl])
120+
await operation.result(600)
121+
except MethodNotImplemented as e:
122+
pytest.skip(f"Queues are not implemented yet: {e}")
123+
except GoogleAPIError as e:
124+
if getattr(e, 'code', None) == 501 or (getattr(e, 'grpc_status_code', None) and e.grpc_status_code.name == 'UNIMPLEMENTED') or "UNIMPLEMENTED" in str(e):
125+
pytest.skip(f"Queues are not implemented yet: {e}")
126+
raise
127+
128+
# Run mutations
129+
async with test_database.batch() as batch:
130+
batch.send(
131+
queue=queue_name,
132+
key=(2,),
133+
payload="Hello, Queues!",
134+
)
135+
136+
print("Acking message in queue...")
137+
async with test_database.batch() as batch:
138+
batch.ack(
139+
queue=queue_name,
140+
key=(2,),
141+
)
142+
172143
finally:
173-
print("Dropping instance...")
174-
await test_instance.delete()
144+
print("Dropping database...")
145+
await test_database.drop()
175146

176147

177148
@pytest.mark.asyncio

packages/google-cloud-spanner/tests/system/conftest.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,27 @@ def shared_instance(
205205
_helpers.cleanup_old_instances(spanner_client)
206206

207207
if _helpers.CREATE_INSTANCE:
208+
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
208209
create_time = str(int(time.time()))
209210
labels = {"python-spanner-systests": "true", "created": create_time}
210211

212+
request = spanner_instance_admin.CreateInstanceRequest(
213+
parent=spanner_client.project_name,
214+
instance_id=shared_instance_id,
215+
instance=spanner_instance_admin.Instance(
216+
config=instance_config.name,
217+
display_name=shared_instance_id,
218+
node_count=1,
219+
labels=labels,
220+
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE_PLUS,
221+
),
222+
)
223+
created_op = _helpers.retry_429_503(spanner_client.instance_admin_api.create_instance)(request=request)
224+
created_op.result(instance_operation_timeout) # block until completion
225+
211226
instance = spanner_client.instance(
212227
shared_instance_id, instance_config.name, labels=labels
213228
)
214-
created_op = _helpers.retry_429_503(instance.create)()
215-
created_op.result(instance_operation_timeout) # block until completion
216229

217230
else: # reuse existing instance
218231
instance = spanner_client.instance(shared_instance_id)

packages/google-cloud-spanner/tests/system/test_database_api.py

Lines changed: 47 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -567,88 +567,65 @@ def test_db_batch_insert_then_db_snapshot_read(shared_database):
567567
sd._check_rows_data(from_snap)
568568

569569

570-
def test_db_batch_send_and_ack(not_emulator, spanner_client, database_dialect, instance_config):
570+
def test_db_batch_send_and_ack(not_emulator, spanner_client, database_dialect, shared_instance):
571571
import uuid
572572
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
573573
from google.cloud.spanner_admin_database_v1 import DatabaseDialect
574574
from google.api_core.exceptions import MethodNotImplemented, GoogleAPIError
575575

576-
instance_id = f"test-instance-{uuid.uuid4().hex[:8]}"
577576
db_id = f"test-db-{uuid.uuid4().hex[:8]}"
578577
queue_name = f"test_queue_{uuid.uuid4().hex[:8]}"
579578

580-
config_name = instance_config.name
581-
request = spanner_instance_admin.CreateInstanceRequest(
582-
parent=spanner_client.project_name,
583-
instance_id=instance_id,
584-
instance=spanner_instance_admin.Instance(
585-
config=config_name,
586-
display_name=instance_id,
587-
node_count=1,
588-
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE,
589-
),
590-
)
591-
print(f"instance creation request: {request}")
592-
operation = spanner_client.instance_admin_api.create_instance(request=request)
593-
operation.result(600)
594-
test_instance = spanner_client.instance(instance_id, configuration_name=config_name)
579+
test_database = shared_instance.database(db_id, database_dialect=database_dialect)
580+
operation = test_database.create()
581+
operation.result(300)
582+
print("Database created successfully!")
595583

596584
try:
597-
test_database = test_instance.database(db_id, database_dialect=database_dialect)
598-
operation = test_database.create()
599-
operation.result(300)
600-
print("Database created successfully!")
601-
585+
# Create the Queue
586+
if database_dialect == DatabaseDialect.POSTGRESQL:
587+
queue_ddl = f"""CREATE QUEUE {queue_name} (
588+
id bigint NOT NULL,
589+
"Payload" varchar NOT NULL,
590+
PRIMARY KEY (id)
591+
)"""
592+
else:
593+
queue_ddl = f"""CREATE QUEUE {queue_name} (
594+
Id INT64 NOT NULL,
595+
Payload STRING(MAX) NOT NULL
596+
) PRIMARY KEY (Id)"""
602597
try:
603-
# 3. Create the Queue
604-
if database_dialect == DatabaseDialect.POSTGRESQL:
605-
queue_ddl = f"""CREATE QUEUE {queue_name} (
606-
id bigint NOT NULL,
607-
"Payload" varchar NOT NULL,
608-
PRIMARY KEY (id)
609-
)"""
610-
else:
611-
queue_ddl = f"""CREATE QUEUE {queue_name} (
612-
Id INT64 NOT NULL,
613-
Payload STRING(MAX) NOT NULL
614-
) PRIMARY KEY (Id)"""
615-
try:
616-
operation = test_database.update_ddl([queue_ddl])
617-
operation.result(600)
618-
except MethodNotImplemented as e:
619-
print(f"MethodNotImplemented. Skipping test because Queues are not implemented yet: {e}")
620-
return
621-
except GoogleAPIError as e:
622-
if getattr(e, 'code', None) == 501 or getattr(e, 'grpc_status_code', None) and e.grpc_status_code.name == 'UNIMPLEMENTED' or "UNIMPLEMENTED" in str(e):
623-
print(f"Skipping test because Queues are not implemented yet: {e}")
624-
return
625-
raise
626-
print("Queue created successfully.")
627-
628-
# 4. Run mutations
629-
print("Sending message to queue...")
630-
with test_database.batch() as batch:
631-
batch.send(
632-
queue=queue_name,
633-
key=(2,),
634-
payload="Hello, Queues!",
635-
)
636-
print("Send successful.")
637-
638-
print("Acking message in queue...")
639-
with test_database.batch() as batch:
640-
batch.ack(
641-
queue=queue_name,
642-
key=(2,),
643-
)
644-
print("Ack successful.")
645-
646-
finally:
647-
print("Dropping database...")
648-
test_database.drop()
598+
operation = test_database.update_ddl([queue_ddl])
599+
operation.result(600)
600+
except MethodNotImplemented as e:
601+
pytest.skip(f"Queues are not implemented yet: {e}")
602+
except GoogleAPIError as e:
603+
if getattr(e, 'code', None) == 501 or getattr(e, 'grpc_status_code', None) and e.grpc_status_code.name == 'UNIMPLEMENTED' or "UNIMPLEMENTED" in str(e):
604+
pytest.skip(f"Queues are not implemented yet: {e}")
605+
raise
606+
print("Queue created successfully.")
607+
608+
# Run mutations
609+
print("Sending message to queue...")
610+
with test_database.batch() as batch:
611+
batch.send(
612+
queue=queue_name,
613+
key=(2,),
614+
payload="Hello, Queues!",
615+
)
616+
print("Send successful.")
617+
618+
print("Acking message in queue...")
619+
with test_database.batch() as batch:
620+
batch.ack(
621+
queue=queue_name,
622+
key=(2,),
623+
)
624+
print("Ack successful.")
625+
649626
finally:
650-
print("Dropping instance...")
651-
test_instance.delete()
627+
print("Dropping database...")
628+
test_database.drop()
652629

653630

654631
def test_db_run_in_transaction_then_snapshot_execute_sql(shared_database):

0 commit comments

Comments
 (0)