Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Sep 11, 2019
1 parent 95124d8 commit 0351856
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 1 deletion.
33 changes: 33 additions & 0 deletions python/tests/test_combiner_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from seldon_core.wrapper import get_rest_microservice, SeldonModelGRPC, get_grpc_server
from seldon_core.proto import prediction_pb2
from seldon_core.user_model import SeldonComponent


class UserObject(object):
Expand Down Expand Up @@ -278,3 +279,35 @@ def test_aggregate_proto_lowlevel_ok():
def test_get_grpc_server():
user_object = UserObject(ret_nparray=True)
server = get_grpc_server(user_object)


def test_unimplemented_aggregate_raw_on_seldon_component():
class CustomSeldonComponent(SeldonComponent):
def aggregate(self, Xs, features_names):
return sum(Xs) * 2

user_object = CustomSeldonComponent()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/aggregate?json={"seldonMessages":[{"data":{"ndarray":[1]}},{"data":{"ndarray":[2]}}]}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [6.0]


def test_unimplemented_aggregate_raw():
class CustomObject(object):
def aggregate(self, Xs, features_names):
return sum(Xs) * 2

user_object = CustomObject()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/aggregate?json={"seldonMessages":[{"data":{"ndarray":[1]}},{"data":{"ndarray":[2]}}]}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [6.0]
62 changes: 62 additions & 0 deletions python/tests/test_model_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,3 +524,65 @@ def test_proto_gets_meta():
assert j["meta"]["metrics"][0]["value"] == user_object.metrics()[0]["value"]
assert j["data"]["tensor"]["shape"] == [2, 1]
assert j["data"]["tensor"]["values"] == [1, 2]


def test_unimplemented_predict_raw_on_seldon_component():
class CustomSeldonComponent(SeldonComponent):
def predict(self, X, features_names, **kwargs):
return X * 2

user_object = CustomSeldonComponent()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/predict?json={"data":{"names":["a","b"],"ndarray":[[1,2]]}}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [[2.0, 4.0]]


def test_unimplemented_predict_raw():
class CustomObject(object):
def predict(self, X, features_names, **kwargs):
return X * 2

user_object = CustomObject()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/predict?json={"data":{"names":["a","b"],"ndarray":[[1,2]]}}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [[2.0, 4.0]]


def test_unimplemented_feedback_raw_on_seldon_component():
class CustomSeldonComponent(SeldonComponent):
def feedback(self, features, feature_names, reward, truth):
print("Feedback called")

user_object = CustomSeldonComponent()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/send-feedback?json={"request":{"data":{"ndarray":[]}},"reward":1.0}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200


def test_unimplemented_feedback_raw():
class CustomObject(object):
def feedback(self, features, feature_names, reward, truth):
print("Feedback called")

user_object = CustomObject()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/send-feedback?json={"request":{"data":{"ndarray":[]}},"reward":1.0}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
33 changes: 33 additions & 0 deletions python/tests/test_router_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from google.protobuf import json_format
from seldon_core.wrapper import get_rest_microservice, SeldonModelGRPC, get_grpc_server
from seldon_core.proto import prediction_pb2
from seldon_core.user_model import SeldonComponent


class UserObject(object):
Expand Down Expand Up @@ -273,3 +274,35 @@ def test_proto_feedback():
def test_get_grpc_server():
user_object = UserObject()
server = get_grpc_server(user_object)


def test_unimplemented_route_raw_on_seldon_component():
class CustomSeldonComponent(SeldonComponent):
def route(self, X, features_names):
return 53

user_object = CustomSeldonComponent()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/route?json={"data":{"ndarray":[2]}}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [[53]]


def test_unimplemented_route_raw():
class CustomObject(object):
def route(self, X, features_names):
return 53

user_object = CustomObject()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/route?json={"data":{"ndarray":[2]}}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [[53]]
64 changes: 64 additions & 0 deletions python/tests/test_transformer_microservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,67 @@ def test_transform_output_proto_gets_meta():
assert j["meta"]["metrics"][0]["value"] == user_object.metrics()[0]["value"]
assert j["data"]["tensor"]["shape"] == [2, 1]
assert j["data"]["tensor"]["values"] == [1, 2]


def test_unimplemented_transform_input_raw_on_seldon_component():
class CustomSeldonComponent(SeldonComponent):
def transform_input(self, X, features_names, **kwargs):
return X * 2

user_object = CustomSeldonComponent()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/transform-input?json={"data":{"ndarray":[1]}}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [2.0]


def test_unimplemented_transform_input_raw():
class CustomObject(object):
def transform_input(self, X, features_names, **kwargs):
return X * 2

user_object = CustomObject()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/transform-input?json={"data":{"ndarray":[1]}}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [2.0]


def test_unimplemented_transform_output_raw_on_seldon_component():
class CustomSeldonComponent(SeldonComponent):
def transform_output(self, X, features_names, **kwargs):
return X * 2

user_object = CustomSeldonComponent()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/transform-output?json={"data":{"ndarray":[1]}}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [2.0]


def test_unimplemented_transform_output_raw():
class CustomObject(object):
def transform_output(self, X, features_names, **kwargs):
return X * 2

user_object = CustomObject()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/transform-output?json={"data":{"ndarray":[1]}}')
j = json.loads(rv.data)

print(j)
assert rv.status_code == 200
assert j["data"]["ndarray"] == [2.0]
19 changes: 18 additions & 1 deletion python/tests/test_user_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,21 @@ def test_class_names_method(caplog):
predictions = np.array([[1, 2], [3, 4]])
names = client_class_names(user_object, predictions)
assert names == ["x", "y"]
assert not "class_names attribute is deprecated. Please define a class_names method" in caplog.text
assert not "class_names attribute is deprecated. Please define a class_names method" in caplog.text

def test_no_class_names_on_seldon_component(caplog):
caplog.set_level(logging.INFO)
user_object = SeldonComponent()
predictions = np.array([[1, 2], [3, 4]])
names = client_class_names(user_object, predictions)
assert names == ["t:0", "t:1"]

def test_no_class_names(caplog):
caplog.set_level(logging.INFO)
class X():
pass
user_object = X()
predictions = np.array([[1, 2], [3, 4]])
names = client_class_names(user_object, predictions)


0 comments on commit 0351856

Please sign in to comment.