Skip to content

Commit

Permalink
More formatting fixes, add tests for not found and fail exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelltaylor committed Apr 11, 2024
1 parent 79e2115 commit 0169ab4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
4 changes: 2 additions & 2 deletions florist/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ def check_status(client_uuid: str, redis_host: str, redis_port: str) -> JSONResp
redis_connection = redis.Redis(host=redis_host, port=redis_port)

result = redis_connection.get(client_uuid)
assert isinstance(result, bytes)

if result is not None:
assert isinstance(result, bytes)
return JSONResponse(json.loads(result))

return JSONResponse({"error": f"Client {client_uuid} Not Found"}, status_code=404)

except Exception as ex:
return JSONResponse({"error": str(ex)}, status_code=500)
return JSONResponse({"error": str(ex)}, status_code=500)
23 changes: 3 additions & 20 deletions florist/tests/integration/api/test_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,9 @@

def test_train():
# Define services
server_config = uvicorn.Config(
"florist.api.server:app",
host="localhost",
port=8000,
log_level="debug"
)
server_config = uvicorn.Config("florist.api.server:app", host="localhost", port=8000, log_level="debug")
server_service = TestUvicornServer(config=server_config)
client_config = uvicorn.Config(
"florist.api.client:app",
host="localhost",
port=8001,
log_level="debug"
)
client_config = uvicorn.Config("florist.api.client:app", host="localhost", port=8001, log_level="debug")
client_service = TestUvicornServer(config=client_config)

# Start services
Expand Down Expand Up @@ -74,14 +64,7 @@ def test_train():
client_uuid = response.json()["client_uuids"][0]

# Wait for training to finish
wait_for_metric(
server_uuid,
"fit_end",
test_redis_host,
test_redis_port,
LOGGER,
max_retries=80
)
wait_for_metric(server_uuid, "fit_end", test_redis_host,test_redis_port, LOGGER, max_retries=80)

# Check server metrics
server_metrics_result = redis_conn.get(server_uuid)
Expand Down
30 changes: 30 additions & 0 deletions florist/tests/unit/api/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,34 @@ def test_check_status(mock_redis: Mock) -> None:

response = client.check_status(test_uuid, test_redis_host, test_redis_port)

mock_redis.Redis.assert_called_with(host=test_redis_host, port=test_redis_port)
assert json.loads(response.body.decode()) == {"info": "test"}

@patch("florist.api.client.redis")
def test_check_status_not_found(mock_redis: Mock) -> None:
mock_redis_connection = Mock()
mock_redis_connection.get.return_value = None

test_uuid = "test_uuid"
test_redis_host = "localhost"
test_redis_port = "testport"

mock_redis.Redis.return_value = mock_redis_connection

response = client.check_status(test_uuid, test_redis_host, test_redis_port)

mock_redis.Redis.assert_called_with(host=test_redis_host, port=test_redis_port)
assert response.status_code == 404
assert json.loads(response.body.decode()) == {"error": f"Client {test_uuid} Not Found"}

@patch("florist.api.client.redis.Redis", side_effect=Exception("test exception"))
def test_check_status_fail_exception(mock_redis: Mock) -> None:

test_uuid = "test_uuid"
test_redis_host = "localhost"
test_redis_port = "testport"

response = client.check_status(test_uuid, test_redis_host, test_redis_port)

assert response.status_code == 500
assert json.loads(response.body.decode()) == {"error": "test exception"}

0 comments on commit 0169ab4

Please sign in to comment.