test_photo_command.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 logging
  18. import unittest.mock
  19. # pylint: disable=import-private-name; tests
  20. from location_guessing_game_telegram_bot import _photo_command
  21. def test_send_photo(caplog, wikimap_photos):
  22. update_mock = unittest.mock.MagicMock()
  23. update_mock.effective_chat.send_photo.return_value.message_id = "photo message id"
  24. context_mock = unittest.mock.MagicMock()
  25. context_mock.bot_data = {"photos": wikimap_photos[:1]}
  26. context_mock.chat_data = {}
  27. http_response_mock = unittest.mock.MagicMock()
  28. with unittest.mock.patch(
  29. "urllib.request.urlopen", return_value=http_response_mock
  30. ) as urlopen_mock, caplog.at_level(logging.INFO):
  31. _photo_command(update=update_mock, context=context_mock)
  32. assert caplog.record_tuples == [
  33. (
  34. "location_guessing_game_telegram_bot",
  35. logging.INFO,
  36. "sending photo https://commons.wikimedia.org/wiki"
  37. "/File:H%C3%BCtteltalkopf_(Venedigergruppe)_from_Tristkopf.jpg",
  38. )
  39. ]
  40. update_mock.effective_chat.send_message.assert_called_once_with(
  41. text="Neues Photo wird ausgewählt und gesendet.", disable_notification=True
  42. )
  43. urlopen_mock.assert_called_once_with(
  44. "https://upload.wikimedia.org/wikipedia/commons/a/ab"
  45. "/H%C3%BCtteltalkopf_%28Venedigergruppe%29_from_Tristkopf.jpg"
  46. )
  47. update_mock.effective_chat.send_photo.assert_called_once_with(
  48. photo=http_response_mock.__enter__(), # pylint: disable=unnecessary-dunder-call
  49. caption="Wo wurde dieses Photo aufgenommen?",
  50. )
  51. assert context_mock.chat_data == {
  52. "last_photo": wikimap_photos[0],
  53. "last_photo_message_id": "photo message id",
  54. }
  55. def test_send_solution_and_next_photo(caplog, wikimap_photos):
  56. update_mock = unittest.mock.MagicMock()
  57. update_mock.effective_chat.send_photo.return_value.message_id = (
  58. "second photo message id"
  59. )
  60. update_mock.effective_chat.id = "chat id for send_location"
  61. context_mock = unittest.mock.MagicMock()
  62. context_mock.bot_data = {"photos": wikimap_photos[1:2]}
  63. context_mock.chat_data = {
  64. "last_photo": wikimap_photos[0],
  65. "last_photo_message_id": "first photo message id",
  66. }
  67. http_response_mock = unittest.mock.MagicMock()
  68. with unittest.mock.patch(
  69. "urllib.request.urlopen", return_value=http_response_mock
  70. ) as urlopen_mock, caplog.at_level(logging.INFO):
  71. _photo_command(update=update_mock, context=context_mock)
  72. assert update_mock.effective_chat.send_message.call_count == 2
  73. assert update_mock.effective_chat.send_message.call_args_list[
  74. 0
  75. ] == unittest.mock.call(
  76. text="Lösung: https://commons.wikimedia.org/wiki"
  77. "/File:H%C3%BCtteltalkopf_(Venedigergruppe)_from_Tristkopf.jpg",
  78. disable_web_page_preview=True,
  79. reply_to_message_id="first photo message id",
  80. )
  81. # update_mock.effective_chat.send_location.assert_called_once_with(
  82. context_mock.bot.send_location.assert_called_once_with(
  83. chat_id="chat id for send_location",
  84. # float comparison? :o
  85. latitude=47.288805,
  86. longitude=12.144116,
  87. disable_notification=True,
  88. )
  89. # next photo:
  90. urlopen_mock.assert_called_once_with(
  91. "https://upload.wikimedia.org/wikipedia/commons/6/65/Gro%C3%9Fvenediger3.JPG"
  92. )
  93. update_mock.effective_chat.send_photo.assert_called_once_with(
  94. photo=http_response_mock.__enter__(), # pylint: disable=unnecessary-dunder-call
  95. caption="Wo wurde dieses Photo aufgenommen?",
  96. )
  97. assert context_mock.chat_data == {
  98. "last_photo": wikimap_photos[1],
  99. "last_photo_message_id": "second photo message id",
  100. }