Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Refactor JobQueue#1981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Bibo-Joshi merged 19 commits intov13fromrefactor-job-queue
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
19 commits
Select commitHold shift + click to select a range
ac6046c
First go on refactoring JobQueue
Bibo-JoshiJun 5, 2020
bc9b05c
Temporarily enable tests for the v13 branch
Bibo-JoshiJun 6, 2020
522558d
Merge remote-tracking branch 'origin/v13' into refactor-job-queue
Bibo-JoshiJun 6, 2020
2eb198e
Work on tests
Bibo-JoshiJun 6, 2020
8862a32
Temporarily enable tests for the v13 branch
Bibo-JoshiJun 6, 2020
0e02483
Merge branch 'v13' into refactor-job-queue
Bibo-JoshiJun 15, 2020
e56af1c
Increase coverage
Bibo-JoshiJun 20, 2020
c723f5b
Remove JobQueue.tick()
Bibo-JoshiJun 22, 2020
6e1a014
Address review
Bibo-JoshiJun 24, 2020
8e587fb
Merge branch 'refactor-job-queue' of https://github.com/python-telegr…
Bibo-JoshiJun 24, 2020
b7e635b
Temporarily enable tests for the v13 branch
Bibo-JoshiJun 6, 2020
585e5f2
Merge branch 'v13' into refactor-job-queue
Bibo-JoshiJun 25, 2020
9171103
Address review
Bibo-JoshiJun 25, 2020
8d22dcf
Dispatch errors
Bibo-JoshiJun 26, 2020
d44acb9
Fix handling of job_kwargs
Bibo-JoshiJul 2, 2020
271a105
Merge branch 'refactor-job-queue' of github.com:/python-telegram-bot/…
Bibo-JoshiJul 2, 2020
46dcff4
Remove possibility to pass a Bot to JobQueue
Bibo-JoshiJul 8, 2020
065ce12
Merge remote-tracking branch 'origin/refactor-job-queue' into refacto…
Bibo-JoshiJul 8, 2020
8867e1c
Merge branch 'v13' into refactor-job-queue
Bibo-JoshiJul 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion.github/workflows/test.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,7 +16,7 @@ jobs:
runs-on: ${{matrix.os}}
strategy:
matrix:
python-version: [3.5, 3.6, 3.7, 3.8]
python-version: [3.6, 3.7, 3.8]
os: [ubuntu-latest, windows-latest]
include:
- os: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletionREADME.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -83,7 +83,7 @@ Introduction

This library provides a pure Python interface for the
`Telegram Bot API <https://core.telegram.org/bots/api>`_.
It's compatible with Python versions 3.5+ and `PyPy <http://pypy.org/>`_.
It's compatible with Python versions 3.6+ and `PyPy <http://pypy.org/>`_.

In addition to the pure API implementation, this library features a number of high-level classes to
make the development of bots easy and straightforward. These classes are contained in the
Expand Down
1 change: 1 addition & 0 deletionsrequirements.txt
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,3 +2,4 @@ certifi
tornado>=5.1
cryptography
decorator>=4.4.0
APScheduler==3.6.3
1 change: 0 additions & 1 deletionsetup.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -61,7 +61,6 @@ def requirements():
'Topic :: Internet',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
Expand Down
682 changes: 271 additions & 411 deletionstelegram/ext/jobqueue.py
View file
Open in desktop

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletionstests/conftest.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@
from time import sleep

import pytest
import pytz

