mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-04 20:13:04 +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.
111 lines
3.7 KiB
Python
111 lines
3.7 KiB
Python
"""Tests for connector base classes."""
|
|
|
|
|
|
import pytest
|
|
|
|
from docsgpt.parser.connectors.base import BaseConnectorAuth, BaseConnectorLoader
|
|
from docsgpt.parser.schema.base import Document
|
|
|
|
|
|
class ConcreteAuth(BaseConnectorAuth):
|
|
"""Minimal concrete implementation for testing the ABC."""
|
|
|
|
def get_authorization_url(self, state=None):
|
|
return f"https://example.com/auth?state={state}"
|
|
|
|
def exchange_code_for_tokens(self, authorization_code):
|
|
return {"access_token": "tok", "code": authorization_code}
|
|
|
|
def refresh_access_token(self, refresh_token):
|
|
return {"access_token": "new_tok", "refresh_token": refresh_token}
|
|
|
|
def is_token_expired(self, token_info):
|
|
return token_info.get("expired", False)
|
|
|
|
|
|
class ConcreteLoader(BaseConnectorLoader):
|
|
"""Minimal concrete implementation for testing the ABC."""
|
|
|
|
def __init__(self, session_token):
|
|
self.session_token = session_token
|
|
|
|
def load_data(self, inputs):
|
|
return [Document(text="test", doc_id="1", extra_info={})]
|
|
|
|
def download_to_directory(self, local_dir, source_config=None):
|
|
return {"files_downloaded": 0, "directory_path": local_dir}
|
|
|
|
|
|
class TestBaseConnectorAuth:
|
|
|
|
@pytest.mark.unit
|
|
def test_sanitize_token_info_extracts_standard_fields(self):
|
|
auth = ConcreteAuth()
|
|
token_info = {
|
|
"access_token": "at",
|
|
"refresh_token": "rt",
|
|
"token_uri": "https://token.uri",
|
|
"expiry": 12345,
|
|
"extra_field": "should_not_appear",
|
|
}
|
|
result = auth.sanitize_token_info(token_info)
|
|
assert result == {
|
|
"access_token": "at",
|
|
"refresh_token": "rt",
|
|
"token_uri": "https://token.uri",
|
|
"expiry": 12345,
|
|
}
|
|
|
|
@pytest.mark.unit
|
|
def test_sanitize_token_info_with_extra_kwargs(self):
|
|
auth = ConcreteAuth()
|
|
token_info = {
|
|
"access_token": "at",
|
|
"refresh_token": "rt",
|
|
"token_uri": "https://token.uri",
|
|
"expiry": 100,
|
|
}
|
|
result = auth.sanitize_token_info(token_info, custom_field="custom_val")
|
|
assert result["custom_field"] == "custom_val"
|
|
assert result["access_token"] == "at"
|
|
|
|
@pytest.mark.unit
|
|
def test_sanitize_token_info_missing_fields_returns_none(self):
|
|
auth = ConcreteAuth()
|
|
result = auth.sanitize_token_info({})
|
|
assert result["access_token"] is None
|
|
assert result["refresh_token"] is None
|
|
assert result["token_uri"] is None
|
|
assert result["expiry"] is None
|
|
|
|
@pytest.mark.unit
|
|
def test_abstract_methods_invocable_on_concrete(self):
|
|
auth = ConcreteAuth()
|
|
assert "example.com" in auth.get_authorization_url("s1")
|
|
assert auth.exchange_code_for_tokens("code1")["access_token"] == "tok"
|
|
assert auth.refresh_access_token("rt")["access_token"] == "new_tok"
|
|
assert auth.is_token_expired({"expired": True}) is True
|
|
assert auth.is_token_expired({"expired": False}) is False
|
|
|
|
|
|
class TestBaseConnectorLoader:
|
|
|
|
@pytest.mark.unit
|
|
def test_concrete_loader_init(self):
|
|
loader = ConcreteLoader("session123")
|
|
assert loader.session_token == "session123"
|
|
|
|
@pytest.mark.unit
|
|
def test_concrete_loader_load_data(self):
|
|
loader = ConcreteLoader("s")
|
|
docs = loader.load_data({})
|
|
assert len(docs) == 1
|
|
assert docs[0].text == "test"
|
|
|
|
@pytest.mark.unit
|
|
def test_concrete_loader_download_to_directory(self):
|
|
loader = ConcreteLoader("s")
|
|
result = loader.download_to_directory("/tmp/test")
|
|
assert result["directory_path"] == "/tmp/test"
|
|
assert result["files_downloaded"] == 0
|