test_run.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # location-guessing-game-telegram-bot - Telegram Bot Sending Random Wikimedia Commons Photos
  2. #
  3. # Copyright (C) 2021 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import unittest.mock
  18. from location_guessing_game_telegram_bot import _run
  19. def test__run(tmp_path, wikimap_export_path):
  20. telegram_token_path = tmp_path.joinpath("token")
  21. telegram_token_path.write_text("secret\n")
  22. with unittest.mock.patch("telegram.ext.Updater") as updater_mock:
  23. _run(
  24. telegram_token_path=telegram_token_path,
  25. wikimap_export_path=wikimap_export_path,
  26. )
  27. assert updater_mock.call_count == 1
  28. # > Changed in version 3.8: Added args and kwargs properties.
  29. # https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.call_args_list
  30. init_args, init_kwargs = updater_mock.call_args
  31. assert not init_args
  32. assert set(init_kwargs.keys()) == {
  33. "persistence",
  34. "token",
  35. "use_context",
  36. }
  37. assert init_kwargs["token"] == "secret"
  38. assert init_kwargs["use_context"] is True
  39. photos = init_kwargs["persistence"].get_bot_data()["photos"]
  40. assert len(photos) == 25
  41. assert (
  42. photos[1].photo_url
  43. == "https://upload.wikimedia.org/wikipedia/commons/6/65/Gro%C3%9Fvenediger3.JPG"
  44. )