From f8a1d3f9a774a71ccd8ba6bdcf4d034f547ff9b9 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 3 Apr 2020 09:58:33 -0700 Subject: [PATCH] [healthcare] fix: fix broken tests for healthcare API (#3253) * [healthcare] fix: fix broken tests for healthcare API fixes #3120 * use uuid for the resource names * add retries around list_messages calls --- healthcare/api-client/hl7v2/hl7v2_messages.py | 2 +- .../api-client/hl7v2/hl7v2_messages_test.py | 78 +++++++++++-------- .../api-client/hl7v2/hl7v2_stores_test.py | 6 +- .../api-client/hl7v2/requirements-test.txt | 2 + 4 files changed, 53 insertions(+), 35 deletions(-) diff --git a/healthcare/api-client/hl7v2/hl7v2_messages.py b/healthcare/api-client/hl7v2/hl7v2_messages.py index 603edf3c302f..1304e03510dd 100644 --- a/healthcare/api-client/hl7v2/hl7v2_messages.py +++ b/healthcare/api-client/hl7v2/hl7v2_messages.py @@ -190,7 +190,7 @@ def list_hl7v2_messages( hl7v2_messages_parent, hl7v2_store_id) hl7v2_messages = client.projects().locations().datasets().hl7V2Stores( - ).messages().list(parent=hl7v2_message_path).execute().get('messages', []) + ).messages().list(parent=hl7v2_message_path).execute().get('hl7V2Messages', []) for hl7v2_message in hl7v2_messages: print(hl7v2_message) diff --git a/healthcare/api-client/hl7v2/hl7v2_messages_test.py b/healthcare/api-client/hl7v2/hl7v2_messages_test.py index efc3c3cbd05b..ebe6faae3fd2 100644 --- a/healthcare/api-client/hl7v2/hl7v2_messages_test.py +++ b/healthcare/api-client/hl7v2/hl7v2_messages_test.py @@ -14,9 +14,10 @@ import os import pytest -import re import sys -import time +import uuid + +from gcp_devrel.testing import eventually_consistent # Add datasets for bootstrapping datasets for testing sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'datasets')) # noqa @@ -28,8 +29,8 @@ project_id = os.environ['GOOGLE_CLOUD_PROJECT'] service_account_json = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] -dataset_id = 'test_dataset_{}'.format(int(time.time())) -hl7v2_store_id = 'test_hl7v2_store-{}'.format(int(time.time())) +dataset_id = 'test_dataset_{}'.format(uuid.uuid4()) +hl7v2_store_id = 'test_hl7v2_store-{}'.format(uuid.uuid4()) hl7v2_message_file = 'resources/hl7-sample-ingest.json' label_key = 'PROCESSED' label_value = 'TRUE' @@ -81,15 +82,20 @@ def test_CRUD_hl7v2_message(test_dataset, test_hl7v2_store, capsys): hl7v2_store_id, hl7v2_message_file) - hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages( - service_account_json, - project_id, - cloud_region, - dataset_id, - hl7v2_store_id) - - hl7v2_messages_list_to_str = str(hl7v2_messages_list).split('/', 9)[9] - hl7v2_message_id = re.sub('\']', '', hl7v2_messages_list_to_str) + hl7v2_message_id = "" + @eventually_consistent.call + def _(): + hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages( + service_account_json, + project_id, + cloud_region, + dataset_id, + hl7v2_store_id) + + assert len(hl7v2_messages_list) > 0 + hl7v2_message_name = hl7v2_messages_list[0].get('name') + nonlocal hl7v2_message_id + hl7v2_message_id = hl7v2_message_name.split('/', 9)[9] hl7v2_messages.get_hl7v2_message( service_account_json, @@ -124,15 +130,20 @@ def test_ingest_hl7v2_message(test_dataset, test_hl7v2_store, capsys): hl7v2_store_id, hl7v2_message_file) - hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages( - service_account_json, - project_id, - cloud_region, - dataset_id, - hl7v2_store_id) - - hl7v2_messages_list_to_str = str(hl7v2_messages_list).split('/', 9)[9] - hl7v2_message_id = re.sub('\']', '', hl7v2_messages_list_to_str) + hl7v2_message_id = "" + @eventually_consistent.call + def _(): + hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages( + service_account_json, + project_id, + cloud_region, + dataset_id, + hl7v2_store_id) + + assert len(hl7v2_messages_list) > 0 + hl7v2_message_name = hl7v2_messages_list[0].get('name') + nonlocal hl7v2_message_id + hl7v2_message_id = hl7v2_message_name.split('/', 9)[9] hl7v2_messages.get_hl7v2_message( service_account_json, @@ -167,15 +178,20 @@ def test_patch_hl7v2_message(test_dataset, test_hl7v2_store, capsys): hl7v2_store_id, hl7v2_message_file) - hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages( - service_account_json, - project_id, - cloud_region, - dataset_id, - hl7v2_store_id) - - hl7v2_messages_list_to_str = str(hl7v2_messages_list).split('/', 9)[9] - hl7v2_message_id = re.sub('\']', '', hl7v2_messages_list_to_str) + hl7v2_message_id = "" + @eventually_consistent.call + def _(): + hl7v2_messages_list = hl7v2_messages.list_hl7v2_messages( + service_account_json, + project_id, + cloud_region, + dataset_id, + hl7v2_store_id) + + assert len(hl7v2_messages_list) > 0 + hl7v2_message_name = hl7v2_messages_list[0].get('name') + nonlocal hl7v2_message_id + hl7v2_message_id = hl7v2_message_name.split('/', 9)[9] hl7v2_messages.patch_hl7v2_message( service_account_json, diff --git a/healthcare/api-client/hl7v2/hl7v2_stores_test.py b/healthcare/api-client/hl7v2/hl7v2_stores_test.py index c8295d21d0ad..3417e4f603a5 100644 --- a/healthcare/api-client/hl7v2/hl7v2_stores_test.py +++ b/healthcare/api-client/hl7v2/hl7v2_stores_test.py @@ -15,7 +15,7 @@ import os import pytest import sys -import time +import uuid # Add datasets for bootstrapping datasets for testing sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'datasets')) # noqa @@ -26,8 +26,8 @@ project_id = os.environ['GOOGLE_CLOUD_PROJECT'] service_account_json = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] -dataset_id = 'test_dataset_{}'.format(int(time.time())) -hl7v2_store_id = 'test_hl7v2_store-{}'.format(int(time.time())) +dataset_id = 'test_dataset_{}'.format(uuid.uuid4()) +hl7v2_store_id = 'test_hl7v2_store-{}'.format(uuid.uuid4()) @pytest.fixture(scope='module') diff --git a/healthcare/api-client/hl7v2/requirements-test.txt b/healthcare/api-client/hl7v2/requirements-test.txt index 781d4326c947..758bc040f3a8 100644 --- a/healthcare/api-client/hl7v2/requirements-test.txt +++ b/healthcare/api-client/hl7v2/requirements-test.txt @@ -1 +1,3 @@ pytest==5.3.2 +gcp-devrel-py-tools==0.0.15 +google-cloud-core