|
| 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