Skip to content

Commit

Permalink
tests: create tests for Album endpoint
Browse files Browse the repository at this point in the history
* (tests): tests for `.get_album()` of `Album`
* (tests): tests for `.create()` of `Album`
* (tests): add fixtures for album get and post methods
  • Loading branch information
nautics889 committed Feb 10, 2024
1 parent bdb7af0 commit c0af464
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 10 deletions.
20 changes: 10 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
14 changes: 14 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
55 changes: 55 additions & 0 deletions tests/fixtures/templates/imgur_album_get_200.json.j2
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 8 additions & 0 deletions tests/fixtures/templates/imgur_album_post_200.json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"data": {
"id": "{{ id }}",
"deletehash": "{{ delete_hash or 'abcdefghijklmno' }}"
},
"success": true,
"status": 200
}
48 changes: 48 additions & 0 deletions tests/test_album.py
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit c0af464

Please sign in to comment.