from telegram import (Bot, Message, User, Chat, MessageEntity, Update,
InlineQuery, CallbackQuery, ShippingQuery, PreCheckoutQuery,
Expand DownExpand Up@@ -271,14 +272,14 @@ def false_update(request):
return Update(update_id=1, **request.param)


@pytest.fixture(params=[1, 2], ids=lambda h:'UTC +{hour:0>2}:00'.format(hour=h))
defutc_offset(request):
returndatetime.timedelta(hours=request.param)
@pytest.fixture(params=['Europe/Berlin', 'Asia/Singapore','UTC'])
deftzinfo(request):
returnpytz.timezone(request.param)


@pytest.fixture()
def timezone(utc_offset):
returndatetime.timezone(utc_offset)
def timezone(tzinfo):
returntzinfo


def expect_bad_request(func, message, reason):
Expand Down
56 changes: 22 additions & 34 deletionstests/test_conversationhandler.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -24,7 +24,7 @@
from telegram import (CallbackQuery, Chat, ChosenInlineResult, InlineQuery, Message,
PreCheckoutQuery, ShippingQuery, Update, User, MessageEntity)
from telegram.ext import (ConversationHandler, CommandHandler, CallbackQueryHandler,
MessageHandler, Filters, InlineQueryHandler, CallbackContext)
MessageHandler, Filters, InlineQueryHandler, CallbackContext, JobQueue)


@pytest.fixture(scope='class')
Expand All@@ -37,6 +37,15 @@ def user2():
return User(first_name='Mister Test', id=124, is_bot=False)


@pytest.fixture(autouse=True)
def start_stop_job_queue(dp):
dp.job_queue = JobQueue()
dp.job_queue.set_dispatcher(dp)
dp.job_queue.start()
yield
dp.job_queue.stop()


class TestConversationHandler:
# State definitions
# At first we're thirsty. Then we brew coffee, we drink it
Expand DownExpand Up@@ -530,20 +539,17 @@ def test_conversation_timeout(self, dp, bot, user1):
bot=bot)
dp.process_update(Update(update_id=0, message=message))
assert handler.conversations.get((self.group.id, user1.id)) == self.THIRSTY
sleep(0.5)
dp.job_queue.tick()
sleep(0.65)
assert handler.conversations.get((self.group.id, user1.id)) is None

# Start state machine, do something, then reach timeout
dp.process_update(Update(update_id=1, message=message))
assert handler.conversations.get((self.group.id, user1.id)) == self.THIRSTY
message.text = '/brew'
message.entities[0].length = len('/brew')
dp.job_queue.tick()
dp.process_update(Update(update_id=2, message=message))
assert handler.conversations.get((self.group.id, user1.id)) == self.BREWING
sleep(0.5)
dp.job_queue.tick()
sleep(0.6)
assert handler.conversations.get((self.group.id, user1.id)) is None

def test_conversation_handler_timeout_update_and_context(self, cdp, bot, user1):
Expand DownExpand Up@@ -578,8 +584,7 @@ def timeout_callback(u, c):
timeout_handler.callback = timeout_callback

cdp.process_update(update)
sleep(0.5)
cdp.job_queue.tick()
sleep(0.6)
assert handler.conversations.get((self.group.id, user1.id)) is None
assert self.is_timeout

Expand All@@ -602,24 +607,20 @@ def test_conversation_timeout_keeps_extending(self, dp, bot, user1):
dp.process_update(Update(update_id=0, message=message))
assert handler.conversations.get((self.group.id, user1.id)) == self.THIRSTY
sleep(0.25) # t=.25
dp.job_queue.tick()
assert handler.conversations.get((self.group.id, user1.id)) == self.THIRSTY
message.text = '/brew'
message.entities[0].length = len('/brew')
dp.process_update(Update(update_id=0, message=message))
assert handler.conversations.get((self.group.id, user1.id)) == self.BREWING
sleep(0.35) # t=.6
dp.job_queue.tick()
assert handler.conversations.get((self.group.id, user1.id)) == self.BREWING
message.text = '/pourCoffee'
message.entities[0].length = len('/pourCoffee')
dp.process_update(Update(update_id=0, message=message))
assert handler.conversations.get((self.group.id, user1.id)) == self.DRINKING
sleep(.4) # t=1
dp.job_queue.tick()
assert handler.conversations.get((self.group.id, user1.id)) == self.DRINKING
sleep(.1) # t=1.1
dp.job_queue.tick()
sleep(.2) # t=1.2
assert handler.conversations.get((self.group.id, user1.id)) is None

