From c0af4642659a6754390b253a64d12c61b67ec849 Mon Sep 17 00:00:00 2001 From: Nautics889 Date: Sat, 10 Feb 2024 19:31:31 +0200 Subject: [PATCH] tests: create tests for `Album` endpoint * (tests): tests for `.get_album()` of `Album` * (tests): tests for `.create()` of `Album` * (tests): add fixtures for album get and post methods --- CONTRIBUTING.md | 20 +++---- tests/conftest.py | 14 +++++ .../templates/imgur_album_get_200.json.j2 | 55 +++++++++++++++++++ .../templates/imgur_album_post_200.json.j2 | 8 +++ tests/test_album.py | 48 ++++++++++++++++ 5 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 tests/fixtures/templates/imgur_album_get_200.json.j2 create mode 100644 tests/fixtures/templates/imgur_album_post_200.json.j2 create mode 100644 tests/test_album.py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 10403e5..40c2847 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -45,16 +45,16 @@ $ poetry run flake8 . --max-doc-length 72 --show-source Here's a table which illustrates current progress of endpoints implementation: -| Endpoint | Implementation | Tests | Documentation | -|:---------------------------------------------------------------------------------------------------|:---------------|:------------|:--------------| -| [Account](https://api.imgur.com/endpoints/account) | In progress | In progress | In progress | -| [Album](https://api.imgur.com/endpoints/album) | Done ✓ | Not impl. | Not impl. | -| [Comment](https://api.imgur.com/endpoints/comment) | Done ✓ | Done ✓ | In progress | -| Feed | Not impl. | Not impl. | Not impl. | -| [Gallery](https://api.imgur.com/endpoints/gallery) | Not impl. | Not impl. | Not impl. | -| [Image](https://api.imgur.com/endpoints/image) | Done ✓ | Done ✓ | Done ✓ | -| [Memegen](https://api.imgur.com/endpoints/memegen) | Not impl. | Not impl. | Not impl. | -| [Notification](https://api.imgur.com/endpoints/notification) | Not impl. | Not impl. | Not impl. | +| Endpoint | Implementation | Tests | Documentation | +|:---------------------------------------------------------------------------------------------------|:---------------|:-------------|:--------------| +| [Account](https://api.imgur.com/endpoints/account) | In progress | In progress | In progress | +| [Album](https://api.imgur.com/endpoints/album) | Done ✓ | In progress | Not impl. | +| [Comment](https://api.imgur.com/endpoints/comment) | Done ✓ | Done ✓ | In progress | +| Feed | Not impl. | Not impl. | Not impl. | +| [Gallery](https://api.imgur.com/endpoints/gallery) | Not impl. | Not impl. | Not impl. | +| [Image](https://api.imgur.com/endpoints/image) | Done ✓ | Done ✓ | Done ✓ | +| [Memegen](https://api.imgur.com/endpoints/memegen) | Not impl. | Not impl. | Not impl. | +| [Notification](https://api.imgur.com/endpoints/notification) | Not impl. | Not impl. | Not impl. | Additionally, there are some preferable features that would be great to implement in the package: * Method for bulk uploading images (e.g. from directory). diff --git a/tests/conftest.py b/tests/conftest.py index aff8f11..24b3985 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -41,6 +41,20 @@ def imgur_image_post_200_response(): return ResponseFixture(status=200, reason="OK", content=content) +@pytest.fixture +def imgur_album_get_200_response(): + template = get_template("imgur_album_get_200.json.j2") + content = template.render(id=get_random_imgur_id()).encode("utf-8") + return ResponseFixture(status=200, reason="OK", content=content) + + +@pytest.fixture +def imgur_album_post_200_response(): + template = get_template("imgur_album_post_200.json.j2") + content = template.render(id=get_random_imgur_id()).encode("utf-8") + return ResponseFixture(status=200, reason="OK", content=content) + + @pytest.fixture def imgur_comment_get_200_response(): template = get_template("imgur_comment_get_200.json.j2") diff --git a/tests/fixtures/templates/imgur_album_get_200.json.j2 b/tests/fixtures/templates/imgur_album_get_200.json.j2 new file mode 100644 index 0000000..6193400 --- /dev/null +++ b/tests/fixtures/templates/imgur_album_get_200.json.j2 @@ -0,0 +1,55 @@ +{ + "data": { + "id": "{{ id }}", + "title": "{{ title or 'Test album' }}", + "description": "lorem ipsum dolor sit amet", + "datetime": {{ datetime or range(1000000000, 1899999999) | random}}, + "cover": null, + "cover_edited": null, + "cover_width": null, + "cover_height": null, + "account_url": null, + "account_id": {{ account_id or range(1000000, 99999999) | random }}, + "privacy": "hidden", + "layout": "blog", + "views": 0, + "link": "https://imgur.com/a/{{ id }}", + "favorite": false, + "nsfw": false, + "section": null, + "images_count": 0, + "in_gallery": false, + "is_ad": false, + "include_album_ads": false, + "is_album": true, + "deletehash": "{{ delete_hash or 'abcdefghijklmno' }}", + "images": [], + "ad_config": { + "safeFlags": [ + "not_in_gallery", + "share" + ], + "highRiskFlags": [], + "unsafeFlags": [ + "updated_date" + ], + "wallUnsafeFlags": [], + "showsAds": false, + "showAdLevel": 1, + "safe_flags": [ + "not_in_gallery", + "share" + ], + "high_risk_flags": [], + "unsafe_flags": [ + "updated_date" + ], + "wall_unsafe_flags": [], + "show_ads": false, + "show_ad_level": 1, + "nsfw_score": 0 + } + }, + "success": true, + "status": 200 +} diff --git a/tests/fixtures/templates/imgur_album_post_200.json.j2 b/tests/fixtures/templates/imgur_album_post_200.json.j2 new file mode 100644 index 0000000..1902612 --- /dev/null +++ b/tests/fixtures/templates/imgur_album_post_200.json.j2 @@ -0,0 +1,8 @@ +{ + "data": { + "id": "{{ id }}", + "deletehash": "{{ delete_hash or 'abcdefghijklmno' }}" + }, + "success": true, + "status": 200 +} diff --git a/tests/test_album.py b/tests/test_album.py new file mode 100644 index 0000000..2f36d37 --- /dev/null +++ b/tests/test_album.py @@ -0,0 +1,48 @@ +from unittest.mock import patch + +from pyimgurapi.endpoints import Album +from pyimgurapi.utils import DynamicResponseData + + +class TestAlbum: + @patch("urllib.request.urlopen") + def test_get_album(self, urlopen_mock, imgur_album_get_200_response): + urlopen_mock.return_value = imgur_album_get_200_response + album_id = ( + imgur_album_get_200_response.json().get("data", {}).get("id") + ) + + album = Album() + res = album.get_album(album_id) + + urlopen_mock.assert_called_once() + assert urlopen_mock.call_args[0][0].method == "GET" + assert album_id in urlopen_mock.call_args[0][0].full_url + assert isinstance(res, DynamicResponseData) + assert res.data.id == album_id + + @patch("urllib.request.urlopen") + def test_create( + self, + urlopen_mock, + imgur_album_post_200_response, + title_fixture, + description_fixture, + ): + urlopen_mock.return_value = imgur_album_post_200_response + + album = Album() + res = album.create( + title=title_fixture, description=description_fixture + ) + + urlopen_mock.assert_called_once() + assert urlopen_mock.call_args[0][0].method == "POST" + assert urlopen_mock.call_args[0][0].full_url.endswith("/album") + assert isinstance(res, DynamicResponseData) + assert title_fixture in urlopen_mock.call_args[0][0].data.decode( + "utf-8" + ) + assert description_fixture in urlopen_mock.call_args[0][0].data.decode( + "utf-8" + )