diff --git a/tests/test_cache_function.py b/tests/test_cache_function.py index 4a635734f..519366be3 100644 --- a/tests/test_cache_function.py +++ b/tests/test_cache_function.py @@ -1,28 +1,31 @@ +from __future__ import annotations + from timeit import timeit import proselint from proselint import memoizer +DEMO_PATH = proselint.path / "demo.md" +DEMO_TEXT = DEMO_PATH.open(encoding="utf-8", errors="replace").read() + -def test_speed(): - """Compare uncached vs cached reads""" - repetitions = 2 +def test_speed() -> None: + """Compare uncached vs cached reads.""" + repetitions = 3 demo_path = proselint.path / "demo.md" + _text = DEMO_TEXT _code = "errors = proselint.tools.lint(_text)" _setup = "import proselint" - with demo_path.open(encoding="utf-8", errors="replace") as demo_fh: - _text = demo_fh.read() - # make sure it works - memoizer.cache.clear() - errors = proselint.tools.lint(_text) + memoizer.cache.clear_internal() + errors = proselint.tools.lint(DEMO_TEXT) assert len(errors) > 0 # without cache _dur1 = 0.0 for _ in range(repetitions): - memoizer.cache.clear() + memoizer.cache.clear_internal() _dur1 += timeit(_code, setup=_setup, globals=locals(), number=1) # with cache @@ -33,20 +36,21 @@ def test_speed(): # without cache, confirmation _dur3 = 0.0 for _ in range(repetitions): - memoizer.cache.clear() + memoizer.cache.clear_internal() _dur3 += timeit(_code, setup=_setup, globals=locals(), number=1) assert _dur2 < 0.7 * _dur1 assert _dur2 < 0.7 * _dur3 -def test_consistency(): - """Compare result-count""" - demo_path = proselint.path / "demo.md" - demo_fh = demo_path.open(encoding="utf-8", errors="replace") - demo_str = demo_fh.read() - memoizer.cache.clear() - errors_a = proselint.tools.lint(demo_str) - errors_b = proselint.tools.lint(demo_str) - - assert len(errors_a) == len(errors_b) +def test_consistency() -> None: + """Compare result count from cached and uncached runs.""" + memoizer.cache.clear_internal() + errors_a = proselint.tools.lint(DEMO_TEXT) + errors_b = proselint.tools.lint(DEMO_TEXT) + assert len(errors_a) == len( + list(memoizer.cache.values())[-1] + ), "Errors were not cached correctly - quantity mismatch." + assert len(errors_a) == len( + errors_b + ), "Errors were not cached correctly - differing output."