From c8394cfac79d672add0e8eb41c90a5eec2e0d7f0 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 8 Jan 2020 14:39:59 -0700 Subject: [PATCH 1/7] dialogflow: use unique names for all resources for tests to avoid collision --- dialogflow/cloud-client/context_management_test.py | 5 +++-- dialogflow/cloud-client/detect_intent_audio_test.py | 3 ++- dialogflow/cloud-client/detect_intent_stream_test.py | 3 ++- dialogflow/cloud-client/detect_intent_texts_test.py | 3 ++- .../detect_intent_with_model_selection_test.py | 3 ++- .../detect_intent_with_sentiment_analysis_test.py | 3 ++- .../detect_intent_with_texttospeech_response_test.py | 3 ++- dialogflow/cloud-client/entity_management_test.py | 3 ++- dialogflow/cloud-client/intent_management_test.py | 3 ++- dialogflow/cloud-client/knowledge_base_management_test.py | 7 ++++--- .../cloud-client/session_entity_type_management_test.py | 5 +++-- 11 files changed, 26 insertions(+), 15 deletions(-) diff --git a/dialogflow/cloud-client/context_management_test.py b/dialogflow/cloud-client/context_management_test.py index 8460a9c65fb..e8ebc3bb622 100644 --- a/dialogflow/cloud-client/context_management_test.py +++ b/dialogflow/cloud-client/context_management_test.py @@ -14,14 +14,15 @@ from __future__ import absolute_import +import datetime import os import context_management import detect_intent_texts PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'fake_session_for_testing' -CONTEXT_ID = 'fake_context_for_testing' +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") def test_create_context(capsys): diff --git a/dialogflow/cloud-client/detect_intent_audio_test.py b/dialogflow/cloud-client/detect_intent_audio_test.py index 5f67f6ef149..f9413f66fca 100644 --- a/dialogflow/cloud-client/detect_intent_audio_test.py +++ b/dialogflow/cloud-client/detect_intent_audio_test.py @@ -13,13 +13,14 @@ from __future__ import absolute_import +import datetime import os from detect_intent_audio import detect_intent_audio DIRNAME = os.path.realpath(os.path.dirname(__file__)) PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'fake_session_for_testing' +SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") AUDIOS = [ '{0}/resources/book_a_room.wav'.format(DIRNAME), '{0}/resources/mountain_view.wav'.format(DIRNAME), diff --git a/dialogflow/cloud-client/detect_intent_stream_test.py b/dialogflow/cloud-client/detect_intent_stream_test.py index f83ab07b5e6..82a64c8f845 100644 --- a/dialogflow/cloud-client/detect_intent_stream_test.py +++ b/dialogflow/cloud-client/detect_intent_stream_test.py @@ -13,12 +13,13 @@ from __future__ import absolute_import +import datetime import os from detect_intent_stream import detect_intent_stream PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'fake_session_for_testing' +SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") AUDIO_FILE_PATH = '{0}/resources/book_a_room.wav'.format( os.path.realpath(os.path.dirname(__file__)), ) diff --git a/dialogflow/cloud-client/detect_intent_texts_test.py b/dialogflow/cloud-client/detect_intent_texts_test.py index 1995b9b3d26..6aec7d2f958 100644 --- a/dialogflow/cloud-client/detect_intent_texts_test.py +++ b/dialogflow/cloud-client/detect_intent_texts_test.py @@ -13,12 +13,13 @@ from __future__ import absolute_import +import datetime import os from detect_intent_texts import detect_intent_texts PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'fake_session_for_testing' +SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") TEXTS = ["hello", "book a meeting room", "Mountain View", "tomorrow", "10 AM", "2 hours", "10 people", "A", "yes"] diff --git a/dialogflow/cloud-client/detect_intent_with_model_selection_test.py b/dialogflow/cloud-client/detect_intent_with_model_selection_test.py index 12762289331..7c55ac0c043 100644 --- a/dialogflow/cloud-client/detect_intent_with_model_selection_test.py +++ b/dialogflow/cloud-client/detect_intent_with_model_selection_test.py @@ -13,6 +13,7 @@ from __future__ import absolute_import +import datetime import os from detect_intent_with_model_selection import \ @@ -20,7 +21,7 @@ DIRNAME = os.path.realpath(os.path.dirname(__file__)) PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'fake_session_for_testing' +SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") AUDIOS = [ '{0}/resources/book_a_room.wav'.format(DIRNAME), '{0}/resources/mountain_view.wav'.format(DIRNAME), diff --git a/dialogflow/cloud-client/detect_intent_with_sentiment_analysis_test.py b/dialogflow/cloud-client/detect_intent_with_sentiment_analysis_test.py index 5e9041b177a..4d519f47844 100644 --- a/dialogflow/cloud-client/detect_intent_with_sentiment_analysis_test.py +++ b/dialogflow/cloud-client/detect_intent_with_sentiment_analysis_test.py @@ -13,13 +13,14 @@ from __future__ import absolute_import +import datetime import os from detect_intent_with_sentiment_analysis import \ detect_intent_with_sentiment_analysis PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'fake_session_for_testing' +SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") TEXTS = ["hello", "book a meeting room", "Mountain View", "tomorrow", "10 AM", "2 hours", "10 people", "A", "yes"] diff --git a/dialogflow/cloud-client/detect_intent_with_texttospeech_response_test.py b/dialogflow/cloud-client/detect_intent_with_texttospeech_response_test.py index 96419b2f229..5fb9ba499ef 100644 --- a/dialogflow/cloud-client/detect_intent_with_texttospeech_response_test.py +++ b/dialogflow/cloud-client/detect_intent_with_texttospeech_response_test.py @@ -13,13 +13,14 @@ from __future__ import absolute_import +import datetime import os from detect_intent_with_texttospeech_response import \ detect_intent_with_texttospeech_response PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'fake_session_for_testing' +SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") TEXTS = ["hello"] diff --git a/dialogflow/cloud-client/entity_management_test.py b/dialogflow/cloud-client/entity_management_test.py index e2433d7a4e9..c1bfc852755 100644 --- a/dialogflow/cloud-client/entity_management_test.py +++ b/dialogflow/cloud-client/entity_management_test.py @@ -14,13 +14,14 @@ from __future__ import absolute_import +import datetime import os import entity_management import entity_type_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -ENTITY_TYPE_DISPLAY_NAME = 'fake_entity_type_for_testing' +ENTITY_TYPE_DISPLAY_NAME = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") ENTITY_VALUE_1 = 'fake_entity_for_testing_1' ENTITY_VALUE_2 = 'fake_entity_for_testing_2' SYNONYMS = ['fake_synonym_for_testing_1', 'fake_synonym_for_testing_2'] diff --git a/dialogflow/cloud-client/intent_management_test.py b/dialogflow/cloud-client/intent_management_test.py index 53105fe0002..536a71d005d 100644 --- a/dialogflow/cloud-client/intent_management_test.py +++ b/dialogflow/cloud-client/intent_management_test.py @@ -14,12 +14,13 @@ from __future__ import absolute_import +import datetime import os import intent_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -INTENT_DISPLAY_NAME = 'fake_display_name_for_testing' +INTENT_DISPLAY_NAME = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") MESSAGE_TEXTS = [ 'fake_message_text_for_testing_1', 'fake_message_text_for_testing_2' diff --git a/dialogflow/cloud-client/knowledge_base_management_test.py b/dialogflow/cloud-client/knowledge_base_management_test.py index d5e6f26cf31..b4011c83931 100644 --- a/dialogflow/cloud-client/knowledge_base_management_test.py +++ b/dialogflow/cloud-client/knowledge_base_management_test.py @@ -14,6 +14,7 @@ from __future__ import absolute_import +import datetime import os import detect_intent_knowledge @@ -21,11 +22,11 @@ import knowledge_base_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'fake_session_for_testing' +SESSION_ID = 'test_session_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") TEXTS = ['Where is my data stored?'] -KNOWLEDGE_BASE_NAME = 'fake_knowledge_base_name' -DOCUMENT_BASE_NAME = 'fake_document_name' +KNOWLEDGE_BASE_NAME = 'test_knowledge_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +DOCUMENT_BASE_NAME = 'test_document_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") def test_create_knowledge_base(capsys): diff --git a/dialogflow/cloud-client/session_entity_type_management_test.py b/dialogflow/cloud-client/session_entity_type_management_test.py index 5931ea2ea81..77c02a6d015 100644 --- a/dialogflow/cloud-client/session_entity_type_management_test.py +++ b/dialogflow/cloud-client/session_entity_type_management_test.py @@ -14,14 +14,15 @@ from __future__ import absolute_import +import datetime import os import entity_type_management import session_entity_type_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'fake_session_for_testing' -ENTITY_TYPE_DISPLAY_NAME = 'fake_display_name_for_testing' +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") ENTITY_VALUES = ['fake_entity_value_1', 'fake_entity_value_2'] From 2efa87df788ac9288d4720b82f7e2dee7483330f Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 8 Jan 2020 14:49:14 -0700 Subject: [PATCH 2/7] lint: line length --- dialogflow/cloud-client/entity_management_test.py | 3 ++- dialogflow/cloud-client/intent_management_test.py | 3 ++- dialogflow/cloud-client/knowledge_base_management_test.py | 6 ++++-- .../cloud-client/session_entity_type_management_test.py | 3 ++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/dialogflow/cloud-client/entity_management_test.py b/dialogflow/cloud-client/entity_management_test.py index c1bfc852755..af75bc5bd76 100644 --- a/dialogflow/cloud-client/entity_management_test.py +++ b/dialogflow/cloud-client/entity_management_test.py @@ -21,7 +21,8 @@ import entity_type_management PROJECT_ID = os.getenv('GCLOUD_PROJECT') -ENTITY_TYPE_DISPLAY_NAME = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +ENTITY_TYPE_DISPLAY_NAME = 'test_' \ + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") ENTITY_VALUE_1 = 'fake_entity_for_testing_1' ENTITY_VALUE_2 = 'fake_entity_for_testing_2' SYNONYMS = ['fake_synonym_for_testing_1', 'fake_synonym_for_testing_2'] diff --git a/dialogflow/cloud-client/intent_management_test.py b/dialogflow/cloud-client/intent_management_test.py index 536a71d005d..e4d426d4546 100644 --- a/dialogflow/cloud-client/intent_management_test.py +++ b/dialogflow/cloud-client/intent_management_test.py @@ -20,7 +20,8 @@ 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_' \ + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") MESSAGE_TEXTS = [ 'fake_message_text_for_testing_1', 'fake_message_text_for_testing_2' diff --git a/dialogflow/cloud-client/knowledge_base_management_test.py b/dialogflow/cloud-client/knowledge_base_management_test.py index b4011c83931..f3a84fd92f3 100644 --- a/dialogflow/cloud-client/knowledge_base_management_test.py +++ b/dialogflow/cloud-client/knowledge_base_management_test.py @@ -25,8 +25,10 @@ SESSION_ID = 'test_session_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") TEXTS = ['Where is my data stored?'] -KNOWLEDGE_BASE_NAME = 'test_knowledge_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") -DOCUMENT_BASE_NAME = 'test_document_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +KNOWLEDGE_BASE_NAME = 'test_knowledge_' \ + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +DOCUMENT_BASE_NAME = 'test_document_' \ + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") def test_create_knowledge_base(capsys): diff --git a/dialogflow/cloud-client/session_entity_type_management_test.py b/dialogflow/cloud-client/session_entity_type_management_test.py index 77c02a6d015..e4d6e6420be 100644 --- a/dialogflow/cloud-client/session_entity_type_management_test.py +++ b/dialogflow/cloud-client/session_entity_type_management_test.py @@ -22,7 +22,8 @@ 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") +ENTITY_TYPE_DISPLAY_NAME = 'test_type_' \ + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") ENTITY_VALUES = ['fake_entity_value_1', 'fake_entity_value_2'] From b864630fdfe3c4ebb5d2e6211e0d25083120f9c4 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 8 Jan 2020 16:19:15 -0700 Subject: [PATCH 3/7] break up knowledge base tests and document tests from one file into individual tests --- .../cloud-client/create_document_test.py | 57 +++++++++ .../create_knowledge_base_test.py | 49 ++++++++ .../cloud-client/delete_document_test.py | 69 +++++++++++ .../delete_knowledge_base_test.py | 48 ++++++++ .../detect_intent_knowledge_test.py | 33 ++++++ dialogflow/cloud-client/get_document_test.py | 31 +++++ .../cloud-client/get_knowledge_base_test.py | 54 +++++++++ .../knowledge_base_management_test.py | 110 ------------------ .../cloud-client/list_documents_test.py | 29 +++++ .../cloud-client/list_knowledge_bases_test.py | 52 +++++++++ 10 files changed, 422 insertions(+), 110 deletions(-) create mode 100644 dialogflow/cloud-client/create_document_test.py create mode 100644 dialogflow/cloud-client/create_knowledge_base_test.py create mode 100644 dialogflow/cloud-client/delete_document_test.py create mode 100644 dialogflow/cloud-client/delete_knowledge_base_test.py create mode 100644 dialogflow/cloud-client/detect_intent_knowledge_test.py create mode 100644 dialogflow/cloud-client/get_document_test.py create mode 100644 dialogflow/cloud-client/get_knowledge_base_test.py delete mode 100644 dialogflow/cloud-client/knowledge_base_management_test.py create mode 100644 dialogflow/cloud-client/list_documents_test.py create mode 100644 dialogflow/cloud-client/list_knowledge_bases_test.py diff --git a/dialogflow/cloud-client/create_document_test.py b/dialogflow/cloud-client/create_document_test.py new file mode 100644 index 00000000000..a511a054d6f --- /dev/null +++ b/dialogflow/cloud-client/create_document_test.py @@ -0,0 +1,57 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import datetime +import os + +import dialogflow_v2beta1 as dialogflow +import pytest + +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") +pytest.KNOWLEDGE_BASE_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def setup_teardown(): + # Create a knowledge base to use in document management + client = dialogflow.KnowledgeBasesClient() + project_path = client.project_path(PROJECT_ID) + knowledge_base = dialogflow.types.KnowledgeBase( + display_name=KNOWLEDGE_BASE_NAME) + response = client.create_knowledge_base(project_path, knowledge_base) + pytest.KNOWLEDGE_BASE_ID = response.name.split( + '/knowledgeBases/')[1].split('\n')[0] + + yield + + # Delete the created knowledge base + knowledge_base_path = client.knowledge_base_path( + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) + client.delete_knowledge_base(knowledge_base_path, force=True) + + +def test_create_document(capsys): + document_management.create_document( + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID, DOCUMENT_DISPLAY_NAME, + 'text/html', 'FAQ', 'https://cloud.google.com/storage/docs/faq') + out, _ = capsys.readouterr() + assert DOCUMENT_DISPLAY_NAME in out diff --git a/dialogflow/cloud-client/create_knowledge_base_test.py b/dialogflow/cloud-client/create_knowledge_base_test.py new file mode 100644 index 00000000000..edfc8b11494 --- /dev/null +++ b/dialogflow/cloud-client/create_knowledge_base_test.py @@ -0,0 +1,49 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import datetime +import os +import pytest + +import dialogflow_v2beta1 as dialogflow + +import knowledge_base_management + +PROJECT_ID = os.getenv('GCLOUD_PROJECT') +KNOWLEDGE_BASE_NAME = 'knowledge_' \ + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +pytest.KNOWLEDGE_BASE_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def teardown(): + yield + + # Delete the created knowledge base + client = dialogflow.KnowledgeBasesClient() + assert pytest.KNOWLEDGE_BASE_ID is not None + knowledge_base_path = client.knowledge_base_path( + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) + client.delete_knowledge_base(knowledge_base_path) + + +def test_create_knowledge_base(capsys): + knowledge_base_management.create_knowledge_base(PROJECT_ID, + KNOWLEDGE_BASE_NAME) + out, _ = capsys.readouterr() + assert KNOWLEDGE_BASE_NAME in out + + pytest.KNOWLEDGE_BASE_ID = out.split('/knowledgeBases/')[1].split('\n')[0] diff --git a/dialogflow/cloud-client/delete_document_test.py b/dialogflow/cloud-client/delete_document_test.py new file mode 100644 index 00000000000..aacfc5dab4b --- /dev/null +++ b/dialogflow/cloud-client/delete_document_test.py @@ -0,0 +1,69 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import datetime +import os + +import dialogflow_v2beta1 as dialogflow +import pytest + +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") +pytest.KNOWLEDGE_BASE_ID = None +pytest.DOCUMENT_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def setup_teardown(): + # Create a knowledge base to use in document management + client = dialogflow.KnowledgeBasesClient() + project_path = client.project_path(PROJECT_ID) + knowledge_base = dialogflow.types.KnowledgeBase( + display_name=KNOWLEDGE_BASE_NAME) + response = client.create_knowledge_base(project_path, knowledge_base) + pytest.KNOWLEDGE_BASE_ID = response.name.split( + '/knowledgeBases/')[1].split('\n')[0] + + # Create a document to delete + knowledge_base_path = client.knowledge_base_path( + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) + document = dialogflow.types.Document( + display_name=DOCUMENT_DISPLAY_NAME, mime_type='text/html', + content_uri='https://cloud.google.com/storage/docs/faq') + document.knowledge_types.append( + dialogflow.types.Document.KnowledgeType.Value('FAQ')) + documents_client = dialogflow.DocumentsClient() + response = documents_client.create_document(knowledge_base_path, document) + document = response.result(timeout=90) + pytest.DOCUMENT_ID = document.name.split('/documents/')[1].split('\n')[0] + + yield + + # Delete the created knowledge base + client.delete_knowledge_base(knowledge_base_path, force=True) + + +def test_delete_document(capsys): + document_management.delete_document( + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID, pytest.DOCUMENT_ID) + document_management.list_documents(PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) + out, _ = capsys.readouterr() + assert DOCUMENT_DISPLAY_NAME not in out diff --git a/dialogflow/cloud-client/delete_knowledge_base_test.py b/dialogflow/cloud-client/delete_knowledge_base_test.py new file mode 100644 index 00000000000..eeda4e06791 --- /dev/null +++ b/dialogflow/cloud-client/delete_knowledge_base_test.py @@ -0,0 +1,48 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import datetime +import os + +import dialogflow_v2beta1 as dialogflow +import pytest + +import knowledge_base_management + +PROJECT_ID = os.getenv('GCLOUD_PROJECT') +KNOWLEDGE_BASE_NAME = 'knowledge_' \ + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +pytest.KNOWLEDGE_BASE_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def setup(): + # Create a knowledge base to delete + client = dialogflow.KnowledgeBasesClient() + project_path = client.project_path(PROJECT_ID) + knowledge_base = dialogflow.types.KnowledgeBase( + display_name=KNOWLEDGE_BASE_NAME) + response = client.create_knowledge_base(project_path, knowledge_base) + pytest.KNOWLEDGE_BASE_ID = response.name.split( + '/knowledgeBases/')[1].split('\n')[0] + + +def test_delete_knowledge_base(capsys): + knowledge_base_management.delete_knowledge_base( + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) + knowledge_base_management.list_knowledge_bases(PROJECT_ID) + out, _ = capsys.readouterr() + assert KNOWLEDGE_BASE_NAME not in out diff --git a/dialogflow/cloud-client/detect_intent_knowledge_test.py b/dialogflow/cloud-client/detect_intent_knowledge_test.py new file mode 100644 index 00000000000..fa466ba55a0 --- /dev/null +++ b/dialogflow/cloud-client/detect_intent_knowledge_test.py @@ -0,0 +1,33 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import datetime +import os + +import detect_intent_knowledge + +PROJECT_ID = os.getenv('GCLOUD_PROJECT') +SESSION_ID = 'session_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +KNOWLEDGE_BASE_ID = 'MjEwMjE4MDQ3MDQwMDc0NTQ3Mg' +TEXTS = ['Where is my data stored?'] + + +def test_detect_intent_knowledge(capsys): + detect_intent_knowledge.detect_intent_knowledge( + PROJECT_ID, SESSION_ID, 'en-us', KNOWLEDGE_BASE_ID, TEXTS) + + out, _ = capsys.readouterr() + assert 'Knowledge results' in out diff --git a/dialogflow/cloud-client/get_document_test.py b/dialogflow/cloud-client/get_document_test.py new file mode 100644 index 00000000000..3b6563e5f22 --- /dev/null +++ b/dialogflow/cloud-client/get_document_test.py @@ -0,0 +1,31 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import os + +import document_management + +PROJECT_ID = os.getenv('GCLOUD_PROJECT') +DOCUMENT_DISPLAY_NAME = 'DO_NOT_DELETE_TEST_DOCUMENT' +KNOWLEDGE_BASE_ID = 'MjEwMjE4MDQ3MDQwMDc0NTQ3Mg' +DOCUMENT_ID = 'MzMxOTU0NjU1MDUzNDc5OTM2MA' + + +def test_get_document(capsys): + document_management.get_document( + PROJECT_ID, KNOWLEDGE_BASE_ID, DOCUMENT_ID) + out, _ = capsys.readouterr() + assert DOCUMENT_DISPLAY_NAME in out diff --git a/dialogflow/cloud-client/get_knowledge_base_test.py b/dialogflow/cloud-client/get_knowledge_base_test.py new file mode 100644 index 00000000000..1fe74cdccac --- /dev/null +++ b/dialogflow/cloud-client/get_knowledge_base_test.py @@ -0,0 +1,54 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import datetime +import os + +import dialogflow_v2beta1 as dialogflow +import pytest + +import knowledge_base_management + +PROJECT_ID = os.getenv('GCLOUD_PROJECT') +KNOWLEDGE_BASE_NAME = 'knowledge_' \ + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +pytest.KNOWLEDGE_BASE_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def setup_teardown(): + # Create a knowledge base to get + client = dialogflow.KnowledgeBasesClient() + project_path = client.project_path(PROJECT_ID) + knowledge_base = dialogflow.types.KnowledgeBase( + display_name=KNOWLEDGE_BASE_NAME) + response = client.create_knowledge_base(project_path, knowledge_base) + pytest.KNOWLEDGE_BASE_ID = response.name.split( + '/knowledgeBases/')[1].split('\n')[0] + + yield + + # Delete the created knowledge base + knowledge_base_path = client.knowledge_base_path( + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) + client.delete_knowledge_base(knowledge_base_path) + + +def test_get_knowledge_base(capsys): + knowledge_base_management.get_knowledge_base(PROJECT_ID, + pytest.KNOWLEDGE_BASE_ID) + out, _ = capsys.readouterr() + assert KNOWLEDGE_BASE_NAME in out diff --git a/dialogflow/cloud-client/knowledge_base_management_test.py b/dialogflow/cloud-client/knowledge_base_management_test.py deleted file mode 100644 index f3a84fd92f3..00000000000 --- a/dialogflow/cloud-client/knowledge_base_management_test.py +++ /dev/null @@ -1,110 +0,0 @@ -# Copyright 2018 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import absolute_import - -import datetime -import os - -import detect_intent_knowledge -import document_management -import knowledge_base_management - -PROJECT_ID = os.getenv('GCLOUD_PROJECT') -SESSION_ID = 'test_session_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S") -TEXTS = ['Where is my data stored?'] - -KNOWLEDGE_BASE_NAME = 'test_knowledge_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") -DOCUMENT_BASE_NAME = 'test_document_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") - - -def test_create_knowledge_base(capsys): - # Check the knowledge base does not yet exists - knowledge_base_management.list_knowledge_bases(PROJECT_ID) - - out, _ = capsys.readouterr() - assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) not in out - - # Create a knowledge base - knowledge_base_management.create_knowledge_base(PROJECT_ID, - KNOWLEDGE_BASE_NAME) - - out, _ = capsys.readouterr() - assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) in out - - # List the knowledge base - knowledge_base_management.list_knowledge_bases(PROJECT_ID) - - out, _ = capsys.readouterr() - assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) in out - - knowledge_base_id = out.split('knowledgeBases/')[1].rstrip() - - # Get the knowledge base - knowledge_base_management.get_knowledge_base(PROJECT_ID, knowledge_base_id) - - out, _ = capsys.readouterr() - assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) in out - - # Create a Document - document_management.create_document( - PROJECT_ID, knowledge_base_id, DOCUMENT_BASE_NAME, 'text/html', 'FAQ', - 'https://cloud.google.com/storage/docs/faq') - - out, _ = capsys.readouterr() - assert 'Display Name: {}'.format(DOCUMENT_BASE_NAME) in out - - # List the Document - document_management.list_documents(PROJECT_ID, knowledge_base_id) - - out, _ = capsys.readouterr() - assert 'Display Name: {}'.format(DOCUMENT_BASE_NAME) in out - - document_id = out.split('documents/')[1].split(' - MIME Type:')[0].rstrip() - - # Get the Document - document_management.get_document(PROJECT_ID, knowledge_base_id, - document_id) - - out, _ = capsys.readouterr() - assert 'Display Name: {}'.format(DOCUMENT_BASE_NAME) in out - - # Detect intent with Knowledge Base - detect_intent_knowledge.detect_intent_knowledge( - PROJECT_ID, SESSION_ID, 'en-us', knowledge_base_id, TEXTS) - - out, _ = capsys.readouterr() - assert 'Knowledge results' in out - - # Delete the Document - document_management.delete_document(PROJECT_ID, knowledge_base_id, - document_id) - - # List the Document - document_management.list_documents(PROJECT_ID, knowledge_base_id) - - out, _ = capsys.readouterr() - assert 'Display Name: {}'.format(DOCUMENT_BASE_NAME) not in out - - # Delete the Knowledge Base - knowledge_base_management.delete_knowledge_base(PROJECT_ID, - knowledge_base_id) - - # List the knowledge base - knowledge_base_management.list_knowledge_bases(PROJECT_ID) - - out, _ = capsys.readouterr() - assert 'Display Name: {}'.format(KNOWLEDGE_BASE_NAME) not in out diff --git a/dialogflow/cloud-client/list_documents_test.py b/dialogflow/cloud-client/list_documents_test.py new file mode 100644 index 00000000000..d1be64ccd6d --- /dev/null +++ b/dialogflow/cloud-client/list_documents_test.py @@ -0,0 +1,29 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import os + +import document_management + +PROJECT_ID = os.getenv('GCLOUD_PROJECT') +DOCUMENT_DISPLAY_NAME = 'DO_NOT_DELETE_TEST_DOCUMENT' +KNOWLEDGE_BASE_ID = 'MjEwMjE4MDQ3MDQwMDc0NTQ3Mg' + + +def test_list_documents(capsys): + document_management.list_documents(PROJECT_ID, KNOWLEDGE_BASE_ID) + out, _ = capsys.readouterr() + assert DOCUMENT_DISPLAY_NAME in out diff --git a/dialogflow/cloud-client/list_knowledge_bases_test.py b/dialogflow/cloud-client/list_knowledge_bases_test.py new file mode 100644 index 00000000000..d3bdf0378ab --- /dev/null +++ b/dialogflow/cloud-client/list_knowledge_bases_test.py @@ -0,0 +1,52 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import + +import datetime +import os + +import dialogflow_v2beta1 as dialogflow +import pytest + +import knowledge_base_management + +PROJECT_ID = os.getenv('GCLOUD_PROJECT') +KNOWLEDGE_BASE_NAME = 'knowledge_' \ + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + + +@pytest.fixture(scope="function", autouse=True) +def setup_teardown(): + # Create a knowledge base to list + client = dialogflow.KnowledgeBasesClient() + project_path = client.project_path(PROJECT_ID) + knowledge_base = dialogflow.types.KnowledgeBase( + display_name=KNOWLEDGE_BASE_NAME) + response = client.create_knowledge_base(project_path, knowledge_base) + knowledge_base_id = response.name.split( + '/knowledgeBases/')[1].split('\n')[0] + + yield + + # Delete the created knowledge base + knowledge_base_path = client.knowledge_base_path( + PROJECT_ID, knowledge_base_id) + client.delete_knowledge_base(knowledge_base_path) + + +def test_list_knowledge_base(capsys): + knowledge_base_management.list_knowledge_bases(PROJECT_ID) + out, _ = capsys.readouterr() + assert KNOWLEDGE_BASE_NAME in out From 59f5bd97df1f64cd2ab62c2f30c477ad59fdf514 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 9 Jan 2020 13:28:36 -0700 Subject: [PATCH 4/7] lint --- dialogflow/cloud-client/get_knowledge_base_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dialogflow/cloud-client/get_knowledge_base_test.py b/dialogflow/cloud-client/get_knowledge_base_test.py index 1fe74cdccac..b10cad31f45 100644 --- a/dialogflow/cloud-client/get_knowledge_base_test.py +++ b/dialogflow/cloud-client/get_knowledge_base_test.py @@ -48,7 +48,7 @@ def setup_teardown(): def test_get_knowledge_base(capsys): - knowledge_base_management.get_knowledge_base(PROJECT_ID, - pytest.KNOWLEDGE_BASE_ID) + knowledge_base_management.get_knowledge_base( + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) out, _ = capsys.readouterr() assert KNOWLEDGE_BASE_NAME in out From 26b469eb39e29cab0d36acbb714ae11cd2692f68 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 14 Jan 2020 15:17:59 -0700 Subject: [PATCH 5/7] bump library version --- dialogflow/cloud-client/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dialogflow/cloud-client/requirements.txt b/dialogflow/cloud-client/requirements.txt index 59b3862007a..fb04ea6d61c 100644 --- a/dialogflow/cloud-client/requirements.txt +++ b/dialogflow/cloud-client/requirements.txt @@ -1 +1 @@ -dialogflow==0.6.0 +dialogflow==0.7.2 From c0c12614ff49e13cfe6ab78df956734f0b6a6c38 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 15 Jan 2020 10:24:50 -0700 Subject: [PATCH 6/7] ping googleapis-common-protos to avoid release breakage --- dialogflow/cloud-client/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/dialogflow/cloud-client/requirements.txt b/dialogflow/cloud-client/requirements.txt index fb04ea6d61c..0539912c05b 100644 --- a/dialogflow/cloud-client/requirements.txt +++ b/dialogflow/cloud-client/requirements.txt @@ -1 +1,2 @@ dialogflow==0.7.2 +googleapis-common-protos==1.6.0 \ No newline at end of file From f6987c3e459f5bba06baf73cbe6e03082ee59584 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Thu, 16 Jan 2020 16:03:53 -0700 Subject: [PATCH 7/7] Update requirements.txt --- dialogflow/cloud-client/requirements.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/dialogflow/cloud-client/requirements.txt b/dialogflow/cloud-client/requirements.txt index 0539912c05b..fb04ea6d61c 100644 --- a/dialogflow/cloud-client/requirements.txt +++ b/dialogflow/cloud-client/requirements.txt @@ -1,2 +1 @@ dialogflow==0.7.2 -googleapis-common-protos==1.6.0 \ No newline at end of file