kscrape / tests /test_stdlib_utils.py
kscrape
chore: restore repository maintenance standards
69c00d1 unverified
Raw History Blame Contribute Delete
1.78 kB
"""Tests for generic package utility helpers."""
from __future__ import annotations
import pytest
import kscrape.utils as ut
def test_deep_get_reads_nested_values_and_returns_default() -> None:
"""Read nested mapping values without exposing missing-key errors."""
data = {"source": {"request": {"limit": 20}}}
assert ut.deep_get(data, ("source", "request", "limit")) == 20
assert ut.deep_get(data, ("source", "missing"), "default") == "default"
assert ut.deep_get(data, ("source", "request", "limit", "extra")) is None
def test_deep_set_creates_mappings_and_raises_for_empty_keypath() -> None:
"""Create intermediate dictionaries for nested writes."""
data: dict[str, object] = {}
ut.deep_set(data, ("source", "request", "limit"), 20)
assert data == {"source": {"request": {"limit": 20}}}
with pytest.raises(IndexError):
ut.deep_set(data, (), "value")
def test_deep_right_merge_does_not_mutate_inputs() -> None:
"""Recursively overlay mapping leaves while retaining both source mappings."""
base = {"source": {"limit": 20, "rating": "s"}, "asset": "original"}
overlay = {"source": {"limit": 10}, "asset": "sample"}
assert ut.deep_right_merge(base, overlay) == {
"source": {"limit": 10, "rating": "s"},
"asset": "sample",
}
assert base["source"] == {"limit": 20, "rating": "s"}
assert overlay["source"] == {"limit": 10}
def test_timer_prints_its_label(capsys: pytest.CaptureFixture[str]) -> None:
"""Provide a short timing report for manual diagnostic blocks.
:param capsys: Captured standard output supplied by pytest.
:return: None.
"""
with ut.timer("download check"):
pass
assert "download check finished in" in capsys.readouterr().out