Explorar el Código

test updater initialization

Fabian Peter Hammerle hace 3 años
padre
commit
0a9465d7b8
Se han modificado 2 ficheros con 63 adiciones y 10 borrados
  1. 17 10
      location_guessing_game_telegram_bot/__init__.py
  2. 46 0
      tests/test_run.py

+ 17 - 10
location_guessing_game_telegram_bot/__init__.py

@@ -147,6 +147,20 @@ class _EnvDefaultArgparser(argparse.ArgumentParser):
         super().add_argument(*args, **kwargs)
 
 
+def _run(telegram_token_path: pathlib.Path, wikimap_export_path: pathlib.Path) -> None:
+    photos = [
+        _Photo.from_wikimap_export(attrs)
+        for attrs in json.loads(wikimap_export_path.read_text())
+    ]
+    updater = telegram.ext.Updater(
+        token=telegram_token_path.read_text().rstrip(),
+        use_context=True,
+        persistence=_Persistence(photos=photos),
+    )
+    updater.dispatcher.add_handler(telegram.ext.CommandHandler("photo", _photo_command))
+    updater.start_polling()
+
+
 def _main():
     argparser = _EnvDefaultArgparser()
     argparser.add_argument(
@@ -175,14 +189,7 @@ def _main():
         datefmt="%Y-%m-%dT%H:%M:%S%z",
     )
     _LOGGER.debug("args=%r", args)
-    photos = [
-        _Photo.from_wikimap_export(attrs)
-        for attrs in json.loads(args.wikimap_export_path.read_text())
-    ]
-    updater = telegram.ext.Updater(
-        token=args.telegram_token_path.read_text().rstrip(),
-        use_context=True,
-        persistence=_Persistence(photos=photos),
+    _run(
+        telegram_token_path=args.telegram_token_path,
+        wikimap_export_path=args.wikimap_export_path,
     )
-    updater.dispatcher.add_handler(telegram.ext.CommandHandler("photo", _photo_command))
-    updater.start_polling()

+ 46 - 0
tests/test_run.py

@@ -0,0 +1,46 @@
+# location-guessing-game-telegram-bot - Telegram Bot Sending Random Wikimedia Commons Photos
+#
+# Copyright (C) 2021 Fabian Peter Hammerle <fabian@hammerle.me>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+import unittest.mock
+
+from location_guessing_game_telegram_bot import _run
+
+
+def test__run(tmp_path, wikimap_export_path):
+    telegram_token_path = tmp_path.joinpath("token")
+    telegram_token_path.write_text("secret\n")
+    with unittest.mock.patch("telegram.ext.Updater") as updater_mock:
+        _run(
+            telegram_token_path=telegram_token_path,
+            wikimap_export_path=wikimap_export_path,
+        )
+    assert updater_mock.call_count == 1
+    assert not updater_mock.call_args.args
+    assert set(updater_mock.call_args.kwargs.keys()) == {
+        "persistence",
+        "token",
+        "use_context",
+    }
+    assert updater_mock.call_args.kwargs["token"] == "secret"
+    assert updater_mock.call_args.kwargs["use_context"] is True
+    persistence = updater_mock.call_args.kwargs["persistence"]
+    photos = persistence.get_bot_data()["photos"]
+    assert len(photos) == 25
+    assert (
+        photos[1].photo_url
+        == "https://upload.wikimedia.org/wikipedia/commons/6/65/Gro%C3%9Fvenediger3.JPG"
+    )