Browse Source

test _Photo class

Fabian Peter Hammerle 3 years ago
parent
commit
0a9aab39c4

+ 38 - 0
tests/conftest.py

@@ -0,0 +1,38 @@
+# 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 json
+import pathlib
+
+import pytest
+
+
+@pytest.fixture(scope="session")
+def wikimap_export_path() -> pathlib.Path:
+    return pathlib.Path(__file__).parent.joinpath(
+        "resources",
+        "wikimap.toolforge.org",
+        "api.php@cat=Images_with_annotations&lang=de&year=2020-2020&region=48%7C12%7C47%7C13",
+    )
+
+
+# pylint: disable=redefined-outer-name; fixture
+
+
+@pytest.fixture(scope="session")
+def wikimap_export(wikimap_export_path) -> pathlib.Path:
+    return json.loads(wikimap_export_path.read_text())

+ 1 - 0
tests/resources/wikimap.toolforge.org/.gitattributes

@@ -0,0 +1 @@
+api.php@* filter=lfs diff=lfs merge=lfs -text

+ 3 - 0
tests/resources/wikimap.toolforge.org/README.md

@@ -0,0 +1,3 @@
+```sh
+wget --restrict-file-names=windows 'https://wikimap.toolforge.org/api.php?cat=Images_with_annotations&lang=de&year=2020-2020&region=48|12|47|13'
+```

+ 3 - 0
tests/resources/wikimap.toolforge.org/api.php@cat=Images_with_annotations&lang=de&year=2020-2020&region=48%7C12%7C47%7C13

@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:0eb20424c37f7c1546468942dd39aef299fc7fee16f0082c88aff50700192f5f
+size 34297

+ 68 - 0
tests/test_photo.py

@@ -0,0 +1,68 @@
+# 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 pytest
+
+from location_guessing_game_telegram_bot import _Photo
+
+
+@pytest.mark.parametrize(
+    ("index", "expected_vars"),
+    (
+        (
+            0,
+            {
+                "description_url": "https://commons.wikimedia.org/wiki"
+                "/File:H%C3%BCtteltalkopf_(Venedigergruppe)_from_Tristkopf.jpg",
+                "latitude": 47.288805,
+                "longitude": 12.144116,
+                "photo_url": "https://upload.wikimedia.org/wikipedia/commons/a/ab"
+                "/H%C3%BCtteltalkopf_%28Venedigergruppe%29_from_Tristkopf.jpg",
+            },
+        ),
+        (
+            1,
+            {
+                "description_url": "https://commons.wikimedia.org/wiki"
+                "/File:Gro%C3%9Fvenediger3.JPG",
+                "latitude": 47.24854167,
+                "longitude": 12.25381667,
+                "photo_url": "https://upload.wikimedia.org/wikipedia/commons/6/65"
+                "/Gro%C3%9Fvenediger3.JPG",
+            },
+        ),
+    ),
+)
+def test_from_wikimap_export(wikimap_export, index, expected_vars):
+    # https://github.com/pytest-dev/pytest/issues/3164 recursive pytest.approx not available
+    assert vars(_Photo.from_wikimap_export(wikimap_export[index])) == expected_vars
+
+
+def test___str__():
+    assert (
+        str(
+            _Photo(
+                photo_url="https://upload.wikimedia.org/wikipedia/commons/6/65"
+                "/Gro%C3%9Fvenediger3.JPG",
+                description_url="https://commons.wikimedia.org/wiki"
+                "/File:Gro%C3%9Fvenediger3.JPG",
+                latitude=47.24854167,
+                longitude=12.25381667,
+            )
+        )
+        == "photo https://commons.wikimedia.org/wiki/File:Gro%C3%9Fvenediger3.JPG"
+    )