File size: 1,780 Bytes
aacc0cb 69c00d1 aacc0cb 69c00d1 aacc0cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | """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
|