Selaa lähdekoodia

add test for _photo_command handling telegram.error.TimedOut

Fabian Peter Hammerle 6 kuukautta sitten
vanhempi
commit
671d47cb2e
2 muutettua tiedostoa jossa 34 lisäystä ja 1 poistoa
  1. 1 1
      .github/workflows/python.yml
  2. 33 0
      tests/test_photo_command.py

+ 1 - 1
.github/workflows/python.yml

@@ -58,7 +58,7 @@ jobs:
       env:
         PYTHON_VERSION: ${{ matrix.python-version }}
     - run: pipenv graph
-    - run: pipenv run pytest --cov="$(cat *.egg-info/top_level.txt)" --cov-report=term-missing --cov-fail-under=77
+    - run: pipenv run pytest --cov="$(cat *.egg-info/top_level.txt)" --cov-report=term-missing --cov-fail-under=100
     - run: pipenv run pylint "$(cat *.egg-info/top_level.txt)"
     # workaround pylint reporting:
     # > E0401: Unable to import 'ical2vdir' (import-error)

+ 33 - 0
tests/test_photo_command.py

@@ -148,3 +148,36 @@ def test__photo_command_file_size_exceeded(caplog, wikimap_photos):
         "last_photo": wikimap_photos[0],
         "last_photo_message_id": "photo message id",
     }
+
+
+def test__photo_command_timeout(caplog, wikimap_photos):
+    update_mock = unittest.mock.MagicMock()
+    update_mock.effective_chat.send_photo.side_effect = [
+        telegram.error.TimedOut
+    ] * 3 + [
+        collections.namedtuple("Photo", ["message_id"])(
+            message_id="photo message id after timeout"
+        ),
+    ]
+    context_mock = unittest.mock.MagicMock()
+    context_mock.bot_data = {"photos": wikimap_photos[:1]}
+    context_mock.chat_data = {}
+    http_response_mock = unittest.mock.MagicMock()
+    with unittest.mock.patch(
+        "urllib.request.urlopen", return_value=http_response_mock
+    ) as urlopen_mock, caplog.at_level(logging.INFO):
+        _photo_command(update=update_mock, context=context_mock)
+    assert urlopen_mock.call_count == 3 + 1
+    assert update_mock.effective_chat.send_photo.call_count == 3 + 1
+    assert len(caplog.records) == 3 * 2 + 1
+    assert caplog.record_tuples[1::2] == [
+        (
+            "location_guessing_game_telegram_bot",
+            logging.WARNING,
+            "timeout",
+        )
+    ] * (3)
+    assert context_mock.chat_data == {
+        "last_photo": wikimap_photos[0],
+        "last_photo_message_id": "photo message id after timeout",
+    }