-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When pipelines fail to load in framework code, for whatever reason
We need to display some error about what is happening.
- Loading branch information
Showing
4 changed files
with
105 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os | ||
from unittest import TestCase | ||
import logging | ||
from api_inference_community.routes import status_ok, pipeline_route | ||
from starlette.applications import Starlette | ||
from starlette.routing import Route | ||
from starlette.testclient import TestClient | ||
|
||
|
||
class ValidationTestCase(TestCase): | ||
def test_invalid_pipeline(self): | ||
os.environ["TASK"] = "invalid" | ||
|
||
def get_pipeline(): | ||
raise Exception("We cannot load the pipeline") | ||
|
||
routes = [ | ||
Route("/{whatever:path}", status_ok), | ||
Route("/{whatever:path}", pipeline_route, methods=["POST"]), | ||
] | ||
|
||
app = Starlette(routes=routes) | ||
|
||
@app.on_event("startup") | ||
async def startup_event(): | ||
logger = logging.getLogger("uvicorn.access") | ||
handler = logging.StreamHandler() | ||
handler.setFormatter( | ||
logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") | ||
) | ||
logger.handlers = [handler] | ||
|
||
# Link between `api-inference-community` and framework code. | ||
app.get_pipeline = get_pipeline | ||
|
||
with TestClient(app) as client: | ||
response = client.post("/", data=b"") | ||
self.assertEqual( | ||
response.status_code, | ||
500, | ||
) | ||
self.assertEqual(response.content, b'{"error":"We cannot load the pipeline"}') |