Refactor news API integration to use NewsApiWrapper and GnewsWrapper; add tests for Gnews API functionality

This commit is contained in:
2025-09-30 00:34:07 +02:00
parent fc4753a245
commit c17a948ae0
10 changed files with 175 additions and 15 deletions

View File

@@ -0,0 +1,34 @@
import pytest
from app.news import GnewsWrapper
@pytest.mark.news
@pytest.mark.api
class TestGnewsAPI:
def test_gnews_api_initialization(self):
gnews_api = GnewsWrapper()
assert gnews_api is not None
def test_gnews_api_get_latest_news(self):
gnews_api = GnewsWrapper()
articles = gnews_api.get_latest_news(query="crypto", total=2)
assert isinstance(articles, list)
assert len(articles) == 2
for article in articles:
assert hasattr(article, 'source')
assert hasattr(article, 'time')
assert hasattr(article, 'title')
assert hasattr(article, 'description')
def test_gnews_api_get_top_headlines(self):
news_api = GnewsWrapper()
articles = news_api.get_top_headlines(query="crypto", total=2)
assert isinstance(articles, list)
assert len(articles) == 2
for article in articles:
assert hasattr(article, 'source')
assert hasattr(article, 'time')
assert hasattr(article, 'title')
assert hasattr(article, 'description')

View File

@@ -1,13 +1,29 @@
from app.news import NewsAPI
import pytest
from app.news import NewsApiWrapper
@pytest.mark.news
@pytest.mark.api
class TestNewsAPI:
def test_news_api_initialization(self):
news_api = NewsAPI()
news_api = NewsApiWrapper()
assert news_api.client is not None
def test_news_api_get_latest_news(self):
news_api = NewsApiWrapper()
articles = news_api.get_latest_news(query="crypto", total=2)
assert isinstance(articles, list)
assert len(articles) > 0 # Ensure we got some articles (apparently it doesn't always return the requested number)
for article in articles:
assert hasattr(article, 'source')
assert hasattr(article, 'time')
assert hasattr(article, 'title')
assert hasattr(article, 'description')
def test_news_api_get_top_headlines(self):
news_api = NewsAPI()
news_api = NewsApiWrapper()
articles = news_api.get_top_headlines(query="crypto", total=2)
assert isinstance(articles, list)
assert len(articles) > 0 # Ensure we got some articles (apparently it doesn't always return the requested number)

View File

@@ -30,7 +30,6 @@ def pytest_collection_modifyitems(config, items):
"""Modifica automaticamente gli item di test aggiungendogli marker basati sul nome"""
markers_to_add = {
"api": pytest.mark.api,
"coinbase": pytest.mark.api,
"cryptocompare": pytest.mark.api,
"overview": pytest.mark.slow,
@@ -38,7 +37,6 @@ def pytest_collection_modifyitems(config, items):
"gemini": pytest.mark.gemini,
"ollama_gpt": pytest.mark.ollama_gpt,
"ollama_qwen": pytest.mark.ollama_qwen,
"news": pytest.mark.news,
}
for item in items: