/
kikinaski
/
SE_FastAPI_Example
Обзор
Документация
Войти
/
kikinaski
/
SE_FastAPI_Example
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
release/2
tests/test_main.py
83 строки
2 KB
Tikhomirov
merge main into fix/test-init
14 май 2026, 17:36
14 май 2026, 17:36
dc23631
Код
Авторство
О чём код?
import importlib import sys import types import pytest from fastapi.testclient import TestClient class FakeClassifier: def __init__(self): self.calls = [] def __call__(self, text): self.calls.append(text) return [{"label": "POSITIVE", "score": 0.99, "text": text}] @pytest.fixture() def app_module(monkeypatch): fake_classifier = FakeClassifier() def fake_pipeline(task): assert task == "sentiment-analysis" return fake_classifier monkeypatch.setitem( sys.modules, "transformers", types.SimpleNamespace(pipeline=fake_pipeline), ) sys.modules.pop("main", None) module = importlib.import_module("main") yield module sys.modules.pop("main", None) @pytest.fixture() def client(app_module): return TestClient(app_module.app) def test_root_returns_service_status(client): response = client.get("/") assert response.status_code == 200 assert response.json() == ["FastApi service started!"] def test_get_params_returns_classifier_result(client, app_module): response = client.get("/great") assert response.status_code == 200 assert response.json() == [ {"label": "POSITIVE", "score": 0.99, "text": "great"} ] assert app_module.classifier.calls == ["great"] def test_predict_returns_classifier_result(client, app_module): response = client.post("/predict/", json={"text": "nice text"}) assert response.status_code == 200 assert response.json() == [ {"label": "POSITIVE", "score": 0.99, "text": "nice text"} ] assert app_module.classifier.calls == ["nice text"] @pytest.mark.parametrize("text", ["", " "]) def test_predict_rejects_empty_text(client, text): response = client.post("/predict/", json={"text": text}) assert response.status_code == 422 assert "Text cannot be empty" in response.text def test_predict_rejects_too_long_text(client): response = client.post("/predict/", json={"text": "a" * 1001}) assert response.status_code == 422 assert "Text is too long" in response.text