Redis fixtures and fixture factories for Pytest.
Overall
score
95%
Build a session counter system that tracks user session counts in Redis and provides utilities to manage these counts. The system should be fully tested to ensure proper isolation between tests.
Your implementation should include:
A SessionCounter class with the following methods:
increment(user_id: str) - Increments the session count for a given user ID by 1get_count(user_id: str) -> int - Returns the current session count for a user (returns 0 if user has no sessions)reset(user_id: str) - Resets a user's session count to 0get_all_users() -> list - Returns a list of all user IDs that have session countsThe SessionCounter class constructor should accept a Redis client as a parameter
Test file that validates:
Provides Redis fixtures and automatic database cleanup for testing.
Your test file should include at least the following test cases:
@generates
class SessionCounter:
"""Manages user session counts in Redis."""
def __init__(self, redis_client):
"""
Initialize the session counter with a Redis client.
Args:
redis_client: A Redis client instance
"""
pass
def increment(self, user_id: str) -> None:
"""
Increment the session count for a user.
Args:
user_id: The user identifier
"""
pass
def get_count(self, user_id: str) -> int:
"""
Get the current session count for a user.
Args:
user_id: The user identifier
Returns:
The session count (0 if user has no sessions)
"""
pass
def reset(self, user_id: str) -> None:
"""
Reset the session count for a user to 0.
Args:
user_id: The user identifier
"""
pass
def get_all_users(self) -> list:
"""
Get all user IDs that have session counts.
Returns:
List of user IDs
"""
passInstall with Tessl CLI
npx tessl i tessl/pypi-pytest-redisdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10