From daed3de3450dd5da91ffc1d90f63f3454316d1e5 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Fri, 26 Apr 2024 10:49:52 +0200 Subject: [PATCH] Add tests --- telegram/ext/_jobqueue.py | 2 +- tests/ext/test_application.py | 39 +++++++++++++++++++++++++++++++++ tests/ext/test_jobqueue.py | 41 +++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/telegram/ext/_jobqueue.py b/telegram/ext/_jobqueue.py index 0829344a2df..6edd5a892ea 100644 --- a/telegram/ext/_jobqueue.py +++ b/telegram/ext/_jobqueue.py @@ -45,7 +45,7 @@ _ALL_DAYS = tuple(range(7)) -_LOGGER = get_logger(__name__) +_LOGGER = get_logger(__name__, class_name="JobQueue") class JobQueue(Generic[CCT]): diff --git a/tests/ext/test_application.py b/tests/ext/test_application.py index f1dd9dfdd7b..fd013cf20db 100644 --- a/tests/ext/test_application.py +++ b/tests/ext/test_application.py @@ -2460,3 +2460,42 @@ async def callback(update, context): assert received_updates == {2} assert len(caplog.records) == 0 + + async def test_process_error_exception_in_building_context(self, monkeypatch, caplog, app): + # Makes sure that exceptions in building the context don't stop the application + exception = ValueError("TestException") + original_from_error = CallbackContext.from_error + + def raise_exception(update, error, application, *args, **kwargs): + if error == 1: + raise exception + return original_from_error(update, error, application, *args, **kwargs) + + monkeypatch.setattr(CallbackContext, "from_error", raise_exception) + + received_errors = set() + + async def callback(update, context): + received_errors.add(context.error) + + app.add_error_handler(callback) + + async with app: + with caplog.at_level(logging.CRITICAL): + await app.process_error(update=None, error=1) + + assert received_errors == set() + assert len(caplog.records) == 1 + record = caplog.records[0] + assert record.name == "telegram.ext.Application" + assert record.getMessage().startswith( + "Error while building CallbackContext for exception 1" + ) + assert record.levelno == logging.CRITICAL + + caplog.clear() + with caplog.at_level(logging.CRITICAL): + await app.process_error(update=None, error=2) + + assert received_errors == {2} + assert len(caplog.records) == 0 diff --git a/tests/ext/test_jobqueue.py b/tests/ext/test_jobqueue.py index 0a3723763d9..6a69b171f97 100644 --- a/tests/ext/test_jobqueue.py +++ b/tests/ext/test_jobqueue.py @@ -646,3 +646,44 @@ async def test_from_aps_job_missing_reference(self, job_queue): tg_job = Job.from_aps_job(aps_job) assert tg_job is job assert tg_job.job is aps_job + + async def test_run_job_exception_in_building_context( + self, monkeypatch, job_queue, caplog, app + ): + # Makes sure that exceptions in building the context don't stop the application + exception = ValueError("TestException") + original_from_job = CallbackContext.from_job + + def raise_exception(job, application): + print(job, job.data, application) + if job.data == 1: + raise exception + return original_from_job(job, application) + + monkeypatch.setattr(CallbackContext, "from_job", raise_exception) + + received_jobs = set() + + async def job_callback(context): + received_jobs.add(context.job.data) + + with caplog.at_level(logging.CRITICAL): + job_queue.run_once(job_callback, 0.1, data=1) + await asyncio.sleep(0.2) + + assert received_jobs == set() + assert len(caplog.records) == 1 + record = caplog.records[0] + assert record.name == "telegram.ext.JobQueue" + assert record.getMessage().startswith( + "Error while building CallbackContext for job job_callback" + ) + assert record.levelno == logging.CRITICAL + + caplog.clear() + with caplog.at_level(logging.CRITICAL): + job_queue.run_once(job_callback, 0.1, data=2) + await asyncio.sleep(0.2) + + assert received_jobs == {2} + assert len(caplog.records) == 0