2022-01-06 05:12:58 +08:00
|
|
|
"""Contains tests for networking.py and app.py"""
|
|
|
|
|
2022-01-05 01:58:37 +08:00
|
|
|
import os
|
2021-11-13 14:33:59 +08:00
|
|
|
|
2022-01-05 01:58:37 +08:00
|
|
|
from fastapi.testclient import TestClient
|
2021-11-09 22:48:55 +08:00
|
|
|
|
2022-05-10 09:05:30 +08:00
|
|
|
from gradio import Interface, networking
|
2021-10-22 04:02:52 +08:00
|
|
|
|
2022-01-05 01:58:37 +08:00
|
|
|
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
2021-10-22 04:02:52 +08:00
|
|
|
|
2021-10-24 16:56:54 +08:00
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestInterfaceErrors:
|
2022-04-27 15:32:57 +08:00
|
|
|
def test_processing_error(self):
|
2021-11-21 07:45:13 +08:00
|
|
|
io = Interface(lambda x: 1 / x, "number", "number")
|
2021-10-26 20:57:29 +08:00
|
|
|
app, _, _ = io.launch(show_error=True, prevent_thread_lock=True)
|
2022-01-05 01:58:37 +08:00
|
|
|
client = TestClient(app)
|
2022-05-10 09:05:30 +08:00
|
|
|
response = client.post("/api/predict/", json={"data": [0], "fn_index": 0})
|
2022-11-08 08:37:55 +08:00
|
|
|
assert response.status_code == 500
|
|
|
|
assert "error" in response.json()
|
2021-10-26 20:57:29 +08:00
|
|
|
io.close()
|
2022-02-28 05:47:12 +08:00
|
|
|
|
2022-04-27 15:32:57 +08:00
|
|
|
def test_validation_error(self):
|
|
|
|
io = Interface(lambda x: 1 / x, "number", "number")
|
|
|
|
app, _, _ = io.launch(show_error=True, prevent_thread_lock=True)
|
|
|
|
client = TestClient(app)
|
2022-05-10 09:05:30 +08:00
|
|
|
response = client.post("/api/predict/", json={"fn_index": [0]})
|
2022-11-08 08:37:55 +08:00
|
|
|
assert response.status_code == 422
|
2022-04-27 15:32:57 +08:00
|
|
|
io.close()
|
|
|
|
|
2022-02-28 05:47:12 +08:00
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestURLs:
|
2022-02-10 13:10:00 +08:00
|
|
|
def test_url_ok(self):
|
2021-10-27 05:36:11 +08:00
|
|
|
res = networking.url_ok("https://www.gradio.app")
|
2022-11-08 08:37:55 +08:00
|
|
|
assert res
|
2023-05-20 17:10:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_start_server_app_kwargs():
|
|
|
|
"""
|
|
|
|
Test that start_server accepts app_kwargs and they're propagated to FastAPI.
|
|
|
|
"""
|
|
|
|
io = Interface(lambda x: x, "number", "number")
|
|
|
|
app, _, _ = io.launch(
|
|
|
|
show_error=True,
|
|
|
|
prevent_thread_lock=True,
|
|
|
|
app_kwargs={
|
|
|
|
"docs_url": "/docs",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
client = TestClient(app)
|
|
|
|
assert client.get("/docs").status_code == 200
|
|
|
|
io.close()
|