|
| 1 | +# Copyright 2020 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from __future__ import absolute_import |
| 16 | + |
| 17 | +import datetime |
| 18 | +import os |
| 19 | + |
| 20 | +import dialogflow_v2beta1 as dialogflow |
| 21 | +import pytest |
| 22 | + |
| 23 | +import document_management |
| 24 | + |
| 25 | +PROJECT_ID = os.getenv('GCLOUD_PROJECT') |
| 26 | +KNOWLEDGE_BASE_NAME = 'knowledge_' \ |
| 27 | + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") |
| 28 | +DOCUMENT_DISPLAY_NAME = 'test_document_' \ |
| 29 | + + datetime.datetime.now().strftime("%Y%m%d%H%M%S") |
| 30 | +pytest.KNOWLEDGE_BASE_ID = None |
| 31 | +pytest.DOCUMENT_ID = None |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture(scope="function", autouse=True) |
| 35 | +def setup_teardown(): |
| 36 | + # Create a knowledge base to use in document management |
| 37 | + client = dialogflow.KnowledgeBasesClient() |
| 38 | + project_path = client.project_path(PROJECT_ID) |
| 39 | + knowledge_base = dialogflow.types.KnowledgeBase( |
| 40 | + display_name=KNOWLEDGE_BASE_NAME) |
| 41 | + response = client.create_knowledge_base(project_path, knowledge_base) |
| 42 | + pytest.KNOWLEDGE_BASE_ID = response.name.split( |
| 43 | + '/knowledgeBases/')[1].split('\n')[0] |
| 44 | + |
| 45 | + # Create a document to delete |
| 46 | + knowledge_base_path = client.knowledge_base_path( |
| 47 | + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) |
| 48 | + document = dialogflow.types.Document( |
| 49 | + display_name=DOCUMENT_DISPLAY_NAME, mime_type='text/html', |
| 50 | + content_uri='https://cloud.google.com/storage/docs/faq') |
| 51 | + document.knowledge_types.append( |
| 52 | + dialogflow.types.Document.KnowledgeType.Value('FAQ')) |
| 53 | + documents_client = dialogflow.DocumentsClient() |
| 54 | + response = documents_client.create_document(knowledge_base_path, document) |
| 55 | + document = response.result(timeout=90) |
| 56 | + pytest.DOCUMENT_ID = document.name.split('/documents/')[1].split('\n')[0] |
| 57 | + |
| 58 | + yield |
| 59 | + |
| 60 | + # Delete the created knowledge base |
| 61 | + client.delete_knowledge_base(knowledge_base_path, force=True) |
| 62 | + |
| 63 | + |
| 64 | +def test_delete_document(capsys): |
| 65 | + document_management.delete_document( |
| 66 | + PROJECT_ID, pytest.KNOWLEDGE_BASE_ID, pytest.DOCUMENT_ID) |
| 67 | + document_management.list_documents(PROJECT_ID, pytest.KNOWLEDGE_BASE_ID) |
| 68 | + out, _ = capsys.readouterr() |
| 69 | + assert DOCUMENT_DISPLAY_NAME not in out |
0 commit comments