def test_create_database_pitr_success( not_emulator, # PITR-lite features are not supported by the emulator shared_instance, databases_to_delete, ): pool = spanner_v1.BurstyPool(labels={"testcase": "create_database_pitr"}) temp_db_id = _helpers.unique_id("pitr_db", separator="_") retention_period = "7d" ddl_statements = [ f"ALTER DATABASE {temp_db_id}" f" SET OPTIONS (version_retention_period = '{retention_period}')" ] temp_db = shared_instance.database( temp_db_id, pool=pool, ddl_statements=ddl_statements ) operation = temp_db.create() databases_to_delete.append(temp_db) operation.result(DBAPI_OPERATION_TIMEOUT) # raises on failure / timeout. database_ids = [database.name for database in shared_instance.list_databases()] assert temp_db.name in database_ids temp_db.reload() temp_db.version_retention_period == retention_period with temp_db.snapshot() as snapshot: results = snapshot.execute_sql( "SELECT OPTION_VALUE AS version_retention_period " "FROM INFORMATION_SCHEMA.DATABASE_OPTIONS " "WHERE SCHEMA_NAME = '' " "AND OPTION_NAME = 'version_retention_period'" ) for result in results: assert result[0] == retention_period
def test_update_ddl_w_default_leader_success( not_emulator, multiregion_instance, databases_to_delete, ): pool = spanner_v1.BurstyPool( labels={"testcase": "update_database_ddl_default_leader"}, ) temp_db_id = _helpers.unique_id("dfl_ldrr_upd_ddl", separator="_") default_leader = "us-east4" temp_db = multiregion_instance.database(temp_db_id, pool=pool) create_op = temp_db.create() databases_to_delete.append(temp_db) create_op.result(DBAPI_OPERATION_TIMEOUT) # raises on failure / timeout. assert temp_db.default_leader is None ddl_statements = _helpers.DDL_STATEMENTS + [ f"ALTER DATABASE {temp_db_id}" f" SET OPTIONS (default_leader = '{default_leader}')" ] operation = temp_db.update_ddl(ddl_statements) operation.result(DBAPI_OPERATION_TIMEOUT) # raises on failure / timeout. temp_db.reload() assert temp_db.default_leader == default_leader assert len(temp_db.ddl_statements) == len(ddl_statements)
def test_create_database_with_default_leader_success( not_emulator, # Default leader setting not supported by the emulator multiregion_instance, databases_to_delete, ): pool = spanner_v1.BurstyPool(labels={"testcase": "create_database_default_leader"}) temp_db_id = _helpers.unique_id("dflt_ldr_db", separator="_") default_leader = "us-east4" ddl_statements = [ f"ALTER DATABASE {temp_db_id}" f" SET OPTIONS (default_leader = '{default_leader}')" ] temp_db = multiregion_instance.database( temp_db_id, pool=pool, ddl_statements=ddl_statements ) operation = temp_db.create() databases_to_delete.append(temp_db) operation.result(30) # raises on failure / timeout. database_ids = [database.name for database in multiregion_instance.list_databases()] assert temp_db.name in database_ids temp_db.reload() assert temp_db.default_leader == default_leader with temp_db.snapshot() as snapshot: results = snapshot.execute_sql( "SELECT OPTION_VALUE AS default_leader " "FROM INFORMATION_SCHEMA.DATABASE_OPTIONS " "WHERE SCHEMA_NAME = '' AND OPTION_NAME = 'default_leader'" ) for result in results: assert result[0] == default_leader
def test_update_ddl_w_pitr_success( not_emulator, shared_instance, databases_to_delete, ): pool = spanner_v1.BurstyPool( labels={"testcase": "update_database_ddl_pitr"}) temp_db_id = _helpers.unique_id("pitr_upd_ddl_inv", separator="_") retention_period = "7d" temp_db = shared_instance.database(temp_db_id, pool=pool) create_op = temp_db.create() databases_to_delete.append(temp_db) create_op.result(DBAPI_OPERATION_TIMEOUT) # raises on failure / timeout. assert temp_db.version_retention_period is None ddl_statements = _helpers.DDL_STATEMENTS + [ f"ALTER DATABASE {temp_db_id}" f" SET OPTIONS (version_retention_period = '{retention_period}')" ] operation = temp_db.update_ddl(ddl_statements) operation.result(DBAPI_OPERATION_TIMEOUT) # raises on failure / timeout. temp_db.reload() assert temp_db.version_retention_period == retention_period assert len(temp_db.ddl_statements) == len(ddl_statements)
def test_update_ddl_w_operation_id(shared_instance, databases_to_delete): # We used to have: # @pytest.mark.skip( # reason="'Database.update_ddl' has a flaky timeout. See: " # https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5629 # ) pool = spanner_v1.BurstyPool(labels={"testcase": "update_database_ddl"}) temp_db_id = _helpers.unique_id("update_ddl", separator="_") temp_db = shared_instance.database(temp_db_id, pool=pool) create_op = temp_db.create() databases_to_delete.append(temp_db) create_op.result(DBAPI_OPERATION_TIMEOUT) # raises on failure / timeout. # random but shortish always start with letter operation_id = f"a{str(uuid.uuid4())[:8]}" operation = temp_db.update_ddl(_helpers.DDL_STATEMENTS, operation_id=operation_id) assert operation_id == operation.operation.name.split("/")[-1] operation.result(DBAPI_OPERATION_TIMEOUT) # raises on failure / timeout. temp_db.reload() assert len(temp_db.ddl_statements) == len(_helpers.DDL_STATEMENTS)
def test_create_database(shared_instance, databases_to_delete): pool = spanner_v1.BurstyPool(labels={"testcase": "create_database"}) temp_db_id = _helpers.unique_id("temp_db") temp_db = shared_instance.database(temp_db_id, pool=pool) operation = temp_db.create() databases_to_delete.append(temp_db) # We want to make sure the operation completes. operation.result(DBAPI_OPERATION_TIMEOUT) # raises on failure / timeout. database_ids = [database.name for database in shared_instance.list_databases()] assert temp_db.name in database_ids
def shared_database(shared_instance, database_operation_timeout): database_name = _helpers.unique_id("test_database") pool = spanner_v1.BurstyPool(labels={"testcase": "database_api"}) database = shared_instance.database( database_name, ddl_statements=_helpers.DDL_STATEMENTS, pool=pool ) operation = database.create() operation.result(database_operation_timeout) # raises on failure / timeout. yield database database.drop()
def test_create_database_pitr_invalid_retention_period( not_emulator, # PITR-lite features are not supported by the emulator shared_instance, ): pool = spanner_v1.BurstyPool(labels={"testcase": "create_database_pitr"}) temp_db_id = _helpers.unique_id("pitr_inv_db", separator="_") retention_period = "0d" ddl_statements = [ f"ALTER DATABASE {temp_db_id}" f" SET OPTIONS (version_retention_period = '{retention_period}')" ] temp_db = shared_instance.database( temp_db_id, pool=pool, ddl_statements=ddl_statements ) with pytest.raises(exceptions.InvalidArgument): temp_db.create()