Skip to content

Commit 536ae4c

Browse files
nnegreygguuss
authored andcommitted
dialogflow: use unique names for all resources for tests to avoid col… [(#2696)](#2696)
* dialogflow: use unique names for all resources for tests to avoid collision * lint: line length * break up knowledge base tests and document tests from one file into individual tests * lint * bump library version * ping googleapis-common-protos to avoid release breakage * Update requirements.txt Co-authored-by: Gus Class <[email protected]>
1 parent a447c18 commit 536ae4c

21 files changed

+448
-120
lines changed

dialogflow/context_management_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414

1515
from __future__ import absolute_import
1616

17+
import datetime
1718
import os
1819

1920
import context_management
2021
import detect_intent_texts
2122

2223
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
23-
SESSION_ID = 'fake_session_for_testing'
24-
CONTEXT_ID = 'fake_context_for_testing'
24+
SESSION_ID = 'test_session_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
25+
CONTEXT_ID = 'test_context_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
2526

2627

2728
def test_create_context(capsys):

dialogflow/create_document_test.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
from __future__ import absolute_import
16+
17+
import datetime
18+
import os
19+
20+
import dialogflow_v2beta1 as dialogflow
21+
import pytest
22+
23+
import document_management
24+
25+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
26+
KNOWLEDGE_BASE_NAME = 'knowledge_' \
27+
+ datetime.datetime.now().strftime("%Y%m%d%H%M%S")
28+
DOCUMENT_DISPLAY_NAME = 'test_document_' \
29+
+ datetime.datetime.now().strftime("%Y%m%d%H%M%S")
30+
pytest.KNOWLEDGE_BASE_ID = None
31+
32+
33+
@pytest.fixture(scope="function", autouse=True)
34+
def setup_teardown():
35+
# Create a knowledge base to use in document management
36+
client = dialogflow.KnowledgeBasesClient()
37+
project_path = client.project_path(PROJECT_ID)
38+
knowledge_base = dialogflow.types.KnowledgeBase(
39+
display_name=KNOWLEDGE_BASE_NAME)
40+
response = client.create_knowledge_base(project_path, knowledge_base)
41+
pytest.KNOWLEDGE_BASE_ID = response.name.split(
42+
'/knowledgeBases/')[1].split('\n')[0]
43+
44+
yield
45+
46+
# Delete the created knowledge base
47+
knowledge_base_path = client.knowledge_base_path(
48+
PROJECT_ID, pytest.KNOWLEDGE_BASE_ID)
49+
client.delete_knowledge_base(knowledge_base_path, force=True)
50+
51+
52+
def test_create_document(capsys):
53+
document_management.create_document(
54+
PROJECT_ID, pytest.KNOWLEDGE_BASE_ID, DOCUMENT_DISPLAY_NAME,
55+
'text/html', 'FAQ', 'https://cloud.google.com/storage/docs/faq')
56+
out, _ = capsys.readouterr()
57+
assert DOCUMENT_DISPLAY_NAME in out
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
from __future__ import absolute_import
16+
17+
import datetime
18+
import os
19+
import pytest
20+
21+
import dialogflow_v2beta1 as dialogflow
22+
23+
import knowledge_base_management
24+
25+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
26+
KNOWLEDGE_BASE_NAME = 'knowledge_' \
27+
+ datetime.datetime.now().strftime("%Y%m%d%H%M%S")
28+
pytest.KNOWLEDGE_BASE_ID = None
29+
30+
31+
@pytest.fixture(scope="function", autouse=True)
32+
def teardown():
33+
yield
34+
35+
# Delete the created knowledge base
36+
client = dialogflow.KnowledgeBasesClient()
37+
assert pytest.KNOWLEDGE_BASE_ID is not None
38+
knowledge_base_path = client.knowledge_base_path(
39+
PROJECT_ID, pytest.KNOWLEDGE_BASE_ID)
40+
client.delete_knowledge_base(knowledge_base_path)
41+
42+
43+
def test_create_knowledge_base(capsys):
44+
knowledge_base_management.create_knowledge_base(PROJECT_ID,
45+
KNOWLEDGE_BASE_NAME)
46+
out, _ = capsys.readouterr()
47+
assert KNOWLEDGE_BASE_NAME in out
48+
49+
pytest.KNOWLEDGE_BASE_ID = out.split('/knowledgeBases/')[1].split('\n')[0]

dialogflow/delete_document_test.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
from __future__ import absolute_import
16+
17+
import datetime
18+
import os
19+
20+
import dialogflow_v2beta1 as dialogflow
21+
import pytest
22+
23+
import document_management
24+
25+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
26+
KNOWLEDGE_BASE_NAME = 'knowledge_' \
27+
+ datetime.datetime.now().strftime("%Y%m%d%H%M%S")
28+
DOCUMENT_DISPLAY_NAME = 'test_document_' \
29+
+ datetime.datetime.now().strftime("%Y%m%d%H%M%S")
30+
pytest.KNOWLEDGE_BASE_ID = None
31+
pytest.DOCUMENT_ID = None
32+
33+
34+
@pytest.fixture(scope="function", autouse=True)
35+
def setup_teardown():
36+
# Create a knowledge base to use in document management
37+
client = dialogflow.KnowledgeBasesClient()
38+
project_path = client.project_path(PROJECT_ID)
39+
knowledge_base = dialogflow.types.KnowledgeBase(
40+
display_name=KNOWLEDGE_BASE_NAME)
41+
response = client.create_knowledge_base(project_path, knowledge_base)
42+
pytest.KNOWLEDGE_BASE_ID = response.name.split(
43+
'/knowledgeBases/')[1].split('\n')[0]
44+
45+
# Create a document to delete
46+
knowledge_base_path = client.knowledge_base_path(
47+
PROJECT_ID, pytest.KNOWLEDGE_BASE_ID)
48+
document = dialogflow.types.Document(
49+
display_name=DOCUMENT_DISPLAY_NAME, mime_type='text/html',
50+
content_uri='https://cloud.google.com/storage/docs/faq')
51+
document.knowledge_types.append(
52+
dialogflow.types.Document.KnowledgeType.Value('FAQ'))
53+
documents_client = dialogflow.DocumentsClient()
54+
response = documents_client.create_document(knowledge_base_path, document)
55+
document = response.result(timeout=90)
56+
pytest.DOCUMENT_ID = document.name.split('/documents/')[1].split('\n')[0]
57+
58+
yield
59+
60+
# Delete the created knowledge base
61+
client.delete_knowledge_base(knowledge_base_path, force=True)
62+
63+
64+
def test_delete_document(capsys):
65+
document_management.delete_document(
66+
PROJECT_ID, pytest.KNOWLEDGE_BASE_ID, pytest.DOCUMENT_ID)
67+
document_management.list_documents(PROJECT_ID, pytest.KNOWLEDGE_BASE_ID)
68+
out, _ = capsys.readouterr()
69+
assert DOCUMENT_DISPLAY_NAME not in out
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
from __future__ import absolute_import
16+
17+
import datetime
18+
import os
19+
20+
import dialogflow_v2beta1 as dialogflow
21+
import pytest
22+
23+
import knowledge_base_management
24+
25+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
26+
KNOWLEDGE_BASE_NAME = 'knowledge_' \
27+
+ datetime.datetime.now().strftime("%Y%m%d%H%M%S")
28+
pytest.KNOWLEDGE_BASE_ID = None
29+
30+
31+
@pytest.fixture(scope="function", autouse=True)
32+
def setup():
33+
# Create a knowledge base to delete
34+
client = dialogflow.KnowledgeBasesClient()
35+
project_path = client.project_path(PROJECT_ID)
36+
knowledge_base = dialogflow.types.KnowledgeBase(
37+
display_name=KNOWLEDGE_BASE_NAME)
38+
response = client.create_knowledge_base(project_path, knowledge_base)
39+
pytest.KNOWLEDGE_BASE_ID = response.name.split(
40+
'/knowledgeBases/')[1].split('\n')[0]
41+
42+
43+
def test_delete_knowledge_base(capsys):
44+
knowledge_base_management.delete_knowledge_base(
45+
PROJECT_ID, pytest.KNOWLEDGE_BASE_ID)
46+
knowledge_base_management.list_knowledge_bases(PROJECT_ID)
47+
out, _ = capsys.readouterr()
48+
assert KNOWLEDGE_BASE_NAME not in out

dialogflow/detect_intent_audio_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
from __future__ import absolute_import
1515

16+
import datetime
1617
import os
1718

1819
from detect_intent_audio import detect_intent_audio
1920

2021
DIRNAME = os.path.realpath(os.path.dirname(__file__))
2122
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
22-
SESSION_ID = 'fake_session_for_testing'
23+
SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
2324
AUDIOS = [
2425
'{0}/resources/book_a_room.wav'.format(DIRNAME),
2526
'{0}/resources/mountain_view.wav'.format(DIRNAME),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
from __future__ import absolute_import
16+
17+
import datetime
18+
import os
19+
20+
import detect_intent_knowledge
21+
22+
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
23+
SESSION_ID = 'session_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
24+
KNOWLEDGE_BASE_ID = 'MjEwMjE4MDQ3MDQwMDc0NTQ3Mg'
25+
TEXTS = ['Where is my data stored?']
26+
27+
28+
def test_detect_intent_knowledge(capsys):
29+
detect_intent_knowledge.detect_intent_knowledge(
30+
PROJECT_ID, SESSION_ID, 'en-us', KNOWLEDGE_BASE_ID, TEXTS)
31+
32+
out, _ = capsys.readouterr()
33+
assert 'Knowledge results' in out

dialogflow/detect_intent_stream_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
from __future__ import absolute_import
1515

16+
import datetime
1617
import os
1718

1819
from detect_intent_stream import detect_intent_stream
1920

2021
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
21-
SESSION_ID = 'fake_session_for_testing'
22+
SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
2223
AUDIO_FILE_PATH = '{0}/resources/book_a_room.wav'.format(
2324
os.path.realpath(os.path.dirname(__file__)),
2425
)

dialogflow/detect_intent_texts_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
from __future__ import absolute_import
1515

16+
import datetime
1617
import os
1718

1819
from detect_intent_texts import detect_intent_texts
1920

2021
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
21-
SESSION_ID = 'fake_session_for_testing'
22+
SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
2223
TEXTS = ["hello", "book a meeting room", "Mountain View",
2324
"tomorrow", "10 AM", "2 hours", "10 people", "A", "yes"]
2425

dialogflow/detect_intent_with_model_selection_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313

1414
from __future__ import absolute_import
1515

16+
import datetime
1617
import os
1718

1819
from detect_intent_with_model_selection import \
1920
detect_intent_with_model_selection
2021

2122
DIRNAME = os.path.realpath(os.path.dirname(__file__))
2223
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
23-
SESSION_ID = 'fake_session_for_testing'
24+
SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
2425
AUDIOS = [
2526
'{0}/resources/book_a_room.wav'.format(DIRNAME),
2627
'{0}/resources/mountain_view.wav'.format(DIRNAME),

dialogflow/detect_intent_with_sentiment_analysis_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
from __future__ import absolute_import
1515

16+
import datetime
1617
import os
1718

1819
from detect_intent_with_sentiment_analysis import \
1920
detect_intent_with_sentiment_analysis
2021

2122
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
22-
SESSION_ID = 'fake_session_for_testing'
23+
SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
2324
TEXTS = ["hello", "book a meeting room", "Mountain View",
2425
"tomorrow", "10 AM", "2 hours", "10 people", "A", "yes"]
2526

dialogflow/detect_intent_with_texttospeech_response_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
from __future__ import absolute_import
1515

16+
import datetime
1617
import os
1718

1819
from detect_intent_with_texttospeech_response import \
1920
detect_intent_with_texttospeech_response
2021

2122
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
22-
SESSION_ID = 'fake_session_for_testing'
23+
SESSION_ID = 'test_' + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
2324
TEXTS = ["hello"]
2425

2526

dialogflow/entity_management_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
from __future__ import absolute_import
1616

17+
import datetime
1718
import os
1819

1920
import entity_management
2021
import entity_type_management
2122

2223
PROJECT_ID = os.getenv('GCLOUD_PROJECT')
23-
ENTITY_TYPE_DISPLAY_NAME = 'fake_entity_type_for_testing'
24+
ENTITY_TYPE_DISPLAY_NAME = 'test_' \
25+
+ datetime.datetime.now().strftime("%Y%m%d%H%M%S")
2426
ENTITY_VALUE_1 = 'fake_entity_for_testing_1'
2527
ENTITY_VALUE_2 = 'fake_entity_for_testing_2'
2628
SYNONYMS = ['fake_synonym_for_testing_1', 'fake_synonym_for_testing_2']

0 commit comments

Comments
 (0)