61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
"""Тесты разбора переменных окружения (без БД и сети).
|
|
|
|
Проверяем граничные случаи, которые уже ломали запуск:
|
|
- ``ADMIN_IDS`` пустой / через запятую / как JSON-массив;
|
|
- пустые целочисленные поля ``CHAT_ID``/``ALERT_CHAT_ID``;
|
|
- обязательный ``BOT_TOKEN``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from common.config import Settings
|
|
|
|
#: Переменные, которые чистим перед каждым тестом, чтобы окружение не влияло.
|
|
_ENV_KEYS = ("BOT_TOKEN", "APP_ENV", "ADMIN_IDS", "CHAT_ID", "ALERT_CHAT_ID")
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _clean_env(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
for key in _ENV_KEYS:
|
|
monkeypatch.delenv(key, raising=False)
|
|
|
|
|
|
def _settings(monkeypatch: pytest.MonkeyPatch, **env: str) -> Settings:
|
|
for key, value in env.items():
|
|
monkeypatch.setenv(key, value)
|
|
return Settings(_env_file=None)
|
|
|
|
|
|
def test_admin_ids_empty(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
assert _settings(monkeypatch, BOT_TOKEN="1:a", ADMIN_IDS="").admin_ids == []
|
|
|
|
|
|
def test_admin_ids_not_set(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
assert _settings(monkeypatch, BOT_TOKEN="1:a").admin_ids == []
|
|
|
|
|
|
def test_admin_ids_comma(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
assert _settings(monkeypatch, BOT_TOKEN="1:a", ADMIN_IDS="1,2, 3").admin_ids == [1, 2, 3]
|
|
|
|
|
|
def test_admin_ids_json_like(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
assert _settings(monkeypatch, BOT_TOKEN="1:a", ADMIN_IDS="[7, 8]").admin_ids == [7, 8]
|
|
|
|
|
|
def test_chat_ids_empty_are_zero(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
settings = _settings(monkeypatch, BOT_TOKEN="1:a", CHAT_ID="", ALERT_CHAT_ID="")
|
|
assert settings.chat_id == 0
|
|
assert settings.alert_chat_id == 0
|
|
|
|
|
|
def test_chat_id_value(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
assert _settings(monkeypatch, BOT_TOKEN="1:a", CHAT_ID="-1001234567890").chat_id == -1001234567890
|
|
|
|
|
|
def test_bot_token_required(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
with pytest.raises(ValidationError):
|
|
Settings(_env_file=None)
|