Browse Source

test _photo_command

Fabian Peter Hammerle 3 years ago
parent
commit
4effec5d8c
3 changed files with 122 additions and 0 deletions
  1. 8 0
      tests/conftest.py
  2. 12 0
      tests/test_photo.py
  3. 102 0
      tests/test_photo_command.py

+ 8 - 0
tests/conftest.py

@@ -17,9 +17,12 @@
 
 import json
 import pathlib
+import typing
 
 import pytest
 
+from location_guessing_game_telegram_bot import _Photo
+
 
 @pytest.fixture(scope="session")
 def wikimap_export_path() -> pathlib.Path:
@@ -36,3 +39,8 @@ def wikimap_export_path() -> pathlib.Path:
 @pytest.fixture(scope="session")
 def wikimap_export(wikimap_export_path) -> pathlib.Path:
     return json.loads(wikimap_export_path.read_text())
+
+
+@pytest.fixture(scope="session")
+def wikimap_photos(wikimap_export) -> typing.List[_Photo]:
+    return [_Photo.from_wikimap_export(attrs) for attrs in wikimap_export]

+ 12 - 0
tests/test_photo.py

@@ -45,6 +45,18 @@ from location_guessing_game_telegram_bot import _Photo
                 "/Gro%C3%9Fvenediger3.JPG",
             },
         ),
+        # coordinates["1"]
+        (
+            8,
+            {
+                "description_url": "https://commons.wikimedia.org/wiki"
+                "/File:Kasern_-_hinteres_Ahrntal.JPG",
+                "latitude": 47.06111,
+                "longitude": 12.15333,
+                "photo_url": "https://upload.wikimedia.org/wikipedia/commons/c/ce"
+                "/Kasern_-_hinteres_Ahrntal.JPG",
+            },
+        ),
     ),
 )
 def test_from_wikimap_export(wikimap_export, index, expected_vars):

+ 102 - 0
tests/test_photo_command.py

@@ -0,0 +1,102 @@
+# 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 logging
+import unittest.mock
+
+from location_guessing_game_telegram_bot import _photo_command
+
+
+def test_send_photo(caplog, wikimap_photos):
+    update_mock = unittest.mock.MagicMock()
+    update_mock.effective_chat.send_photo.return_value.message_id = "photo message id"
+    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 caplog.record_tuples == [
+        (
+            "location_guessing_game_telegram_bot",
+            logging.INFO,
+            "sending photo https://commons.wikimedia.org/wiki"
+            "/File:H%C3%BCtteltalkopf_(Venedigergruppe)_from_Tristkopf.jpg",
+        )
+    ]
+    update_mock.effective_chat.send_message.assert_called_once_with(
+        text="Neues Photo wird ausgewählt und gesendet.", disable_notification=True
+    )
+    urlopen_mock.assert_called_once_with(
+        "https://upload.wikimedia.org/wikipedia/commons/a/ab"
+        "/H%C3%BCtteltalkopf_%28Venedigergruppe%29_from_Tristkopf.jpg"
+    )
+    update_mock.effective_chat.send_photo.assert_called_once_with(
+        photo=http_response_mock.__enter__(),
+        caption="Wo wurde dieses Photo aufgenommen?",
+    )
+    assert context_mock.chat_data == {
+        "last_photo": wikimap_photos[0],
+        "last_photo_message_id": "photo message id",
+    }
+
+
+def test_send_solution_and_next_photo(caplog, wikimap_photos):
+    update_mock = unittest.mock.MagicMock()
+    update_mock.effective_chat.send_photo.return_value.message_id = (
+        "second photo message id"
+    )
+    context_mock = unittest.mock.MagicMock()
+    context_mock.bot_data = {"photos": wikimap_photos[1:2]}
+    context_mock.chat_data = {
+        "last_photo": wikimap_photos[0],
+        "last_photo_message_id": "first photo message id",
+    }
+    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 update_mock.effective_chat.send_message.call_count == 2
+    assert update_mock.effective_chat.send_message.call_args_list[
+        0
+    ] == unittest.mock.call(
+        text="Lösung: https://commons.wikimedia.org/wiki"
+        "/File:H%C3%BCtteltalkopf_(Venedigergruppe)_from_Tristkopf.jpg",
+        disable_web_page_preview=True,
+        reply_to_message_id="first photo message id",
+    )
+    update_mock.effective_chat.send_location.assert_called_once_with(
+        # float comparison? :O
+        latitude=47.288805,
+        longitude=12.144116,
+        disable_notification=True,
+    )
+    # next photo:
+    urlopen_mock.assert_called_once_with(
+        "https://upload.wikimedia.org/wikipedia/commons/6/65/Gro%C3%9Fvenediger3.JPG"
+    )
+    update_mock.effective_chat.send_photo.assert_called_once_with(
+        photo=http_response_mock.__enter__(),
+        caption="Wo wurde dieses Photo aufgenommen?",
+    )
+    assert context_mock.chat_data == {
+        "last_photo": wikimap_photos[1],
+        "last_photo_message_id": "second photo message id",
+    }