Skip to content

Commit

Permalink
added back unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Zhao <[email protected]>
  • Loading branch information
VincentZhao12 committed Dec 9, 2023
1 parent c73f2f4 commit 68e2284
Showing 1 changed file with 112 additions and 0 deletions.
112 changes: 112 additions & 0 deletions opensearch_py_ml/ml_commons/ml_commons_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,118 @@ def load_model(self, model_id: str, wait_until_loaded: bool = True) -> object:

return self._get_task_info(task_id)

def test_train_and_predict():
input_json = {

Check warning on line 350 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L350

Added line #L350 was not covered by tests
"parameters": {"centroids": 2, "iterations": 1, "distance_type": "EUCLIDEAN"},
"input_data": {
"column_metas": [
{"name": "k1", "column_type": "DOUBLE"},
{"name": "k2", "column_type": "DOUBLE"},
],
"rows": [
{
"values": [
{"column_type": "DOUBLE", "value": 1.00},
{"column_type": "DOUBLE", "value": 2.00},
]
},
{
"values": [
{"column_type": "DOUBLE", "value": 1.00},
{"column_type": "DOUBLE", "value": 4.00},
]
},
{
"values": [
{"column_type": "DOUBLE", "value": 1.00},
{"column_type": "DOUBLE", "value": 0.00},
]
},
{
"values": [
{"column_type": "DOUBLE", "value": 10.00},
{"column_type": "DOUBLE", "value": 2.00},
]
},
{
"values": [
{"column_type": "DOUBLE", "value": 10.00},
{"column_type": "DOUBLE", "value": 4.00},
]
},
{
"values": [
{"column_type": "DOUBLE", "value": 10.00},
{"column_type": "DOUBLE", "value": 0.00},
]
},
],
},
}

input_str = json.dumps(input_json)

Check warning on line 398 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L398

Added line #L398 was not covered by tests

raised = False
try:
train_and_predict_obj = ml_client.train_and_predict(

Check warning on line 402 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L400-L402

Added lines #L400 - L402 were not covered by tests
algorithm_name="kmeans", input_json=input_json
)
assert train_and_predict_obj["status"] == "COMPLETED"
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

Check warning on line 408 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L405-L408

Added lines #L405 - L408 were not covered by tests

raised = False
try:
train_and_predict_obj = ml_client.train_and_predict(

Check warning on line 412 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L410-L412

Added lines #L410 - L412 were not covered by tests
algorithm_name="kmeans", input_json=input_str
)
assert train_and_predict_obj["status"] == "COMPLETED"
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

Check warning on line 418 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L415-L418

Added lines #L415 - L418 were not covered by tests

raised = False
try:
train_and_predict_obj = ml_client.train_and_predict(

Check warning on line 422 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L420-L422

Added lines #L420 - L422 were not covered by tests
algorithm_name="not an alg", input_json=input_json
)
assert train_and_predict_obj == "Invalid algorithm name passed as argument."
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

Check warning on line 428 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L425-L428

Added lines #L425 - L428 were not covered by tests

raised = False
try:
train_and_predict_obj = ml_client.train_and_predict(

Check warning on line 432 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L430-L432

Added lines #L430 - L432 were not covered by tests
algorithm_name="kmeans", input_json=input_str + " invalid json"
)
assert train_and_predict_obj == "Invalid JSON string passed as argument."
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

Check warning on line 438 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L435-L438

Added lines #L435 - L438 were not covered by tests

raised = False
try:
train_and_predict_obj = ml_client.train_and_predict(

Check warning on line 442 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L440-L442

Added lines #L440 - L442 were not covered by tests
algorithm_name="kmeans", input_json="15"
)
assert train_and_predict_obj == "Invalid JSON object passed as argument."
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

Check warning on line 448 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L445-L448

Added lines #L445 - L448 were not covered by tests

raised = False
try:
train_and_predict_obj = ml_client.train_and_predict(

Check warning on line 452 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L450-L452

Added lines #L450 - L452 were not covered by tests
algorithm_name="kmeans", input_json=15
)
assert train_and_predict_obj == "Invalid JSON object passed as argument."
except: # noqa: E722
raised = True
assert raised == False, "Raised Exception in training and predicting task"

Check warning on line 458 in opensearch_py_ml/ml_commons/ml_commons_client.py

View check run for this annotation

Codecov / codecov/patch

opensearch_py_ml/ml_commons/ml_commons_client.py#L455-L458

Added lines #L455 - L458 were not covered by tests


def deploy_model(self, model_id: str, wait_until_deployed: bool = True) -> object:
"""
This method deploys a model in the opensearch cluster using ml-common plugin's deploy model api
Expand Down

0 comments on commit 68e2284

Please sign in to comment.