Skip to content

Commit

Permalink
docs(samples): add samples to create/delete featurestore (#980)
Browse files Browse the repository at this point in the history
* feat: SDK feature store samples (create/delete fs)

* feat: adding to conftest.py

* docs(samples): fixed testing

* docs(samples): fixed testing

* docs(samples): fixed testing

* docs(samples) added changes

* docs(samples): style issues

* Update samples/model-builder/create_featurestore_sample_test.py

Co-authored-by: Morgan Du <morgandu@google.com>

* Update samples/model-builder/test_constants.py

Co-authored-by: Morgan Du <morgandu@google.com>

* Update samples/model-builder/create_featurestore_sample_test.py

Co-authored-by: Morgan Du <morgandu@google.com>

Co-authored-by: Morgan Du <morgandu@google.com>
  • Loading branch information
nayaknishant and morgandu authored Feb 24, 2022
1 parent 095bea2 commit 5ee6354
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 0 deletions.
35 changes: 35 additions & 0 deletions samples/model-builder/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,38 @@ def mock_endpoint_explain(mock_endpoint):
with patch.object(mock_endpoint, "explain") as mock_endpoint_explain:
mock_get_endpoint.return_value = mock_endpoint
yield mock_endpoint_explain


"""
----------------------------------------------------------------------------
FeatureStore Fixtures
----------------------------------------------------------------------------
"""


@pytest.fixture
def mock_featurestore():
mock = MagicMock(aiplatform.featurestore.Featurestore)
yield mock


@pytest.fixture
def mock_get_featurestore(mock_featurestore):
with patch.object(aiplatform.featurestore, "Featurestore") as mock_get_featurestore:
mock_get_featurestore.return_value = mock_featurestore
yield mock_get_featurestore


@pytest.fixture
def mock_create_featurestore(mock_featurestore):
with patch.object(
aiplatform.featurestore.Featurestore, "create"
) as mock_create_featurestore:
mock_create_featurestore.return_value = mock_featurestore
yield mock_create_featurestore


@pytest.fixture
def mock_delete_featurestore(mock_featurestore):
with patch.object(mock_featurestore, "delete") as mock_delete_featurestore:
yield mock_delete_featurestore
41 changes: 41 additions & 0 deletions samples/model-builder/create_featurestore_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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.


# [START aiplatform_sdk_create_featurestore_sample]
from google.cloud import aiplatform


def create_featurestore_sample(
project: str,
location: str,
featurestore_id: str,
online_store_fixed_node_count: int = 1,
sync: bool = True,
):

aiplatform.init(project=project, location=location)

fs = aiplatform.Featurestore.create(
featurestore_id=featurestore_id,
online_store_fixed_node_count=online_store_fixed_node_count,
sync=sync,
)

fs.wait()

return fs


# [END aiplatform_sdk_create_featurestore_sample]
37 changes: 37 additions & 0 deletions samples/model-builder/create_featurestore_sample_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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 create_featurestore_sample
import test_constants as constants


def test_create_featurestore_sample(mock_sdk_init, mock_create_featurestore):

create_featurestore_sample.create_featurestore_sample(
project=constants.PROJECT,
location=constants.LOCATION,
featurestore_id=constants.FEAUTURESTORE_ID,
online_store_fixed_node_count=constants.ONLINE_STORE_FIXED_NODE_COUNT,
sync=constants.SYNC,
)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)

mock_create_featurestore.assert_called_once_with(
featurestore_id=constants.FEAUTURESTORE_ID,
online_store_fixed_node_count=constants.ONLINE_STORE_FIXED_NODE_COUNT,
sync=constants.SYNC,
)
34 changes: 34 additions & 0 deletions samples/model-builder/delete_featurestore_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.


# [START aiplatform_sdk_delete_featurestore_sample]
from google.cloud import aiplatform


def delete_featurestore_sample(
project: str,
location: str,
featurestore_name: str,
sync: bool = True,
force: bool = True,
):

aiplatform.init(project=project, location=location)

fs = aiplatform.featurestore.Featurestore(featurestore_name=featurestore_name)
fs.delete(sync=sync, force=force)


# [END aiplatform_sdk_delete_featurestore_sample]
41 changes: 41 additions & 0 deletions samples/model-builder/delete_featurestore_sample_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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_featurestore_sample
import test_constants as constants


def test_delete_featurestore_sample(
mock_sdk_init, mock_get_featurestore, mock_delete_featurestore
):

delete_featurestore_sample.delete_featurestore_sample(
project=constants.PROJECT,
location=constants.LOCATION,
featurestore_name=constants.FEAUTURESTORE_NAME,
sync=constants.SYNC,
force=constants.FORCE,
)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)

mock_get_featurestore.assert_called_once_with(
featurestore_name=constants.FEAUTURESTORE_NAME
)

mock_delete_featurestore.assert_called_once_with(
sync=constants.SYNC, force=constants.FORCE
)
7 changes: 7 additions & 0 deletions samples/model-builder/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,10 @@
)
PYTHON_MODULE_NAME = "trainer.task"
MODEL_TYPE = "CLOUD"

# Feature store constants
FEAUTURESTORE_NAME = "projects/123/locations/us-central1/featurestores/featurestore_id"
FEAUTURESTORE_ID = "featurestore_id"
ONLINE_STORE_FIXED_NODE_COUNT = 1
SYNC = True
FORCE = True

0 comments on commit 5ee6354

Please sign in to comment.