Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
docs(samples): added page management samples (#152)
Browse files Browse the repository at this point in the history
* 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 <partheniou@google.com>
  • Loading branch information
galz10 and parthea committed Sep 1, 2021
1 parent 3caac48 commit 41d15f8
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 3 deletions.
5 changes: 2 additions & 3 deletions samples/snippets/create_agent_test.py
Expand Up @@ -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
Expand All @@ -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)

Expand Down
79 changes: 79 additions & 0 deletions 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]
114 changes: 114 additions & 0 deletions 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 ..")

0 comments on commit 41d15f8

Please sign in to comment.