diff --git a/dialogflow/cloud-client/create_entity_test.py b/dialogflow/cloud-client/create_entity_test.py new file mode 100644 index 000000000000..a33ed97d717d --- /dev/null +++ b/dialogflow/cloud-client/create_entity_test.py @@ -0,0 +1,70 @@ +# Copyright 2020 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 __future__ import absolute_import + +import datetime +import os +import pytest + +import dialogflow_v2 as dialogflow + +import entity_management + +PROJECT_ID = os.getenv("GCLOUD_PROJECT") +DISPLAY_NAME = "entity_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +ENTITY_VALUE_1 = "test_entity_value_1" +ENTITY_VALUE_2 = "test_entity_value_2" +SYNONYMS = ["fake_synonym_for_testing_1", "fake_synonym_for_testing_2"] + +pytest.ENTITY_TYPE_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def setup_teardown(): + # Create an entity type to use with create entity + entity_types_client = dialogflow.EntityTypesClient() + parent = entity_types_client.project_agent_path(PROJECT_ID) + entity_type = dialogflow.types.EntityType( + display_name=DISPLAY_NAME, + kind=dialogflow.enums.EntityType.Kind.KIND_MAP, + ) + + response = entity_types_client.create_entity_type(parent, entity_type) + pytest.ENTITY_TYPE_ID = response.name.split("agent/entityTypes/")[1] + + yield + # Delete the created entity type and its entities + assert pytest.ENTITY_TYPE_ID is not None + entity_type_path = entity_types_client.entity_type_path( + PROJECT_ID, pytest.ENTITY_TYPE_ID + ) + entity_types_client.delete_entity_type(entity_type_path) + + +def test_create_entity(capsys): + entity_management.create_entity( + PROJECT_ID, pytest.ENTITY_TYPE_ID, ENTITY_VALUE_1, [] + ) + entity_management.create_entity( + PROJECT_ID, pytest.ENTITY_TYPE_ID, ENTITY_VALUE_2, SYNONYMS + ) + + entity_management.list_entities(PROJECT_ID, pytest.ENTITY_TYPE_ID) + + out, _ = capsys.readouterr() + assert ENTITY_VALUE_1 in out + assert ENTITY_VALUE_2 in out + for synonym in SYNONYMS: + assert synonym in out diff --git a/dialogflow/cloud-client/create_entity_type_test.py b/dialogflow/cloud-client/create_entity_type_test.py new file mode 100644 index 000000000000..5465c1585689 --- /dev/null +++ b/dialogflow/cloud-client/create_entity_type_test.py @@ -0,0 +1,54 @@ +# Copyright 2020 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 __future__ import absolute_import + +import datetime +import os +import pytest + +import dialogflow_v2 as dialogflow + +import entity_type_management + +PROJECT_ID = os.getenv("GCLOUD_PROJECT") +DISPLAY_NAME = "entity_type_" + datetime.datetime.now().strftime( + "%Y%m%d%H%M%S" +) +pytest.ENTITY_TYPE_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def teardown(): + yield + + # Delete the created entity type + entity_types_client = dialogflow.EntityTypesClient() + assert pytest.ENTITY_TYPE_ID is not None + entity_type_path = entity_types_client.entity_type_path( + PROJECT_ID, pytest.ENTITY_TYPE_ID + ) + entity_types_client.delete_entity_type(entity_type_path) + + +def test_create_entity_type(capsys): + entity_type_management.create_entity_type( + PROJECT_ID, DISPLAY_NAME, "KIND_MAP" + ) + out, _ = capsys.readouterr() + + assert 'display_name: "{}"'.format(DISPLAY_NAME) in out + + # Save the entity id so that it can be deleted + pytest.ENTITY_TYPE_ID = out.split("agent/entityTypes/")[1].split('"\n')[0] diff --git a/dialogflow/cloud-client/delete_entity_test.py b/dialogflow/cloud-client/delete_entity_test.py new file mode 100644 index 000000000000..14d5d9afdcea --- /dev/null +++ b/dialogflow/cloud-client/delete_entity_test.py @@ -0,0 +1,71 @@ +# Copyright 2020 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 __future__ import absolute_import + +import datetime +import os +import pytest + +import dialogflow_v2 as dialogflow + +import entity_management + +PROJECT_ID = os.getenv("GCLOUD_PROJECT") +DISPLAY_NAME = "entity_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +ENTITY_VALUE_1 = "test_delete_entity_value" + +pytest.ENTITY_TYPE_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def setup_teardown(): + # Create an entity type + entity_types_client = dialogflow.EntityTypesClient() + parent = entity_types_client.project_agent_path(PROJECT_ID) + entity_type = dialogflow.types.EntityType( + display_name=DISPLAY_NAME, + kind=dialogflow.enums.EntityType.Kind.KIND_MAP, + ) + + response = entity_types_client.create_entity_type(parent, entity_type) + pytest.ENTITY_TYPE_ID = response.name.split("agent/entityTypes/")[1] + + # Create an entity inside the entity type + entity_type_path = entity_types_client.entity_type_path( + PROJECT_ID, pytest.ENTITY_TYPE_ID + ) + entity = dialogflow.types.EntityType.Entity( + value=ENTITY_VALUE_1, synonyms=[ENTITY_VALUE_1] + ) + response = entity_types_client.batch_create_entities( + entity_type_path, [entity] + ) + response.result() + + yield + + # Delete the created entity type and its entities + entity_types_client.delete_entity_type(entity_type_path) + + +def test_delete_entity(capsys): + entity_management.delete_entity( + PROJECT_ID, pytest.ENTITY_TYPE_ID, ENTITY_VALUE_1 + ) + + entity_management.list_entities(PROJECT_ID, pytest.ENTITY_TYPE_ID) + + out, _ = capsys.readouterr() + assert ENTITY_VALUE_1 not in out diff --git a/dialogflow/cloud-client/delete_entity_type_test.py b/dialogflow/cloud-client/delete_entity_type_test.py new file mode 100644 index 000000000000..3a62976e0239 --- /dev/null +++ b/dialogflow/cloud-client/delete_entity_type_test.py @@ -0,0 +1,53 @@ +# Copyright 2020 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 __future__ import absolute_import + +import datetime +import os +import pytest + +import dialogflow_v2 as dialogflow + +import entity_type_management + +PROJECT_ID = os.getenv("GCLOUD_PROJECT") +DISPLAY_NAME = "entity_type_" + datetime.datetime.now().strftime( + "%Y%m%d%H%M%S" +) +pytest.ENTITY_TYPE_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def setup(): + # Create an entity type for deletion + entity_types_client = dialogflow.EntityTypesClient() + parent = entity_types_client.project_agent_path(PROJECT_ID) + entity_type = dialogflow.types.EntityType( + display_name=DISPLAY_NAME, + kind=dialogflow.enums.EntityType.Kind.KIND_MAP, + ) + + response = entity_types_client.create_entity_type(parent, entity_type) + pytest.ENTITY_TYPE_ID = response.name.split("agent/entityTypes/")[1] + + +def test_delete_entity_type(capsys): + entity_type_management.delete_entity_type( + PROJECT_ID, pytest.ENTITY_TYPE_ID + ) + + entity_type_management.list_entity_types(PROJECT_ID) + out, _ = capsys.readouterr() + assert DISPLAY_NAME not in out diff --git a/dialogflow/cloud-client/entity_management_test.py b/dialogflow/cloud-client/entity_management_test.py deleted file mode 100644 index af75bc5bd76a..000000000000 --- a/dialogflow/cloud-client/entity_management_test.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright 2017 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 __future__ import absolute_import - -import datetime -import os - -import entity_management -import entity_type_management - -PROJECT_ID = os.getenv('GCLOUD_PROJECT') -ENTITY_TYPE_DISPLAY_NAME = 'test_' \ - + datetime.datetime.now().strftime("%Y%m%d%H%M%S") -ENTITY_VALUE_1 = 'fake_entity_for_testing_1' -ENTITY_VALUE_2 = 'fake_entity_for_testing_2' -SYNONYMS = ['fake_synonym_for_testing_1', 'fake_synonym_for_testing_2'] - - -def test_create_entity_type(capsys): - entity_type_ids = entity_type_management._get_entity_type_ids( - PROJECT_ID, ENTITY_TYPE_DISPLAY_NAME) - - assert len(entity_type_ids) == 0 - - entity_type_management.create_entity_type( - PROJECT_ID, ENTITY_TYPE_DISPLAY_NAME, 'KIND_MAP') - out, _ = capsys.readouterr() - - assert 'display_name: "{}"'.format(ENTITY_TYPE_DISPLAY_NAME) in out - - entity_type_ids = entity_type_management._get_entity_type_ids( - PROJECT_ID, ENTITY_TYPE_DISPLAY_NAME) - - assert len(entity_type_ids) == 1 - - -def test_create_entity(capsys): - entity_type_id = entity_type_management._get_entity_type_ids( - PROJECT_ID, ENTITY_TYPE_DISPLAY_NAME)[0] - - entity_management.create_entity( - PROJECT_ID, entity_type_id, ENTITY_VALUE_1, []) - entity_management.create_entity( - PROJECT_ID, entity_type_id, ENTITY_VALUE_2, SYNONYMS) - - entity_management.list_entities(PROJECT_ID, entity_type_id) - out, _ = capsys.readouterr() - - assert 'Entity value: {}'.format(ENTITY_VALUE_1) in out - assert 'Entity value: {}'.format(ENTITY_VALUE_2) in out - for synonym in SYNONYMS: - assert synonym in out - - -def test_delete_entity(capsys): - entity_type_id = entity_type_management._get_entity_type_ids( - PROJECT_ID, ENTITY_TYPE_DISPLAY_NAME)[0] - - entity_management.delete_entity( - PROJECT_ID, entity_type_id, ENTITY_VALUE_1) - entity_management.delete_entity( - PROJECT_ID, entity_type_id, ENTITY_VALUE_2) - - entity_management.list_entities(PROJECT_ID, entity_type_id) - out, _ = capsys.readouterr() - - assert out == '' - - -def test_delete_entity_type(capsys): - entity_type_ids = entity_type_management._get_entity_type_ids( - PROJECT_ID, ENTITY_TYPE_DISPLAY_NAME) - - for entity_type_id in entity_type_ids: - entity_type_management.delete_entity_type(PROJECT_ID, entity_type_id) - - entity_type_ids = entity_type_management._get_entity_type_ids( - PROJECT_ID, ENTITY_TYPE_DISPLAY_NAME) - - assert len(entity_type_ids) == 0 diff --git a/dialogflow/cloud-client/list_entities_test.py b/dialogflow/cloud-client/list_entities_test.py new file mode 100644 index 000000000000..5f462eab5fd1 --- /dev/null +++ b/dialogflow/cloud-client/list_entities_test.py @@ -0,0 +1,66 @@ +# Copyright 2020 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 __future__ import absolute_import + +import datetime +import os +import pytest + +import dialogflow_v2 as dialogflow + +import entity_management + +PROJECT_ID = os.getenv("GCLOUD_PROJECT") +DISPLAY_NAME = "entity_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") +ENTITY_VALUE_1 = "test_list_entity_value" + +pytest.ENTITY_TYPE_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def setup_teardown(): + # Create an entity type + entity_types_client = dialogflow.EntityTypesClient() + parent = entity_types_client.project_agent_path(PROJECT_ID) + entity_type = dialogflow.types.EntityType( + display_name=DISPLAY_NAME, + kind=dialogflow.enums.EntityType.Kind.KIND_MAP, + ) + + response = entity_types_client.create_entity_type(parent, entity_type) + pytest.ENTITY_TYPE_ID = response.name.split("agent/entityTypes/")[1] + + # Create an entity inside the entity type + entity_type_path = entity_types_client.entity_type_path( + PROJECT_ID, pytest.ENTITY_TYPE_ID + ) + entity = dialogflow.types.EntityType.Entity( + value=ENTITY_VALUE_1, synonyms=[ENTITY_VALUE_1] + ) + response = entity_types_client.batch_create_entities( + entity_type_path, [entity] + ) + response.result() + + yield + + # Delete the created entity type and its entities + entity_types_client.delete_entity_type(entity_type_path) + + +def test_list_entities(capsys): + entity_management.list_entities(PROJECT_ID, pytest.ENTITY_TYPE_ID) + out, _ = capsys.readouterr() + assert ENTITY_VALUE_1 in out diff --git a/dialogflow/cloud-client/list_entity_types_test.py b/dialogflow/cloud-client/list_entity_types_test.py new file mode 100644 index 000000000000..7ad7cd52d4a4 --- /dev/null +++ b/dialogflow/cloud-client/list_entity_types_test.py @@ -0,0 +1,55 @@ +# Copyright 2020 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 __future__ import absolute_import + +import datetime +import os +import pytest + +import dialogflow_v2 as dialogflow + +import entity_type_management + +PROJECT_ID = os.getenv("GCLOUD_PROJECT") +DISPLAY_NAME = "entity_type_" + datetime.datetime.now().strftime( + "%Y%m%d%H%M%S" +) + + +@pytest.fixture(scope="function", autouse=True) +def setup_teardown(): + # Create an entity type to list + entity_types_client = dialogflow.EntityTypesClient() + parent = entity_types_client.project_agent_path(PROJECT_ID) + entity_type = dialogflow.types.EntityType( + display_name=DISPLAY_NAME, + kind=dialogflow.enums.EntityType.Kind.KIND_MAP, + ) + response = entity_types_client.create_entity_type(parent, entity_type) + entity_type_id = response.name.split("agent/entityTypes/")[1] + + yield + + # Delete the created entity type + entity_type_path = entity_types_client.entity_type_path( + PROJECT_ID, entity_type_id + ) + entity_types_client.delete_entity_type(entity_type_path) + + +def test_list_entity_types(capsys): + entity_type_management.list_entity_types(PROJECT_ID) + out, _ = capsys.readouterr() + assert DISPLAY_NAME in out