2021-07-02 16:00:48 +12:00
|
|
|
#!/usr/bin/env python3
|
2022-12-20 12:32:37 +13:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-07-02 16:00:48 +12:00
|
|
|
|
|
|
|
import re
|
|
|
|
import shutil
|
|
|
|
from pathlib import Path
|
2022-12-15 17:04:33 +13:00
|
|
|
from unittest.mock import MagicMock, patch
|
2021-07-02 16:00:48 +12:00
|
|
|
|
2022-12-15 17:04:33 +13:00
|
|
|
import prawcore
|
2021-07-02 16:00:48 +12:00
|
|
|
import pytest
|
|
|
|
from click.testing import CliRunner
|
|
|
|
|
|
|
|
from bdfr.__main__ import cli
|
|
|
|
|
2022-12-03 18:11:17 +13:00
|
|
|
does_test_config_exist = Path("./tests/test_config.cfg").exists()
|
2021-07-02 16:00:48 +12:00
|
|
|
|
|
|
|
|
|
|
|
def copy_test_config(run_path: Path):
|
2022-12-03 18:11:17 +13:00
|
|
|
shutil.copy(Path("./tests/test_config.cfg"), Path(run_path, "test_config.cfg"))
|
2021-07-02 16:00:48 +12:00
|
|
|
|
|
|
|
|
|
|
|
def create_basic_args_for_archive_runner(test_args: list[str], run_path: Path):
|
|
|
|
copy_test_config(run_path)
|
|
|
|
out = [
|
2022-12-03 18:11:17 +13:00
|
|
|
"archive",
|
2021-07-02 16:00:48 +12:00
|
|
|
str(run_path),
|
2022-12-03 18:11:17 +13:00
|
|
|
"-v",
|
|
|
|
"--config",
|
|
|
|
str(Path(run_path, "test_config.cfg")),
|
|
|
|
"--log",
|
|
|
|
str(Path(run_path, "test_log.txt")),
|
2021-07-02 16:00:48 +12:00
|
|
|
] + test_args
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.online
|
|
|
|
@pytest.mark.reddit
|
2022-12-03 18:11:17 +13:00
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_args",
|
|
|
|
(
|
|
|
|
["-l", "gstd4hk"],
|
|
|
|
["-l", "m2601g", "-f", "yaml"],
|
|
|
|
["-l", "n60t4c", "-f", "xml"],
|
|
|
|
),
|
|
|
|
)
|
2021-07-02 16:00:48 +12:00
|
|
|
def test_cli_archive_single(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
2022-12-03 18:11:17 +13:00
|
|
|
assert re.search(r"Writing entry .*? to file in .*? format", result.output)
|
2021-07-02 16:00:48 +12:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.online
|
|
|
|
@pytest.mark.reddit
|
2022-12-03 18:11:17 +13:00
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_args",
|
|
|
|
(
|
|
|
|
["--subreddit", "Mindustry", "-L", 25],
|
|
|
|
["--subreddit", "Mindustry", "-L", 25, "--format", "xml"],
|
|
|
|
["--subreddit", "Mindustry", "-L", 25, "--format", "yaml"],
|
|
|
|
["--subreddit", "Mindustry", "-L", 25, "--sort", "new"],
|
|
|
|
["--subreddit", "Mindustry", "-L", 25, "--time", "day"],
|
|
|
|
["--subreddit", "Mindustry", "-L", 25, "--time", "day", "--sort", "new"],
|
|
|
|
),
|
|
|
|
)
|
2021-07-02 16:00:48 +12:00
|
|
|
def test_cli_archive_subreddit(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
2022-12-03 18:11:17 +13:00
|
|
|
assert re.search(r"Writing entry .*? to file in .*? format", result.output)
|
2021-07-02 16:00:48 +12:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.online
|
|
|
|
@pytest.mark.reddit
|
2022-12-03 18:11:17 +13:00
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_args",
|
|
|
|
(
|
|
|
|
["--user", "me", "--authenticate", "--all-comments", "-L", "10"],
|
|
|
|
["--user", "me", "--user", "djnish", "--authenticate", "--all-comments", "-L", "10"],
|
|
|
|
),
|
|
|
|
)
|
2021-07-02 16:00:48 +12:00
|
|
|
def test_cli_archive_all_user_comments(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.online
|
|
|
|
@pytest.mark.reddit
|
2022-12-03 18:11:17 +13:00
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize("test_args", (["--comment-context", "--link", "gxqapql"],))
|
2021-07-02 16:00:48 +12:00
|
|
|
def test_cli_archive_full_context(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
2022-12-03 18:11:17 +13:00
|
|
|
assert "Converting comment" in result.output
|
2021-07-02 16:00:48 +12:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.online
|
|
|
|
@pytest.mark.reddit
|
|
|
|
@pytest.mark.slow
|
2022-12-03 18:11:17 +13:00
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_args",
|
|
|
|
(
|
|
|
|
["--subreddit", "all", "-L", 100],
|
|
|
|
["--subreddit", "all", "-L", 100, "--sort", "new"],
|
|
|
|
),
|
|
|
|
)
|
2021-07-02 16:00:48 +12:00
|
|
|
def test_cli_archive_long(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
2022-12-03 18:11:17 +13:00
|
|
|
assert re.search(r"Writing entry .*? to file in .*? format", result.output)
|
2021-11-24 13:58:18 +13:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.online
|
|
|
|
@pytest.mark.reddit
|
2022-12-03 18:11:17 +13:00
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize("test_args", (["--ignore-user", "ArjanEgges", "-l", "m3hxzd"],))
|
2021-11-24 13:58:18 +13:00
|
|
|
def test_cli_archive_ignore_user(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
2022-12-03 18:11:17 +13:00
|
|
|
assert "being an ignored user" in result.output
|
|
|
|
assert "Attempting to archive submission" not in result.output
|
2022-02-18 15:30:38 +13:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.online
|
|
|
|
@pytest.mark.reddit
|
2022-12-03 18:11:17 +13:00
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize("test_args", (["--file-scheme", "{TITLE}", "-l", "suy011"],))
|
2022-02-18 15:30:38 +13:00
|
|
|
def test_cli_archive_file_format(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
2022-12-03 18:11:17 +13:00
|
|
|
assert "Attempting to archive submission" in result.output
|
|
|
|
assert re.search("format at /.+?/Judge says Trump and two adult", result.output)
|
2022-07-06 18:52:01 +12:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.online
|
|
|
|
@pytest.mark.reddit
|
2022-12-03 18:11:17 +13:00
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize("test_args", (["-l", "m2601g", "--exclude-id", "m2601g"],))
|
2022-07-06 18:52:01 +12:00
|
|
|
def test_cli_archive_links_exclusion(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
2022-12-03 18:11:17 +13:00
|
|
|
assert "in exclusion list" in result.output
|
|
|
|
assert "Attempting to archive" not in result.output
|
|
|
|
|
2022-11-22 15:37:59 +13:00
|
|
|
|
|
|
|
@pytest.mark.online
|
|
|
|
@pytest.mark.reddit
|
2022-12-03 18:11:17 +13:00
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_args",
|
|
|
|
(
|
|
|
|
["-l", "ijy4ch"], # user deleted post
|
|
|
|
["-l", "kw4wjm"], # post from banned subreddit
|
|
|
|
),
|
|
|
|
)
|
2022-11-22 15:37:59 +13:00
|
|
|
def test_cli_archive_soft_fail(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
2022-12-03 18:11:17 +13:00
|
|
|
assert "failed to be archived due to a PRAW exception" in result.output
|
|
|
|
assert "Attempting to archive" not in result.output
|
2022-12-15 17:04:33 +13:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not does_test_config_exist, reason="A test config file is required for integration tests")
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("test_args", "response"),
|
|
|
|
(
|
2022-12-18 14:35:58 +13:00
|
|
|
(["--user", "nasa", "--submitted"], 502),
|
|
|
|
(["--user", "nasa", "--submitted"], 504),
|
2022-12-15 17:04:33 +13:00
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_user_serv_fail(test_args: list[str], response: int, tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_archive_runner(test_args, tmp_path)
|
|
|
|
with patch("bdfr.connector.sleep", return_value=None):
|
|
|
|
with patch(
|
|
|
|
"bdfr.connector.RedditConnector.check_user_existence",
|
|
|
|
side_effect=prawcore.exceptions.ResponseException(MagicMock(status_code=response)),
|
|
|
|
):
|
|
|
|
result = runner.invoke(cli, test_args)
|
|
|
|
assert result.exit_code == 0
|
|
|
|
assert f"received {response} HTTP response" in result.output
|