Skip to content

Commit

Permalink
feat: Add metadata SDK sample for delete method. (#1530)
Browse files Browse the repository at this point in the history
* feat: Add metadata SDK sample for delete method.

* add test fixtures.

* fix lint errors
  • Loading branch information
SinaChavoshi authored Jul 22, 2022
1 parent 34bbd0a commit 46aa9b5
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 0 deletions.
34 changes: 34 additions & 0 deletions samples/model-builder/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -613,13 +619,41 @@ def mock_artifact_get(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:
mock_pipeline_job_create.return_value = 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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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,
)
29 changes: 29 additions & 0 deletions samples/model-builder/experiment_tracking/delete_context_sample.py
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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_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,
)
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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,
)

0 comments on commit 46aa9b5

Please sign in to comment.