test_google_trends_server.py•6.17 kB
import os
import pytest
from google_trends_server import get_google_trends
# Check if API key is set, skip tests if not
pytestmark = pytest.mark.skipif(
not os.environ.get("SERPAPI_API_KEY"),
reason="SERPAPI_API_KEY not set"
)
@pytest.mark.asyncio
async def test_get_google_trends_basic():
"""Test basic Google Trends query"""
result = await get_google_trends.fn(
query="Python programming",
cutoff_date="2024-01-31"
)
assert result is not None
assert isinstance(result, str)
assert len(result) > 0
# Should contain the query name
assert "Python programming" in result
# Should contain period information
assert "Period:" in result
@pytest.mark.asyncio
async def test_get_google_trends_with_recent_date():
"""Test Google Trends with recent cutoff date"""
result = await get_google_trends.fn(
query="artificial intelligence",
cutoff_date="2023-12-31"
)
assert result is not None
assert "artificial intelligence" in result
assert "Interest over time" in result
@pytest.mark.asyncio
async def test_get_google_trends_with_older_date():
"""Test Google Trends with older historical date"""
result = await get_google_trends.fn(
query="machine learning",
cutoff_date="2022-06-30"
)
assert result is not None
assert "machine learning" in result
assert "2022" in result
@pytest.mark.asyncio
async def test_get_google_trends_output_format():
"""Test that output format includes all expected sections"""
result = await get_google_trends.fn(
query="Python",
cutoff_date="2024-01-15"
)
assert result is not None
# Check for all expected sections
assert "Google Trends for" in result
assert "Period:" in result
assert "Region:" in result
assert "Interest over time" in result
assert "0-100 scale" in result
@pytest.mark.asyncio
async def test_get_google_trends_date_range():
"""Test that the date range is 30 days"""
result = await get_google_trends.fn(
query="Test query",
cutoff_date="2024-02-29" # Leap year date
)
assert result is not None
# Should contain period information showing 30 day range
assert "Period:" in result
assert "2024-01-30" in result # 30 days before Feb 29
assert "2024-02-29" in result
@pytest.mark.asyncio
async def test_get_google_trends_popular_topic():
"""Test with a very popular topic"""
result = await get_google_trends.fn(
query="weather",
cutoff_date="2024-01-01"
)
assert result is not None
assert "weather" in result
# Should have actual data points
assert ":" in result # Date: value format
@pytest.mark.asyncio
async def test_get_google_trends_niche_topic():
"""Test with a more niche topic"""
result = await get_google_trends.fn(
query="quantum computing",
cutoff_date="2023-12-31"
)
assert result is not None
assert "quantum computing" in result
@pytest.mark.asyncio
async def test_get_google_trends_multi_word_query():
"""Test with multi-word query"""
result = await get_google_trends.fn(
query="climate change impact",
cutoff_date="2024-01-15"
)
assert result is not None
assert "climate change impact" in result
assert "Interest over time" in result
@pytest.mark.asyncio
async def test_get_google_trends_invalid_date():
"""Test with invalid date format"""
result = await get_google_trends.fn(
query="test",
cutoff_date="invalid-date"
)
assert result is not None
# Should contain error message
assert "error" in result.lower() or "iso" in result.lower()
@pytest.mark.asyncio
async def test_get_google_trends_malformed_date():
"""Test with malformed date"""
result = await get_google_trends.fn(
query="test",
cutoff_date="2024-13-45" # Invalid month and day
)
assert result is not None
# Should handle error gracefully
assert "error" in result.lower() or len(result) > 0
@pytest.mark.asyncio
async def test_get_google_trends_future_date():
"""Test with future date (should work, just returns most recent data)"""
result = await get_google_trends.fn(
query="Python",
cutoff_date="2030-01-01"
)
assert result is not None
assert isinstance(result, str)
# Should either return data or an error message
assert len(result) > 0
@pytest.mark.asyncio
async def test_get_google_trends_very_old_date():
"""Test with very old date"""
result = await get_google_trends.fn(
query="internet",
cutoff_date="2015-01-01"
)
assert result is not None
# Should either return historical data or indicate no data
assert isinstance(result, str)
assert len(result) > 0
@pytest.mark.asyncio
async def test_get_google_trends_special_characters():
"""Test query with special characters"""
result = await get_google_trends.fn(
query="C++ programming",
cutoff_date="2024-01-01"
)
assert result is not None
assert isinstance(result, str)
assert len(result) > 0
@pytest.mark.asyncio
async def test_get_google_trends_numeric_query():
"""Test with numeric query"""
result = await get_google_trends.fn(
query="2024 Olympics",
cutoff_date="2024-06-01"
)
assert result is not None
assert "2024 Olympics" in result or "Olympics" in result
@pytest.mark.asyncio
async def test_get_google_trends_brand_name():
"""Test with brand/company name"""
result = await get_google_trends.fn(
query="Tesla",
cutoff_date="2024-01-01"
)
assert result is not None
assert "Tesla" in result
assert "Interest over time" in result
@pytest.mark.asyncio
async def test_get_google_trends_region_info():
"""Test that region information is included"""
result = await get_google_trends.fn(
query="test",
cutoff_date="2024-01-01"
)
assert result is not None
# Should specify US region as per server configuration
assert "Region:" in result
assert "US" in result