def test_conversation_timeout_two_users(self, dp, bot, user1, user2):
Expand All@@ -638,16 +639,13 @@ def test_conversation_timeout_two_users(self, dp, bot, user1, user2):
message.entities[0].length = len('/brew')
message.entities[0].length = len('/brew')
message.from_user = user2
dp.job_queue.tick()
dp.process_update(Update(update_id=0, message=message))
assert handler.conversations.get((self.group.id, user2.id)) is None
message.text = '/start'
message.entities[0].length = len('/start')
dp.job_queue.tick()
dp.process_update(Update(update_id=0, message=message))
assert handler.conversations.get((self.group.id, user2.id)) == self.THIRSTY
sleep(0.5)
dp.job_queue.tick()
sleep(0.6)
assert handler.conversations.get((self.group.id, user1.id)) is None
assert handler.conversations.get((self.group.id, user2.id)) is None

Expand All@@ -670,8 +668,7 @@ def test_conversation_handler_timeout_state(self, dp, bot, user1):
message.text = '/brew'
message.entities[0].length = len('/brew')
dp.process_update(Update(update_id=0, message=message))
sleep(0.5)
dp.job_queue.tick()
sleep(0.6)
assert handler.conversations.get((self.group.id, user1.id)) is None
assert self.is_timeout

Expand All@@ -680,8 +677,7 @@ def test_conversation_handler_timeout_state(self, dp, bot, user1):
message.text = '/start'
message.entities[0].length = len('/start')
dp.process_update(Update(update_id=1, message=message))
sleep(0.5)
dp.job_queue.tick()
sleep(0.6)
assert handler.conversations.get((self.group.id, user1.id)) is None
assert self.is_timeout

Expand All@@ -694,8 +690,7 @@ def test_conversation_handler_timeout_state(self, dp, bot, user1):
message.text = '/startCoding'
message.entities[0].length = len('/startCoding')
dp.process_update(Update(update_id=0, message=message))
sleep(0.5)
dp.job_queue.tick()
sleep(0.6)
assert handler.conversations.get((self.group.id, user1.id)) is None
assert not self.is_timeout

Expand All@@ -718,8 +713,7 @@ def test_conversation_handler_timeout_state_context(self, cdp, bot, user1):
message.text = '/brew'
message.entities[0].length = len('/brew')
cdp.process_update(Update(update_id=0, message=message))
sleep(0.5)
cdp.job_queue.tick()
sleep(0.6)
assert handler.conversations.get((self.group.id, user1.id)) is None
assert self.is_timeout

Expand All@@ -728,8 +722,7 @@ def test_conversation_handler_timeout_state_context(self, cdp, bot, user1):
message.text = '/start'
message.entities[0].length = len('/start')
cdp.process_update(Update(update_id=1, message=message))
sleep(0.5)
cdp.job_queue.tick()
sleep(0.6)
assert handler.conversations.get((self.group.id, user1.id)) is None
assert self.is_timeout

Expand All@@ -742,8 +735,7 @@ def test_conversation_handler_timeout_state_context(self, cdp, bot, user1):
message.text = '/startCoding'
message.entities[0].length = len('/startCoding')
cdp.process_update(Update(update_id=0, message=message))
sleep(0.5)
cdp.job_queue.tick()
sleep(0.6)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

doesnt it make sense to put this in a constant?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Not sure, if that's a good idea. Have one of the tests failing in the future, fiddle with the constant, suddenly everthing fails and it's not obvious why …

assert handler.conversations.get((self.group.id, user1.id)) is None
assert not self.is_timeout

Expand All@@ -759,7 +751,6 @@ def test_conversation_timeout_cancel_conflict(self, dp, bot, user1):
def slowbrew(_bot, update):
sleep(0.25)
# Let's give to the original timeout a chance to execute
dp.job_queue.tick()
sleep(0.25)
# By returning None we do not override the conversation state so
# we can see if the timeout has been executed
Expand All@@ -781,16 +772,13 @@ def slowbrew(_bot, update):
bot=bot)
dp.process_update(Update(update_id=0, message=message))
sleep(0.25)
dp.job_queue.tick()
message.text = '/slowbrew'
message.entities[0].length = len('/slowbrew')
dp.process_update(Update(update_id=0, message=message))
dp.job_queue.tick()
assert handler.conversations.get((self.group.id, user1.id)) is not None
assert not self.is_timeout

