mirror of
https://github.com/bellingcat/auto-archiver.git
synced 2026-06-08 03:18:28 +03:00
Merge main into timestamping_enricher
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
pytest conftest file, for shared fixtures and configuration
|
||||
"""
|
||||
|
||||
import os
|
||||
import pickle
|
||||
from datetime import datetime, timezone
|
||||
@@ -16,32 +17,36 @@ from auto_archiver.core.module import ModuleFactory
|
||||
# that you only want to run if everything else succeeds (e.g. API calls). The order here is important
|
||||
# what comes first will be run first (at the end of all other tests not mentioned)
|
||||
# format is the name of the module (python file) without the .py extension
|
||||
TESTS_TO_RUN_LAST = ['test_twitter_api_archiver']
|
||||
TESTS_TO_RUN_LAST = ["test_twitter_api_archiver"]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def setup_module(request):
|
||||
def _setup_module(module_name, config={}):
|
||||
|
||||
def _setup_module(module_name, config=None):
|
||||
if config is None:
|
||||
config = {}
|
||||
module_factory = ModuleFactory()
|
||||
|
||||
if isinstance(module_name, type):
|
||||
# get the module name:
|
||||
# if the class does not have a .name, use the name of the parent folder
|
||||
module_name = module_name.__module__.rsplit(".",2)[-2]
|
||||
module_name = module_name.__module__.rsplit(".", 2)[-2]
|
||||
|
||||
m = module_factory.get_module(module_name, {module_name: config})
|
||||
# add the tmp_dir to the module
|
||||
tmp_dir = TemporaryDirectory()
|
||||
m.tmp_dir = tmp_dir.name
|
||||
|
||||
|
||||
def cleanup():
|
||||
tmp_dir.cleanup()
|
||||
|
||||
request.addfinalizer(cleanup)
|
||||
|
||||
return m
|
||||
|
||||
return _setup_module
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def check_hash():
|
||||
def _check_hash(filename: str, hash: str):
|
||||
@@ -51,6 +56,7 @@ def check_hash():
|
||||
|
||||
return _check_hash
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_item():
|
||||
def _make_item(url: str, **kwargs) -> Metadata:
|
||||
@@ -62,7 +68,6 @@ def make_item():
|
||||
return _make_item
|
||||
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(items):
|
||||
module_mapping = {item: item.module.__name__.split(".")[-1] for item in items}
|
||||
|
||||
@@ -78,13 +83,13 @@ def pytest_collection_modifyitems(items):
|
||||
items[:] = sorted_items
|
||||
|
||||
|
||||
|
||||
# Incremental testing - fail tests in a class if any previous test fails
|
||||
# taken from https://docs.pytest.org/en/latest/example/simple.html#incremental-testing-test-steps
|
||||
|
||||
# store history of failures per test class name and per index in parametrize (if parametrize used)
|
||||
_test_failed_incremental: Dict[str, Dict[Tuple[int, ...], str]] = {}
|
||||
|
||||
|
||||
def pytest_runtest_makereport(item, call):
|
||||
if "incremental" in item.keywords:
|
||||
# incremental marker is used
|
||||
@@ -93,17 +98,11 @@ def pytest_runtest_makereport(item, call):
|
||||
# retrieve the class name of the test
|
||||
cls_name = str(item.cls)
|
||||
# retrieve the index of the test (if parametrize is used in combination with incremental)
|
||||
parametrize_index = (
|
||||
tuple(item.callspec.indices.values())
|
||||
if hasattr(item, "callspec")
|
||||
else ()
|
||||
)
|
||||
parametrize_index = tuple(item.callspec.indices.values()) if hasattr(item, "callspec") else ()
|
||||
# retrieve the name of the test function
|
||||
test_name = item.originalname or item.name
|
||||
# store in _test_failed_incremental the original name of the failed test
|
||||
_test_failed_incremental.setdefault(cls_name, {}).setdefault(
|
||||
parametrize_index, test_name
|
||||
)
|
||||
_test_failed_incremental.setdefault(cls_name, {}).setdefault(parametrize_index, test_name)
|
||||
|
||||
|
||||
def pytest_runtest_setup(item):
|
||||
@@ -119,16 +118,17 @@ def pytest_runtest_setup(item):
|
||||
pytest.xfail(f"previous test failed ({test_name})")
|
||||
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
@pytest.fixture
|
||||
def unpickle():
|
||||
"""
|
||||
Returns a helper function that unpickles a file
|
||||
** gets the file from the test_files directory: tests/data/ **
|
||||
"""
|
||||
|
||||
def _unpickle(path):
|
||||
with open(os.path.join("tests/data", path), "rb") as f:
|
||||
return pickle.load(f)
|
||||
|
||||
return _unpickle
|
||||
|
||||
|
||||
@@ -151,9 +151,9 @@ def sample_datetime():
|
||||
return datetime(2023, 1, 1, 12, 0, tzinfo=timezone.utc)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@pytest.fixture
|
||||
def mock_sleep(mocker):
|
||||
"""Globally mock time.sleep to avoid delays."""
|
||||
"""Mock time.sleep to avoid delays."""
|
||||
return mocker.patch("time.sleep")
|
||||
|
||||
|
||||
@@ -162,4 +162,4 @@ def metadata():
|
||||
metadata = Metadata()
|
||||
metadata.set("_processed_at", "2021-01-01T00:00:00")
|
||||
metadata.set_url("https://example.com")
|
||||
return metadata
|
||||
return metadata
|
||||
|
||||
Reference in New Issue
Block a user