Spaces:
Runtime error
Runtime error
Download tests/test_stats.py from cloudaiRambo/cloudairambo: direct link, hf CLI and curl.
- Browser
- Download file 2.96 kB
-
https://huggingface.co/spaces/cloudaiRambo/cloudairambo/resolve/main/tests/test_stats.py
- Command line
-
hf download hf://spaces/cloudaiRambo/cloudairambo/tests/test_stats.py
-
curl -L -o test_stats.py https://huggingface.co/spaces/cloudaiRambo/cloudairambo/resolve/main/tests/test_stats.py
2.96 kB
| import pytest | |
| from core.models import ToolUsage | |
| from sqlalchemy.orm import Session | |
| # You will need to import the admin token generator or manually get an admin token | |
| # from your admin_auth.py router for the authenticated request. | |
| def test_tool_usage_logging_and_retrieval(client, db: Session): | |
| # 1. SETUP: Find initial usage count for a specific tool | |
| TEST_TOOL = "pdf_merge" | |
| # Check current count in the database | |
| initial_count = db.query(ToolUsage).filter( | |
| ToolUsage.tool_name == TEST_TOOL | |
| ).count() | |
| # 2. ACTION: Simulate a successful request to the tool endpoint | |
| # NOTE: The client needs to send form-data or files as required by the endpoint. | |
| # We use a POST request that triggers the LogToolUsage dependency. | |
| # Since /pdf/merge takes files, we simulate the file upload: | |
| # (This is a simplified example, actual FastAPI testing requires a BytesIO object) | |
| # --- Actual Code Execution (Simulated Tool Call) --- | |
| response = client.post( | |
| "/pdf/merge", | |
| files=[ | |
| ("files", ("file1.pdf", b"pdf_content_1", "application/pdf")), | |
| ("files", ("file2.pdf", b"pdf_content_2", "application/pdf")), | |
| ], | |
| # Add necessary headers, like the admin token, if the endpoint was protected | |
| # but here we test the logging on the public endpoint first. | |
| ) | |
| # Check that the tool execution was successful (e.g., returns a file) | |
| assert response.status_code == 200 | |
| # 3. VERIFY LOGGING: Check the database directly | |
| # Check the count again after the request | |
| new_count = db.query(ToolUsage).filter( | |
| ToolUsage.tool_name == TEST_TOOL | |
| ).count() | |
| # The count should have increased by exactly 1 | |
| assert new_count == initial_count + 1 | |
| # 4. VERIFY RETRIEVAL (The new stats router) | |
| # ⚠️ For this step, you must get an admin token first | |
| # Simulate admin login to get a token | |
| auth_response = client.post("/admin/login", data={"password": "YOUR_ADMIN_SECRET"}) | |
| admin_token = auth_response.json().get("token") | |
| # Request the statistics endpoint (which requires the admin token) | |
| stats_response = client.get( | |
| "/admin/stats/tool_usage", | |
| headers={"Authorization": f"Bearer {admin_token}"} | |
| ) | |
| # Check that the stats endpoint is accessible and returns data | |
| assert stats_response.status_code == 200 | |
| stats_data = stats_response.json().get("data", []) | |
| # 5. FINAL VERIFICATION: Check the stats data for the updated count | |
| # Find the statistics for our test tool | |
| tool_stat = next((item for item in stats_data if item["tool_name"] == TEST_TOOL), None) | |
| # Check that the tool exists in the returned stats and the count matches the database | |
| assert tool_stat is not None | |
| assert tool_stat["count"] == new_count | |
| print(f"Test Passed: {TEST_TOOL} count increased from {initial_count} to {new_count}") | |