Skip to content

Commit

Permalink
Fix a Deserialization Bug in ptb_sqlalchemy_jobstore (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulianini committed May 31, 2023
1 parent e41645c commit 9232cf9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ptbcontrib/ptb_sqlalchemy_jobstore/jobstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,15 @@ def _prepare_job(job: APSJob) -> APSJob:
# we'll get incorrect argument instead of CallbackContext.
prepped_job = APSJob.__new__(APSJob)
prepped_job.__setstate__(job.__getstate__())
# Get the tg_job instance in memory or the one that we deserialized during _reconstitute
# (first arg in args)
if len(job.args) == 1:
tg_job = Job._from_aps_job(job) # pylint: disable=W0212
else:
# take the tg_job instance deserialized before during _reconstitute
tg_job = job.args[0]
# Extract relevant information from the job and
# store it in the job's args.
tg_job = Job._from_aps_job(job) # pylint: disable=W0212
prepped_job.args = (
tg_job.name,
tg_job.data,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_ptb_sqlalchemy_jobstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
import platform

import apscheduler.triggers.interval
import pytest
from telegram.ext import CallbackContext, JobQueue

Expand Down Expand Up @@ -81,6 +82,14 @@ def test_get_all_jobs(self, jq, jobstore):
jobs = jobstore.get_all_jobs()
assert jobs == [j1.job, j2.job, j3.job]

def test_operations_on_job(self, jq, jobstore):
trigger = apscheduler.triggers.interval.IntervalTrigger(seconds=3)
j1 = jq.run_once(dummy_job, 1)
jq.scheduler.get_job(j1.job.id).pause()
jq.scheduler.get_job(j1.job.id).resume()
j_final = jq.scheduler.get_job(j1.job.id).reschedule(trigger)
assert j_final.id == j1.job.id

def test_remove_job(self, jq, jobstore):
j1 = jq.run_once(dummy_job, 1)
j2 = jq.run_once(dummy_job, 2)
Expand Down

0 comments on commit 9232cf9

Please sign in to comment.