make it great agin

This commit is contained in:
Yansong Zhang
2026-02-04 15:46:24 +08:00
parent 0685e294c4
commit 13be706202

View File

@ -43,8 +43,10 @@ class TestApiTokenCache:
def test_serialize_token(self):
"""Test token serialization."""
import orjson
serialized = ApiTokenCache._serialize_token(self.mock_token)
data = json.loads(serialized)
data = orjson.loads(serialized) # orjson to parse bytes
assert data["id"] == "test-token-id-123"
assert data["app_id"] == "test-app-id-456"
@ -56,6 +58,8 @@ class TestApiTokenCache:
def test_serialize_token_with_nulls(self):
"""Test token serialization with None values."""
import orjson
mock_token = MagicMock()
mock_token.id = "test-id"
mock_token.app_id = None
@ -66,7 +70,7 @@ class TestApiTokenCache:
mock_token.created_at = datetime(2026, 1, 1, 0, 0, 0)
serialized = ApiTokenCache._serialize_token(mock_token)
data = json.loads(serialized)
data = orjson.loads(serialized) # orjson to parse bytes
assert data["app_id"] is None
assert data["tenant_id"] is None
@ -74,7 +78,9 @@ class TestApiTokenCache:
def test_deserialize_token(self):
"""Test token deserialization."""
cached_data = json.dumps(
import orjson
cached_data = orjson.dumps(
{
"id": "test-id",
"app_id": "test-app",
@ -110,7 +116,9 @@ class TestApiTokenCache:
@patch("libs.api_token_cache.redis_client")
def test_get_cache_hit(self, mock_redis):
"""Test cache hit scenario."""
cached_data = json.dumps(
import orjson
cached_data = orjson.dumps(
{
"id": "test-id",
"app_id": "test-app",
@ -121,7 +129,7 @@ class TestApiTokenCache:
"created_at": "2026-01-01T00:00:00",
}
)
mock_redis.get.return_value = cached_data.encode("utf-8")
mock_redis.get.return_value = cached_data # orjson returns bytes
result = ApiTokenCache.get("test-token", "app")
@ -161,7 +169,7 @@ class TestApiTokenCache:
args = mock_redis.setex.call_args[0]
assert args[0] == f"{CACHE_KEY_PREFIX}:app:invalid-token"
assert args[1] == CACHE_NULL_TTL_SECONDS
assert args[2] == "null"
assert args[2] == b"null" # orjson returns bytes
@patch("libs.api_token_cache.redis_client")
def test_delete_with_scope(self, mock_redis):
@ -231,7 +239,7 @@ class TestApiTokenCacheIntegration:
# 2. Simulate cache hit
cached_data = ApiTokenCache._serialize_token(mock_token)
mock_redis.get.return_value = cached_data.encode("utf-8")
mock_redis.get.return_value = cached_data # Already bytes from orjson
retrieved = ApiTokenCache.get("token-abc", "app")
assert retrieved is not None
@ -248,7 +256,7 @@ class TestApiTokenCacheIntegration:
ApiTokenCache.set("non-existent-token", "app", None)
args = mock_redis.setex.call_args[0]
assert args[2] == "null"
assert args[2] == b"null" # orjson returns bytes
assert args[1] == CACHE_NULL_TTL_SECONDS # Shorter TTL for null values