|
| 1 | +# Copyright 2020 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 | +# http://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 os |
| 16 | +import uuid |
| 17 | + |
| 18 | +from google.api_core import exceptions |
| 19 | +from google.cloud import tasks_v2 |
| 20 | +import pytest |
| 21 | + |
| 22 | +import delete_queue |
| 23 | + |
| 24 | + |
| 25 | +TEST_PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT'] |
| 26 | +TEST_LOCATION = os.getenv('TEST_QUEUE_LOCATION', 'us-central1') |
| 27 | +TEST_QUEUE_NAME = f'my-queue-{uuid.uuid4().hex}' |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture() |
| 31 | +def test_queue(): |
| 32 | + client = tasks_v2.CloudTasksClient() |
| 33 | + parent = client.location_path(TEST_PROJECT_ID, TEST_LOCATION) |
| 34 | + queue = { |
| 35 | + # The fully qualified path to the queue |
| 36 | + 'name': client.queue_path( |
| 37 | + TEST_PROJECT_ID, TEST_LOCATION, TEST_QUEUE_NAME), |
| 38 | + } |
| 39 | + q = client.create_queue(parent, queue) |
| 40 | + |
| 41 | + yield q |
| 42 | + |
| 43 | + try: |
| 44 | + # Attempt to delete the queue in case the sample failed. |
| 45 | + client.delete_queue(q.name) |
| 46 | + except exceptions.NotFound: |
| 47 | + # The queue was already successfully deleted. |
| 48 | + print('Queue already deleted successfully') |
| 49 | + |
| 50 | + |
| 51 | +def test_delete_queue(capsys, test_queue): |
| 52 | + delete_queue.delete_queue( |
| 53 | + TEST_PROJECT_ID, TEST_QUEUE_NAME, TEST_LOCATION |
| 54 | + ) |
| 55 | + out, _ = capsys.readouterr() |
| 56 | + assert 'Deleted queue' in out |
0 commit comments