Files
ayako/test_bot.py
2026-05-01 15:13:02 +03:00

169 lines
5.0 KiB
Python

"""
Tests for VNDB Telegram Bot
"""
import asyncio
import pytest
from vndb_client import VndbClient
from config import Config
from utils import Formatter, ErrorHandler, QueryBuilder
class TestVndbClient:
"""Test VNDB client functionality"""
@pytest.fixture
def client(self):
"""Create VNDB client instance"""
return VndbClient()
@pytest.mark.asyncio
async def test_get_stats(self, client):
"""Test getting database statistics"""
stats = await client.get_stats()
assert "vn" in stats
assert "chars" in stats
assert isinstance(stats["vn"], int)
@pytest.mark.asyncio
async def test_get_schema(self, client):
"""Test getting API schema"""
schema = await client.get_schema()
assert "db_types" in schema
assert "fields" in schema
@pytest.mark.asyncio
async def test_query_vn(self, client):
"""Test querying visual novels"""
results = await client.query_vn(
filters=[["id", "=", "v17"]],
fields=["title", "original"]
)
assert "results" in results
assert len(results["results"]) > 0
@pytest.mark.asyncio
async def test_query_vn_search(self, client):
"""Test searching visual novels"""
results = await client.query_vn(
filters=[["search", "=", "Steins"]],
fields=["title", "rating"],
results=5
)
assert "results" in results
@pytest.mark.asyncio
async def test_query_character(self, client):
"""Test querying characters"""
results = await client.query_character(
fields=["name", "gender"],
results=5
)
assert "results" in results
assert len(results["results"]) > 0
@pytest.mark.asyncio
async def test_query_tag(self, client):
"""Test querying tags"""
results = await client.query_tag(
fields=["name"],
results=5
)
assert "results" in results
assert len(results["results"]) > 0
@pytest.mark.asyncio
async def test_query_quote(self, client):
"""Test querying quotes"""
results = await client.query_quote(
fields=["quote", "character"],
results=2
)
assert "results" in results
class TestFormatter:
"""Test formatter utilities"""
def test_truncate_text(self):
"""Test text truncation"""
text = "A" * 300
truncated = Formatter.truncate_text(text, 100)
assert len(truncated) <= 100
assert truncated.endswith("...")
def test_truncate_text_short(self):
"""Test truncation of short text"""
text = "Short text"
truncated = Formatter.truncate_text(text, 100)
assert truncated == text
def test_format_vn_item(self):
"""Test VN item formatting"""
vn = {
"id": "v17",
"title": "Clannad",
"original": "クラナド",
"released": "2004-04-28",
"rating": 85,
"votecount": 1000
}
formatted = Formatter.format_vn_item(vn)
assert "v17" in formatted
assert "Clannad" in formatted
assert "8.5" in formatted
class TestErrorHandler:
"""Test error handling utilities"""
def test_format_error_http_error(self):
"""Test formatting HTTP error"""
error = Exception("HTTP Error")
formatted = ErrorHandler.format_error(error)
assert isinstance(formatted, str)
assert len(formatted) > 0
def test_format_error_value_error(self):
"""Test formatting ValueError"""
error = ValueError("Invalid value")
formatted = ErrorHandler.format_error(error)
assert "Invalid value" in formatted
class TestQueryBuilder:
"""Test query builder utilities"""
def test_build_search_filter(self):
"""Test building search filter"""
filter_result = QueryBuilder.build_search_filter("Clannad")
assert filter_result == ["search", "=", "Clannad"]
def test_build_id_filter(self):
"""Test building ID filter"""
filter_result = QueryBuilder.build_id_filter("v17")
assert filter_result == ["id", "=", "v17"]
def test_build_complex_filter(self):
"""Test building complex filter"""
filter1 = ["search", "=", "Clannad"]
filter2 = ["lang", "=", "en"]
complex_filter = QueryBuilder.build_complex_filter(filter1, filter2)
assert complex_filter[0] == "and"
assert len(complex_filter) == 3
class TestConfig:
"""Test configuration"""
def test_config_dict(self):
"""Test configuration as dictionary"""
config_dict = Config.to_dict()
assert "TELEGRAM_BOT_TOKEN" in config_dict
assert "VNDB_TOKEN" in config_dict
assert "LOG_LEVEL" in config_dict
if __name__ == "__main__":
# Run tests with pytest
pytest.main([__file__, "-v"])