style: apply ruff formatting and move TYPE_CHECKING imports in tests
This commit is contained in:
@@ -12,8 +12,7 @@ Comprehensive tests covering:
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from typing import AsyncGenerator
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -28,6 +27,9 @@ from fastapi_traffic.core.algorithms import (
|
||||
get_algorithm,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def backend() -> AsyncGenerator[MemoryBackend, None]:
|
||||
@@ -41,9 +43,7 @@ async def backend() -> AsyncGenerator[MemoryBackend, None]:
|
||||
class TestTokenBucketAlgorithm:
|
||||
"""Tests for TokenBucketAlgorithm."""
|
||||
|
||||
async def test_allows_requests_within_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_allows_requests_within_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests within limit are allowed."""
|
||||
algo = TokenBucketAlgorithm(10, 60.0, backend)
|
||||
|
||||
@@ -51,9 +51,7 @@ class TestTokenBucketAlgorithm:
|
||||
allowed, _ = await algo.check(f"key_{i % 2}")
|
||||
assert allowed, f"Request {i} should be allowed"
|
||||
|
||||
async def test_blocks_requests_over_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_blocks_requests_over_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests over limit are blocked."""
|
||||
algo = TokenBucketAlgorithm(3, 60.0, backend)
|
||||
|
||||
@@ -86,9 +84,7 @@ class TestTokenBucketAlgorithm:
|
||||
class TestSlidingWindowAlgorithm:
|
||||
"""Tests for SlidingWindowAlgorithm."""
|
||||
|
||||
async def test_allows_requests_within_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_allows_requests_within_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests within limit are allowed."""
|
||||
algo = SlidingWindowAlgorithm(5, 60.0, backend)
|
||||
|
||||
@@ -96,9 +92,7 @@ class TestSlidingWindowAlgorithm:
|
||||
allowed, _ = await algo.check("test_key")
|
||||
assert allowed
|
||||
|
||||
async def test_blocks_requests_over_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_blocks_requests_over_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests over limit are blocked."""
|
||||
algo = SlidingWindowAlgorithm(3, 60.0, backend)
|
||||
|
||||
@@ -115,9 +109,7 @@ class TestSlidingWindowAlgorithm:
|
||||
class TestFixedWindowAlgorithm:
|
||||
"""Tests for FixedWindowAlgorithm."""
|
||||
|
||||
async def test_allows_requests_within_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_allows_requests_within_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests within limit are allowed."""
|
||||
algo = FixedWindowAlgorithm(5, 60.0, backend)
|
||||
|
||||
@@ -125,9 +117,7 @@ class TestFixedWindowAlgorithm:
|
||||
allowed, _ = await algo.check("test_key")
|
||||
assert allowed
|
||||
|
||||
async def test_blocks_requests_over_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_blocks_requests_over_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests over limit are blocked."""
|
||||
algo = FixedWindowAlgorithm(3, 60.0, backend)
|
||||
|
||||
@@ -144,9 +134,7 @@ class TestFixedWindowAlgorithm:
|
||||
class TestLeakyBucketAlgorithm:
|
||||
"""Tests for LeakyBucketAlgorithm."""
|
||||
|
||||
async def test_allows_requests_within_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_allows_requests_within_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests within limit are allowed."""
|
||||
algo = LeakyBucketAlgorithm(5, 60.0, backend)
|
||||
|
||||
@@ -154,9 +142,7 @@ class TestLeakyBucketAlgorithm:
|
||||
allowed, _ = await algo.check("test_key")
|
||||
assert allowed
|
||||
|
||||
async def test_blocks_requests_over_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_blocks_requests_over_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests over limit are blocked."""
|
||||
algo = LeakyBucketAlgorithm(3, 60.0, backend)
|
||||
|
||||
@@ -176,9 +162,7 @@ class TestLeakyBucketAlgorithm:
|
||||
class TestSlidingWindowCounterAlgorithm:
|
||||
"""Tests for SlidingWindowCounterAlgorithm."""
|
||||
|
||||
async def test_allows_requests_within_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_allows_requests_within_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests within limit are allowed."""
|
||||
algo = SlidingWindowCounterAlgorithm(5, 60.0, backend)
|
||||
|
||||
@@ -186,9 +170,7 @@ class TestSlidingWindowCounterAlgorithm:
|
||||
allowed, _ = await algo.check("test_key")
|
||||
assert allowed
|
||||
|
||||
async def test_blocks_requests_over_limit(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_blocks_requests_over_limit(self, backend: MemoryBackend) -> None:
|
||||
"""Test that requests over limit are blocked."""
|
||||
algo = SlidingWindowCounterAlgorithm(3, 60.0, backend)
|
||||
|
||||
@@ -224,9 +206,7 @@ class TestGetAlgorithm:
|
||||
algo = get_algorithm(Algorithm.LEAKY_BUCKET, 10, 60.0, backend)
|
||||
assert isinstance(algo, LeakyBucketAlgorithm)
|
||||
|
||||
async def test_get_sliding_window_counter(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_get_sliding_window_counter(self, backend: MemoryBackend) -> None:
|
||||
"""Test getting sliding window counter algorithm."""
|
||||
algo = get_algorithm(Algorithm.SLIDING_WINDOW_COUNTER, 10, 60.0, backend)
|
||||
assert isinstance(algo, SlidingWindowCounterAlgorithm)
|
||||
@@ -477,9 +457,7 @@ class TestAlgorithmStateManagement:
|
||||
state = await algo.get_state("nonexistent_key")
|
||||
assert state is None
|
||||
|
||||
async def test_reset_restores_full_capacity(
|
||||
self, backend: MemoryBackend
|
||||
) -> None:
|
||||
async def test_reset_restores_full_capacity(self, backend: MemoryBackend) -> None:
|
||||
"""Test that reset restores full capacity."""
|
||||
algo = TokenBucketAlgorithm(5, 60.0, backend)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user