Refactoring file structure, not tested yet
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
# tests/conftest.py
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.main import app
|
||||
from app.db.base import Base
|
||||
from app.db.session import get_db
|
||||
|
||||
|
||||
# Use in-memory SQLite for tests
|
||||
TEST_DATABASE_URL = "sqlite+pysqlite:///:memory:"
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_session():
|
||||
"""
|
||||
Creates a new database session for a test.
|
||||
Rolls back everything after test finishes.
|
||||
"""
|
||||
|
||||
engine = create_engine(
|
||||
TEST_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False},
|
||||
)
|
||||
|
||||
TestingSessionLocal = sessionmaker(
|
||||
autocommit=False,
|
||||
autoflush=False,
|
||||
bind=engine,
|
||||
)
|
||||
|
||||
# Create tables
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
db = TestingSessionLocal()
|
||||
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Base.metadata.drop_all(bind=engine)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def client(db_session):
|
||||
"""
|
||||
Overrides get_db dependency to use test database.
|
||||
"""
|
||||
|
||||
def override_get_db():
|
||||
try:
|
||||
yield db_session
|
||||
finally:
|
||||
pass
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
|
||||
app.dependency_overrides.clear()
|
||||
@@ -0,0 +1,93 @@
|
||||
# tests/test_routes.py
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_home(client):
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"message": "URL Shortener API"}
|
||||
|
||||
|
||||
def test_health(client):
|
||||
response = client.get("/health")
|
||||
assert response.status_code == 200
|
||||
assert response.json() == {"message": "healthy"}
|
||||
|
||||
|
||||
def test_shorten_url(client):
|
||||
response = client.post(
|
||||
"/shorten",
|
||||
json={"url": "https://google.com"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
|
||||
assert "short_url" in data
|
||||
assert data["short_url"].startswith("http://")
|
||||
|
||||
|
||||
def test_shorten_invalid_url(client):
|
||||
response = client.post(
|
||||
"/shorten",
|
||||
json={"url": "not-a-valid-url"},
|
||||
)
|
||||
|
||||
assert response.status_code == 422 # Pydantic validation error
|
||||
|
||||
|
||||
def test_duplicate_url_returns_same_code(client):
|
||||
r1 = client.post("/shorten", json={"url": "https://example.com"})
|
||||
r2 = client.post("/shorten", json={"url": "https://example.com"})
|
||||
|
||||
assert r1.status_code == 200
|
||||
assert r2.status_code == 200
|
||||
|
||||
assert r1.json()["short_url"] == r2.json()["short_url"]
|
||||
|
||||
|
||||
def test_redirect_success(client):
|
||||
shorten = client.post(
|
||||
"/shorten",
|
||||
json={"url": "https://redirect-test.com"},
|
||||
)
|
||||
|
||||
short_url = shorten.json()["short_url"]
|
||||
code = short_url.split("/")[-1]
|
||||
|
||||
response = client.get(f"/{code}", follow_redirects=False)
|
||||
|
||||
assert response.status_code in (302, 307)
|
||||
|
||||
|
||||
def test_redirect_404(client):
|
||||
response = client.get("/nonexistent", follow_redirects=False)
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_stats_success(client):
|
||||
shorten = client.post(
|
||||
"/shorten",
|
||||
json={"url": "https://stats-test.com"},
|
||||
)
|
||||
|
||||
short_url = shorten.json()["short_url"]
|
||||
code = short_url.split("/")[-1]
|
||||
|
||||
# Trigger one click
|
||||
client.get(f"/{code}", follow_redirects=False)
|
||||
|
||||
stats = client.get(f"/stats/{code}")
|
||||
|
||||
assert stats.status_code == 200
|
||||
data = stats.json()
|
||||
|
||||
assert data["original_url"] == "https://stats-test.com"
|
||||
assert data["clicks"] == 1
|
||||
assert "created_at" in data
|
||||
|
||||
|
||||
def test_stats_404(client):
|
||||
response = client.get("/stats/doesnotexist")
|
||||
assert response.status_code == 404
|
||||
Reference in New Issue
Block a user