mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-18 12:13:46 +00:00
feat: prune some deps
This commit is contained in:
1 parent
c5b372df1e
commit
a166796f6c
26 files changed
+1937
-1343
No files matched your search
@@ -1,83 +1,103 @@
|
||||
import json
|
||||
from unittest.mock import patch, MagicMock
|
||||
import sys
|
||||
import types
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from application.parser.remote.reddit_loader import RedditPostsLoaderRemote
|
||||
|
||||
|
||||
def _post(post_id, title, body):
|
||||
post = MagicMock()
|
||||
post.id = post_id
|
||||
post.title = title
|
||||
post.selftext = body
|
||||
post.subreddit_name_prefixed = "r/python"
|
||||
post.score = 42
|
||||
post.url = f"https://reddit.com/{post_id}"
|
||||
post.author = "someone"
|
||||
return post
|
||||
|
||||
|
||||
def _fake_praw(posts):
|
||||
"""Install a stub ``praw`` module whose listings yield ``posts``."""
|
||||
praw = types.ModuleType("praw")
|
||||
reddit = MagicMock()
|
||||
listing = MagicMock()
|
||||
listing.new.return_value = posts
|
||||
listing.hot.return_value = posts
|
||||
reddit.subreddit.return_value = listing
|
||||
reddit.redditor.return_value = MagicMock(submissions=listing)
|
||||
praw.Reddit = MagicMock(return_value=reddit)
|
||||
return praw, reddit, listing
|
||||
|
||||
|
||||
BASE_PAYLOAD = {
|
||||
"client_id": "cid",
|
||||
"client_secret": "csecret",
|
||||
"user_agent": "ua",
|
||||
"search_queries": ["python"],
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestRedditPostsLoaderRemote:
|
||||
def test_invalid_json_raises(self):
|
||||
loader = RedditPostsLoaderRemote()
|
||||
with pytest.raises(ValueError) as exc:
|
||||
loader.load_data("not a json")
|
||||
assert "Invalid JSON input" in str(exc.value)
|
||||
with pytest.raises(ValueError, match="Invalid JSON input"):
|
||||
RedditPostsLoaderRemote().load_data("not a json")
|
||||
|
||||
def test_missing_required_fields_raises(self):
|
||||
loader = RedditPostsLoaderRemote()
|
||||
payload = json.dumps({"client_id": "id"})
|
||||
with pytest.raises(ValueError) as exc:
|
||||
loader.load_data(payload)
|
||||
assert "Missing required fields" in str(exc.value)
|
||||
with pytest.raises(ValueError, match="Missing required fields") as exc:
|
||||
RedditPostsLoaderRemote().load_data(json.dumps({"client_id": "id"}))
|
||||
assert "client_secret" in str(exc.value)
|
||||
|
||||
@patch("application.parser.remote.reddit_loader.RedditPostsLoader")
|
||||
def test_constructs_loader_and_loads_with_defaults(self, MockRedditLoader):
|
||||
loader = RedditPostsLoaderRemote()
|
||||
def test_invalid_mode_raises(self):
|
||||
payload = {**BASE_PAYLOAD, "mode": "comments"}
|
||||
praw, _, _ = _fake_praw([])
|
||||
with patch.dict(sys.modules, {"praw": praw}):
|
||||
with pytest.raises(ValueError, match="username.*subreddit|subreddit"):
|
||||
RedditPostsLoaderRemote().load_data(json.dumps(payload))
|
||||
|
||||
instance = MagicMock()
|
||||
docs = [MagicMock(), MagicMock()]
|
||||
instance.load.return_value = docs
|
||||
MockRedditLoader.return_value = instance
|
||||
def test_subreddit_mode_returns_documents(self):
|
||||
praw, reddit, listing = _fake_praw([_post("p1", "First", "Body one")])
|
||||
with patch.dict(sys.modules, {"praw": praw}):
|
||||
docs = RedditPostsLoaderRemote().load_data(json.dumps(BASE_PAYLOAD))
|
||||
|
||||
payload = {
|
||||
"client_id": "cid",
|
||||
"client_secret": "csecret",
|
||||
"user_agent": "ua",
|
||||
"search_queries": ["r/langchain"],
|
||||
}
|
||||
|
||||
result = loader.load_data(json.dumps(payload))
|
||||
|
||||
MockRedditLoader.assert_called_once_with(
|
||||
client_id="cid",
|
||||
client_secret="csecret",
|
||||
user_agent="ua",
|
||||
categories=["new", "hot"],
|
||||
mode="subreddit",
|
||||
search_queries=["r/langchain"],
|
||||
number_posts=10,
|
||||
praw.Reddit.assert_called_once_with(
|
||||
client_id="cid", client_secret="csecret", user_agent="ua"
|
||||
)
|
||||
instance.load.assert_called_once()
|
||||
assert result == docs
|
||||
reddit.subreddit.assert_called_with("python")
|
||||
# Default categories are new + hot, so the single post arrives twice.
|
||||
assert len(docs) == 2
|
||||
assert docs[0].text == "Body one"
|
||||
assert docs[0].doc_id == "p1"
|
||||
assert docs[0].extra_info["title"] == "First"
|
||||
assert docs[0].extra_info["source"] == "https://reddit.com/p1"
|
||||
assert docs[0].extra_info["post_category"] == "new"
|
||||
|
||||
@patch("application.parser.remote.reddit_loader.RedditPostsLoader")
|
||||
def test_constructs_loader_and_loads_with_overrides(self, MockRedditLoader):
|
||||
loader = RedditPostsLoaderRemote()
|
||||
def test_documents_convert_to_vector_format(self):
|
||||
"""The remote base class calls to_vector_format on every result."""
|
||||
praw, _, _ = _fake_praw([_post("p1", "First", "Body one")])
|
||||
with patch.dict(sys.modules, {"praw": praw}):
|
||||
docs = RedditPostsLoaderRemote().load_data(json.dumps(BASE_PAYLOAD))
|
||||
converted = docs[0].to_vector_format()
|
||||
assert converted.page_content == "Body one"
|
||||
assert converted.metadata["title"] == "First"
|
||||
|
||||
instance = MagicMock()
|
||||
instance.load.return_value = []
|
||||
MockRedditLoader.return_value = instance
|
||||
def test_username_mode_uses_redditor_submissions(self):
|
||||
payload = {**BASE_PAYLOAD, "mode": "username", "categories": ["new"]}
|
||||
praw, reddit, _ = _fake_praw([_post("p2", "Second", "Body two")])
|
||||
with patch.dict(sys.modules, {"praw": praw}):
|
||||
docs = RedditPostsLoaderRemote().load_data(json.dumps(payload))
|
||||
|
||||
payload = {
|
||||
"client_id": "cid",
|
||||
"client_secret": "csecret",
|
||||
"user_agent": "ua",
|
||||
"search_queries": ["python"],
|
||||
"categories": ["hot"],
|
||||
"mode": "comments",
|
||||
"number_posts": 3,
|
||||
}
|
||||
|
||||
loader.load_data(json.dumps(payload))
|
||||
|
||||
MockRedditLoader.assert_called_once_with(
|
||||
client_id="cid",
|
||||
client_secret="csecret",
|
||||
user_agent="ua",
|
||||
categories=["hot"],
|
||||
mode="comments",
|
||||
search_queries=["python"],
|
||||
number_posts=3,
|
||||
)
|
||||
instance.load.assert_called_once()
|
||||
reddit.redditor.assert_called_with("python")
|
||||
assert len(docs) == 1 and docs[0].text == "Body two"
|
||||
|
||||
def test_number_posts_and_categories_are_forwarded(self):
|
||||
payload = {**BASE_PAYLOAD, "categories": ["hot"], "number_posts": 3}
|
||||
praw, _, listing = _fake_praw([])
|
||||
with patch.dict(sys.modules, {"praw": praw}):
|
||||
RedditPostsLoaderRemote().load_data(json.dumps(payload))
|
||||
listing.hot.assert_called_once_with(limit=3)
|
||||
listing.new.assert_not_called()
|
||||
@@ -5,7 +5,7 @@ from urllib.parse import urlparse
|
||||
from application.core.url_validation import SSRFError
|
||||
from application.parser.remote.web_loader import WebLoader, headers
|
||||
from application.parser.schema.base import Document
|
||||
from langchain_core.documents import Document as LCDocument
|
||||
from application.vectorstore.document_class import Document as LCDocument
|
||||
|
||||
|
||||
def _mock_validate_url(url):
|
||||
@@ -301,13 +301,13 @@ class TestWebLoaderIntegration:
|
||||
|
||||
@patch("application.parser.remote.web_loader.validate_url", side_effect=_mock_validate_url)
|
||||
@patch("application.parser.remote.web_loader.pinned_request")
|
||||
def test_load_langchain_documents_method(self, mock_pinned_request, mock_validate, web_loader):
|
||||
def test_load_vector_documents_method(self, mock_pinned_request, mock_validate, web_loader):
|
||||
mock_pinned_request.return_value = _fake_response(
|
||||
"<html><head><title>Test Page</title></head>"
|
||||
"<body><p>Test web page content</p></body></html>"
|
||||
)
|
||||
|
||||
result = web_loader.load_langchain_documents(inputs="https://example.com")
|
||||
result = web_loader.load_vector_documents(inputs="https://example.com")
|
||||
|
||||
assert len(result) == 1
|
||||
assert isinstance(result[0], LCDocument)
|
||||
|
||||
Reference in new issue
Block a user