From 568f0bacef96ab8d87883f06b8077ffe6d0b5378 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Tue, 3 Aug 2021 09:36:51 -0700 Subject: [PATCH 01/23] Added Update Intent Snippet and Test --- samples/snippets/update_intent.py | 16 +++++ samples/snippets/update_intent_test.py | 97 ++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 samples/snippets/update_intent.py create mode 100644 samples/snippets/update_intent_test.py diff --git a/samples/snippets/update_intent.py b/samples/snippets/update_intent.py new file mode 100644 index 000000000..870a1e6b4 --- /dev/null +++ b/samples/snippets/update_intent.py @@ -0,0 +1,16 @@ +from google.cloud.dialogflow_v2 import IntentsClient +from google.protobuf import field_mask_pb2 + + +def update_intent(project_id, intent_id, display_name): + intents_client = IntentsClient() + + intent_name = intents_client.intent_path(project_id, intent_id) + intent = intents_client.get_intent(request={"name": intent_name}) + + intent.display_name = display_name + update_mask = field_mask_pb2.FieldMask(paths=["display_name"]) + response = intents_client.update_intent( + intent=intent, update_mask=update_mask, language_code="en" + ) + return response diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py new file mode 100644 index 000000000..2b06274bd --- /dev/null +++ b/samples/snippets/update_intent_test.py @@ -0,0 +1,97 @@ +import os +import unittest +from datetime import date +from google.cloud.dialogflow_v2.services.agents.client import AgentsClient +from google.cloud.dialogflow_v2.services.intents.client import IntentsClient +from google.cloud.dialogflow_v2 import Agent +from google.cloud.dialogflow_v2.types.agent import ( + DeleteAgentRequest, + SearchAgentsRequest, +) + +from update_intent_ES import updateIntent + + +os.environ[ + "GOOGLE_APPLICATION_CREDENTIALS" +] = "/Users/galzahavi/Documents/Dialogflow Snippets/valiant-marker-319718-a597e6688ab0.json" +os.environ["GOOGLE_CLOUD_PROJECT"] = "valiant-marker-319718" + +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") + + +def create_agent(project_id, display_name): + parent = "projects/" + project_id + agents_client = AgentsClient() + agent = Agent( + parent=parent, + display_name=display_name, + default_language_code="en", + time_zone="America/Los_Angeles", + ) + + response = agents_client.set_agent(request={"agent": agent}) + + +def list_agent(self, project_id): + parent = "projects/" + project_id + agents_client = AgentsClient() + agent = SearchAgentsRequest(parent=parent) + response = agents_client.search_agents(agent) + return len(response._response.agents) + + +def delete_agent(self, project_id): + parent = "projects/" + project_id + agents_client = AgentsClient() + agent = DeleteAgentRequest(parent=parent) + agents_client.delete_agent(agent) + + +def list_intent(self, project_id): + intents_client = IntentsClient() + + parent = AgentsClient.agent_path(project_id) + + intents = intents_client.list_intents(request={"parent": parent}) + + for intent in intents: + return intent.name.split("/")[4] + + +class fieldmaskTest(unittest.TestCase): + def setUp(self): + parent_id = PROJECT_ID or "" + if self.list_agent(parent_id) > 0: + self.delete_agent(parent_id) + print("Deleted in setUp") + today = date.today() + agentName = "tempAgent." + today.strftime("%d.%m.%Y") + create_agent(PROJECT_ID or "", agentName) + print("Created Agent in setUp") + self.intent_id = self.list_intent(project_id=PROJECT_ID or "") + print("Created Intent in setUp") + else: + today = date.today() + agentName = "tempAgent." + today.strftime("%d.%m.%Y") + create_agent(PROJECT_ID or "", agentName) + print("Created Agent in setUp") + self.intent_id = self.list_intent(project_id=PROJECT_ID or "") + print("Created Intent in setUp") + + def test_update_intent(self): + actualResponse = updateIntent( + PROJECT_ID or "", self.intent_id, "Updated Intent" + ) + expectedResponse = "Updated Intent" + self.assertEqual(actualResponse.display_name, expectedResponse) + + def tearDown(self): + parent_id = PROJECT_ID or "" + if self.list_agent(parent_id) > 0: + self.delete_agent(parent_id) + print("Deleted in tearDown") + + +if __name__ == "__main__": + unittest.main() From 2b278e7753f3ff004779fdc0a9cc84574643191a Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Tue, 3 Aug 2021 09:39:03 -0700 Subject: [PATCH 02/23] Deleted Setting Env Vars --- samples/snippets/update_intent_test.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index 2b06274bd..5973c4bc8 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -9,17 +9,10 @@ SearchAgentsRequest, ) -from update_intent_ES import updateIntent - - -os.environ[ - "GOOGLE_APPLICATION_CREDENTIALS" -] = "/Users/galzahavi/Documents/Dialogflow Snippets/valiant-marker-319718-a597e6688ab0.json" -os.environ["GOOGLE_CLOUD_PROJECT"] = "valiant-marker-319718" +from update_intent import updateIntent PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - def create_agent(project_id, display_name): parent = "projects/" + project_id agents_client = AgentsClient() From 4952c45bbbae0e4a326f45d6d00e407ae2b0441f Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Tue, 3 Aug 2021 10:00:02 -0700 Subject: [PATCH 03/23] Fixed Lint Issues --- samples/snippets/update_intent_test.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index 5973c4bc8..0a393a6bf 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -1,18 +1,20 @@ import os -import unittest from datetime import date +import unittest + from google.cloud.dialogflow_v2.services.agents.client import AgentsClient -from google.cloud.dialogflow_v2.services.intents.client import IntentsClient from google.cloud.dialogflow_v2 import Agent +from google.cloud.dialogflow_v2.services.intents.client import IntentsClient from google.cloud.dialogflow_v2.types.agent import ( DeleteAgentRequest, SearchAgentsRequest, ) -from update_intent import updateIntent +from update_intent import update_intent PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") + def create_agent(project_id, display_name): parent = "projects/" + project_id agents_client = AgentsClient() @@ -23,7 +25,7 @@ def create_agent(project_id, display_name): time_zone="America/Los_Angeles", ) - response = agents_client.set_agent(request={"agent": agent}) + agents_client.set_agent(request={"agent": agent}) def list_agent(self, project_id): @@ -73,7 +75,7 @@ def setUp(self): print("Created Intent in setUp") def test_update_intent(self): - actualResponse = updateIntent( + actualResponse = update_intent( PROJECT_ID or "", self.intent_id, "Updated Intent" ) expectedResponse = "Updated Intent" From 78521100666e4ed087cc0df1142bca0915f3e2e5 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Tue, 3 Aug 2021 10:08:33 -0700 Subject: [PATCH 04/23] Fixed Lint and Build Issue --- samples/snippets/update_intent_test.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index 0a393a6bf..e8e6edf5a 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -1,9 +1,9 @@ -import os from datetime import date +import os import unittest -from google.cloud.dialogflow_v2.services.agents.client import AgentsClient from google.cloud.dialogflow_v2 import Agent +from google.cloud.dialogflow_v2.services.agents.client import AgentsClient from google.cloud.dialogflow_v2.services.intents.client import IntentsClient from google.cloud.dialogflow_v2.types.agent import ( DeleteAgentRequest, @@ -57,21 +57,21 @@ def list_intent(self, project_id): class fieldmaskTest(unittest.TestCase): def setUp(self): parent_id = PROJECT_ID or "" - if self.list_agent(parent_id) > 0: - self.delete_agent(parent_id) + if list_agent(parent_id) > 0: + delete_agent(parent_id) print("Deleted in setUp") today = date.today() agentName = "tempAgent." + today.strftime("%d.%m.%Y") create_agent(PROJECT_ID or "", agentName) print("Created Agent in setUp") - self.intent_id = self.list_intent(project_id=PROJECT_ID or "") + self.intent_id = list_intent(project_id=PROJECT_ID or "") print("Created Intent in setUp") else: today = date.today() agentName = "tempAgent." + today.strftime("%d.%m.%Y") create_agent(PROJECT_ID or "", agentName) print("Created Agent in setUp") - self.intent_id = self.list_intent(project_id=PROJECT_ID or "") + self.intent_id = list_intent(project_id=PROJECT_ID or "") print("Created Intent in setUp") def test_update_intent(self): @@ -83,8 +83,8 @@ def test_update_intent(self): def tearDown(self): parent_id = PROJECT_ID or "" - if self.list_agent(parent_id) > 0: - self.delete_agent(parent_id) + if list_agent(parent_id) > 0: + delete_agent(parent_id) print("Deleted in tearDown") From 8d4999c2e4452cca83cce20c599d16ecf3a549b0 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Tue, 3 Aug 2021 10:15:56 -0700 Subject: [PATCH 05/23] Fixed Build Issue --- samples/snippets/update_intent_test.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index e8e6edf5a..2ad5e7067 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -28,7 +28,7 @@ def create_agent(project_id, display_name): agents_client.set_agent(request={"agent": agent}) -def list_agent(self, project_id): +def list_agent(project_id): parent = "projects/" + project_id agents_client = AgentsClient() agent = SearchAgentsRequest(parent=parent) @@ -36,14 +36,14 @@ def list_agent(self, project_id): return len(response._response.agents) -def delete_agent(self, project_id): +def delete_agent(project_id): parent = "projects/" + project_id agents_client = AgentsClient() agent = DeleteAgentRequest(parent=parent) agents_client.delete_agent(agent) -def list_intent(self, project_id): +def list_intent(project_id): intents_client = IntentsClient() parent = AgentsClient.agent_path(project_id) @@ -56,9 +56,8 @@ def list_intent(self, project_id): class fieldmaskTest(unittest.TestCase): def setUp(self): - parent_id = PROJECT_ID or "" - if list_agent(parent_id) > 0: - delete_agent(parent_id) + if list_agent(PROJECT_ID) > 0: + delete_agent(PROJECT_ID) print("Deleted in setUp") today = date.today() agentName = "tempAgent." + today.strftime("%d.%m.%Y") @@ -82,9 +81,8 @@ def test_update_intent(self): self.assertEqual(actualResponse.display_name, expectedResponse) def tearDown(self): - parent_id = PROJECT_ID or "" - if list_agent(parent_id) > 0: - delete_agent(parent_id) + if list_agent(PROJECT_ID) > 0: + delete_agent(PROJECT_ID) print("Deleted in tearDown") From 12590026801e168d22836005d68d35d384e3f3ac Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Tue, 3 Aug 2021 12:12:25 -0700 Subject: [PATCH 06/23] Changed tests to pytests --- samples/snippets/update_intent_test.py | 61 ++++++++++++-------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index 2ad5e7067..abd556008 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -1,6 +1,6 @@ from datetime import date import os -import unittest +import pytest from google.cloud.dialogflow_v2 import Agent from google.cloud.dialogflow_v2.services.agents.client import AgentsClient @@ -13,7 +13,7 @@ from update_intent import update_intent PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") - +pytest.INTENT_ID = None def create_agent(project_id, display_name): parent = "projects/" + project_id @@ -53,38 +53,35 @@ def list_intent(project_id): for intent in intents: return intent.name.split("/")[4] - -class fieldmaskTest(unittest.TestCase): - def setUp(self): - if list_agent(PROJECT_ID) > 0: - delete_agent(PROJECT_ID) - print("Deleted in setUp") - today = date.today() - agentName = "tempAgent." + today.strftime("%d.%m.%Y") - create_agent(PROJECT_ID or "", agentName) - print("Created Agent in setUp") - self.intent_id = list_intent(project_id=PROJECT_ID or "") - print("Created Intent in setUp") - else: - today = date.today() - agentName = "tempAgent." + today.strftime("%d.%m.%Y") - create_agent(PROJECT_ID or "", agentName) - print("Created Agent in setUp") - self.intent_id = list_intent(project_id=PROJECT_ID or "") - print("Created Intent in setUp") - - def test_update_intent(self): +@pytest.fixture(scope="function", autouse=True) +def setup_teardown(): + if list_agent(PROJECT_ID) > 0: + delete_agent(PROJECT_ID) + print("Deleted in setUp") + today = date.today() + agentName = "tempAgent." + today.strftime("%d.%m.%Y") + create_agent(PROJECT_ID, agentName) + print("Created Agent in setUp") + pytest.INTENT_ID = list_intent(project_id=PROJECT_ID or "") + print("Created Intent in setUp") + else: + today = date.today() + agentName = "tempAgent." + today.strftime("%d.%m.%Y") + create_agent(PROJECT_ID, agentName) + print("Created Agent in setUp") + pytest.INTENT_ID = list_intent(project_id=PROJECT_ID or "") + print("Created Intent in setUp") + + yield + + delete_agent(PROJECT_ID) + print("Deleted in tearDown") + +def test_update_intent(): actualResponse = update_intent( - PROJECT_ID or "", self.intent_id, "Updated Intent" + PROJECT_ID or "", pytest.INTENT_ID, "Updated Intent" ) expectedResponse = "Updated Intent" - self.assertEqual(actualResponse.display_name, expectedResponse) - - def tearDown(self): - if list_agent(PROJECT_ID) > 0: - delete_agent(PROJECT_ID) - print("Deleted in tearDown") + assert actualResponse.display_name == expectedResponse -if __name__ == "__main__": - unittest.main() From 755a7d16ae4a7fa77f066c1908ba27f2479ec53e Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Wed, 4 Aug 2021 09:23:32 -0700 Subject: [PATCH 07/23] Removed delete and create agent from test --- samples/snippets/update_intent_test.py | 67 +++----------------------- 1 file changed, 7 insertions(+), 60 deletions(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index abd556008..8fca445b9 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -2,46 +2,14 @@ import os import pytest -from google.cloud.dialogflow_v2 import Agent from google.cloud.dialogflow_v2.services.agents.client import AgentsClient from google.cloud.dialogflow_v2.services.intents.client import IntentsClient -from google.cloud.dialogflow_v2.types.agent import ( - DeleteAgentRequest, - SearchAgentsRequest, -) from update_intent import update_intent PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") pytest.INTENT_ID = None -def create_agent(project_id, display_name): - parent = "projects/" + project_id - agents_client = AgentsClient() - agent = Agent( - parent=parent, - display_name=display_name, - default_language_code="en", - time_zone="America/Los_Angeles", - ) - - agents_client.set_agent(request={"agent": agent}) - - -def list_agent(project_id): - parent = "projects/" + project_id - agents_client = AgentsClient() - agent = SearchAgentsRequest(parent=parent) - response = agents_client.search_agents(agent) - return len(response._response.agents) - - -def delete_agent(project_id): - parent = "projects/" + project_id - agents_client = AgentsClient() - agent = DeleteAgentRequest(parent=parent) - agents_client.delete_agent(agent) - def list_intent(project_id): intents_client = IntentsClient() @@ -53,35 +21,14 @@ def list_intent(project_id): for intent in intents: return intent.name.split("/")[4] + @pytest.fixture(scope="function", autouse=True) def setup_teardown(): - if list_agent(PROJECT_ID) > 0: - delete_agent(PROJECT_ID) - print("Deleted in setUp") - today = date.today() - agentName = "tempAgent." + today.strftime("%d.%m.%Y") - create_agent(PROJECT_ID, agentName) - print("Created Agent in setUp") - pytest.INTENT_ID = list_intent(project_id=PROJECT_ID or "") - print("Created Intent in setUp") - else: - today = date.today() - agentName = "tempAgent." + today.strftime("%d.%m.%Y") - create_agent(PROJECT_ID, agentName) - print("Created Agent in setUp") - pytest.INTENT_ID = list_intent(project_id=PROJECT_ID or "") - print("Created Intent in setUp") - - yield - - delete_agent(PROJECT_ID) - print("Deleted in tearDown") - -def test_update_intent(): - actualResponse = update_intent( - PROJECT_ID or "", pytest.INTENT_ID, "Updated Intent" - ) - expectedResponse = "Updated Intent" - assert actualResponse.display_name == expectedResponse + pytest.INTENT_ID = list_intent(project_id=PROJECT_ID) + print("Created Intent in setUp") +def test_update_intent(): + actualResponse = update_intent(PROJECT_ID, pytest.INTENT_ID, "Updated Intent") + expectedResponse = "Updated Intent" + assert actualResponse.display_name == expectedResponse From b2395e79668e5ca50e35bff40e72ef94de226743 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Wed, 4 Aug 2021 09:27:48 -0700 Subject: [PATCH 08/23] Fixed Import Order --- samples/snippets/update_intent_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index 8fca445b9..d88c9dff0 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -1,10 +1,11 @@ from datetime import date import os -import pytest from google.cloud.dialogflow_v2.services.agents.client import AgentsClient from google.cloud.dialogflow_v2.services.intents.client import IntentsClient +import pytest + from update_intent import update_intent PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") From cb6128ecb2bdeaae8fc44317be3d71e765f540b6 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Wed, 4 Aug 2021 09:32:35 -0700 Subject: [PATCH 09/23] Deleted unused import --- samples/snippets/update_intent_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index d88c9dff0..4d41442e8 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -1,4 +1,3 @@ -from datetime import date import os from google.cloud.dialogflow_v2.services.agents.client import AgentsClient From af3377e64ac91c24a58b1a8ea4a1358536794baf Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Wed, 4 Aug 2021 09:47:38 -0700 Subject: [PATCH 10/23] Removed Language from update_intent Snippet --- samples/snippets/update_intent.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/samples/snippets/update_intent.py b/samples/snippets/update_intent.py index 870a1e6b4..3e8d46109 100644 --- a/samples/snippets/update_intent.py +++ b/samples/snippets/update_intent.py @@ -10,7 +10,5 @@ def update_intent(project_id, intent_id, display_name): intent.display_name = display_name update_mask = field_mask_pb2.FieldMask(paths=["display_name"]) - response = intents_client.update_intent( - intent=intent, update_mask=update_mask, language_code="en" - ) + response = intents_client.update_intent(intent=intent, update_mask=update_mask) return response From a77471874601f38e895f7df10dc4ef6ca5edd228 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Wed, 4 Aug 2021 11:38:43 -0700 Subject: [PATCH 11/23] Added copyright --- samples/snippets/update_intent.py | 14 ++++++++++++++ samples/snippets/update_intent_test.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/samples/snippets/update_intent.py b/samples/snippets/update_intent.py index 3e8d46109..906ed84e4 100644 --- a/samples/snippets/update_intent.py +++ b/samples/snippets/update_intent.py @@ -1,3 +1,17 @@ +# 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.dialogflow_v2 import IntentsClient from google.protobuf import field_mask_pb2 diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index 4d41442e8..a8763a8be 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -1,3 +1,17 @@ +# 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 os from google.cloud.dialogflow_v2.services.agents.client import AgentsClient From c922a4c0fe6c17fa063af635dbd4e8d43aa9794d Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Thu, 5 Aug 2021 13:15:28 -0700 Subject: [PATCH 12/23] Changed intent name to random name --- samples/snippets/update_intent_test.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index a8763a8be..4fe62f391 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -13,6 +13,7 @@ # limitations under the License. import os +import uuid from google.cloud.dialogflow_v2.services.agents.client import AgentsClient from google.cloud.dialogflow_v2.services.intents.client import IntentsClient @@ -43,6 +44,9 @@ def setup_teardown(): def test_update_intent(): - actualResponse = update_intent(PROJECT_ID, pytest.INTENT_ID, "Updated Intent") - expectedResponse = "Updated Intent" + + fake_intent = "fake_intent_{}".format(uuid.uuid4()) + + actualResponse = update_intent(PROJECT_ID, pytest.INTENT_ID, fake_intent) + expectedResponse = fake_intent assert actualResponse.display_name == expectedResponse From 100af60e8ef012127d5cbbf92273c296ced44a96 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Thu, 5 Aug 2021 13:36:42 -0700 Subject: [PATCH 13/23] delete intent after testing --- samples/snippets/update_intent_test.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index 4fe62f391..1fa2ccd0d 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -49,4 +49,9 @@ def test_update_intent(): actualResponse = update_intent(PROJECT_ID, pytest.INTENT_ID, fake_intent) expectedResponse = fake_intent + + intents_client = IntentsClient() + + intents_client.delete_intent(name=actualResponse.name) + assert actualResponse.display_name == expectedResponse From 7ba7ed4b71ffc136f32a8ee95cb24dca339e2da3 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Thu, 5 Aug 2021 13:54:23 -0700 Subject: [PATCH 14/23] fix test --- samples/snippets/update_intent_test.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index 1fa2ccd0d..bcb53abd3 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -34,7 +34,8 @@ def list_intent(project_id): intents = intents_client.list_intents(request={"parent": parent}) for intent in intents: - return intent.name.split("/")[4] + if intent.display_name.contains("fake_intent"): + return intent.name.split("/")[4] @pytest.fixture(scope="function", autouse=True) From 48c9f1368790ef147eb11a560acdc9e863ad6641 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Thu, 5 Aug 2021 14:53:09 -0700 Subject: [PATCH 15/23] remove contains --- samples/snippets/update_intent_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index bcb53abd3..aa7a4ba0f 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -34,7 +34,7 @@ def list_intent(project_id): intents = intents_client.list_intents(request={"parent": parent}) for intent in intents: - if intent.display_name.contains("fake_intent"): + if "fake_intent" in intent.display_name: return intent.name.split("/")[4] From 7ad7509f56aa4b38f3ac4c06058d0f2762b61d31 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Thu, 5 Aug 2021 15:02:00 -0700 Subject: [PATCH 16/23] Added Create Intent --- samples/snippets/update_intent_test.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index aa7a4ba0f..d4dfeff5c 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -17,6 +17,7 @@ from google.cloud.dialogflow_v2.services.agents.client import AgentsClient from google.cloud.dialogflow_v2.services.intents.client import IntentsClient +from google.cloud.dialogflow_v2.types.intent import Intent import pytest @@ -26,21 +27,23 @@ pytest.INTENT_ID = None -def list_intent(project_id): +def create_intent(project_id): intents_client = IntentsClient() parent = AgentsClient.agent_path(project_id) - intents = intents_client.list_intents(request={"parent": parent}) + intent = Intent() - for intent in intents: - if "fake_intent" in intent.display_name: - return intent.name.split("/")[4] + intent.display_name = "fake_intent" + + intents = intents_client.create_intent(request={"parent": parent,"intent":intent}) + + return intents.name.split("/")[4] @pytest.fixture(scope="function", autouse=True) def setup_teardown(): - pytest.INTENT_ID = list_intent(project_id=PROJECT_ID) + pytest.INTENT_ID = create_intent(project_id=PROJECT_ID) print("Created Intent in setUp") From 75958c2b334f4009ed4c881d40a712a155df8584 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Thu, 5 Aug 2021 15:19:04 -0700 Subject: [PATCH 17/23] fix lint --- samples/snippets/update_intent_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/update_intent_test.py b/samples/snippets/update_intent_test.py index d4dfeff5c..1feb298f4 100644 --- a/samples/snippets/update_intent_test.py +++ b/samples/snippets/update_intent_test.py @@ -36,7 +36,7 @@ def create_intent(project_id): intent.display_name = "fake_intent" - intents = intents_client.create_intent(request={"parent": parent,"intent":intent}) + intents = intents_client.create_intent(request={"parent": parent, "intent": intent}) return intents.name.split("/")[4] From c776f7fb54d014ebd56d239b93d87eab992fe4c9 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Wed, 18 Aug 2021 20:03:05 -0700 Subject: [PATCH 18/23] docs(samples): add set agent code sample --- samples/snippets/set_agent.py | 37 ++++++++++++++++++++++++++++++ samples/snippets/set_agent_test.py | 23 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 samples/snippets/set_agent.py create mode 100644 samples/snippets/set_agent_test.py diff --git a/samples/snippets/set_agent.py b/samples/snippets/set_agent.py new file mode 100644 index 000000000..6d19db917 --- /dev/null +++ b/samples/snippets/set_agent.py @@ -0,0 +1,37 @@ +# 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 +# +# https://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. + +# [START dialogflow_set_agent_sample] + +from google.cloud.dialogflow_v2 import AgentsClient +from google.cloud.dialogflow_v2 import Agent + +def set_agent(project_id, display_name): + + parent = "projects/" + project_id + + agents_client = AgentsClient() + + agent = Agent( + parent=parent, + display_name=display_name, + default_language_code="en", + time_zone="America/Los_Angeles", + ) + + response = agents_client.set_agent(request={"agent": agent}) + + return response + +# [END dialogflow_set_agent_sample] diff --git a/samples/snippets/set_agent_test.py b/samples/snippets/set_agent_test.py new file mode 100644 index 000000000..3592f661f --- /dev/null +++ b/samples/snippets/set_agent_test.py @@ -0,0 +1,23 @@ +# 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 os + +PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") + +# We cannot test set_agent because Dialogflow ES can only have one agent +# and if we create a agent it will delete the exisitng testing agent and +# would cause all tests to fail +def test_set_agent(): + assert True From 8d87b9b643e35327193b8be5ca443ebd45c18fa3 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Wed, 18 Aug 2021 20:23:32 -0700 Subject: [PATCH 19/23] lint fix --- samples/snippets/set_agent.py | 3 ++- samples/snippets/set_agent_test.py | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/samples/snippets/set_agent.py b/samples/snippets/set_agent.py index 6d19db917..74af37b70 100644 --- a/samples/snippets/set_agent.py +++ b/samples/snippets/set_agent.py @@ -14,8 +14,9 @@ # [START dialogflow_set_agent_sample] -from google.cloud.dialogflow_v2 import AgentsClient from google.cloud.dialogflow_v2 import Agent +from google.cloud.dialogflow_v2 import AgentsClient + def set_agent(project_id, display_name): diff --git a/samples/snippets/set_agent_test.py b/samples/snippets/set_agent_test.py index 3592f661f..7307b263b 100644 --- a/samples/snippets/set_agent_test.py +++ b/samples/snippets/set_agent_test.py @@ -16,8 +16,9 @@ PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -# We cannot test set_agent because Dialogflow ES can only have one agent -# and if we create a agent it will delete the exisitng testing agent and + +# We cannot test set_agent because Dialogflow ES can only have one agent +# and if we create a agent it will delete the exisitng testing agent and # would cause all tests to fail def test_set_agent(): - assert True + assert True From 0556d84f57fa7c3d682fb5c23d7281ded007dd46 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Tue, 31 Aug 2021 14:11:59 -0700 Subject: [PATCH 20/23] Resolved comments --- samples/snippets/set_agent.py | 4 ++-- samples/snippets/set_agent_test.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/samples/snippets/set_agent.py b/samples/snippets/set_agent.py index 74af37b70..299f2a5f1 100644 --- a/samples/snippets/set_agent.py +++ b/samples/snippets/set_agent.py @@ -20,10 +20,10 @@ def set_agent(project_id, display_name): - parent = "projects/" + project_id - agents_client = AgentsClient() + parent = agents_client.common_project_path(project_id) + agent = Agent( parent=parent, display_name=display_name, diff --git a/samples/snippets/set_agent_test.py b/samples/snippets/set_agent_test.py index 7307b263b..7c0623f1f 100644 --- a/samples/snippets/set_agent_test.py +++ b/samples/snippets/set_agent_test.py @@ -14,11 +14,15 @@ import os +from google.api_core.exceptions import InvalidArgument + +import pytest + +import set_agent + PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -# We cannot test set_agent because Dialogflow ES can only have one agent -# and if we create a agent it will delete the exisitng testing agent and -# would cause all tests to fail def test_set_agent(): - assert True + with pytest.raises(InvalidArgument): + set_agent(PROJECT_ID,"") From 5ba68cce35f4692c0fd09dee16df33dc937135a6 Mon Sep 17 00:00:00 2001 From: Gal Zahavi Date: Tue, 31 Aug 2021 14:32:50 -0700 Subject: [PATCH 21/23] test and lint changes --- samples/snippets/set_agent.py | 1 + samples/snippets/set_agent_test.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/samples/snippets/set_agent.py b/samples/snippets/set_agent.py index 299f2a5f1..f1b993d15 100644 --- a/samples/snippets/set_agent.py +++ b/samples/snippets/set_agent.py @@ -35,4 +35,5 @@ def set_agent(project_id, display_name): return response + # [END dialogflow_set_agent_sample] diff --git a/samples/snippets/set_agent_test.py b/samples/snippets/set_agent_test.py index 7c0623f1f..691693dff 100644 --- a/samples/snippets/set_agent_test.py +++ b/samples/snippets/set_agent_test.py @@ -18,11 +18,11 @@ import pytest -import set_agent +from set_agent import set_agent PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") def test_set_agent(): with pytest.raises(InvalidArgument): - set_agent(PROJECT_ID,"") + set_agent(PROJECT_ID, "") From 245e62fd8fb0dca546d90b482a3b53a4c9b01514 Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Wed, 8 Sep 2021 08:07:14 -0700 Subject: [PATCH 22/23] Added Comments --- samples/snippets/set_agent_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/samples/snippets/set_agent_test.py b/samples/snippets/set_agent_test.py index 691693dff..88d44f4af 100644 --- a/samples/snippets/set_agent_test.py +++ b/samples/snippets/set_agent_test.py @@ -23,6 +23,9 @@ PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") +# We cannot test setAgent because Dialogflow ES can only have one agent +# and if we create a agent it will delete the exisitng testing agent and +# would cause all tests to fail def test_set_agent(): with pytest.raises(InvalidArgument): set_agent(PROJECT_ID, "") From a98adb31951f6a8818af867731bb523ee56d27ce Mon Sep 17 00:00:00 2001 From: Gal Zahavi <38544478+galz10@users.noreply.github.com> Date: Wed, 8 Sep 2021 08:26:10 -0700 Subject: [PATCH 23/23] lint fix --- samples/snippets/set_agent_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/set_agent_test.py b/samples/snippets/set_agent_test.py index 88d44f4af..98b0eeb7f 100644 --- a/samples/snippets/set_agent_test.py +++ b/samples/snippets/set_agent_test.py @@ -23,8 +23,8 @@ PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") -# We cannot test setAgent because Dialogflow ES can only have one agent -# and if we create a agent it will delete the exisitng testing agent and +# We cannot test setAgent because Dialogflow ES can only have one agent +# and if we create a agent it will delete the exisitng testing agent and # would cause all tests to fail def test_set_agent(): with pytest.raises(InvalidArgument):