sleep(0.5)
dp.job_queue.tick()
sleep(0.6)
assert handler.conversations.get((self.group.id, user1.id)) is None
assert self.is_timeout

Expand Down
17 changes: 10 additions & 7 deletionstests/test_helpers.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,9 +86,10 @@ def test_to_float_timestamp_absolute_aware(self, timezone):
"""Conversion from timezone-aware datetime to timestamp"""
# we're parametrizing this with two different UTC offsets to exclude the possibility
# of an xpass when the test is run in a timezone with the same UTC offset
datetime = dtm.datetime(2019, 11, 11, 0, 26, 16, 10**5, tzinfo=timezone)
test_datetime = dtm.datetime(2019, 11, 11, 0, 26, 16, 10**5)
datetime = timezone.localize(test_datetime)
assert (helpers.to_float_timestamp(datetime)
== 1573431976.1 - timezone.utcoffset(None).total_seconds())
== 1573431976.1 - timezone.utcoffset(test_datetime).total_seconds())

def test_to_float_timestamp_absolute_no_reference(self):
"""A reference timestamp is only relevant for relative time specifications"""
Expand DownExpand Up@@ -116,14 +117,15 @@ def test_to_float_timestamp_time_of_day_timezone(self, timezone):
"""Conversion from timezone-aware time-of-day specification to timestamp"""
# we're parametrizing this with two different UTC offsets to exclude the possibility
# of an xpass when the test is run in a timezone with the same UTC offset
utc_offset = timezone.utcoffset(None)
ref_datetime = dtm.datetime(1970, 1, 1, 12)
utc_offset = timezone.utcoffset(ref_datetime)
ref_t, time_of_day = _datetime_to_float_timestamp(ref_datetime), ref_datetime.time()
aware_time_of_day = timezone.localize(ref_datetime).timetz()

# first test that naive time is assumed to be utc:
assert helpers.to_float_timestamp(time_of_day, ref_t) == pytest.approx(ref_t)
# test that by setting the timezone the timestamp changes accordingly:
assert (helpers.to_float_timestamp(time_of_day.replace(tzinfo=timezone), ref_t)
assert (helpers.to_float_timestamp(aware_time_of_day, ref_t)
== pytest.approx(ref_t + (-utc_offset.total_seconds() % (24 * 60 * 60))))

@pytest.mark.parametrize('time_spec', RELATIVE_TIME_SPECS, ids=str)
Expand All@@ -149,9 +151,10 @@ def test_from_timestamp_naive(self):
def test_from_timestamp_aware(self, timezone):
# we're parametrizing this with two different UTC offsets to exclude the possibility
# of an xpass when the test is run in a timezone with the same UTC offset
datetime = dtm.datetime(2019, 11, 11, 0, 26, 16, 10**5, tzinfo=timezone)
assert (helpers.from_timestamp(1573431976.1 - timezone.utcoffset(None).total_seconds())
== datetime)
test_datetime = dtm.datetime(2019, 11, 11, 0, 26, 16, 10 ** 5)
datetime = timezone.localize(test_datetime)
assert (helpers.from_timestamp(
1573431976.1 - timezone.utcoffset(test_datetime).total_seconds()) == datetime)

def test_create_deep_linked_url(self):
username = 'JamesTheMock'
Expand Down
3 changes: 1 addition & 2 deletionstests/test_inputfile.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,8 +51,7 @@ def test_subprocess_pipe(self):
def test_mimetypes(self):
# Only test a few to make sure logic works okay
assert InputFile(open('tests/data/telegram.jpg', 'rb')).mimetype == 'image/jpeg'
if sys.version_info >= (3, 5):
assert InputFile(open('tests/data/telegram.webp', 'rb')).mimetype == 'image/webp'
assert InputFile(open('tests/data/telegram.webp', 'rb')).mimetype == 'image/webp'
assert InputFile(open('tests/data/telegram.mp3', 'rb')).mimetype == 'audio/mpeg'

# Test guess from file
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp