Skip to content

Commit

Permalink
Add tiktok tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mariocj89 committed Jul 1, 2024
1 parent 41dc894 commit 694242c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
1 change: 1 addition & 0 deletions eas/api/tests/int/data/lamatok-success-response.json

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions eas/api/tests/int/test_lamatok.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pathlib

import pytest
import requests_mock

from eas.api.tiktok import InvalidURL, NotFoundError, get_comments

RESPONSES_PATH = pathlib.Path(__file__, "..", "data").resolve()
SUCCESS_RESPONSE = RESPONSES_PATH.joinpath("lamatok-success-response.json").read_text()


@pytest.fixture
def requestsm():
with requests_mock.Mocker() as m:
yield m


def test_success_response(requestsm):
url = "https://www.tiktok.com/@fanmallorcashopping/video/7385245034506964257"
requestsm.get(
"https://api.lamatok.com/v1/media/comments/by/id", text=SUCCESS_RESPONSE
)
comments = get_comments(url)
assert len(comments) == 20


def test_post_no_comments(requestsm):
url = "https://www.tiktok.com/@echaloasuerte/video/7382573284749102368"
requestsm.get(
"https://api.lamatok.com/v1/media/comments/by/id",
status_code=404,
text='{"detail":"Comments (or media) Not found","exc_type":"CommentsNotFoundError","tt_status_code":0}',
)
with pytest.raises(NotFoundError):
get_comments(url)


def test_fail_on_fake_url(requestsm):
url = "https://www.tiktok.com/@echaloasuerte/video/7382573284749102369"
requestsm.get(
"https://api.lamatok.com/v1/media/comments/by/id",
status_code=404,
text='{"detail":"Comments (or media) Not found","exc_type":"CommentsNotFoundError","tt_status_code":0}',
)
with pytest.raises(NotFoundError):
get_comments(url)


def test_min_mentions_filter(requestsm):
url = "https://www.tiktok.com/@fanmallorcashopping/video/7385245034506964257"
requestsm.get(
"https://api.lamatok.com/v1/media/comments/by/id", text=SUCCESS_RESPONSE
)
comments = get_comments(url, min_mentions=1)
assert len(comments) == 17
comments = get_comments(url, min_mentions=2)
assert len(comments) == 13
comments = get_comments(url, min_mentions=3)
assert len(comments) == 0
7 changes: 2 additions & 5 deletions eas/api/tiktok/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
TIKTOK_RE = re.compile(r"/video/([^?/&]*)")
ONE_MINUTE = 60


class NotFoundError(Exception):
pass


NotFoundError = lamatok.NotFoundError
InvalidURL = lamatok.InvalidURL
TiktokTimeoutError = requests.exceptions.ConnectionError


Expand Down
10 changes: 9 additions & 1 deletion eas/api/tiktok/lamatok.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
ONE_MINUTE = 60 # seconds


class NotFoundError(Exception):
pass


class InvalidURL(Exception):
pass


@functools.lru_cache(None)
def _session(): # pragma: no cover
return requests.Session()
Expand Down Expand Up @@ -40,7 +48,7 @@ def fetch_comments(media_pk): # pragma: no cover
if not response.ok:
LOG.warning("Failed lamatok request! %s", response.text)
with contextlib.suppress(Exception):
if response.json()["exc_type"] == "NotFoundError":
if response.json()["exc_type"] == "CommentsNotFoundError":
return []
response.raise_for_status()
return response.json()["comments"]

0 comments on commit 694242c

Please sign in to comment.