Skip to content

Commit

Permalink
test: update test_cache_function
Browse files Browse the repository at this point in the history
  • Loading branch information
Nytelife26 committed Jul 22, 2024
1 parent 56b784f commit 244ddf8
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions tests/test_cache_function.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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."

0 comments on commit 244ddf8

Please sign in to comment.