mirror of
https://github.com/bellingcat/tiktok-hashtag-analysis.git
synced 2026-06-12 13:28:30 +03:00
added tests, changed __main__ to cli
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user