refactor: modernize code quality tooling with Ruff
- Replace black, isort, and flake8 with Ruff (all-in-one linter and formatter) - Add comprehensive pyproject.toml configuration - Simplify Makefile code quality targets - Configure exclusions for venv/.venv in pyproject.toml - Auto-fix 1,359 linting issues across codebase Benefits: - Much faster builds (Ruff is written in Rust) - Single tool replaces multiple tools - More comprehensive rule set (UP, B, C4, SIM, PIE, RET, Q) - All configuration centralized in pyproject.toml - Better import sorting and formatting consistency 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -12,8 +12,7 @@ Tests cover:
|
||||
"""
|
||||
|
||||
from collections import deque
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from unittest.mock import Mock, patch
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -53,7 +52,7 @@ class TestRateLimiterBasic:
|
||||
# Make 10 requests (at the limit)
|
||||
for i in range(max_requests):
|
||||
result = limiter.allow_request(client_id, max_requests, 3600)
|
||||
assert result is True, f"Request {i+1} should be allowed"
|
||||
assert result is True, f"Request {i + 1} should be allowed"
|
||||
|
||||
assert len(limiter.clients[client_id]) == max_requests
|
||||
|
||||
@@ -109,7 +108,7 @@ class TestRateLimiterSlidingWindow:
|
||||
window_seconds = 10
|
||||
|
||||
# Manually add old requests
|
||||
old_time = datetime.now(timezone.utc) - timedelta(seconds=15)
|
||||
old_time = datetime.now(UTC) - timedelta(seconds=15)
|
||||
limiter.clients[client_id].append(old_time)
|
||||
limiter.clients[client_id].append(old_time)
|
||||
|
||||
@@ -127,7 +126,7 @@ class TestRateLimiterSlidingWindow:
|
||||
window_seconds = 60
|
||||
|
||||
# Add recent requests
|
||||
recent_time = datetime.now(timezone.utc) - timedelta(seconds=30)
|
||||
recent_time = datetime.now(UTC) - timedelta(seconds=30)
|
||||
limiter.clients[client_id].append(recent_time)
|
||||
limiter.clients[client_id].append(recent_time)
|
||||
|
||||
@@ -147,12 +146,12 @@ class TestRateLimiterSlidingWindow:
|
||||
window_seconds = 30
|
||||
|
||||
# Add old requests (outside window)
|
||||
old_time = datetime.now(timezone.utc) - timedelta(seconds=60)
|
||||
old_time = datetime.now(UTC) - timedelta(seconds=60)
|
||||
limiter.clients[client_id].append(old_time)
|
||||
limiter.clients[client_id].append(old_time)
|
||||
|
||||
# Add recent request (within window)
|
||||
recent_time = datetime.now(timezone.utc) - timedelta(seconds=10)
|
||||
recent_time = datetime.now(UTC) - timedelta(seconds=10)
|
||||
limiter.clients[client_id].append(recent_time)
|
||||
|
||||
# Old requests removed, only 1 recent request, so 2 more allowed
|
||||
@@ -170,7 +169,7 @@ class TestRateLimiterSlidingWindow:
|
||||
window_seconds = 1 # 1 second window
|
||||
|
||||
# Add old request
|
||||
old_time = datetime.now(timezone.utc) - timedelta(seconds=2)
|
||||
old_time = datetime.now(UTC) - timedelta(seconds=2)
|
||||
limiter.clients[client_id].append(old_time)
|
||||
|
||||
# Should allow request because old one is outside 1-second window
|
||||
@@ -188,12 +187,12 @@ class TestRateLimiterCleanup:
|
||||
limiter = RateLimiter()
|
||||
|
||||
# Add clients with old requests
|
||||
old_time = datetime.now(timezone.utc) - timedelta(hours=25)
|
||||
old_time = datetime.now(UTC) - timedelta(hours=25)
|
||||
limiter.clients["old_client_1"].append(old_time)
|
||||
limiter.clients["old_client_2"].append(old_time)
|
||||
|
||||
# Add client with recent requests
|
||||
recent_time = datetime.now(timezone.utc) - timedelta(hours=1)
|
||||
recent_time = datetime.now(UTC) - timedelta(hours=1)
|
||||
limiter.clients["recent_client"].append(recent_time)
|
||||
|
||||
# Run cleanup
|
||||
@@ -215,7 +214,7 @@ class TestRateLimiterCleanup:
|
||||
limiter.clients["empty_client_2"] = deque()
|
||||
|
||||
# Add client with requests
|
||||
limiter.clients["active_client"].append(datetime.now(timezone.utc))
|
||||
limiter.clients["active_client"].append(datetime.now(UTC))
|
||||
|
||||
# Run cleanup
|
||||
limiter._cleanup_old_entries()
|
||||
@@ -233,12 +232,12 @@ class TestRateLimiterCleanup:
|
||||
client_id = "mixed_client"
|
||||
|
||||
# Add old requests
|
||||
old_time = datetime.now(timezone.utc) - timedelta(hours=30)
|
||||
old_time = datetime.now(UTC) - timedelta(hours=30)
|
||||
limiter.clients[client_id].append(old_time)
|
||||
limiter.clients[client_id].append(old_time)
|
||||
|
||||
# Add recent requests
|
||||
recent_time = datetime.now(timezone.utc) - timedelta(hours=1)
|
||||
recent_time = datetime.now(UTC) - timedelta(hours=1)
|
||||
limiter.clients[client_id].append(recent_time)
|
||||
limiter.clients[client_id].append(recent_time)
|
||||
|
||||
@@ -255,10 +254,10 @@ class TestRateLimiterCleanup:
|
||||
limiter.cleanup_interval = 0 # Force immediate cleanup
|
||||
|
||||
# Set last_cleanup to past
|
||||
limiter.last_cleanup = datetime.now(timezone.utc) - timedelta(hours=2)
|
||||
limiter.last_cleanup = datetime.now(UTC) - timedelta(hours=2)
|
||||
|
||||
# Add old client
|
||||
old_time = datetime.now(timezone.utc) - timedelta(hours=25)
|
||||
old_time = datetime.now(UTC) - timedelta(hours=25)
|
||||
limiter.clients["old_client"].append(old_time)
|
||||
|
||||
# Make request (should trigger cleanup)
|
||||
@@ -272,7 +271,7 @@ class TestRateLimiterCleanup:
|
||||
limiter = RateLimiter()
|
||||
|
||||
# Add multiple active clients
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
for i in range(5):
|
||||
limiter.clients[f"client_{i}"].append(now - timedelta(hours=i))
|
||||
|
||||
@@ -305,7 +304,7 @@ class TestRateLimiterStatistics:
|
||||
client_id = "active_client"
|
||||
|
||||
# Add requests at different times
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
limiter.clients[client_id].append(now - timedelta(minutes=30)) # Within hour
|
||||
limiter.clients[client_id].append(now - timedelta(hours=2)) # Within day
|
||||
limiter.clients[client_id].append(now - timedelta(hours=12)) # Within day
|
||||
@@ -322,7 +321,7 @@ class TestRateLimiterStatistics:
|
||||
client_id = "old_requests_client"
|
||||
|
||||
# Add very old requests
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
limiter.clients[client_id].append(now - timedelta(days=2))
|
||||
limiter.clients[client_id].append(now - timedelta(days=3))
|
||||
|
||||
@@ -347,7 +346,7 @@ class TestRateLimiterStatistics:
|
||||
limiter = RateLimiter()
|
||||
client_id = "boundary_client"
|
||||
|
||||
now = datetime.now(timezone.utc)
|
||||
now = datetime.now(UTC)
|
||||
|
||||
# Exactly 1 hour ago (should be included)
|
||||
limiter.clients[client_id].append(now - timedelta(hours=1, seconds=1))
|
||||
@@ -485,7 +484,7 @@ class TestRateLimiterEdgeCases:
|
||||
limiter = RateLimiter()
|
||||
|
||||
# Set last_cleanup to past to ensure cleanup triggers
|
||||
old_cleanup_time = datetime.now(timezone.utc) - timedelta(hours=2)
|
||||
old_cleanup_time = datetime.now(UTC) - timedelta(hours=2)
|
||||
limiter.last_cleanup = old_cleanup_time
|
||||
limiter.cleanup_interval = 0 # Force cleanup on next request
|
||||
|
||||
@@ -523,7 +522,7 @@ class TestRateLimiterMemoryManagement:
|
||||
client_id = "efficiency_test"
|
||||
|
||||
# Add many old requests
|
||||
old_time = datetime.now(timezone.utc) - timedelta(hours=2)
|
||||
old_time = datetime.now(UTC) - timedelta(hours=2)
|
||||
for _ in range(1000):
|
||||
limiter.clients[client_id].append(old_time)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user