mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-03 22:13:01 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
84 lines
2.7 KiB
Python
84 lines
2.7 KiB
Python
import pytest
|
|
from docsgpt.error import bad_request, response_error, sanitize_api_error
|
|
from flask import Flask
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
app = Flask(__name__)
|
|
return app
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_bad_request_with_message(app):
|
|
with app.app_context():
|
|
message = "Invalid input"
|
|
response = bad_request(status_code=400, message=message)
|
|
assert response.status_code == 400
|
|
assert response.json == {"error": "Bad Request", "message": message}
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_bad_request_without_message(app):
|
|
with app.app_context():
|
|
response = bad_request(status_code=400)
|
|
assert response.status_code == 400
|
|
assert response.json == {"error": "Bad Request"}
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_response_error_with_message(app):
|
|
with app.app_context():
|
|
message = "Something went wrong"
|
|
response = response_error(code_status=500, message=message)
|
|
assert response.status_code == 500
|
|
assert response.json == {"error": "Internal Server Error", "message": message}
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_response_error_without_message(app):
|
|
with app.app_context():
|
|
response = response_error(code_status=500)
|
|
assert response.status_code == 500
|
|
assert response.json == {"error": "Internal Server Error"}
|
|
|
|
|
|
@pytest.mark.unit
|
|
class TestSanitizeApiError:
|
|
|
|
def test_503_unavailable(self):
|
|
assert "temporarily unavailable" in sanitize_api_error("503 Service Unavailable")
|
|
|
|
def test_high_demand(self):
|
|
assert "temporarily unavailable" in sanitize_api_error("high demand")
|
|
|
|
def test_429_rate_limit(self):
|
|
assert "Rate limit" in sanitize_api_error("429 Too Many Requests")
|
|
|
|
def test_quota_exceeded(self):
|
|
assert "Rate limit" in sanitize_api_error("Quota exceeded")
|
|
|
|
def test_401_unauthorized(self):
|
|
assert "Authentication" in sanitize_api_error("401 Unauthorized")
|
|
|
|
def test_invalid_api_key(self):
|
|
assert "Authentication" in sanitize_api_error("Invalid API key provided")
|
|
|
|
def test_timeout(self):
|
|
assert "timed out" in sanitize_api_error("Request timed out")
|
|
|
|
def test_connection_error(self):
|
|
assert "Network" in sanitize_api_error("Connection refused")
|
|
|
|
def test_long_message_sanitized(self):
|
|
assert "error occurred" in sanitize_api_error("x" * 201)
|
|
|
|
def test_traceback_sanitized(self):
|
|
assert "error occurred" in sanitize_api_error("Traceback (most recent call)")
|
|
|
|
def test_json_sanitized(self):
|
|
assert "error occurred" in sanitize_api_error('{"error": "something"}')
|
|
|
|
def test_short_safe_message_passed_through(self):
|
|
assert sanitize_api_error("Something broke") == "Something broke"
|