Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

Add comprehensive tests for news API routes

LearningCircuit8c36c80b01bf
diff --git a/tests/web/routes/test_news_routes.py b/tests/web/routes/test_news_routes.py
new file mode 100644
index 0000000000..ba65490670
--- /dev/null
+++ b/tests/web/routes/test_news_routes.py
@@ -0,0 +1,420 @@
+"""
+Tests for the news API routes.
+
+Tests cover:
+- News feed retrieval
+- Subscription management
+- Feedback submission
+- News preferences
+- Categories
+"""
+
+from unittest.mock import patch
+import pytest
+
+
+class TestGetNewsFeed:
+ """Tests for get_news_feed endpoint."""
+
+ def test_get_news_feed_success(self, client):
+ """Get news feed returns feed items."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.get_news_feed"
+ ) as mock_get_feed:
+ mock_get_feed.return_value = {"items": [], "total": 0}
+
+ response = client.get("/api/news/feed")
+
+ assert response.status_code == 200
+ data = response.get_json()
+ assert "items" in data
+
+ def test_get_news_feed_with_params(self, client):
+ """Get news feed with query parameters."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.get_news_feed"
+ ) as mock_get_feed:
+ mock_get_feed.return_value = {"items": [], "total": 0}
+
+ response = client.get(
+ "/api/news/feed?limit=10&use_cache=false&focus=technology"
+ )
+
+ assert response.status_code == 200
+ mock_get_feed.assert_called_once()
+ call_kwargs = mock_get_feed.call_args[1]
+ assert call_kwargs["limit"] == 10
+ assert call_kwargs["use_cache"] is False
+ assert call_kwargs["focus"] == "technology"
+
+ def test_get_news_feed_exception(self, client):
+ """Get news feed handles exceptions."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.get_news_feed"
+ ) as mock_get_feed:
+ mock_get_feed.side_effect = Exception("Database error")
+
+ response = client.get("/api/news/feed")
+
+ assert response.status_code == 500
+ data = response.get_json()
+ assert "error" in data
+
+
+class TestDebugResearchItems:
+ """Tests for debug_research_items endpoint."""
+
+ def test_debug_research_items_success(self, client):
+ """Debug research items returns debug info."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.debug_research_items"
+ ) as mock_debug:
+ mock_debug.return_value = {"items": [], "count": 0}
+
+ response = client.get("/api/news/debug/research")
+
+ assert response.status_code == 200
+
+
+class TestGetSubscriptions:
+ """Tests for get_subscriptions endpoint."""
+
+ def test_get_subscriptions_success(self, client):
+ """Get subscriptions returns list."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.get_subscriptions"
+ ) as mock_get:
+ mock_get.return_value = {"subscriptions": []}
+
+ response = client.get("/api/news/subscriptions")
+
+ assert response.status_code == 200
+ data = response.get_json()
+ assert "subscriptions" in data
+
+
+class TestCreateSubscription:
+ """Tests for create_subscription endpoint."""
+
+ def test_create_subscription_success(self, client):
+ """Create subscription succeeds."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.create_subscription"
+ ) as mock_create:
+ mock_create.return_value = {"id": "sub-123", "query": "Test"}
+
+ response = client.post(
+ "/api/news/subscriptions",
+ json={"query": "Test query", "type": "search"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 201
+ data = response.get_json()
+ assert "id" in data
+
+ def test_create_subscription_with_all_params(self, client):
+ """Create subscription with all parameters."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.create_subscription"
+ ) as mock_create:
+ mock_create.return_value = {"id": "sub-123"}
+
+ response = client.post(
+ "/api/news/subscriptions",
+ json={
+ "query": "Test",
+ "type": "search",
+ "refresh_minutes": 60,
+ "model_provider": "ollama",
+ "model": "llama3",
+ "search_strategy": "standard",
+ "name": "My Subscription",
+ "is_active": True,
+ "search_engine": "searxng",
+ "search_iterations": 3,
+ "questions_per_iteration": 2,
+ },
+ content_type="application/json",
+ )
+
+ assert response.status_code == 201
+ mock_create.assert_called_once()
+
+
+class TestGetSubscription:
+ """Tests for get_subscription endpoint."""
+
+ def test_get_subscription_success(self, client):
+ """Get single subscription returns data."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.get_subscription"
+ ) as mock_get:
+ mock_get.return_value = {"id": "sub-123", "query": "Test"}
+
+ response = client.get("/api/news/subscriptions/sub-123")
+
+ assert response.status_code == 200
+ data = response.get_json()
+ assert data["id"] == "sub-123"
+
+
+class TestUpdateSubscription:
+ """Tests for update_subscription endpoint."""
+
+ def test_update_subscription_put(self, client):
+ """Update subscription via PUT."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.update_subscription"
+ ) as mock_update:
+ mock_update.return_value = {"id": "sub-123", "query": "Updated"}
+
+ response = client.put(
+ "/api/news/subscriptions/sub-123",
+ json={"query": "Updated query"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 200
+
+ def test_update_subscription_patch(self, client):
+ """Update subscription via PATCH."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.update_subscription"
+ ) as mock_update:
+ mock_update.return_value = {"id": "sub-123"}
+
+ response = client.patch(
+ "/api/news/subscriptions/sub-123",
+ json={"is_active": False},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 200
+
+
+class TestDeleteSubscription:
+ """Tests for delete_subscription endpoint."""
+
+ def test_delete_subscription_success(self, client):
+ """Delete subscription succeeds."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.delete_subscription"
+ ) as mock_delete:
+ mock_delete.return_value = {"deleted": True}
+
+ response = client.delete("/api/news/subscriptions/sub-123")
+
+ assert response.status_code == 200
+
+
+class TestGetSubscriptionHistory:
+ """Tests for get_subscription_history endpoint."""
+
+ def test_get_subscription_history_success(self, client):
+ """Get subscription history returns history."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.get_subscription_history"
+ ) as mock_get:
+ mock_get.return_value = {"history": []}
+
+ response = client.get("/api/news/subscriptions/sub-123/history")
+
+ assert response.status_code == 200
+
+ def test_get_subscription_history_with_limit(self, client):
+ """Get subscription history with limit parameter."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.get_subscription_history"
+ ) as mock_get:
+ mock_get.return_value = {"history": []}
+
+ response = client.get(
+ "/api/news/subscriptions/sub-123/history?limit=10"
+ )
+
+ assert response.status_code == 200
+ mock_get.assert_called_once_with("sub-123", 10)
+
+
+class TestSubmitFeedback:
+ """Tests for submit_feedback endpoint."""
+
+ def test_submit_feedback_upvote(self, client):
+ """Submit upvote feedback."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.submit_feedback"
+ ) as mock_submit:
+ mock_submit.return_value = {"success": True}
+
+ response = client.post(
+ "/api/news/feedback",
+ json={"card_id": "card-123", "vote": "up"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 200
+
+ def test_submit_feedback_downvote(self, client):
+ """Submit downvote feedback."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.submit_feedback"
+ ) as mock_submit:
+ mock_submit.return_value = {"success": True}
+
+ response = client.post(
+ "/api/news/feedback",
+ json={"card_id": "card-123", "vote": "down"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 200
+
+ def test_submit_feedback_invalid_vote(self, client):
+ """Submit feedback with invalid vote."""
+ response = client.post(
+ "/api/news/feedback",
+ json={"card_id": "card-123", "vote": "invalid"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 400
+
+ def test_submit_feedback_missing_card_id(self, client):
+ """Submit feedback without card_id."""
+ response = client.post(
+ "/api/news/feedback",
+ json={"vote": "up"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 400
+
+
+class TestResearchNewsItem:
+ """Tests for research_news_item endpoint."""
+
+ def test_research_news_item_success(self, client):
+ """Research news item succeeds."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.research_news_item"
+ ) as mock_research:
+ mock_research.return_value = {"research_id": "res-123"}
+
+ response = client.post(
+ "/api/news/research",
+ json={"card_id": "card-123"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 200
+
+ def test_research_news_item_with_depth(self, client):
+ """Research news item with custom depth."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.research_news_item"
+ ) as mock_research:
+ mock_research.return_value = {"research_id": "res-123"}
+
+ response = client.post(
+ "/api/news/research",
+ json={"card_id": "card-123", "depth": "detailed"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 200
+ mock_research.assert_called_once_with("card-123", "detailed")
+
+ def test_research_news_item_missing_card_id(self, client):
+ """Research news item without card_id."""
+ response = client.post(
+ "/api/news/research",
+ json={},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 400
+
+
+class TestSavePreferences:
+ """Tests for save_preferences endpoint."""
+
+ def test_save_preferences_success(self, client):
+ """Save preferences succeeds."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.save_news_preferences"
+ ) as mock_save:
+ mock_save.return_value = {"saved": True}
+
+ response = client.post(
+ "/api/news/preferences",
+ json={"categories": ["tech", "science"]},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 200
+
+
+class TestGetCategories:
+ """Tests for get_categories endpoint."""
+
+ def test_get_categories_success(self, client):
+ """Get categories returns category list."""
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.get_news_categories"
+ ) as mock_get:
+ mock_get.return_value = {
+ "categories": [
+ {"name": "Technology", "count": 10},
+ {"name": "Science", "count": 5},
+ ]
+ }
+
+ response = client.get("/api/news/categories")
+
+ assert response.status_code == 200
+ data = response.get_json()
+ assert "categories" in data
+
+
+class TestNewsAPIExceptionHandler:
+ """Tests for NewsAPIException error handler."""
+
+ def test_news_api_exception_handled(self, client):
+ """NewsAPIException is handled properly."""
+ from src.local_deep_research.news.exceptions import NewsAPIException
+
+ with patch(
+ "src.local_deep_research.web.routes.news_routes.news_api.get_news_feed"
+ ) as mock_get:
+ mock_get.side_effect = NewsAPIException(
+ message="Test error",
+ error_code="TEST_ERROR",
+ status_code=400,
+ )
+
+ response = client.get("/api/news/feed")
+
+ assert response.status_code == 400
+ data = response.get_json()
+ assert data["error_code"] == "TEST_ERROR"
+
+
+# Test fixtures
+def client():
+ """Create test client."""
+ from flask import Flask
+
+ app = Flask(__name__)
+ app.config["TESTING"] = True
+ app.config["SECRET_KEY"] = "test_secret"
+
+ # Register news blueprint
+ from src.local_deep_research.web.routes.news_routes import bp
+
+ app.register_blueprint(bp)
+
+ with app.test_client() as client:
+ with app.app_context():
+ yield client