Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
from google.api_core.exceptions import DeadlineExceeded
from test_utils.retry import RetryErrors


@pytest.fixture(scope="module")
def sample_name():
return "backup"


def unique_database_id():
"""Creates a unique id for the database."""
return f"test-db-{uuid.uuid4().hex[:10]}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@
retry_429 = RetryErrors(exceptions.ResourceExhausted, delay=15)


@pytest.fixture(scope="module")
def sample_name():
return "pg_snippets"


@pytest.fixture(scope="module")
def database_dialect():
"""Spanner dialect to be used for this sample.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@
retry_429 = RetryErrors(exceptions.ResourceExhausted, delay=15)


@pytest.fixture(scope="module")
def sample_name():
return "snippets"


@pytest.fixture(scope="module")
def database_dialect():
"""Spanner dialect to be used for this sample.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
import autocommit


@pytest.fixture(scope="module")
def sample_name():
return "autocommit"


@RetryErrors(exception=Aborted, max_tries=2)
def test_enable_autocommit_mode(capsys, instance_id, sample_database):
# Delete table if it exists for retry attempts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
import backup_sample


@pytest.fixture(scope="module")
def sample_name():
return "backup"


def unique_database_id():
"""Creates a unique id for the database."""
return f"test-db-{uuid.uuid4().hex[:10]}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
__INCREMENTAL_BACKUP_SCHEDULE_ID = "incremental-backup-schedule"


@pytest.fixture(scope="module")
def sample_name():
return "backup_schedule"


@pytest.fixture(scope="module")
def database_id():
return f"test-db-{uuid.uuid4().hex[:10]}"
Expand Down
33 changes: 10 additions & 23 deletions packages/google-cloud-spanner/samples/samples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@
(exceptions.ResourceExhausted, exceptions.FailedPrecondition), max_tries=6, delay=15
)


@pytest.fixture(scope="module")
def sample_name():
"""Sample testcase modules must define this fixture.

The name is used to label the instance created by the sample, to
aid in debugging leaked instances.
"""
raise NotImplementedError("Define 'sample_name' fixture in sample test driver")


@pytest.fixture(scope="module")
def database_dialect():
"""Database dialect to be used for this sample.
Expand Down Expand Up @@ -99,31 +88,31 @@ def cleanup_old_instances(spanner_client):
scrub_instance_ignore_not_found(inst)


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def instance_id():
"""Unique id for the instance used in samples."""
return f"test-instance-{uuid.uuid4().hex[:10]}"


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def multi_region_instance_id():
"""Unique id for the multi-region instance used in samples."""
return f"multi-instance-{uuid.uuid4().hex[:10]}"


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def instance_config(spanner_client):
return "{}/instanceConfigs/{}".format(
spanner_client.project_name, "regional-us-central1"
)


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def multi_region_instance_config(spanner_client):
return "{}/instanceConfigs/{}".format(spanner_client.project_name, "nam3")


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def proto_descriptor_file():
import os

Expand All @@ -134,13 +123,12 @@ def proto_descriptor_file():
file.close()


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def sample_instance(
spanner_client,
cleanup_old_instances,
Comment on lines 128 to 129
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The sample_instance fixture has been upgraded to session scope, but it depends on spanner_client and cleanup_old_instances. If these fixtures are still module-scoped (which was the previous scope for most fixtures in this file), pytest will raise a ScopeMismatch error. Please ensure that spanner_client, cleanup_old_instances, and any other dependencies of session-scoped fixtures are also upgraded to session scope.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spanner_client and cleanup_old_instances were already session scope, so this should not cause any problems.

instance_id,
instance_config,
sample_name,
):
operation = spanner_client.instance_admin_api.create_instance(
parent=spanner_client.project_name,
Expand All @@ -151,7 +139,7 @@ def sample_instance(
node_count=1,
labels={
"cloud_spanner_samples": "true",
"sample_name": sample_name,
"sample_name": "shared-samples",
"created": str(int(time.time())),
},
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE_PLUS, # Optional
Expand All @@ -170,20 +158,19 @@ def sample_instance(
scrub_instance_ignore_not_found(sample_instance)


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def multi_region_instance(
spanner_client,
cleanup_old_instances,
multi_region_instance_id,
multi_region_instance_config,
sample_name,
):
multi_region_instance = spanner_client.instance(
multi_region_instance_id,
multi_region_instance_config,
labels={
"cloud_spanner_samples": "true",
"sample_name": sample_name,
"sample_name": "shared-samples",
"created": str(int(time.time())),
},
)
Expand All @@ -205,7 +192,7 @@ def database_id():

Sample testcase modules can override as needed.
"""
return "my-database-id"
return f"my-db-{uuid.uuid4().hex[:10]}"


@pytest.fixture(scope="module")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@
LABEL Transfers)
"""


@pytest.fixture(scope="module")
def sample_name():
return "snippets"


@pytest.fixture(scope="module")
def database_dialect():
"""Spanner dialect to be used for this sample.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
retry_429 = RetryErrors(exceptions.ResourceExhausted, delay=15)


@pytest.fixture(scope="module")
def sample_name():
return "pg_snippets"


@pytest.fixture(scope="module")
def database_dialect():
"""Spanner dialect to be used for this sample.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
import quickstart


@pytest.fixture(scope="module")
def sample_name():
return "quickstart"


def test_quickstart(capsys, instance_id, sample_database):
quickstart.run_quickstart(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@
retry_429 = RetryErrors(exceptions.ResourceExhausted, delay=15)


@pytest.fixture(scope="module")
def sample_name():
return "snippets"


@pytest.fixture(scope="module")
def database_dialect():
"""Spanner dialect to be used for this sample.
Expand Down
Loading