mirror of
https://github.com/bellingcat/tiktok-hashtag-analysis.git
synced 2026-06-07 19:08:32 +03:00
added tests, changed __main__ to cli
This commit is contained in:
15
pytest.ini
Normal file
15
pytest.ini
Normal file
@@ -0,0 +1,15 @@
|
||||
[pytest]
|
||||
minversion =
|
||||
7.0.0
|
||||
testpaths =
|
||||
tests/
|
||||
python_files =
|
||||
*.py
|
||||
addopts =
|
||||
-vvv
|
||||
--cov='tiktok_hashtag_analysis'
|
||||
--cov-report html:reports/coverage
|
||||
--html='reports/tests.html'
|
||||
--self-contained-html
|
||||
filterwarnings =
|
||||
ignore:Glyph (.*) missing from current font
|
||||
3
setup.py
3
setup.py
@@ -16,6 +16,7 @@ setup(
|
||||
url="https://github.com/bellingcat/tiktok-hashtag-analysis",
|
||||
license="MIT License",
|
||||
install_requires=["seaborn", "matplotlib", "TikTokApi", "requests", "yt-dlp"],
|
||||
extras_require={"test": ["pytest", "pytest-cov", "pytest-html", "pytest-metadata"]},
|
||||
classifiers=[
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Intended Audience :: Information Technology",
|
||||
@@ -25,7 +26,7 @@ setup(
|
||||
],
|
||||
entry_points={
|
||||
"console_scripts": [
|
||||
"tiktok-hashtag-analysis=tiktok_hashtag_analysis.__main__:main",
|
||||
"tiktok-hashtag-analysis=tiktok_hashtag_analysis.cli:main",
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
24
tests/auth.py
Normal file
24
tests/auth.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import pytest
|
||||
|
||||
from tiktok_hashtag_analysis.auth import Authorization
|
||||
|
||||
MS_TOKEN = "thisisafakemstokenfortiktok"
|
||||
|
||||
|
||||
def test_auth_input(tmp_path, monkeypatch):
|
||||
config_file = tmp_path / ".tiktok"
|
||||
monkeypatch.setattr("builtins.input", lambda _: MS_TOKEN)
|
||||
auth = Authorization(config_file=config_file)
|
||||
auth.get_token()
|
||||
|
||||
assert auth.ms_token == MS_TOKEN
|
||||
|
||||
|
||||
def test_auth(tmp_path):
|
||||
config_file = tmp_path / ".tiktok"
|
||||
auth = Authorization(config_file=config_file)
|
||||
|
||||
auth.dump_token(ms_token=MS_TOKEN)
|
||||
auth.get_token()
|
||||
|
||||
assert auth.ms_token == MS_TOKEN
|
||||
15
tests/base.py
Normal file
15
tests/base.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from tiktok_hashtag_analysis.base import TikTokDownloader, load_hashtags_from_file
|
||||
|
||||
|
||||
def test_scrape(tmp_path, hashtags):
|
||||
downloader = TikTokDownloader(hashtags=hashtags[:1], data_dir=tmp_path)
|
||||
downloader.run(download=True, plot=True, table=True, number=20)
|
||||
|
||||
|
||||
def test_load_hashtags_from_file(tmp_path, hashtags):
|
||||
file = tmp_path / "hashtags.txt"
|
||||
with open(file, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(hashtags))
|
||||
|
||||
loaded_hashtags = load_hashtags_from_file(file=file)
|
||||
assert loaded_hashtags == hashtags
|
||||
31
tests/cli.py
Normal file
31
tests/cli.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import pytest
|
||||
|
||||
from tiktok_hashtag_analysis.cli import create_parser
|
||||
|
||||
ARGUMENTS = [
|
||||
("file", "hashtags.txt", "--file"),
|
||||
("download", True, "--download"),
|
||||
("download", True, "-d"),
|
||||
("number", 20, "--number"),
|
||||
("plot", True, "--plot"),
|
||||
("plot", True, "-p"),
|
||||
("table", True, "--table"),
|
||||
("table", True, "-t"),
|
||||
("output_dir", "/tmp/tiktok_download", "--output-dir"),
|
||||
("config", "~/.tiktok", "--config"),
|
||||
("log", "../logfile.log", "--log"),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("attribute,value,flag", ARGUMENTS)
|
||||
def test_parser(hashtags, attribute, value, flag):
|
||||
argument_list = [*hashtags, flag]
|
||||
|
||||
if not isinstance(value, bool):
|
||||
argument_list.append(str(value))
|
||||
|
||||
parser = create_parser()
|
||||
args = vars(parser.parse_args(argument_list))
|
||||
|
||||
assert args.get(attribute) == value
|
||||
assert args.get("hashtags") == hashtags
|
||||
11
tests/conftest.py
Normal file
11
tests/conftest.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
import pytest
|
||||
|
||||
TEST_HASHTAGS = ["embraceeuropa", "francisparkeryockey"]
|
||||
|
||||
|
||||
@pytest.fixture(scope="package")
|
||||
def hashtags():
|
||||
return TEST_HASHTAGS
|
||||
@@ -1 +1,3 @@
|
||||
__version__ = "2.0.0"
|
||||
|
||||
from .base import TikTokDownloader
|
||||
|
||||
@@ -15,7 +15,6 @@ class Authorization:
|
||||
self.config_file = Path.home() / ".tiktok"
|
||||
|
||||
self.section = "TikTok"
|
||||
self.get_token()
|
||||
|
||||
def get_token(self) -> str:
|
||||
"""Load the "msToken" cookie taken from TikTok, which the scraper requires."""
|
||||
|
||||
@@ -109,7 +109,7 @@ class TikTokDownloader:
|
||||
os.makedirs(self.data_dir, exist_ok=True)
|
||||
|
||||
self.auth = Authorization(config_file=config_file)
|
||||
self.ms_token = self.auth.ms_token
|
||||
self.ms_token = self.auth.get_token()
|
||||
|
||||
def get_hashtag_posts(self, hashtag: str):
|
||||
"""Fetch data about posts that used a specified hashtag and merge with
|
||||
|
||||
Reference in New Issue
Block a user