|
16 | 16 | # |
17 | 17 |
|
18 | 18 | from importlib import reload |
| 19 | +from unittest import mock |
19 | 20 | from unittest.mock import patch |
20 | 21 |
|
21 | 22 | import pytest |
|
28 | 29 | from google.cloud.aiplatform.metadata import artifact |
29 | 30 | from google.cloud.aiplatform.metadata import context |
30 | 31 | from google.cloud.aiplatform.metadata import execution |
| 32 | +from google.cloud.aiplatform.metadata import metadata_store |
31 | 33 | from google.cloud.aiplatform.metadata import utils as metadata_utils |
32 | 34 | from google.cloud.aiplatform_v1 import ( |
33 | 35 | MetadataServiceClient, |
|
49 | 51 | _TEST_METADATA_STORE = "test-metadata-store" |
50 | 52 | _TEST_ALT_LOCATION = "europe-west4" |
51 | 53 | _TEST_PARENT = f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/metadataStores/{_TEST_METADATA_STORE}" |
| 54 | +_TEST_DEFAULT_PARENT = ( |
| 55 | + f"projects/{_TEST_PROJECT}/locations/{_TEST_LOCATION}/metadataStores/default" |
| 56 | +) |
52 | 57 |
|
53 | 58 | # resource attributes |
54 | 59 | _TEST_DISPLAY_NAME = "test-display-name" |
@@ -749,6 +754,57 @@ def test_init_execution_with_id(self, get_execution_mock): |
749 | 754 | name=_TEST_EXECUTION_NAME, retry=base._DEFAULT_RETRY |
750 | 755 | ) |
751 | 756 |
|
| 757 | + def test_create_execution_uses_default_credentials_and_metadata_store(self): |
| 758 | + aiplatform.init(project=_TEST_PROJECT, location=_TEST_LOCATION) |
| 759 | + api_client = mock.Mock() |
| 760 | + api_client.create_execution.return_value = GapicExecution( |
| 761 | + name=f"{_TEST_DEFAULT_PARENT}/executions/{_TEST_EXECUTION_ID}", |
| 762 | + display_name=_TEST_DISPLAY_NAME, |
| 763 | + schema_title=_TEST_SCHEMA_TITLE, |
| 764 | + schema_version=_TEST_SCHEMA_VERSION, |
| 765 | + description=_TEST_DESCRIPTION, |
| 766 | + metadata=_TEST_METADATA, |
| 767 | + state=GapicExecution.State.RUNNING, |
| 768 | + ) |
| 769 | + |
| 770 | + with patch.object( |
| 771 | + metadata_store._MetadataStore, "ensure_default_metadata_store_exists" |
| 772 | + ) as ensure_metadata_store_mock, patch.object( |
| 773 | + execution.Execution, "_instantiate_client", return_value=api_client |
| 774 | + ) as instantiate_client_mock: |
| 775 | + my_execution = execution.Execution.create( |
| 776 | + resource_id=_TEST_EXECUTION_ID, |
| 777 | + schema_title=_TEST_SCHEMA_TITLE, |
| 778 | + display_name=_TEST_DISPLAY_NAME, |
| 779 | + schema_version=_TEST_SCHEMA_VERSION, |
| 780 | + description=_TEST_DESCRIPTION, |
| 781 | + metadata=_TEST_METADATA, |
| 782 | + ) |
| 783 | + |
| 784 | + ensure_metadata_store_mock.assert_called_once_with( |
| 785 | + project=None, location=None, credentials=None |
| 786 | + ) |
| 787 | + assert instantiate_client_mock.call_args_list[0] == mock.call( |
| 788 | + location=None, |
| 789 | + credentials=None, |
| 790 | + appended_user_agent=[ |
| 791 | + "sdk_command/aiplatform.metadata.execution.Execution.create" |
| 792 | + ], |
| 793 | + ) |
| 794 | + api_client.create_execution.assert_called_once_with( |
| 795 | + parent=_TEST_DEFAULT_PARENT, |
| 796 | + execution_id=_TEST_EXECUTION_ID, |
| 797 | + execution=GapicExecution( |
| 798 | + schema_title=_TEST_SCHEMA_TITLE, |
| 799 | + schema_version=_TEST_SCHEMA_VERSION, |
| 800 | + display_name=_TEST_DISPLAY_NAME, |
| 801 | + description=_TEST_DESCRIPTION, |
| 802 | + metadata=_TEST_METADATA, |
| 803 | + state=GapicExecution.State.RUNNING, |
| 804 | + ), |
| 805 | + ) |
| 806 | + assert my_execution._gca_resource == api_client.create_execution.return_value |
| 807 | + |
752 | 808 | def test_get_or_create_execution( |
753 | 809 | self, get_execution_for_get_or_create_mock, create_execution_mock |
754 | 810 | ): |
|
0 commit comments