2021-07-02 16:00:48 +12:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
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_cloner_runner(test_args: list[str], tmp_path: Path):
|
2022-09-03 12:47:58 +12:00
|
|
|
copy_test_config(tmp_path)
|
2021-07-02 16:00:48 +12:00
|
|
|
out = [
|
2022-12-03 18:11:17 +13:00
|
|
|
"clone",
|
2021-07-02 16:00:48 +12:00
|
|
|
str(tmp_path),
|
2022-12-03 18:11:17 +13:00
|
|
|
"-v",
|
|
|
|
"--config",
|
|
|
|
str(Path(tmp_path, "test_config.cfg")),
|
|
|
|
"--log",
|
|
|
|
str(Path(tmp_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", "6l7778"],
|
|
|
|
["-s", "TrollXChromosomes/", "-L", 1],
|
|
|
|
["-l", "eiajjw"],
|
|
|
|
["-l", "xl0lhi"],
|
|
|
|
),
|
|
|
|
)
|
2021-07-02 16:00:48 +12:00
|
|
|
def test_cli_scrape_general(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_cloner_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 "Downloaded submission" in result.output
|
|
|
|
assert "Record for entry item" in result.output
|
2022-11-24 13:48:17 +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-24 13:48:17 +13:00
|
|
|
def test_cli_scrape_soft_fail(test_args: list[str], tmp_path: Path):
|
|
|
|
runner = CliRunner()
|
|
|
|
test_args = create_basic_args_for_cloner_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 "Downloaded submission" not in result.output
|
|
|
|
assert "Record for entry item" 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_cloner_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
|