From 66003212b0d85cfeffbe3dc808c35c9a0ab29097 Mon Sep 17 00:00:00 2001 From: Rafal Skolasinski Date: Mon, 2 Nov 2020 12:13:08 +0000 Subject: [PATCH] add tests --- python/tests/test_model_microservice.py | 80 +++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/python/tests/test_model_microservice.py b/python/tests/test_model_microservice.py index e1b479f21c..63db885f8d 100644 --- a/python/tests/test_model_microservice.py +++ b/python/tests/test_model_microservice.py @@ -6,6 +6,8 @@ from PIL import Image import io +from unittest import mock + from seldon_core.wrapper import get_rest_microservice, SeldonModelGRPC, get_grpc_server from seldon_core.metrics import SeldonMetrics from seldon_core.proto import prediction_pb2 @@ -309,6 +311,84 @@ def test_model_puid_ok(): assert j["meta"]["puid"] == "123" +@mock.patch("seldon_core.utils.model_name", "my-test-model") +@mock.patch("seldon_core.utils.image_name", "my-test-model-image") +def test_requestPath_ok(): + user_object = UserObject() + seldon_metrics = SeldonMetrics() + app = get_rest_microservice(user_object, seldon_metrics) + client = app.test_client() + rv = client.get( + '/predict?json={"meta":{"puid":"123"},"data":{"names":["a","b"],"ndarray":[[1,2]]}}' + ) + j = json.loads(rv.data) + logging.info(j) + assert rv.status_code == 200 + assert j["meta"]["requestPath"] == {"my-test-model": "my-test-model-image"} + + +@mock.patch("seldon_core.utils.model_name", "my-test-model") +@mock.patch("seldon_core.utils.image_name", "my-test-model-image") +def test_requestPath_2nd_node_ok(): + user_object = UserObject() + seldon_metrics = SeldonMetrics() + app = get_rest_microservice(user_object, seldon_metrics) + client = app.test_client() + rv = client.get( + '/predict?json={"meta":{"requestPath":{"earlier-node": "earlier-image"}},"data":{"names":["a","b"],"ndarray":[[1,2]]}}' + ) + j = json.loads(rv.data) + logging.info(j) + assert rv.status_code == 200 + assert j["meta"]["requestPath"] == { + "my-test-model": "my-test-model-image", + "earlier-node": "earlier-image", + } + + +@mock.patch("seldon_core.utils.model_name", "my-test-model") +@mock.patch("seldon_core.utils.image_name", "my-test-model-image") +def test_proto_requestPath_ok(): + user_object = UserObject() + seldon_metrics = SeldonMetrics() + app = SeldonModelGRPC(user_object, seldon_metrics) + arr = np.array([1, 2]) + datadef = prediction_pb2.DefaultData( + tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr) + ) + meta = prediction_pb2.Meta() + json_format.ParseDict({"tags": {"foo": "bar"}}, meta) + request = prediction_pb2.SeldonMessage(data=datadef, meta=meta) + resp = app.Predict(request, None) + jStr = json_format.MessageToJson(resp) + j = json.loads(jStr) + logging.info(j) + assert j["meta"]["requestPath"] == {"my-test-model": "my-test-model-image"} + + +@mock.patch("seldon_core.utils.model_name", "my-test-model") +@mock.patch("seldon_core.utils.image_name", "my-test-model-image") +def test_proto_requestPath_2nd_node_ok(): + user_object = UserObject() + seldon_metrics = SeldonMetrics() + app = SeldonModelGRPC(user_object, seldon_metrics) + arr = np.array([1, 2]) + datadef = prediction_pb2.DefaultData( + tensor=prediction_pb2.Tensor(shape=(2, 1), values=arr) + ) + meta = prediction_pb2.Meta() + json_format.ParseDict({"requestPath": {"earlier-node": "earlier-image"}}, meta) + request = prediction_pb2.SeldonMessage(data=datadef, meta=meta) + resp = app.Predict(request, None) + jStr = json_format.MessageToJson(resp) + j = json.loads(jStr) + logging.info(j) + assert j["meta"]["requestPath"] == { + "my-test-model": "my-test-model-image", + "earlier-node": "earlier-image", + } + + def test_model_lowlevel_ok(): user_object = UserObjectLowLevel() seldon_metrics = SeldonMetrics()