From b23f1cabbc6c840904ffbd040c3e781c71915033 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Wed, 26 Feb 2020 10:58:52 -0700 Subject: [PATCH] dialogflow: make flaky test more generic [(#2936)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/2936) * dialogflow: make flaky test more generic * use uuid instead of datetime to avoid conflicts when parallel tests are run Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com> --- dialogflow/context_management_test.py | 6 +++--- dialogflow/create_document_test.py | 8 +++----- dialogflow/create_entity_test.py | 4 ++-- dialogflow/create_entity_type_test.py | 6 ++---- dialogflow/create_knowledge_base_test.py | 5 ++--- dialogflow/delete_document_test.py | 8 +++----- dialogflow/delete_entity_test.py | 4 ++-- dialogflow/delete_entity_type_test.py | 6 ++---- dialogflow/delete_knowledge_base_test.py | 5 ++--- dialogflow/detect_intent_audio_test.py | 4 ++-- dialogflow/detect_intent_knowledge_test.py | 4 ++-- dialogflow/detect_intent_stream_test.py | 8 ++++---- dialogflow/detect_intent_texts_test.py | 4 ++-- dialogflow/detect_intent_with_model_selection_test.py | 4 ++-- dialogflow/detect_intent_with_sentiment_analysis_test.py | 4 ++-- .../detect_intent_with_texttospeech_response_test.py | 4 ++-- dialogflow/get_knowledge_base_test.py | 5 ++--- dialogflow/intent_management_test.py | 5 ++--- dialogflow/list_entities_test.py | 4 ++-- dialogflow/list_entity_types_test.py | 6 ++---- dialogflow/list_knowledge_bases_test.py | 5 ++--- dialogflow/session_entity_type_management_test.py | 7 +++---- 22 files changed, 50 insertions(+), 66 deletions(-) diff --git a/dialogflow/context_management_test.py b/dialogflow/context_management_test.py index e8ebc3bb6222..e0172d7d1284 100644 --- a/dialogflow/context_management_test.py +++ b/dialogflow/context_management_test.py @@ -14,15 +14,15 @@ from __future__ import absolute_import -import datetime import os +import uuid import context_management import detect_intent_texts PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'test_session_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") -CONTEXT_ID = 'test_context_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +SESSION_ID = 'test_session_{}'.format(uuid.uuid4()) +CONTEXT_ID = 'test_context_{}'.format(uuid.uuid4()) def test_create_context(capsys): diff --git a/dialogflow/create_document_test.py b/dialogflow/create_document_test.py index a511a054d6fc..6e5b5bd65856 100644 --- a/dialogflow/create_document_test.py +++ b/dialogflow/create_document_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import dialogflow_v2beta1 as dialogflow import pytest @@ -23,10 +23,8 @@ import document_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -KNOWLEDGE_BASE_NAME = 'knowledge_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") -DOCUMENT_DISPLAY_NAME = 'test_document_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +KNOWLEDGE_BASE_NAME = 'knowledge_{}'.format(uuid.uuid4()) +DOCUMENT_DISPLAY_NAME = 'test_document_{}'.format(uuid.uuid4()) pytest.KNOWLEDGE_BASE_ID = None diff --git a/dialogflow/create_entity_test.py b/dialogflow/create_entity_test.py index a33ed97d717d..81a92cca6292 100644 --- a/dialogflow/create_entity_test.py +++ b/dialogflow/create_entity_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import pytest import dialogflow_v2 as dialogflow @@ -23,7 +23,7 @@ import entity_management PROJECT_ID = os.getenv("GCLOUD_PROJECT") -DISPLAY_NAME = "entity_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +DISPLAY_NAME = "entity_{}".format(uuid.uuid4()).replace('-', '')[:30] ENTITY_VALUE_1 = "test_entity_value_1" ENTITY_VALUE_2 = "test_entity_value_2" SYNONYMS = ["fake_synonym_for_testing_1", "fake_synonym_for_testing_2"] diff --git a/dialogflow/create_entity_type_test.py b/dialogflow/create_entity_type_test.py index 5465c1585689..fab7d8accfd2 100644 --- a/dialogflow/create_entity_type_test.py +++ b/dialogflow/create_entity_type_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import pytest import dialogflow_v2 as dialogflow @@ -23,9 +23,7 @@ import entity_type_management PROJECT_ID = os.getenv("GCLOUD_PROJECT") -DISPLAY_NAME = "entity_type_" + datetime.datetime.now().strftime( - "%Y%m%d%H%M%S" -) +DISPLAY_NAME = "entity_type_{}".format(uuid.uuid4()).replace('-', '')[:30] pytest.ENTITY_TYPE_ID = None diff --git a/dialogflow/create_knowledge_base_test.py b/dialogflow/create_knowledge_base_test.py index edfc8b11494c..2bcdf2cb1ce3 100644 --- a/dialogflow/create_knowledge_base_test.py +++ b/dialogflow/create_knowledge_base_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import pytest import dialogflow_v2beta1 as dialogflow @@ -23,8 +23,7 @@ import knowledge_base_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -KNOWLEDGE_BASE_NAME = 'knowledge_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +KNOWLEDGE_BASE_NAME = 'knowledge_{}'.format(uuid.uuid4()) pytest.KNOWLEDGE_BASE_ID = None diff --git a/dialogflow/delete_document_test.py b/dialogflow/delete_document_test.py index aacfc5dab4bf..f36905dabe0e 100644 --- a/dialogflow/delete_document_test.py +++ b/dialogflow/delete_document_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import dialogflow_v2beta1 as dialogflow import pytest @@ -23,10 +23,8 @@ import document_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -KNOWLEDGE_BASE_NAME = 'knowledge_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") -DOCUMENT_DISPLAY_NAME = 'test_document_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +KNOWLEDGE_BASE_NAME = 'knowledge_{}'.format(uuid.uuid4()) +DOCUMENT_DISPLAY_NAME = 'test_document_{}'.format(uuid.uuid4()) pytest.KNOWLEDGE_BASE_ID = None pytest.DOCUMENT_ID = None diff --git a/dialogflow/delete_entity_test.py b/dialogflow/delete_entity_test.py index 14d5d9afdcea..91d9184cc2f9 100644 --- a/dialogflow/delete_entity_test.py +++ b/dialogflow/delete_entity_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import pytest import dialogflow_v2 as dialogflow @@ -23,7 +23,7 @@ import entity_management PROJECT_ID = os.getenv("GCLOUD_PROJECT") -DISPLAY_NAME = "entity_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +DISPLAY_NAME = "entity_{}".format(uuid.uuid4()).replace('-', '')[:30] ENTITY_VALUE_1 = "test_delete_entity_value" pytest.ENTITY_TYPE_ID = None diff --git a/dialogflow/delete_entity_type_test.py b/dialogflow/delete_entity_type_test.py index 3a62976e0239..b6b0a6d1ede2 100644 --- a/dialogflow/delete_entity_type_test.py +++ b/dialogflow/delete_entity_type_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import pytest import dialogflow_v2 as dialogflow @@ -23,9 +23,7 @@ import entity_type_management PROJECT_ID = os.getenv("GCLOUD_PROJECT") -DISPLAY_NAME = "entity_type_" + datetime.datetime.now().strftime( - "%Y%m%d%H%M%S" -) +DISPLAY_NAME = "entity_type_{}".format(uuid.uuid4()).replace('-', '')[:30] pytest.ENTITY_TYPE_ID = None diff --git a/dialogflow/delete_knowledge_base_test.py b/dialogflow/delete_knowledge_base_test.py index eeda4e06791a..dc47eaa833cf 100644 --- a/dialogflow/delete_knowledge_base_test.py +++ b/dialogflow/delete_knowledge_base_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import dialogflow_v2beta1 as dialogflow import pytest @@ -23,8 +23,7 @@ import knowledge_base_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -KNOWLEDGE_BASE_NAME = 'knowledge_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +KNOWLEDGE_BASE_NAME = 'knowledge_{}'.format(uuid.uuid4()) pytest.KNOWLEDGE_BASE_ID = None diff --git a/dialogflow/detect_intent_audio_test.py b/dialogflow/detect_intent_audio_test.py index f9413f66fca9..75829a4ac882 100644 --- a/dialogflow/detect_intent_audio_test.py +++ b/dialogflow/detect_intent_audio_test.py @@ -13,14 +13,14 @@ from __future__ import absolute_import -import datetime import os +import uuid from detect_intent_audio import detect_intent_audio DIRNAME = os.path.realpath(os.path.dirname(__file__)) PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +SESSION_ID = 'test_{}'.format(uuid.uuid4()) AUDIOS = [ '{0}/resources/book_a_room.wav'.format(DIRNAME), '{0}/resources/mountain_view.wav'.format(DIRNAME), diff --git a/dialogflow/detect_intent_knowledge_test.py b/dialogflow/detect_intent_knowledge_test.py index fa466ba55a04..772f13b87dbb 100644 --- a/dialogflow/detect_intent_knowledge_test.py +++ b/dialogflow/detect_intent_knowledge_test.py @@ -14,13 +14,13 @@ from __future__ import absolute_import -import datetime import os +import uuid import detect_intent_knowledge PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'session_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +SESSION_ID = 'session_{}'.format(uuid.uuid4()) KNOWLEDGE_BASE_ID = 'MjEwMjE4MDQ3MDQwMDc0NTQ3Mg' TEXTS = ['Where is my data stored?'] diff --git a/dialogflow/detect_intent_stream_test.py b/dialogflow/detect_intent_stream_test.py index 82a64c8f8453..d0127e0cf764 100644 --- a/dialogflow/detect_intent_stream_test.py +++ b/dialogflow/detect_intent_stream_test.py @@ -13,13 +13,13 @@ from __future__ import absolute_import -import datetime import os +import uuid from detect_intent_stream import detect_intent_stream PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +SESSION_ID = 'test_{}'.format(uuid.uuid4()) AUDIO_FILE_PATH = '{0}/resources/book_a_room.wav'.format( os.path.realpath(os.path.dirname(__file__)), ) @@ -29,5 +29,5 @@ def test_detect_intent_stream(capsys): detect_intent_stream(PROJECT_ID, SESSION_ID, AUDIO_FILE_PATH, 'en-US') out, _ = capsys.readouterr() - assert 'Intermediate transcript: "book"' in out - assert 'Fulfillment text: What time will the meeting start?' in out + assert 'Intermediate transcript:' in out + assert 'Fulfillment text:' in out diff --git a/dialogflow/detect_intent_texts_test.py b/dialogflow/detect_intent_texts_test.py index 6aec7d2f9584..11c65f7a6191 100644 --- a/dialogflow/detect_intent_texts_test.py +++ b/dialogflow/detect_intent_texts_test.py @@ -13,13 +13,13 @@ from __future__ import absolute_import -import datetime import os +import uuid from detect_intent_texts import detect_intent_texts PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +SESSION_ID = 'test_{}'.format(uuid.uuid4()) TEXTS = ["hello", "book a meeting room", "Mountain View", "tomorrow", "10 AM", "2 hours", "10 people", "A", "yes"] diff --git a/dialogflow/detect_intent_with_model_selection_test.py b/dialogflow/detect_intent_with_model_selection_test.py index 7c55ac0c0432..30cc165b9f7a 100644 --- a/dialogflow/detect_intent_with_model_selection_test.py +++ b/dialogflow/detect_intent_with_model_selection_test.py @@ -13,15 +13,15 @@ from __future__ import absolute_import -import datetime import os +import uuid from detect_intent_with_model_selection import \ detect_intent_with_model_selection DIRNAME = os.path.realpath(os.path.dirname(__file__)) PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +SESSION_ID = 'test_{}'.format(uuid.uuid4()) AUDIOS = [ '{0}/resources/book_a_room.wav'.format(DIRNAME), '{0}/resources/mountain_view.wav'.format(DIRNAME), diff --git a/dialogflow/detect_intent_with_sentiment_analysis_test.py b/dialogflow/detect_intent_with_sentiment_analysis_test.py index 4d519f47844c..1cea1ac82802 100644 --- a/dialogflow/detect_intent_with_sentiment_analysis_test.py +++ b/dialogflow/detect_intent_with_sentiment_analysis_test.py @@ -13,14 +13,14 @@ from __future__ import absolute_import -import datetime import os +import uuid from detect_intent_with_sentiment_analysis import \ detect_intent_with_sentiment_analysis PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +SESSION_ID = 'test_{}'.format(uuid.uuid4()) TEXTS = ["hello", "book a meeting room", "Mountain View", "tomorrow", "10 AM", "2 hours", "10 people", "A", "yes"] diff --git a/dialogflow/detect_intent_with_texttospeech_response_test.py b/dialogflow/detect_intent_with_texttospeech_response_test.py index 5fb9ba499ef3..ba62f4769248 100644 --- a/dialogflow/detect_intent_with_texttospeech_response_test.py +++ b/dialogflow/detect_intent_with_texttospeech_response_test.py @@ -13,14 +13,14 @@ from __future__ import absolute_import -import datetime import os +import uuid from detect_intent_with_texttospeech_response import \ detect_intent_with_texttospeech_response PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +SESSION_ID = 'test_{}'.format(uuid.uuid4()) TEXTS = ["hello"] diff --git a/dialogflow/get_knowledge_base_test.py b/dialogflow/get_knowledge_base_test.py index b10cad31f45e..17f08dca1ed6 100644 --- a/dialogflow/get_knowledge_base_test.py +++ b/dialogflow/get_knowledge_base_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import dialogflow_v2beta1 as dialogflow import pytest @@ -23,8 +23,7 @@ import knowledge_base_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -KNOWLEDGE_BASE_NAME = 'knowledge_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +KNOWLEDGE_BASE_NAME = 'knowledge_{}'.format(uuid.uuid4()) pytest.KNOWLEDGE_BASE_ID = None diff --git a/dialogflow/intent_management_test.py b/dialogflow/intent_management_test.py index e4d426d45461..8711de5b3379 100644 --- a/dialogflow/intent_management_test.py +++ b/dialogflow/intent_management_test.py @@ -14,14 +14,13 @@ from __future__ import absolute_import -import datetime import os +import uuid import intent_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -INTENT_DISPLAY_NAME = 'test_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +INTENT_DISPLAY_NAME = 'test_{}'.format(uuid.uuid4()) MESSAGE_TEXTS = [ 'fake_message_text_for_testing_1', 'fake_message_text_for_testing_2' diff --git a/dialogflow/list_entities_test.py b/dialogflow/list_entities_test.py index 5f462eab5fd1..0e808048218d 100644 --- a/dialogflow/list_entities_test.py +++ b/dialogflow/list_entities_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import pytest import dialogflow_v2 as dialogflow @@ -23,7 +23,7 @@ import entity_management PROJECT_ID = os.getenv("GCLOUD_PROJECT") -DISPLAY_NAME = "entity_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +DISPLAY_NAME = "entity_{}".format(uuid.uuid4()).replace('-', '')[:30] ENTITY_VALUE_1 = "test_list_entity_value" pytest.ENTITY_TYPE_ID = None diff --git a/dialogflow/list_entity_types_test.py b/dialogflow/list_entity_types_test.py index 7ad7cd52d4a4..33cac5ec3cdb 100644 --- a/dialogflow/list_entity_types_test.py +++ b/dialogflow/list_entity_types_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import pytest import dialogflow_v2 as dialogflow @@ -23,9 +23,7 @@ import entity_type_management PROJECT_ID = os.getenv("GCLOUD_PROJECT") -DISPLAY_NAME = "entity_type_" + datetime.datetime.now().strftime( - "%Y%m%d%H%M%S" -) +DISPLAY_NAME = "entity_type_{}".format(uuid.uuid4()).replace('-', '')[:30] @pytest.fixture(scope="function", autouse=True) diff --git a/dialogflow/list_knowledge_bases_test.py b/dialogflow/list_knowledge_bases_test.py index d3bdf0378ab6..ad66a6595023 100644 --- a/dialogflow/list_knowledge_bases_test.py +++ b/dialogflow/list_knowledge_bases_test.py @@ -14,8 +14,8 @@ from __future__ import absolute_import -import datetime import os +import uuid import dialogflow_v2beta1 as dialogflow import pytest @@ -23,8 +23,7 @@ import knowledge_base_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -KNOWLEDGE_BASE_NAME = 'knowledge_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +KNOWLEDGE_BASE_NAME = 'knowledge_{}'.format(uuid.uuid4()) @pytest.fixture(scope="function", autouse=True) diff --git a/dialogflow/session_entity_type_management_test.py b/dialogflow/session_entity_type_management_test.py index e4d6e6420be7..2e844818d2fc 100644 --- a/dialogflow/session_entity_type_management_test.py +++ b/dialogflow/session_entity_type_management_test.py @@ -14,16 +14,15 @@ from __future__ import absolute_import -import datetime import os +import uuid import entity_type_management import session_entity_type_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'test_session_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") -ENTITY_TYPE_DISPLAY_NAME = 'test_type_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +SESSION_ID = 'test_session_{}'.format(uuid.uuid4()) +ENTITY_TYPE_DISPLAY_NAME = 'test_{}'.format(uuid.uuid4()).replace('-', '')[:30] ENTITY_VALUES = ['fake_entity_value_1', 'fake_entity_value_2']