test_photo_command.py 4.3 KB

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