Telethon unit tests + tidyup

This commit is contained in:
Patrick Robertson
2025-03-26 22:53:27 +04:00
parent 17d2d14680
commit 95ea9fb231
5 changed files with 68 additions and 16 deletions

View File

@@ -20,6 +20,14 @@ from auto_archiver.core.module import ModuleFactory
TESTS_TO_RUN_LAST = ["test_twitter_api_archiver"]
@pytest.fixture
def get_lazy_module():
def _get_lazy_module(module_name):
return ModuleFactory().get_module_lazy(module_name)
return _get_lazy_module
@pytest.fixture
def setup_module(request):
def _setup_module(module_name, config=None):

View File

@@ -0,0 +1,38 @@
import os
from datetime import date
import pytest
from .test_extractor_base import TestExtractorBase
from auto_archiver.modules.telethon_extractor import TelethonExtractor
class TestTelethonExtractor(TestExtractorBase):
extractor_module = "telethon_extractor"
extractor: TelethonExtractor
config = {
"api_id": 123,
"api_hash": "ABC",
}
@pytest.fixture(autouse=True)
def mock_client_setup(self, mocker):
mocker.patch("telethon.client.auth.AuthMethods.start")
def test_setup_fails_clear_session_file(self, get_lazy_module, tmp_path, mocker):
start = mocker.patch("telethon.client.auth.AuthMethods.start")
start.side_effect = Exception("Test exception")
# make sure the default setup file is created
session_file = tmp_path / "test.session"
lazy_module = get_lazy_module("telethon_extractor")
with pytest.raises(Exception):
lazy_module.load(
{"telethon_extractor": {"session_file": str(session_file), "api_id": 123, "api_hash": "ABC"}}
)
assert session_file.exists()
assert f"telethon-{date.today().strftime('%Y-%m-%d')}" in lazy_module._instance.session_file
assert os.path.exists(lazy_module._instance.session_file + ".session")