added tests, changed __main__ to cli

This commit is contained in:
Tristan Lee
2023-09-04 13:26:38 -05:00
parent 0f8e865bf3
commit 5ae9624968
11 changed files with 101 additions and 3 deletions

15
pytest.ini Normal file
View 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

View File

@@ -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
View File

24
tests/auth.py Normal file
View 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
View 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
View 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
View File

@@ -0,0 +1,11 @@
import os
import tempfile
import pytest
TEST_HASHTAGS = ["embraceeuropa", "francisparkeryockey"]
@pytest.fixture(scope="package")
def hashtags():
return TEST_HASHTAGS

View File

@@ -1 +1,3 @@
__version__ = "2.0.0"
from .base import TikTokDownloader

View File

@@ -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."""

View File

@@ -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