diff --git a/tests/test_deepstack.py b/tests/test_deepstack.py index cc3db02..587907d 100644 --- a/tests/test_deepstack.py +++ b/tests/test_deepstack.py @@ -5,12 +5,18 @@ MOCK_IP_ADDRESS = "localhost" MOCK_PORT = 5000 -MOCK_URL = "http://{}:{}/v1/vision/detection".format(MOCK_IP_ADDRESS, MOCK_PORT) +OBJ_URL = "http://{}:{}/v1/vision/detection".format(MOCK_IP_ADDRESS, MOCK_PORT) +SCENE_URL = "http://{}:{}/v1/vision/scene".format(MOCK_IP_ADDRESS, MOCK_PORT) +FACE_DETECTION_URL = "http://{}:{}/v1/vision/face".format(MOCK_IP_ADDRESS, MOCK_PORT) + +CONFIDENCE_THRESHOLD = 0.7 MOCK_BYTES = b"Test" MOCK_API_KEY = "mock_api_key" MOCK_TIMEOUT = 8 +MOCK_SCENE_RESPONSE = {"success": True, "label": "street", "confidence": 0.86745402} + MOCK_OBJECT_DETECTION_RESPONSE = { "success": True, "predictions": [ @@ -41,6 +47,10 @@ ], } +MOCK_OBJECT_PREDICTIONS = MOCK_OBJECT_DETECTION_RESPONSE["predictions"] +MOCK_OBJECT_CONFIDENCES = [0.6998661, 0.7996547] + + MOCK_FACE_RECOGNITION_RESPONSE = { "success": True, "predictions": [ @@ -63,9 +73,20 @@ ], } -MOCK_OBJECT_PREDICTIONS = MOCK_OBJECT_DETECTION_RESPONSE["predictions"] -MOCK_OBJECT_CONFIDENCES = [0.6998661, 0.7996547] -CONFIDENCE_THRESHOLD = 0.7 +MOCK_FACE_DETECTION_RESPONSE = { + "success": True, + "predictions": [ + { + "confidence": 0.9999999, + "y_min": 173, + "x_min": 203, + "y_max": 834, + "x_max": 667, + } + ], +} + + MOCK_RECOGNISED_FACES = {"Idris Elba": 75.0} @@ -73,7 +94,7 @@ def test_DeepstackObject_detect(): """Test a good response from server.""" with requests_mock.Mocker() as mock_req: mock_req.post( - MOCK_URL, status_code=ds.HTTP_OK, json=MOCK_OBJECT_DETECTION_RESPONSE + OBJ_URL, status_code=ds.HTTP_OK, json=MOCK_OBJECT_DETECTION_RESPONSE ) dsobject = ds.DeepstackObject(MOCK_IP_ADDRESS, MOCK_PORT) @@ -85,13 +106,37 @@ def test_DeepstackObject_detect_timeout(): """Test a timeout. THIS SHOULD FAIL""" with pytest.raises(ds.DeepstackException) as excinfo: with requests_mock.Mocker() as mock_req: - mock_req.post(MOCK_URL, exc=requests.exceptions.ConnectTimeout) + mock_req.post(OBJ_URL, exc=requests.exceptions.ConnectTimeout) dsobject = ds.DeepstackObject(MOCK_IP_ADDRESS, MOCK_PORT) dsobject.detect(MOCK_BYTES) assert False assert "SHOULD FAIL" in str(excinfo.value) +def test_DeepstackScene(): + """Test a good response from server.""" + with requests_mock.Mocker() as mock_req: + mock_req.post(SCENE_URL, status_code=ds.HTTP_OK, json=MOCK_SCENE_RESPONSE) + + dsscene = ds.DeepstackScene(MOCK_IP_ADDRESS, MOCK_PORT) + dsscene.detect(MOCK_BYTES) + assert dsscene.predictions == MOCK_SCENE_RESPONSE + + +def test_DeepstackFace(): + """Test a good response from server.""" + with requests_mock.Mocker() as mock_req: + mock_req.post( + FACE_DETECTION_URL, + status_code=ds.HTTP_OK, + json=MOCK_FACE_DETECTION_RESPONSE, + ) + + dsface = ds.DeepstackFace(MOCK_IP_ADDRESS, MOCK_PORT) + dsface.detect(MOCK_BYTES) + assert dsface.predictions == MOCK_FACE_DETECTION_RESPONSE["predictions"] + + def test_get_objects(): """Cant always be sure order of returned list items.""" objects = ds.get_objects(MOCK_OBJECT_PREDICTIONS)