/
liquid-g
/
liquid-code
Обзор
Документация
Войти
/
liquid-g
/
liquid-code
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop-0.2.0
tests/test_test_client.py
228 строк
6 KB
User
ci: настроен Black и flake8 для автоматического форматирования кода
04 июл 2026, 08:14
04 июл 2026, 08:14
4778ed6
Код
Авторство
О чём код?
"""Тесты для TestClient.""" import pytest from liquidcode import Application from liquidcode.test import TestClient, TestResponse def test_test_client_get(): """GET-запрос через TestClient.""" app = Application() @app.route("/users") def get_users(): return {"users": ["Alice"]} client = TestClient(app) response = client.get("/users") assert response.status == 200 assert response.json() == {"users": ["Alice"]} def test_test_client_post_json(): """POST-запрос с JSON-телом.""" app = Application() @app.route("/users", methods=["POST"]) def create_user(request): data = request.get_attr("parsed_body") return {"created": data} client = TestClient(app) response = client.post("/users", body={"name": "Bob"}) assert response.status == 200 assert response.json() == {"created": {"name": "Bob"}} def test_test_client_with_query_params(): """Запрос с query-параметрами.""" app = Application() @app.route("/search") def search(request): query = request.get_query_params() return {"query": query} client = TestClient(app) response = client.get("/search?q=test", query_params={"page": "1"}) assert response.status == 200 assert response.json() == {"query": {"q": "test", "page": "1"}} class TestTestClient: """Тесты TestClient.""" def test_test_client_init(self): """Инициализация TestClient.""" app = Application() client = TestClient(app) assert client.app == app assert client.kernel is not None def test_test_client_request_method(self): """Метод request() для всех HTTP-методов.""" app = Application() @app.route("/test", methods=["GET", "POST", "PUT", "PATCH", "DELETE"]) def handler(request): return {"method": request.get_method()} client = TestClient(app) assert client.get("/test").status == 200 assert client.post("/test").status == 200 assert client.put("/test").status == 200 assert client.patch("/test").status == 200 assert client.delete("/test").status == 200 def test_test_client_text_method(self): """Метод text() для строкового ответа.""" app = Application() @app.route("/text") def text_handler(): return "Hello, World!" client = TestClient(app) response = client.get("/text") assert response.text() == "Hello, World!" def test_test_client_json_method_with_dict(self): """Метод json() с dict ответом.""" app = Application() @app.route("/json") def json_handler(): return {"key": "value"} client = TestClient(app) response = client.get("/json") assert response.json() == {"key": "value"} def test_test_client_json_method_with_str(self): """Метод json() с строковым JSON.""" app = Application() @app.route("/json-str") def json_str_handler(): return '{"key": "value"}' client = TestClient(app) response = client.get("/json-str") assert response.json() == {"key": "value"} def test_test_client_json_method_with_bytes(self): """Метод json() с байтовым JSON.""" app = Application() @app.route("/json-bytes") def json_bytes_handler(): return b'{"key": "value"}' client = TestClient(app) response = client.get("/json-bytes") assert response.json() == {"key": "value"} def test_test_client_headers(self): """Заголовки ответа.""" app = Application() @app.route("/headers") def headers_handler(): return {"status": "ok"} client = TestClient(app) response = client.get("/headers") # Проверяем, что заголовки существуют (Content-Type должен быть) assert "Content-Type" in response.headers def test_test_client_status_code(self): """Статус код ответа.""" app = Application() @app.route("/ok") def ok_handler(): return {"status": "ok"} client = TestClient(app) response = client.get("/ok") assert response.status == 200 def test_test_client_empty_body(self): """Запрос с пустым телом.""" app = Application() @app.route("/echo", methods=["POST"]) def echo_handler(): return {"echo": "ok"} client = TestClient(app) response = client.post("/echo", body={}) assert response.status == 200 def test_test_client_custom_headers(self): """Запрос с кастомными заголовками.""" app = Application() @app.route("/custom-headers") def custom_headers_handler(request): return {"headers": dict(request.get_headers())} client = TestClient(app) response = client.get("/custom-headers", headers={"X-API-Key": "secret"}) assert "X-API-Key" in response.json()["headers"] class TestTestResponse: """Тесты TestResponse.""" def test_test_response_init(self): """Инициализация TestResponse.""" app = Application() @app.route("/test") def handler(): return "OK" client = TestClient(app) response = client.get("/test") assert response.status == 200 def test_test_response_content_property(self): """Свойство content.""" app = Application() @app.route("/content") def handler(): return {"data": "test"} client = TestClient(app) response = client.get("/content") assert response.content is not None def test_test_response_text_method_bytes(self): """Метод text() с байтами.""" app = Application() @app.route("/bytes") def handler(): return b"Hello" client = TestClient(app) response = client.get("/bytes") assert response.text() == "Hello"