Skip to content

Commit b9fad0c

Browse files
galz10parthea
andauthored
docs(samples): added page management samples (#152)
* docs(samples): added page management samples * fixed failing test * Remvoe await * added await * added pytest async * Lint fix * Lint and test case fix * removed pytest.async * test fix * update tests * fix uuid * added asyncio * added loop * removed async * test fix * changed pytest scope to session * changes fixture scope * test change * test fix * changed response type * fixed list page test * lint fix * failing test fix * revised code per comments * converted object to string Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent c72d43a commit b9fad0c

File tree

3 files changed

+195
-3
lines changed

3 files changed

+195
-3
lines changed

Dialogflow-CX/create_agent_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
"""Test for create_agent"""
1515

16-
from datetime import date
1716
import os
17+
import uuid
1818

1919
from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
2020
from google.cloud.dialogflowcx_v3.types.agent import DeleteAgentRequest
@@ -34,8 +34,7 @@ def delete_agent(name):
3434

3535

3636
def test_create_agent():
37-
today = date.today()
38-
agentName = "tempAgent." + today.strftime("%d.%m.%Y")
37+
agentName = f"fake_agent_{uuid.uuid4()}"
3938
response = create_agent(PROJECT_ID, agentName)
4039
delete_agent(response.name)
4140

Dialogflow-CX/page_management.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2021, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from google.cloud.dialogflowcx_v3 import PagesAsyncClient
15+
from google.cloud.dialogflowcx_v3.types.page import (
16+
CreatePageRequest,
17+
DeletePageRequest,
18+
ListPagesRequest,
19+
Page,
20+
)
21+
22+
23+
# [START dialogflow_cx_create_page]
24+
async def create_page(project_id, agent_id, flow_id, location, displayName):
25+
pages_client = PagesAsyncClient()
26+
27+
page = Page()
28+
page.display_name = displayName
29+
30+
request = CreatePageRequest()
31+
request.parent = (
32+
"projects/"
33+
+ project_id
34+
+ "/locations/"
35+
+ location
36+
+ "/agents/"
37+
+ agent_id
38+
+ "/flows/"
39+
+ flow_id
40+
)
41+
request.page = page
42+
43+
response = await pages_client.create_page(request=request)
44+
return response
45+
46+
47+
# [END dialogflow_cx_create_page]
48+
49+
50+
# [START dialogflow_cx_list_page]
51+
async def list_page(project_id, agent_id, flow_id, location):
52+
pages_client = PagesAsyncClient()
53+
54+
request = ListPagesRequest()
55+
request.parent = (
56+
f"projects/{project_id}/locations/{location}/agents/{agent_id}/flows/{flow_id}"
57+
)
58+
59+
request.language_code = "en"
60+
61+
response = await pages_client.list_pages(request=request)
62+
return response
63+
64+
65+
# [END dialogflow_cx_list_page]
66+
67+
68+
# [START dialogflow_cx_delete_page]
69+
async def delete_page(project_id, agent_id, flow_id, page_id, location):
70+
pages_client = PagesAsyncClient()
71+
72+
request = DeletePageRequest()
73+
request.name = f"projects/{project_id}/locations/{location}/agents/{agent_id}/flows/{flow_id}/pages/{page_id}"
74+
75+
response = await pages_client.delete_page(request=request)
76+
return response
77+
78+
79+
# [END dialogflow_cx_delete_page]

Dialogflow-CX/page_management_test.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Copyright 2021, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import asyncio
15+
from copy import Error
16+
import os
17+
import uuid
18+
19+
from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
20+
from google.cloud.dialogflowcx_v3.types.agent import Agent, DeleteAgentRequest
21+
22+
import pytest
23+
24+
from page_management import create_page, delete_page, list_page
25+
26+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
27+
pytest.AGENT_ID = None
28+
pytest.PARENT = None
29+
pytest.CREATED_PAGE = None
30+
pytest.PAGE_ID = None
31+
32+
33+
def delete_agent(name):
34+
agents_client = AgentsClient()
35+
agent = DeleteAgentRequest(name=name)
36+
agents_client.delete_agent(request=agent)
37+
38+
39+
@pytest.fixture
40+
def loop():
41+
loop = asyncio.new_event_loop()
42+
yield loop
43+
loop.close()
44+
45+
46+
@pytest.fixture(scope="module", autouse=True)
47+
def setup_teardown():
48+
loop = asyncio.new_event_loop()
49+
agentName = "temp_agent_" + str(uuid.uuid4())
50+
51+
parent = "projects/" + PROJECT_ID + "/locations/global"
52+
53+
agents_client = AgentsClient()
54+
55+
agent = Agent(
56+
display_name=agentName,
57+
default_language_code="en",
58+
time_zone="America/Los_Angeles",
59+
)
60+
61+
response = agents_client.create_agent(request={"agent": agent, "parent": parent})
62+
pytest.PARENT = response.name
63+
64+
pytest.AGENT_ID = pytest.PARENT.split("/")[5]
65+
print("Created Agent in setUp")
66+
67+
yield
68+
69+
delete_agent(pytest.PARENT)
70+
loop.close()
71+
72+
73+
def test_create_page(loop: asyncio.AbstractEventLoop):
74+
pytest.CREATED_PAGE = f"fake_page_{uuid.uuid4()}"
75+
actualResponse = loop.run_until_complete(
76+
create_page(
77+
PROJECT_ID,
78+
pytest.AGENT_ID,
79+
"00000000-0000-0000-0000-000000000000",
80+
"global",
81+
pytest.CREATED_PAGE,
82+
)
83+
)
84+
85+
pytest.PAGE_ID = actualResponse.name.split("/")[9]
86+
assert actualResponse.display_name == pytest.CREATED_PAGE
87+
88+
89+
def test_list_page(loop: asyncio.AbstractEventLoop):
90+
actualResponse = loop.run_until_complete(
91+
list_page(
92+
PROJECT_ID,
93+
pytest.AGENT_ID,
94+
"00000000-0000-0000-0000-000000000000",
95+
"global",
96+
)
97+
)
98+
99+
assert pytest.PAGE_ID in str(actualResponse)
100+
101+
102+
def test_delete_page(loop: asyncio.AbstractEventLoop):
103+
try:
104+
loop.run_until_complete(
105+
delete_page(
106+
PROJECT_ID,
107+
pytest.AGENT_ID,
108+
"00000000-0000-0000-0000-000000000000",
109+
pytest.PAGE_ID,
110+
"global",
111+
)
112+
)
113+
except Error:
114+
pytest.fail("Unexpected MyError ..")

0 commit comments

Comments
 (0)