Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
testpaths = tests
python_files = test_*.py
19 changes: 19 additions & 0 deletions src/tests/tests_factcheck_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# tests/test_factcheck_endpoint.py

import pytest
from httpx import AsyncClient
from app import app # Adjust the import based on your project structure

@pytest.mark.asyncio
async def test_factcheck_endpoint():
async with AsyncClient(app=app, base_url="http://test") as ac:
payload = {
"claim": "The Eiffel Tower is located in Berlin."
}
response = await ac.post("/factcheck", json=payload)
assert response.status_code == 200
data = response.json()
assert "verdict" in data
assert "evidence" in data
assert data["verdict"] in ["true", "false", "uncertain"]

12 changes: 12 additions & 0 deletions src/tests/tests_health_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# tests/test_health_endpoint.py

import pytest
from httpx import AsyncClient
from app import app

@pytest.mark.asyncio
async def test_health_endpoint():
async with AsyncClient(app=app, base_url="http://test") as ac:
response = await ac.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
17 changes: 17 additions & 0 deletions src/tests/tests_search_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# tests/test_search_endpoint.py

import pytest
from httpx import AsyncClient
from app import app

@pytest.mark.asyncio
async def test_search_endpoint():
async with AsyncClient(app=app, base_url="http://test") as ac:
params = {"query": "climate change"}
response = await ac.get("/search", params=params)
assert response.status_code == 200
data = response.json()
assert isinstance(data, list)
for item in data:
assert "title" in item
assert "url" in item
Comment on lines +7 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test coverage and add documentation.

The test implementation is solid but could benefit from several improvements:

  1. Missing docstring (as flagged by static analysis)
  2. Limited test scenarios - only tests the happy path
  3. Hard-coded test data - consider parameterizing
  4. Basic validation - could be more comprehensive
 @pytest.mark.asyncio
 async def test_search_endpoint():
+    """Test the /search endpoint returns expected JSON structure with search results."""
     async with AsyncClient(app=app, base_url="http://test") as ac:
         params = {"query": "climate change"}
         response = await ac.get("/search", params=params)
         assert response.status_code == 200
         data = response.json()
         assert isinstance(data, list)
         for item in data:
             assert "title" in item
+            assert isinstance(item["title"], str)
             assert "url" in item
+            assert isinstance(item["url"], str)
+            assert item["url"].startswith(("http://", "https://"))

Consider adding these additional test cases:

@pytest.mark.asyncio
@pytest.mark.parametrize("query", ["climate change", "AI technology", ""])
async def test_search_endpoint_various_queries(query):
    """Test search endpoint with different query parameters."""
    async with AsyncClient(app=app, base_url="http://test") as ac:
        params = {"query": query}
        response = await ac.get("/search", params=params)
        assert response.status_code == 200
        data = response.json()
        assert isinstance(data, list)

@pytest.mark.asyncio
async def test_search_endpoint_missing_query():
    """Test search endpoint behavior when query parameter is missing."""
    async with AsyncClient(app=app, base_url="http://test") as ac:
        response = await ac.get("/search")
        # Assert expected behavior (400, 422, or default search results)
        assert response.status_code in [200, 400, 422]
🧰 Tools
🪛 Ruff (0.11.9)

8-8: Missing docstring in public function

(D103)

🤖 Prompt for AI Agents
In src/tests/tests_search_endpoint.py around lines 7 to 17, the test lacks a
docstring, only covers the happy path with hard-coded data, and performs basic
validation. Add a descriptive docstring to the existing test function. Introduce
parameterized tests to cover multiple query inputs including empty strings. Add
a separate test to verify the endpoint's behavior when the query parameter is
missing, asserting for expected status codes like 200, 400, or 422. Expand
assertions to validate response content more comprehensively.