From e60318166e45cd77986026da40427022a9e2faba Mon Sep 17 00:00:00 2001 From: ikkemaniac Date: Tue, 23 Jun 2020 22:25:58 +0000 Subject: [PATCH] Add test for clean argument of Updater.start_polling/webhook (#2002) * added test for 'clean' argument passed to 'start_polling()' * remove TODO * prettify * remove bool from func name * improve name-ing of fake update func * cleanup class and nameing * replace while for for * swap valueerror for runtimeerror * remove all other code to reduce testing * add comments * don't raise error, complete cycle and assert * remove inf loop protection * Revert "remove all other code to reduce testing" This reverts commit 4566a1debd659ce831a52085867c5a74d1575262. * remove error parametrization * remove comment * remove pass from class * rename update_id to offset as the original get_updates() takes argument offset (which is the update_id) * rename test func to match original func * fix comment * shorten for loop * mock get_updates() behavior when 'offset' is passed. Assert with get_updates() * remove other functions to reduce testing * replicate original get_updates() * move fakeupdate class and list creation outside get_updates and store in var * loop from 0 to make update_id consistant w array key, just easier to debug * update comments * Revert "remove other functions to reduce testing" This reverts commit 1fb498a6ccb619a745b745d61b8345bf1b29b6ba. * fix typo * Revert "fix typo" This reverts commit ade9fec609d7262051b24d17fa6b5b1cf579d760. * Revert "Revert "remove other functions to reduce testing"" This reverts commit 734de1371cfddbd003ceb3c4498e837bab55bd05. * Revert "update comments" This reverts commit f3a032e75eee8d2416a236208b71c35ccc237345. * Revert "loop from 0 to make update_id consistant w array key, just easier to debug" This reverts commit 0c6881d8a1066d5762131d28fb350eaeb92b341c. * Revert "move fakeupdate class and list creation outside get_updates and store in var" This reverts commit 71de999300f052afd6b0ffe2914bfdfc201b6b11. * Revert "replicate original get_updates()" This reverts commit 5d0710ac3a8e6f6ddf628b1ad1d53bfa219decdc. * Revert "remove other functions to reduce testing" This reverts commit 1fb498a6ccb619a745b745d61b8345bf1b29b6ba. * Revert "mock get_updates() behavior when 'offset' is passed. Assert with get_updates()" This reverts commit 8c727ba1e814871a59d7ae66d3dcf411847330d2. * loop from 0 to make update_id consistant w array key, for consitency Co-authored-by: ikkemaniac --- tests/test_updater.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/tests/test_updater.py b/tests/test_updater.py index f57dfdea4..7eb722ff9 100644 --- a/tests/test_updater.py +++ b/tests/test_updater.py @@ -75,6 +75,7 @@ class TestUpdater: attempts = 0 err_handler_called = Event() cb_handler_called = Event() + offset = 0 @pytest.fixture(autouse=True) def reset(self): @@ -92,8 +93,6 @@ class TestUpdater: self.received = update.message.text self.cb_handler_called.set() - # TODO: test clean= argument of Updater._bootstrap - @pytest.mark.parametrize(('error',), argvalues=[(TelegramError('Test Error 2'),), (Unauthorized('Test Unauthorized'),)], @@ -331,6 +330,42 @@ class TestUpdater: updater._bootstrap(retries, False, 'path', None, bootstrap_interval=0) assert self.attempts == attempts + def test_bootstrap_clean_updates(self, monkeypatch, updater): + clean = True + expected_id = 4 + self.offset = 0 + + def get_updates(*args, **kwargs): + # we're hitting this func twice + # 1. no args, return list of updates + # 2. with 1 arg, int => if int == expected_id => test successful + + # case 2 + # 2nd call from bootstrap____clean + # we should be called with offset = 4 + # save value passed in self.offset for assert down below + if len(args) > 0: + self.offset = int(args[0]) + return [] + + class FakeUpdate(): + def __init__(self, update_id): + self.update_id = update_id + + # case 1 + # return list of obj's + + # build list of fake updates + # returns list of 4 objects with + # update_id's 0, 1, 2 and 3 + return [FakeUpdate(i) for i in range(0, expected_id)] + + monkeypatch.setattr(updater.bot, 'get_updates', get_updates) + + updater.running = True + updater._bootstrap(1, clean, None, None, bootstrap_interval=0) + assert self.offset == expected_id + @flaky(3, 1) def test_webhook_invalid_posts(self, updater): ip = '127.0.0.1'