Skip to content

Commit

Permalink
add method get_comments_on_answers to class Site
Browse files Browse the repository at this point in the history
  • Loading branch information
wchistow committed Jul 20, 2023
1 parent 98c115d commit d8f60b1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
8 changes: 4 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
+ [X] `get_posts(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]` - API method `posts/` and `posts/{ids}`;
+ [X] `get_post(self, c_id: int, **kwargs: Any) -> Item | None`;
+ [X] `get_suggested_edits(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]` - API method `suggested-edits/` and `suggested-edits/{ids}`;
+ [ ] `get_comments_on_answers(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]` - API method `answers/{ids}/comments`;
+ [ ] `get_comments_on_articles(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]` - API method `articles/{ids}/comments`;
+ [ ] `get_comments_on_posts(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]` - API method `posts/{ids}/comments`;
+ [ ] `get_comments_on_questions(self, ids: list[int] | None = None, **kwargs: Any) -> list[Item]` - API method `questions/{ids}/comments`;
+ [X] `get_comments_on_answers(self, ids: list[int], **kwargs: Any) -> list[Item]` - API method `answers/{ids}/comments`;
+ [ ] `get_comments_on_articles(self, ids: list[int], **kwargs: Any) -> list[Item]` - API method `articles/{ids}/comments`;
+ [ ] `get_comments_on_posts(self, ids: list[int], **kwargs: Any) -> list[Item]` - API method `posts/{ids}/comments`;
+ [ ] `get_comments_on_questions(self, ids: list[int], **kwargs: Any) -> list[Item]` - API method `questions/{ids}/comments`;
+ [X] `get_non_tag_based_badges(self, **kwargs: Any) -> list[Item]` - API method `badges/name`;
+ [X] `IndexError` raises in methods `Site.get_<singular>`, when there is no items in response;
+ [X] add constant `API_VERSION` to file `tests/test_client/__init__.py` and usage of it to tests.
Expand Down
7 changes: 7 additions & 0 deletions src/pystackapi/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ def get_comment(self, c_id: int, **kwargs: Any) -> Item | None:
except IndexError:
return None

def get_comments_on_answers(self, ids: list[int], **kwargs: Any) -> list[Item]:
"""Returns the comments on a set of answers."""
if not ids:
raise BadArgumentsError('the `ids` argument should be a non empty list.')
return [Item(data) for data in
self.get(f'answers/{";".join(map(str, ids))}/comments/', **kwargs)['items']]

def get_info(self) -> Item:
"""Returns a collection of statistics about the site."""
return Item(self.get('info/')['items'][0]) # here can't be `IndexError`
Expand Down
20 changes: 19 additions & 1 deletion tests/test_client/test_get_comments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for `Site.get_comments` and `Site.get_comment`."""
"""Tests for `Site.get_comments`, `Site.get_comment` and `Site.get_comments_on_*`."""
import lest

from pystackapi import site as site_m
Expand Down Expand Up @@ -64,3 +64,21 @@ def test_get_comment_with_no_data() -> None:
lest.assert_true(res is None)

requests.no_data = []


# ---- tests for `Site.get_comments_on_answers` ---


@lest.register
def test_get_comments_on_answers_url() -> None:
site.get_comments_on_answers([1, 2])

lest.assert_eq(requests.url, f'https://api.stackexchange.com/{API_VERSION}/answers/1;2/'
f'comments/?site=stackoverflow')


@lest.register
def test_get_comments_on_answers_return_value() -> None:
res = site.get_comments_on_answers([1, 2])

lest.assert_eq(res, [Item({'id': 1})])

0 comments on commit d8f60b1

Please sign in to comment.