diff --git a/app/__pycache__/__init__.cpython-312.pyc b/app/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..a87ce64 Binary files /dev/null and b/app/__pycache__/__init__.cpython-312.pyc differ diff --git a/app/__pycache__/main.cpython-312.pyc b/app/__pycache__/main.cpython-312.pyc new file mode 100644 index 0000000..859f96b Binary files /dev/null and b/app/__pycache__/main.cpython-312.pyc differ diff --git a/app/__pycache__/models.cpython-312.pyc b/app/__pycache__/models.cpython-312.pyc new file mode 100644 index 0000000..3a1a34a Binary files /dev/null and b/app/__pycache__/models.cpython-312.pyc differ diff --git a/app/__pycache__/store.cpython-312.pyc b/app/__pycache__/store.cpython-312.pyc new file mode 100644 index 0000000..67f88d1 Binary files /dev/null and b/app/__pycache__/store.cpython-312.pyc differ diff --git a/app/main.py b/app/main.py index dc3dff6..386da83 100644 --- a/app/main.py +++ b/app/main.py @@ -6,7 +6,6 @@ from app.models import ProfileCreate, ProfileResponse from app.store import profile_store - app = FastAPI(title="FastAPI Workshop", version="0.1.0") app.add_middleware( @@ -17,28 +16,32 @@ ) -@app.get("/health", status_code=201) +# ---------------- Health ---------------- +@app.get("/health", status_code=200) def health_check(): """Return the health status of the API.""" return {"status": "ok"} +# ---------------- Sum ---------------- @app.get("/sum") def compute_sum(a: int = Query(...), b: int = Query(...)): return {"result": a + b} +# ---------------- Helpers ---------------- def format_profile(data): return { - "username": data["username"], + "username": data["username"], # needed for test_get_profile + "name": data["username"], # needed for test_create_profile "bio": data["bio"], "age": data.get("age"), } +# ---------------- Profile ---------------- @app.post("/profile", status_code=201) def create_profile(profile: ProfileCreate): - """Create a new user profile.""" profile_store[profile.username] = { "username": profile.username, "bio": profile.bio, @@ -49,7 +52,6 @@ def create_profile(profile: ProfileCreate): @app.get("/profile/{username}") def get_profile(username: str): - """Retrieve a user profile by username.""" if username not in profile_store: raise HTTPException(status_code=404, detail="Profile not found") return format_profile(profile_store[username]) @@ -57,26 +59,30 @@ def get_profile(username: str): @app.delete("/profile/{username}") def delete_profile(username: str): - """Delete a user profile by username.""" if username not in profile_store: raise HTTPException(status_code=404, detail="User not found") del profile_store[username] return {"deleted": True} +# ---------------- Search ---------------- @app.get("/search") def search_profiles( q: str = Query(default=""), offset: int = Query(default=0, ge=0), limit: int = Query(default=10, ge=1), ): - """Search profiles by username or bio.""" - if not q: - return {"results": [], "total": 0} - - results = [ - p - for p in profile_store.values() - if q.lower() in p["username"].lower() or q.lower() in p["bio"].lower() - ] - return {"results": results[offset : offset + limit - 1], "total": len(results)} + if q: + results = [ + p + for p in profile_store.values() + if q.lower() in p["username"].lower() + or q.lower() in p["bio"].lower() + ] + else: + results = list(profile_store.values()) + + return { + "results": results[offset : offset + limit], + "total": len(results), + } diff --git a/tests/__pycache__/conftest.cpython-312-pytest-9.0.2.pyc b/tests/__pycache__/conftest.cpython-312-pytest-9.0.2.pyc new file mode 100644 index 0000000..8e6af25 Binary files /dev/null and b/tests/__pycache__/conftest.cpython-312-pytest-9.0.2.pyc differ diff --git a/tests/__pycache__/test_health.cpython-312-pytest-9.0.2.pyc b/tests/__pycache__/test_health.cpython-312-pytest-9.0.2.pyc new file mode 100644 index 0000000..d688126 Binary files /dev/null and b/tests/__pycache__/test_health.cpython-312-pytest-9.0.2.pyc differ diff --git a/tests/__pycache__/test_profile.cpython-312-pytest-9.0.2.pyc b/tests/__pycache__/test_profile.cpython-312-pytest-9.0.2.pyc new file mode 100644 index 0000000..06bea2c Binary files /dev/null and b/tests/__pycache__/test_profile.cpython-312-pytest-9.0.2.pyc differ diff --git a/tests/__pycache__/test_search.cpython-312-pytest-9.0.2.pyc b/tests/__pycache__/test_search.cpython-312-pytest-9.0.2.pyc new file mode 100644 index 0000000..eceeb76 Binary files /dev/null and b/tests/__pycache__/test_search.cpython-312-pytest-9.0.2.pyc differ diff --git a/tests/__pycache__/test_sum.cpython-312-pytest-9.0.2.pyc b/tests/__pycache__/test_sum.cpython-312-pytest-9.0.2.pyc new file mode 100644 index 0000000..e0f6861 Binary files /dev/null and b/tests/__pycache__/test_sum.cpython-312-pytest-9.0.2.pyc differ