-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
Copy pathconversation_profile_management_test.py
124 lines (103 loc) · 4.33 KB
/
conversation_profile_management_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright 2021 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
from unittest import mock
from google.cloud import dialogflow_v2beta1 as dialogflow
import pytest
import conversation_profile_management
import test_utils
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
CONVERSATION_PROFILE_DISPLAY_NAME = "fake_conversation_profile_name"
CONVERSATION_PROFILE_NAME = f"conversationProfiles/{CONVERSATION_PROFILE_DISPLAY_NAME}"
@pytest.fixture(scope="function")
def mock_conversation():
yield mock.MagicMock(
return_value=test_utils.create_mock_conversation(
CONVERSATION_PROFILE_DISPLAY_NAME, CONVERSATION_PROFILE_NAME
)
)
@pytest.fixture(scope="function")
def mock_conversation_list():
yield mock.MagicMock(
return_value=[
test_utils.create_mock_conversation(
CONVERSATION_PROFILE_DISPLAY_NAME, CONVERSATION_PROFILE_NAME
)
]
)
def test_create_conversation_profile(capsys, mock_conversation, mock_conversation_list):
# Check the conversation profile does not yet exist.
with mock.patch(
"conversation_profile_management.dialogflow.ConversationProfilesClient.list_conversation_profiles",
mock.MagicMock(spec=dialogflow.ListConversationProfilesResponse),
):
response = conversation_profile_management.list_conversation_profiles(
PROJECT_ID
)
assert not any(
x.display_name == CONVERSATION_PROFILE_DISPLAY_NAME for x in response
)
# Create a conversation profile.
with mock.patch(
"conversation_profile_management.dialogflow.ConversationProfilesClient.create_conversation_profile",
mock_conversation,
):
response = (
conversation_profile_management.create_conversation_profile_article_faq(
project_id=PROJECT_ID,
display_name=CONVERSATION_PROFILE_DISPLAY_NAME,
article_suggestion_knowledge_base_id="abc",
)
)
out, _ = capsys.readouterr()
assert response.display_name == CONVERSATION_PROFILE_DISPLAY_NAME
conversation_profile_id = out.split("conversationProfiles/")[1].rstrip()
# List conversation profiles.
with mock.patch(
"conversation_profile_management.dialogflow.ConversationProfilesClient.list_conversation_profiles",
mock_conversation_list,
):
response = conversation_profile_management.list_conversation_profiles(
PROJECT_ID
)
assert any(
x.display_name == CONVERSATION_PROFILE_DISPLAY_NAME for x in response
)
# Get the conversation profile.
with mock.patch(
"conversation_profile_management.dialogflow.ConversationProfilesClient.get_conversation_profile",
mock_conversation,
):
conversation_profile_management.get_conversation_profile(
PROJECT_ID, conversation_profile_id
)
out, _ = capsys.readouterr()
assert f"Display Name: {CONVERSATION_PROFILE_DISPLAY_NAME}" in out
# Delete the conversation profile.
with mock.patch(
"conversation_profile_management.dialogflow.ConversationProfilesClient.list_conversation_profiles",
mock.MagicMock(return_value=None),
):
conversation_profile_management.delete_conversation_profile(
PROJECT_ID, conversation_profile_id
)
with mock.patch(
"conversation_profile_management.dialogflow.ConversationProfilesClient.list_conversation_profiles",
mock.MagicMock(spec=dialogflow.ListConversationProfilesResponse),
):
# Verify the conversation profile is deleted.
conversation_profile_management.list_conversation_profiles(PROJECT_ID)
out, _ = capsys.readouterr()
assert f"Display Name: {CONVERSATION_PROFILE_DISPLAY_NAME}" not in out