Added URL stats retrieval with tests
This commit is contained in:
+20
-1
@@ -103,4 +103,23 @@ def redirect_to_url(short_code: str):
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return RedirectResponse(url=original_url)
|
||||
return RedirectResponse(url=original_url)
|
||||
|
||||
@app.get("/stats/{short_code}")
|
||||
def get_url_stats(short_code: str):
|
||||
logger.info(f"Stats request received for short_code: {short_code}")
|
||||
conn = get_db_connection()
|
||||
url_data = conn.execute("SELECT original_url, clicks FROM urls WHERE short_code = ?", (short_code,)).fetchone()
|
||||
|
||||
if url_data is None:
|
||||
logger.warning(f"Stats not found for {short_code}.")
|
||||
conn.close()
|
||||
raise HTTPException(status_code=404, detail="URL stats not found")
|
||||
|
||||
original_url, clicks = url_data
|
||||
|
||||
conn.close()
|
||||
|
||||
logger.info(f"Returning stats for {original_url}. Total clicks: {clicks}")
|
||||
|
||||
return {"original_url": original_url, "clicks": clicks}
|
||||
+9
-1
@@ -4,10 +4,18 @@ import os
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
|
||||
from app.main import app
|
||||
from app.main import settings
|
||||
import tempfile
|
||||
import pytest
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def use_temp_db():
|
||||
with tempfile.NamedTemporaryFile() as tmp:
|
||||
settings.database_url = tmp.name
|
||||
yield
|
||||
|
||||
def test_home():
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
|
||||
Reference in New Issue
Block a user