From 41d15f8f212baab09222c6a350efc3376a3bf9ea Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Wed, 1 Sep 2021 09:48:09 -0700 Subject: [PATCH] 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 --- samples/snippets/create_agent_test.py | 5 +- samples/snippets/page_management.py | 79 ++++++++++++++++ samples/snippets/page_management_test.py | 114 +++++++++++++++++++++++ 3 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 samples/snippets/page_management.py create mode 100644 samples/snippets/page_management_test.py diff --git a/samples/snippets/create_agent_test.py b/samples/snippets/create_agent_test.py index 320a25cb..25b5f80d 100644 --- a/samples/snippets/create_agent_test.py +++ b/samples/snippets/create_agent_test.py @@ -13,8 +13,8 @@ """Test for create_agent""" -from datetime import date import os +import uuid from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient from google.cloud.dialogflowcx_v3.types.agent import DeleteAgentRequest @@ -34,8 +34,7 @@ def delete_agent(name): def test_create_agent(): - today = date.today() - agentName = "tempAgent." + today.strftime("%d.%m.%Y") + agentName = f"fake_agent_{uuid.uuid4()}" response = create_agent(PROJECT_ID, agentName) delete_agent(response.name) diff --git a/samples/snippets/page_management.py b/samples/snippets/page_management.py new file mode 100644 index 00000000..cee6e36c --- /dev/null +++ b/samples/snippets/page_management.py @@ -0,0 +1,79 @@ +# 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 google.cloud.dialogflowcx_v3 import PagesAsyncClient +from google.cloud.dialogflowcx_v3.types.page import ( + CreatePageRequest, + DeletePageRequest, + ListPagesRequest, + Page, +) + + +# [START dialogflow_cx_create_page] +async def create_page(project_id, agent_id, flow_id, location, displayName): + pages_client = PagesAsyncClient() + + page = Page() + page.display_name = displayName + + request = CreatePageRequest() + request.parent = ( + "projects/" + + project_id + + "/locations/" + + location + + "/agents/" + + agent_id + + "/flows/" + + flow_id + ) + request.page = page + + response = await pages_client.create_page(request=request) + return response + + +# [END dialogflow_cx_create_page] + + +# [START dialogflow_cx_list_page] +async def list_page(project_id, agent_id, flow_id, location): + pages_client = PagesAsyncClient() + + request = ListPagesRequest() + request.parent = ( + f"projects/{project_id}/locations/{location}/agents/{agent_id}/flows/{flow_id}" + ) + + request.language_code = "en" + + response = await pages_client.list_pages(request=request) + return response + + +# [END dialogflow_cx_list_page] + + +# [START dialogflow_cx_delete_page] +async def delete_page(project_id, agent_id, flow_id, page_id, location): + pages_client = PagesAsyncClient() + + request = DeletePageRequest() + request.name = f"projects/{project_id}/locations/{location}/agents/{agent_id}/flows/{flow_id}/pages/{page_id}" + + response = await pages_client.delete_page(request=request) + return response + + +# [END dialogflow_cx_delete_page] diff --git a/samples/snippets/page_management_test.py b/samples/snippets/page_management_test.py new file mode 100644 index 00000000..13d9bf80 --- /dev/null +++ b/samples/snippets/page_management_test.py @@ -0,0 +1,114 @@ +# 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. + +import asyncio +from copy import Error +import os +import uuid + +from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient +from google.cloud.dialogflowcx_v3.types.agent import Agent, DeleteAgentRequest + +import pytest + +from page_management import create_page, delete_page, list_page + +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") +pytest.AGENT_ID = None +pytest.PARENT = None +pytest.CREATED_PAGE = None +pytest.PAGE_ID = None + + +def delete_agent(name): + agents_client = AgentsClient() + agent = DeleteAgentRequest(name=name) + agents_client.delete_agent(request=agent) + + +@pytest.fixture +def loop(): + loop = asyncio.new_event_loop() + yield loop + loop.close() + + +@pytest.fixture(scope="module", autouse=True) +def setup_teardown(): + loop = asyncio.new_event_loop() + agentName = "temp_agent_" + str(uuid.uuid4()) + + parent = "projects/" + PROJECT_ID + "/locations/global" + + agents_client = AgentsClient() + + agent = Agent( + display_name=agentName, + default_language_code="en", + time_zone="America/Los_Angeles", + ) + + response = agents_client.create_agent(request={"agent": agent, "parent": parent}) + pytest.PARENT = response.name + + pytest.AGENT_ID = pytest.PARENT.split("/")[5] + print("Created Agent in setUp") + + yield + + delete_agent(pytest.PARENT) + loop.close() + + +def test_create_page(loop: asyncio.AbstractEventLoop): + pytest.CREATED_PAGE = f"fake_page_{uuid.uuid4()}" + actualResponse = loop.run_until_complete( + create_page( + PROJECT_ID, + pytest.AGENT_ID, + "00000000-0000-0000-0000-000000000000", + "global", + pytest.CREATED_PAGE, + ) + ) + + pytest.PAGE_ID = actualResponse.name.split("/")[9] + assert actualResponse.display_name == pytest.CREATED_PAGE + + +def test_list_page(loop: asyncio.AbstractEventLoop): + actualResponse = loop.run_until_complete( + list_page( + PROJECT_ID, + pytest.AGENT_ID, + "00000000-0000-0000-0000-000000000000", + "global", + ) + ) + + assert pytest.PAGE_ID in str(actualResponse) + + +def test_delete_page(loop: asyncio.AbstractEventLoop): + try: + loop.run_until_complete( + delete_page( + PROJECT_ID, + pytest.AGENT_ID, + "00000000-0000-0000-0000-000000000000", + pytest.PAGE_ID, + "global", + ) + ) + except Error: + pytest.fail("Unexpected MyError ..")