/
Euphonium
/
SE_FastAPI_Example
Обзор
Документация
Войти
/
Euphonium
/
SE_FastAPI_Example
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
1
CI/CD
Аналитика
Безопасность
main
tests/test_fastapi_endpoints.py
96 строк
3 KB
Ivanov Maxim
Add tests for FastAPI endpoints and models
13 май 2026, 12:16
13 май 2026, 12:16
e44c0da
Код
Авторство
О чём код?
import pytest class TestHealthEndpoint: def test_health_returns_ok(self, test_client): response = test_client.get("/health") assert response.status_code == 200 assert response.json() == {"status": "ok"} def test_health_response_format(self, test_client): response = test_client.get("/health") data = response.json() assert isinstance(data, dict) assert "status" in data assert isinstance(data["status"], str) class TestRootEndpoint: def test_root_returns_message(self, test_client): response = test_client.get("/") assert response.status_code == 200 data = response.json() assert "FastApi service started!" in str(data) def test_root_response_is_dict(self, test_client): response = test_client.get("/") assert isinstance(response.json(), dict) class TestPredictEndpoint: @pytest.mark.parametrize("text", [ "I love this!", "This is great!", "Amazing product!", ]) def test_predict_positive_sentiment(self, test_client, text): response = test_client.post("/predict/", json={"text": text}) assert response.status_code == 200 data = response.json() assert "results" in data assert isinstance(data["results"], list) @pytest.mark.parametrize("text", [ "I hate this!", "This is terrible!", "Very bad!", ]) def test_predict_negative_sentiment(self, test_client, text): response = test_client.post("/predict/", json={"text": text}) assert response.status_code == 200 data = response.json() assert "results" in data def test_predict_response_structure(self, test_client): response = test_client.post("/predict/", json={"text": "Test text"}) assert response.status_code == 200 data = response.json() if data.get("results"): result = data["results"][0] assert "label" in result assert "score" in result assert isinstance(result["score"], (int, float)) assert 0 <= result["score"] <= 1 def test_predict_empty_string(self, test_client): response = test_client.post("/predict/", json={"text": ""}) assert response.status_code in [200, 422] def test_predict_missing_text_field(self, test_client): response = test_client.post("/predict/", json={}) assert response.status_code == 422 # Validation error def test_predict_invalid_json(self, test_client): response = test_client.post("/predict/", data="not json") assert response.status_code == 422 def test_predict_text_too_long(self, test_client): long_text = "word " * 10000 response = test_client.post("/predict/", json={"text": long_text}) # Должен обработать или вернуть ошибку assert response.status_code in [200, 413, 422] @pytest.mark.parametrize("special_text", [ "Text with emoji 😊🎉", "<html>tags</html>", "Text with @mentions #hashtags", "Текст на русском языке", "混合语言", "Text with newlines\nand\ttabs", ]) def test_predict_special_characters(self, test_client, special_text): response = test_client.post("/predict/", json={"text": special_text}) assert response.status_code in [200, 422]