-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from teamdatatonic/feature/add-tests
feature: add tests
- Loading branch information
Showing
10 changed files
with
458 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
components/bigquery-components/tests/test_extract_bq_to_dataset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import google.cloud.bigquery # noqa | ||
from kfp.v2.dsl import Dataset | ||
from unittest import mock | ||
|
||
import bigquery_components | ||
|
||
extract_bq_to_dataset = bigquery_components.extract_bq_to_dataset.python_func | ||
|
||
|
||
@mock.patch("google.cloud.bigquery.client.Client") | ||
@mock.patch("google.cloud.bigquery.table.Table") | ||
@mock.patch("google.cloud.bigquery.job.ExtractJobConfig") | ||
def test_extract_bq_to_dataset(mock_job_config, mock_table, mock_client, tmpdir): | ||
""" | ||
Checks that the extract_bq_to_dataset is called correctly | ||
""" | ||
mock_path = tmpdir | ||
mock_client.extract_table.return_value = "my-job" | ||
mock_table.return_value.table_ref = "my-table" | ||
mock_job_config.return_value = "mock-job-config" | ||
|
||
extract_bq_to_dataset( | ||
bq_client_project_id="my-project-id", | ||
source_project_id="source-project-id", | ||
dataset_id="dataset-id", | ||
table_name="table-name", | ||
dataset=Dataset(uri=mock_path), | ||
destination_gcs_uri="gs://mock_bucket", | ||
dataset_location="EU", | ||
extract_job_config=None, | ||
skip_if_exists=False, | ||
) | ||
|
||
mock_client.return_value.extract_table.assert_called_once_with( | ||
mock_table.return_value, "gs://mock_bucket", job_config="mock-job-config" | ||
) | ||
|
||
|
||
@mock.patch("google.cloud.bigquery.client.Client") | ||
@mock.patch("google.cloud.bigquery.table.Table") | ||
@mock.patch("google.cloud.bigquery.job.ExtractJobConfig") | ||
@mock.patch("pathlib.Path.exists") | ||
def test_extract_bq_to_dataset_skip_existing( | ||
mock_path_exists, mock_job_config, mock_table, mock_client, tmpdir | ||
): | ||
""" | ||
Checks that when the dataset exists the method is not called | ||
""" | ||
mock_path = tmpdir | ||
mock_path_exists.return_value = True | ||
|
||
extract_bq_to_dataset( | ||
bq_client_project_id="my-project-id", | ||
source_project_id="source-project-id", | ||
dataset_id="dataset-id", | ||
table_name="table-name", | ||
dataset=Dataset(uri=mock_path), | ||
destination_gcs_uri="gs://mock_bucket", | ||
dataset_location="EU", | ||
extract_job_config=None, | ||
skip_if_exists=True, | ||
) | ||
|
||
assert not mock_client.return_value.extract_table.called |
93 changes: 93 additions & 0 deletions
93
components/vertex-components/tests/test_custom_training_job.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import google.cloud.aiplatform as aip # noqa | ||
from kfp.v2.dsl import Dataset, Metrics, Artifact | ||
from unittest import mock | ||
import pytest | ||
|
||
|
||
import vertex_components | ||
|
||
custom_train_job = vertex_components.custom_train_job.python_func | ||
|
||
|
||
@mock.patch("google.cloud.aiplatform.CustomTrainingJob") | ||
@mock.patch("os.path.exists") | ||
@mock.patch("builtins.open", new_callable=mock.mock_open, read_data="{}") | ||
def test_custom_train_job(mock_open, mock_exists, mock_job, tmpdir): | ||
""" | ||
Checks that the custom job method is called | ||
""" | ||
mock_exists.return_value = True | ||
|
||
mock_train_data = Dataset(uri=tmpdir) | ||
mock_valid_data = Dataset(uri=tmpdir) | ||
mock_test_data = Dataset(uri=tmpdir) | ||
mock_model = Artifact(uri=tmpdir, metadata={"resourceName": ""}) | ||
mock_metrics = Metrics(uri=tmpdir) | ||
|
||
custom_train_job( | ||
train_script_uri="gs://my-bucket/train_script.py", | ||
train_data=mock_train_data, | ||
valid_data=mock_valid_data, | ||
test_data=mock_test_data, | ||
project_id="my-project-id", | ||
project_location="europe-west4", | ||
model_display_name="my-model", | ||
train_container_uri="gcr.io/my-project/my-image:latest", | ||
serving_container_uri="gcr.io/my-project/my-serving-image:latest", | ||
model=mock_model, | ||
metrics=mock_metrics, | ||
staging_bucket="gs://my-bucket", | ||
job_name="my-job", | ||
) | ||
|
||
mock_job.assert_called_once_with( | ||
project="my-project-id", | ||
location="europe-west4", | ||
staging_bucket="gs://my-bucket", | ||
display_name="my-job", | ||
script_path="/gcs/my-bucket/train_script.py", | ||
container_uri="gcr.io/my-project/my-image:latest", | ||
requirements=None, | ||
model_serving_container_image_uri="gcr.io/my-project/my-serving-image:latest", # noqa: E501 | ||
) | ||
|
||
# Assert metrics loading | ||
mock_open.assert_called_once_with(tmpdir, "r") | ||
|
||
|
||
@mock.patch("google.cloud.aiplatform.CustomTrainingJob") | ||
@mock.patch("os.path.exists") | ||
@mock.patch("builtins.open", new_callable=mock.mock_open, read_data="{}") | ||
def test_custom_train_script_not_found(mock_open, mock_exists, mock_job, tmpdir): | ||
""" | ||
Checks that when the training script is not found | ||
the method fails | ||
""" | ||
mock_exists.return_value = False | ||
|
||
mock_train_data = Dataset(uri=tmpdir) | ||
mock_valid_data = Dataset(uri=tmpdir) | ||
mock_test_data = Dataset(uri=tmpdir) | ||
mock_model = Artifact(uri=tmpdir, metadata={"resourceName": ""}) | ||
mock_metrics = Metrics(uri=tmpdir) | ||
|
||
with pytest.raises(ValueError): | ||
custom_train_job( | ||
train_script_uri="gs://my-bucket/train_script.py", | ||
train_data=mock_train_data, | ||
valid_data=mock_valid_data, | ||
test_data=mock_test_data, | ||
project_id="my-project-id", | ||
project_location="europe-west4", | ||
model_display_name="my-model", | ||
train_container_uri="gcr.io/my-project/my-image:latest", | ||
serving_container_uri="gcr.io/my-project/my-serving-image:latest", | ||
model=mock_model, | ||
metrics=mock_metrics, | ||
staging_bucket="gs://my-bucket", | ||
job_name="my-job", | ||
) | ||
|
||
# Assert the custom training job is not executed | ||
mock_job.assert_not_called() | ||
mock_open.assert_not_called() |
57 changes: 57 additions & 0 deletions
57
components/vertex-components/tests/test_import_model_evaluation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from unittest import mock | ||
from kfp.v2.dsl import Model, Metrics, Dataset | ||
|
||
import vertex_components | ||
from google.cloud.aiplatform_v1 import ModelEvaluation | ||
|
||
|
||
import_model_evaluation = vertex_components.import_model_evaluation.python_func | ||
|
||
|
||
@mock.patch("google.cloud.aiplatform_v1.ModelServiceClient") | ||
@mock.patch( | ||
"builtins.open", | ||
new_callable=mock.mock_open, | ||
read_data='{"accuracy": 0.85, "problemType": "classification"}', | ||
) | ||
@mock.patch("google.protobuf.json_format.ParseDict") | ||
def test_import_model_evaluation( | ||
mock_parse_dict, mock_open, mock_service_client, tmpdir | ||
): | ||
""" | ||
Checks that when the model evaluation is running and it is writing the metrics | ||
""" | ||
mock_model = Model(uri=tmpdir, metadata={"resourceName": ""}) | ||
mock_metrics = Metrics(uri=tmpdir) | ||
mock_dataset = Dataset(uri=tmpdir) | ||
|
||
# Create an instance of the mocked ModelServiceClient. | ||
service_client_instance = mock.MagicMock() | ||
mock_service_client.return_value = service_client_instance | ||
# When import_model_evaluation is called during the test, | ||
# it will return a new ModelEvaluation with the specified name. | ||
service_client_instance.import_model_evaluation.return_value = ModelEvaluation( | ||
name="model_evaluation_name" | ||
) | ||
|
||
# Set the return value for ParseDict to be a mock ModelEvaluation | ||
mock_parse_dict.return_value = mock.MagicMock(spec=ModelEvaluation) | ||
|
||
model_evaluation_name = import_model_evaluation( | ||
model=mock_model, | ||
metrics=mock_metrics, | ||
test_dataset=mock_dataset, | ||
pipeline_job_id="1234", | ||
project_location="my-location", | ||
evaluation_name="Imported evaluation", | ||
) | ||
|
||
service_client_instance.import_model_evaluation.assert_called_once_with( | ||
parent=mock_model.metadata["resourceName"], | ||
model_evaluation=mock_parse_dict.return_value, | ||
) | ||
|
||
# Check that open was called with the correct path | ||
mock_open.assert_called_once_with(mock_metrics.uri) | ||
|
||
assert model_evaluation_name[0] == "model_evaluation_name" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.