Spaces:
Sleeping
Sleeping
feat: add initial test files for health check and YouTube analysis endpoints
Browse files- tests/__init__.py +0 -0
- tests/conftest.py +14 -0
- tests/test_health.py +24 -0
- tests/test_youtube.py +29 -0
tests/__init__.py
ADDED
|
File without changes
|
tests/conftest.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Shared test fixtures for CrownCode backend tests."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import pytest
|
| 6 |
+
from fastapi.testclient import TestClient
|
| 7 |
+
|
| 8 |
+
from app.main import app
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@pytest.fixture()
|
| 12 |
+
def client() -> TestClient:
|
| 13 |
+
"""Create a test client for the FastAPI application."""
|
| 14 |
+
return TestClient(app)
|
tests/test_health.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for health check and root endpoints."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from fastapi.testclient import TestClient
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_root_returns_api_info(client: TestClient) -> None:
|
| 9 |
+
"""Root endpoint should return API name, version, and status."""
|
| 10 |
+
response = client.get("/")
|
| 11 |
+
assert response.status_code == 200
|
| 12 |
+
data = response.json()
|
| 13 |
+
assert data["name"] == "CrownCode Backend API"
|
| 14 |
+
assert data["status"] == "running"
|
| 15 |
+
assert "version" in data
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_health_endpoint(client: TestClient) -> None:
|
| 19 |
+
"""Health endpoint should return ok status with services info."""
|
| 20 |
+
response = client.get("/api/health")
|
| 21 |
+
assert response.status_code == 200
|
| 22 |
+
data = response.json()
|
| 23 |
+
assert data["status"] == "ok"
|
| 24 |
+
assert "services" in data
|
tests/test_youtube.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for YouTube analysis endpoint."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from fastapi.testclient import TestClient
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_youtube_analyze_rejects_invalid_url(client: TestClient) -> None:
|
| 9 |
+
"""YouTube analyze should reject a non-YouTube URL."""
|
| 10 |
+
response = client.post(
|
| 11 |
+
"/api/youtube/analyze",
|
| 12 |
+
json={"url": "https://example.com/not-youtube"},
|
| 13 |
+
)
|
| 14 |
+
assert response.status_code in (400, 422)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def test_youtube_analyze_rejects_empty_url(client: TestClient) -> None:
|
| 18 |
+
"""YouTube analyze should reject an empty URL."""
|
| 19 |
+
response = client.post(
|
| 20 |
+
"/api/youtube/analyze",
|
| 21 |
+
json={"url": ""},
|
| 22 |
+
)
|
| 23 |
+
assert response.status_code in (400, 422)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_youtube_analyze_rejects_missing_body(client: TestClient) -> None:
|
| 27 |
+
"""YouTube analyze should reject a request with no body."""
|
| 28 |
+
response = client.post("/api/youtube/analyze")
|
| 29 |
+
assert response.status_code == 422
|