"""
数据缓冲平台 - MySQL 数据库连接测试
"""
from unittest.mock import MagicMock, patch

from app.mysql_database import init_mysql


def test_init_mysql_url_encodes_password():
    """MySQL 密码应使用 urllib.parse.quote_plus 进行 URL 编码。"""
    with patch("app.mysql_database.create_engine") as mock_create_engine:
        mock_engine = MagicMock()
        mock_create_engine.return_value = mock_engine
        with patch("app.mysql_database.MYSQL_PASSWORD", "p@ss/w+ord"):
            with patch("app.mysql_database.MYSQL_USER", "user"):
                with patch("app.mysql_database.MYSQL_HOST", "host"):
                    with patch("app.mysql_database.MYSQL_PORT", "3306"):
                        with patch("app.mysql_database.MYSQL_DATABASE", "buffer"):
                            init_mysql()

    url = mock_create_engine.call_args[0][0]
    assert "p%40ss%2Fw%2Bord" in url
    assert "p@ss" not in url
