|
| 1 | +# Copyright 2021 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 | +import google.auth |
| 16 | + |
| 17 | +from google.cloud import contact_center_insights_v1 |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +import create_issue_model |
| 22 | + |
| 23 | +MIN_CONVERSATION_COUNT = 10000 |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def project_id(): |
| 28 | + _, project_id = google.auth.default() |
| 29 | + return project_id |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def insights_client(): |
| 34 | + return contact_center_insights_v1.ContactCenterInsightsClient() |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def count_conversations(project_id, insights_client): |
| 39 | + # Check if the project has the minimum number of conversations required to create an issue model. |
| 40 | + # See https://cloud.google.com/contact-center/insights/docs/topic-model. |
| 41 | + list_request = contact_center_insights_v1.ListConversationsRequest() |
| 42 | + list_request.page_size = 1000 |
| 43 | + list_request.parent = contact_center_insights_v1.ContactCenterInsightsClient.common_location_path( |
| 44 | + project_id, "us-central1" |
| 45 | + ) |
| 46 | + conversations = insights_client.list_conversations(request=list_request) |
| 47 | + conversation_count = len(list(conversations)) |
| 48 | + |
| 49 | + yield conversation_count |
| 50 | + |
| 51 | + |
| 52 | +@pytest.fixture |
| 53 | +def issue_model_resource(project_id, insights_client, count_conversations): |
| 54 | + conversation_count = count_conversations |
| 55 | + if conversation_count >= MIN_CONVERSATION_COUNT: |
| 56 | + # Create an issue model. |
| 57 | + issue_model = create_issue_model.create_issue_model(project_id) |
| 58 | + yield issue_model |
| 59 | + |
| 60 | + # Delete the issue model. |
| 61 | + insights_client.delete_issue_model(name=issue_model.name) |
| 62 | + else: |
| 63 | + yield None |
| 64 | + |
| 65 | + |
| 66 | +def test_create_issue_model(capsys, issue_model_resource): |
| 67 | + issue_model = issue_model_resource |
| 68 | + if issue_model: |
| 69 | + out, err = capsys.readouterr() |
| 70 | + assert "Created {}".format(issue_model.name) in out |
0 commit comments