Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibo-Joshi committed Apr 26, 2024
1 parent d367066 commit daed3de
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion telegram/ext/_jobqueue.py
Expand Up @@ -45,7 +45,7 @@


_ALL_DAYS = tuple(range(7))
_LOGGER = get_logger(__name__)
_LOGGER = get_logger(__name__, class_name="JobQueue")


class JobQueue(Generic[CCT]):
Expand Down
39 changes: 39 additions & 0 deletions tests/ext/test_application.py
Expand Up @@ -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
41 changes: 41 additions & 0 deletions tests/ext/test_jobqueue.py
Expand Up @@ -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

0 comments on commit daed3de

Please sign in to comment.