Skip to content

Commit

Permalink
dialogflow: split up entity and entity type tests [(#2735)](GoogleClo…
Browse files Browse the repository at this point in the history
…udPlatform/python-docs-samples#2735)

* dialogflow: split up entity and entity type tests

* delete old file

* run black on new files
  • Loading branch information
nnegrey authored and Doug Mahugh committed Jan 27, 2020
1 parent 7389418 commit 53d660c
Show file tree
Hide file tree
Showing 7 changed files with 369 additions and 92 deletions.
70 changes: 70 additions & 0 deletions samples/snippets/create_entity_test.py
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions samples/snippets/create_entity_type_test.py
Original file line number Diff line number Diff line change
@@ -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]
71 changes: 71 additions & 0 deletions samples/snippets/delete_entity_test.py
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions samples/snippets/delete_entity_type_test.py
Original file line number Diff line number Diff line change
@@ -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
92 changes: 0 additions & 92 deletions samples/snippets/entity_management_test.py

This file was deleted.

Loading

0 comments on commit 53d660c

Please sign in to comment.