""" Shared pytest fixtures.""" import time import uuid from google.api_core import exceptions from google.cloud.spanner_v1 import backup from google.cloud.spanner_v1 import client from google.cloud.spanner_v1 import database from google.cloud.spanner_v1 import instance import pytest from test_utils import retry INSTANCE_CREATION_TIMEOUT = 240 # seconds retry_429 = retry.RetryErrors(exceptions.ResourceExhausted, 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="session") def spanner_client():
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import datetime import grpc from google.api_core import exceptions from google.cloud import exceptions as core_exceptions from google.cloud._helpers import UTC from test_utils import retry retry_429 = retry.RetryErrors(exceptions.TooManyRequests, max_tries=9) retry_504 = retry.RetryErrors(exceptions.DeadlineExceeded) retry_until_true = retry.RetryResult(lambda result: result) retry_until_false = retry.RetryResult(lambda result: not result) def _retry_on_unavailable(exc): """Retry only errors whose status code is 'UNAVAILABLE'.""" return exc.code() == grpc.StatusCode.UNAVAILABLE retry_grpc_unavailable = retry.RetryErrors( core_exceptions.GrpcRendezvous, error_predicate=_retry_on_unavailable, max_tries=9, )
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """ System tests for metadata. """ import pytest from google.cloud import ndb from test_utils import retry _retry_assertion_errors = retry.RetryErrors(AssertionError) @pytest.mark.usefixtures("client_context") def test_kind_metadata(dispose_of): from google.cloud.ndb.metadata import Kind class AnyKind(ndb.Model): foo = ndb.IntegerProperty() class MyKind(ndb.Model): bar = ndb.StringProperty() entity1 = AnyKind(foo=1, id="x", namespace="_test_namespace_") entity1.put() dispose_of(entity1.key._key)
os.getenv("SPANNER_BACKUP_OPERATION_TIMEOUT_IN_SECONDS", 1200)) USE_EMULATOR_ENVVAR = "SPANNER_EMULATOR_HOST" USE_EMULATOR = os.getenv(USE_EMULATOR_ENVVAR) is not None EMULATOR_PROJECT_ENVVAR = "GCLOUD_PROJECT" EMULATOR_PROJECT_DEFAULT = "emulator-test-project" EMULATOR_PROJECT = os.getenv(EMULATOR_PROJECT_ENVVAR, EMULATOR_PROJECT_DEFAULT) DDL_STATEMENTS = (_fixtures.EMULATOR_DDL_STATEMENTS if USE_EMULATOR else _fixtures.DDL_STATEMENTS) retry_true = retry.RetryResult(operator.truth) retry_false = retry.RetryResult(operator.not_) retry_503 = retry.RetryErrors(exceptions.ServiceUnavailable) retry_429_503 = retry.RetryErrors(exceptions.TooManyRequests, exceptions.ServiceUnavailable, 8) retry_mabye_aborted_txn = retry.RetryErrors(exceptions.ServerError, exceptions.Aborted) retry_mabye_conflict = retry.RetryErrors(exceptions.ServerError, exceptions.Conflict) def _has_all_ddl(database): # Predicate to test for EC completion. return len(database.ddl_statements) == len(DDL_STATEMENTS) retry_has_all_dll = retry.RetryInstanceState(_has_all_ddl)