Repository navigation
preserve(#7264): feat(tests): canonical make_async_redis() + patch_async_redis() fixtures (#7264) - #8000
Closed
mrveiss wants to merge 1 commit into
Closed
preserve(#7264): feat(tests): canonical make_async_redis() + patch_async_redis() fixtures (#7264)#8000mrveiss wants to merge 1 commit into
mrveiss wants to merge 1 commit into
Conversation
…res (#7264) Composability follow-up to #7216 (PR #7234) and #7134 (PR #7139). After the async-mock pattern fix consolidated 77 sites onto ``patch(..., new=AsyncMock(return_value=mock_redis))``, 11+ test files still rolled their own ``_make_redis*()`` fixture with slightly different defaults each. This PR extracts the canonical shape. What lands ---------- ``tests/fixtures/mocks.py`` adds: make_async_redis(*, get_returns=None, set_returns=True, ..., **extra_methods) -> AsyncMock Build a redis-shaped AsyncMock with sensible defaults for the top ~30 redis methods used in this codebase (per a grep over all *test*.py files: get×170, hgetall×47, set×46, hset×33, expire×29, delete×29, ...). Per-method override via ``X_returns=`` kwargs. Less common methods (e.g. xadd for streams) via ``**extra_methods`` kwargs — each becomes ``AsyncMock(return_value=value)``. @contextmanager patch_async_redis(target, redis=None) Context manager that wraps ``patch(target, new=AsyncMock(return_ value=redis))`` and yields the inner redis mock for configuration. Eliminates the #7216 footgun: bare ``patch(target, return_value=mock)`` returns the default AsyncMock when the production code awaits ``get_async_redis_client(...)``, NOT the mock the caller wanted. Re-exported from ``tests/fixtures/__init__.py`` (matches #7134 precedent). Sensible defaults (the empty/healthy shape callers usually want) ---------------------------------------------------------------- - get / hget / hgetall / keys / smembers / lrange / zrange → empty/None - set / setex / expire → True - sadd / srem / zadd / lpush / rpush / incr / hset / delete → 1 - sismember / hexists / exists → 0 / False Bug caught during implementation -------------------------------- First version of ``patch_async_redis`` returned ``patch(target, new=...)`` directly — but ``with patch(...) as X: X`` yields the patched ``new`` object, NOT the inner ``redis`` mock the caller needs to configure. So ``with patch_async_redis(target) as redis: redis.get = AsyncMock(...)`` was setting attributes on the wrong object — production saw the default None from the un-overridden inner mock. Caught by my own contract test ``test_patch_async_redis_basic_use`` — asserted production receives the configured value, got None. Fixed by making ``patch_async_redis`` a ``@contextmanager`` that explicitly yields the inner redis mock. The contract test became the regression pin: any future rewrite of the helper that drops the contextmanager form will fail this test. Verification ------------ $ python3 -m pytest tests/fixtures/test_make_async_redis.py -xvs ============================== 17 passed in 1.25s ============================== Migration follow-up ------------------- This PR adds the fixture + tests + documentation. The 11+ ad-hoc ``_make_redis*()`` helpers across the test tree migrate incrementally in separate PRs (per the layered-rot lesson — each migration may surface its own pre-existing rot). Closes #7264 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Owner
Author
|
Closing: this preserve branch has critical CI failures that prevent safe merging. The work-in-progress state has been preserved in the branch. The failures should be fixed before this work is re-submitted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Preservation PR — issue #7264 is closed but work was never submitted. Branch:
issue-7264, 1 commit(s). Created by MVA-588 worktree audit.