Config scan from bot channel implementation

This commit is contained in:
BreadTube 2025-09-23 22:48:35 +09:00 committed by Corentin
commit 157e8c1b17
6 changed files with 453 additions and 34 deletions

44
tests/test_config.py Normal file
View file

@ -0,0 +1,44 @@
from breadtube_bot.config import Config
import pytest
def test_empty():
with pytest.raises(RuntimeError, match='Cannot load config: empty input'):
Config.from_str('')
def test_wrong_header():
with pytest.raises(RuntimeError, match='Cannot load config: first line is not "config"'):
Config.from_str('connfig')
def test_lacking_key():
expected_config = Config(bot_role='test-role', request_timeout=5.5)
config = Config.from_str(
'config\n'
f'request_timeout={expected_config.request_timeout}\n'
f'bot_role={expected_config.bot_role}')
assert config == expected_config
def test_wrong_key():
with pytest.raises(RuntimeError, match=('Invalid config: invalid key bot_channel_init at line 2')):
Config.from_str('config\nrequest_timeout=3.\nbot_channel_init=2')
def test_duplicated_key():
with pytest.raises(RuntimeError, match=('Invalid config: duplicated key request_timeout at line 3')):
Config.from_str('config\nrequest_timeout=3.\nbot_channel_init_retries=2\nrequest_timeout=5.')
def test_correct_config():
expected_config = Config(bot_channel='test-channel', bot_role='test-role', bot_channel_init_retries=298,
request_timeout=5.5)
config = Config.from_str(
'config\n'
f'request_timeout={expected_config.request_timeout}\n'
f'bot_channel_init_retries={expected_config.bot_channel_init_retries}\n'
f'bot_channel={expected_config.bot_channel}\n'
f'bot_role={expected_config.bot_role}')
assert config == expected_config