test_sitemap_stats.py•2.96 kB
"""Tests for sitemap statistics functionality."""
import pytest
from unittest.mock import MagicMock
# Mock implementation of get_sitemap_stats
async def mock_get_sitemap_stats(ctx, url):
"""Mock implementation of get_sitemap_stats for testing."""
try:
# Generate mock statistics
stats = {
"url": url,
"total_pages": 150,
"total_sitemaps": 3,
"sitemaps": [
{
"url": f"{url}/sitemap1.xml",
"pages": 50,
"last_modified": "2025-04-01T12:00:00Z",
},
{
"url": f"{url}/sitemap2.xml",
"pages": 50,
"last_modified": "2025-04-01T12:00:00Z",
},
{
"url": f"{url}/sitemap3.xml",
"pages": 50,
"last_modified": "2025-04-01T12:00:00Z",
},
],
}
return stats
except Exception as e:
return {"error": str(e)}
# Mock Context for testing
class MockContext:
def __init__(self):
self.log = MagicMock()
self.log.info = MagicMock()
self.log.error = MagicMock()
@pytest.mark.asyncio
async def test_get_sitemap_stats_basic():
"""Test basic sitemap statistics retrieval."""
ctx = MockContext()
result = await mock_get_sitemap_stats(ctx=ctx, url="example.com")
# Check basic response structure
assert "url" in result
assert "total_pages" in result
assert "total_sitemaps" in result
assert "sitemaps" in result
# Check statistics values
assert result["url"] == "example.com"
assert result["total_pages"] == 150
assert result["total_sitemaps"] == 3
assert len(result["sitemaps"]) == 3
@pytest.mark.asyncio
async def test_get_sitemap_stats_with_error():
"""Test error handling in sitemap stats retrieval."""
# Create a mock function that raises an exception
async def mock_get_sitemap_stats_with_error(ctx, url):
raise Exception("Test error")
ctx = MockContext()
# Call the function and catch the exception
try:
await mock_get_sitemap_stats_with_error(ctx=ctx, url="example.com")
assert False, "Exception should have been raised"
except Exception as e:
assert str(e) == "Test error"
@pytest.mark.asyncio
async def test_get_sitemap_stats_empty_sitemap():
"""Test handling of empty sitemaps."""
# Create a mock function that returns empty sitemap stats
async def mock_get_empty_sitemap_stats(ctx, url):
return {"url": url, "total_pages": 0, "total_sitemaps": 0, "sitemaps": []}
ctx = MockContext()
result = await mock_get_empty_sitemap_stats(ctx=ctx, url="example.com")
# Check empty sitemap response
assert result["total_pages"] == 0
assert result["total_sitemaps"] == 0
assert len(result["sitemaps"]) == 0