From 768c96bb5c900dfad959855df4eaf9189f6497b2 Mon Sep 17 00:00:00 2001 From: sina chavoshi Date: Wed, 20 Jul 2022 23:02:13 +0000 Subject: [PATCH 1/3] feat: Add metadata SDK sample for delete method. --- samples/model-builder/conftest.py | 21 +++++++++++++ .../delete_artifact_sample.py | 29 +++++++++++++++++ .../delete_artifact_sample_test.py | 31 +++++++++++++++++++ .../delete_context_sample.py | 29 +++++++++++++++++ .../delete_context_sample_test.py | 31 +++++++++++++++++++ .../delete_execution_sample.py | 29 +++++++++++++++++ .../delete_execution_sample_test.py | 31 +++++++++++++++++++ 7 files changed, 201 insertions(+) create mode 100644 samples/model-builder/experiment_tracking/delete_artifact_sample.py create mode 100644 samples/model-builder/experiment_tracking/delete_artifact_sample_test.py create mode 100644 samples/model-builder/experiment_tracking/delete_context_sample.py create mode 100644 samples/model-builder/experiment_tracking/delete_context_sample_test.py create mode 100644 samples/model-builder/experiment_tracking/delete_execution_sample.py create mode 100644 samples/model-builder/experiment_tracking/delete_execution_sample_test.py diff --git a/samples/model-builder/conftest.py b/samples/model-builder/conftest.py index 3e6e2a1c84..07b49790dc 100644 --- a/samples/model-builder/conftest.py +++ b/samples/model-builder/conftest.py @@ -597,6 +597,27 @@ def mock_pipeline_job_create(mock_pipeline_job): yield mock_pipeline_job_create +@pytest.fixture +def mock_artifact_delete(): + with patch.object(aiplatform.Artifact, "delete") as mock_artifact_delete: + mock_artifact_delete.return_value = None + yield mock_artifact_delete + + +@pytest.fixture +def mock_execution_delete(): + with patch.object(aiplatform.Execution, "delete") as mock_execution_delete: + mock_execution_delete.return_value = None + yield mock_execution_delete + + +@pytest.fixture +def mock_context_delete(): + with patch.object(aiplatform.Context, "delete") as mock_context_delete: + mock_context_delete.return_value = None + yield mock_context_delete + + @pytest.fixture def mock_pipeline_job_submit(mock_pipeline_job): with patch.object(mock_pipeline_job, "submit") as mock_pipeline_job_submit: diff --git a/samples/model-builder/experiment_tracking/delete_artifact_sample.py b/samples/model-builder/experiment_tracking/delete_artifact_sample.py new file mode 100644 index 0000000000..853c413c6c --- /dev/null +++ b/samples/model-builder/experiment_tracking/delete_artifact_sample.py @@ -0,0 +1,29 @@ +# Copyright 2022 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 +# +# https://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 google.cloud import aiplatform + + +# [START aiplatform_sdk_delete_artifact_sample] +def delete_artifact_sample( + artifact_id: str, + project: str, + location: str, +): + artifact = aiplatform.Artifact.get( + resource_id=artifact_id, project=project, location=location + ) + artifact.delete() + +# [END aiplatform_sdk_delete_artifact_sample] diff --git a/samples/model-builder/experiment_tracking/delete_artifact_sample_test.py b/samples/model-builder/experiment_tracking/delete_artifact_sample_test.py new file mode 100644 index 0000000000..555b6745ee --- /dev/null +++ b/samples/model-builder/experiment_tracking/delete_artifact_sample_test.py @@ -0,0 +1,31 @@ +# Copyright 2022 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 +# +# https://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. + +import delete_artifact_sample + +import test_constants + + +def test_delete_artifact_sample(mock_artifact, mock_artifact_get): + delete_artifact_sample.delete_artifact_sample( + artifact_id=test_constants.RESOURCE_ID, + project=test_constants.PROJECT, + location=test_constants.LOCATION, + ) + + mock_artifact_get.assert_called_with( + resource_id=test_constants.RESOURCE_ID, + project=test_constants.PROJECT, + location=test_constants.LOCATION, + ) diff --git a/samples/model-builder/experiment_tracking/delete_context_sample.py b/samples/model-builder/experiment_tracking/delete_context_sample.py new file mode 100644 index 0000000000..2753138f41 --- /dev/null +++ b/samples/model-builder/experiment_tracking/delete_context_sample.py @@ -0,0 +1,29 @@ +# Copyright 2022 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 +# +# https://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 google.cloud import aiplatform + + +# [START aiplatform_sdk_delete_context_sample] +def delete_context_sample( + context_id: str, + project: str, + location: str, +): + context = aiplatform.Context.get( + resource_id=context_id, project=project, location=location + ) + context.delete() + +# [END aiplatform_sdk_delete_context_sample] diff --git a/samples/model-builder/experiment_tracking/delete_context_sample_test.py b/samples/model-builder/experiment_tracking/delete_context_sample_test.py new file mode 100644 index 0000000000..a10708d8c6 --- /dev/null +++ b/samples/model-builder/experiment_tracking/delete_context_sample_test.py @@ -0,0 +1,31 @@ +# Copyright 2022 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 +# +# https://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. + +import delete_context_sample + +import test_constants + + +def test_delete_context_sample(mock_context, mock_context_get): + delete_context_sample.delete_context_sample( + context_id=test_constants.RESOURCE_ID, + project=test_constants.PROJECT, + location=test_constants.LOCATION, + ) + + mock_context_get.assert_called_with( + resource_id=test_constants.RESOURCE_ID, + project=test_constants.PROJECT, + location=test_constants.LOCATION, + ) diff --git a/samples/model-builder/experiment_tracking/delete_execution_sample.py b/samples/model-builder/experiment_tracking/delete_execution_sample.py new file mode 100644 index 0000000000..77f6fe959f --- /dev/null +++ b/samples/model-builder/experiment_tracking/delete_execution_sample.py @@ -0,0 +1,29 @@ +# Copyright 2022 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 +# +# https://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 google.cloud import aiplatform + + +# [START aiplatform_sdk_delete_execution_sample] +def delete_execution_sample( + execution_id: str, + project: str, + location: str, +): + execution = aiplatform.Execution.get( + resource_id=execution_id, project=project, location=location + ) + execution.delete() + +# [END aiplatform_sdk_delete_execution_sample] diff --git a/samples/model-builder/experiment_tracking/delete_execution_sample_test.py b/samples/model-builder/experiment_tracking/delete_execution_sample_test.py new file mode 100644 index 0000000000..3e6cba5d8e --- /dev/null +++ b/samples/model-builder/experiment_tracking/delete_execution_sample_test.py @@ -0,0 +1,31 @@ +# Copyright 2022 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 +# +# https://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. + +import delete_execution_sample + +import test_constants + + +def test_delete_execution_sample(mock_execution, mock_execution_get): + delete_execution_sample.delete_execution_sample( + execution_id=test_constants.RESOURCE_ID, + project=test_constants.PROJECT, + location=test_constants.LOCATION, + ) + + mock_execution_get.assert_called_with( + resource_id=test_constants.RESOURCE_ID, + project=test_constants.PROJECT, + location=test_constants.LOCATION, + ) From 9ce4a818d65a94b71f53d066047ed6449a2487cc Mon Sep 17 00:00:00 2001 From: sina chavoshi Date: Fri, 22 Jul 2022 01:06:12 +0000 Subject: [PATCH 2/3] add test fixtures. --- samples/model-builder/conftest.py | 11 +++++++++++ .../experiment_tracking/delete_context_sample_test.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/samples/model-builder/conftest.py b/samples/model-builder/conftest.py index 61c01ead45..30c75c49bf 100644 --- a/samples/model-builder/conftest.py +++ b/samples/model-builder/conftest.py @@ -527,6 +527,12 @@ def mock_artifact(): yield mock +@pytest.fixture +def mock_context(): + mock = MagicMock(aiplatform.Context) + yield mock + + @pytest.fixture def mock_experiment(): mock = MagicMock(aiplatform.Experiment) @@ -612,6 +618,11 @@ def mock_artifact_get(mock_artifact): mock_artifact_get.return_value = mock_artifact yield mock_artifact_get +@pytest.fixture +def mock_context_get(mock_context): + with patch.object(aiplatform.Context, "get") as mock_context_get: + mock_context_get.return_value = mock_context + yield mock_context_get @pytest.fixture def mock_pipeline_job_create(mock_pipeline_job): diff --git a/samples/model-builder/experiment_tracking/delete_context_sample_test.py b/samples/model-builder/experiment_tracking/delete_context_sample_test.py index a10708d8c6..49e2a08c72 100644 --- a/samples/model-builder/experiment_tracking/delete_context_sample_test.py +++ b/samples/model-builder/experiment_tracking/delete_context_sample_test.py @@ -17,7 +17,7 @@ import test_constants -def test_delete_context_sample(mock_context, mock_context_get): +def test_delete_context_sample(mock_context_get): delete_context_sample.delete_context_sample( context_id=test_constants.RESOURCE_ID, project=test_constants.PROJECT, From 3a9dd3ec3b90f0f1b80213ed05f30839ec5a3916 Mon Sep 17 00:00:00 2001 From: sina chavoshi Date: Fri, 22 Jul 2022 03:20:18 +0000 Subject: [PATCH 3/3] fix lint errors --- samples/model-builder/conftest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/model-builder/conftest.py b/samples/model-builder/conftest.py index 30c75c49bf..5375b870d7 100644 --- a/samples/model-builder/conftest.py +++ b/samples/model-builder/conftest.py @@ -618,12 +618,14 @@ def mock_artifact_get(mock_artifact): mock_artifact_get.return_value = mock_artifact yield mock_artifact_get + @pytest.fixture def mock_context_get(mock_context): with patch.object(aiplatform.Context, "get") as mock_context_get: mock_context_get.return_value = mock_context yield mock_context_get + @pytest.fixture def mock_pipeline_job_create(mock_pipeline_job): with patch.object(aiplatform, "PipelineJob") as mock_pipeline_job_create: