|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import google.api_core.exceptions |
| 16 | +from google.cloud.bigquery_reservation_v1.services import reservation_service |
| 17 | +import pytest |
| 18 | +import test_utils.prefixer |
| 19 | + |
| 20 | +from . import reservation_create |
| 21 | +from . import reservation_delete |
| 22 | +from . import reservation_update |
| 23 | + |
| 24 | + |
| 25 | +# Reservation IDs are limited to 64 characters. |
| 26 | +reservation_prefixer = test_utils.prefixer.Prefixer( |
| 27 | + "py-bq-r", "snippets", separator="-" |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture(scope="module", autouse=True) |
| 32 | +def cleanup_reservations( |
| 33 | + reservation_client: reservation_service.ReservationServiceClient, location_path: str |
| 34 | +) -> None: |
| 35 | + for reservation in reservation_client.list_reservations(parent=location_path): |
| 36 | + reservation_id = reservation.name.split("/")[-1] |
| 37 | + if reservation_prefixer.should_cleanup(reservation_id): |
| 38 | + reservation_client.delete_reservation(name=reservation.name) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture(scope="session") |
| 42 | +def reservation_id( |
| 43 | + reservation_client: reservation_service.ReservationServiceClient, |
| 44 | + project_id: str, |
| 45 | + location: str, |
| 46 | +) -> str: |
| 47 | + id_ = reservation_prefixer.create_prefix() |
| 48 | + yield id_ |
| 49 | + |
| 50 | + reservation_name = reservation_client.reservation_path(project_id, location, id_) |
| 51 | + try: |
| 52 | + reservation_client.delete_reservation(name=reservation_name) |
| 53 | + except google.api_core.exceptions.NotFound: |
| 54 | + pass |
| 55 | + |
| 56 | + |
| 57 | +def test_reservation_samples( |
| 58 | + capsys: pytest.CaptureFixture, project_id: str, location: str, reservation_id: str |
| 59 | +) -> None: |
| 60 | + slot_capacity = 100 |
| 61 | + reservation = reservation_create.create_reservation( |
| 62 | + project_id, location, reservation_id, slot_capacity |
| 63 | + ) |
| 64 | + assert reservation.slot_capacity == 100 |
| 65 | + assert reservation_id in reservation.name |
| 66 | + out, _ = capsys.readouterr() |
| 67 | + assert f"Created reservation: {reservation.name}" in out |
| 68 | + |
| 69 | + slot_capacity = 50 |
| 70 | + reservation = reservation_update.update_reservation( |
| 71 | + project_id, location, reservation_id, slot_capacity |
| 72 | + ) |
| 73 | + assert reservation.slot_capacity == 50 |
| 74 | + assert reservation_id in reservation.name |
| 75 | + out, _ = capsys.readouterr() |
| 76 | + assert f"Updated reservation: {reservation.name}" in out |
| 77 | + |
| 78 | + reservation_delete.delete_reservation(project_id, location, reservation_id) |
| 79 | + out, _ = capsys.readouterr() |
| 80 | + assert "Deleted reservation" in out |
| 81 | + assert reservation_id in out |
0 commit comments