- CHANGELOG.md / CONTRIBUTING.md - 新增 exceptions / case_converter 单元测试 - pytest 12 → 22 passed;全项目 flake8 0 Co-Authored-By: Claude <noreply@anthropic.com>
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
"""
|
|
命名转换工具单元测试
|
|
=============================================================
|
|
|
|
- convert_camel_case / convert_snake_case:单字符串转换
|
|
- convert_camel_to_snake / convert_snake_to_camel:字典键递归转换
|
|
"""
|
|
|
|
from src.common.utils.case_converter import (
|
|
convert_camel_case,
|
|
convert_snake_case,
|
|
convert_camel_to_snake,
|
|
convert_snake_to_camel,
|
|
)
|
|
|
|
|
|
class TestSingleString:
|
|
def test_snake_to_camel(self):
|
|
assert convert_camel_case("user_id") == "userId"
|
|
|
|
def test_camel_to_snake(self):
|
|
assert convert_snake_case("userId") == "user_id"
|
|
|
|
|
|
class TestDictKeys:
|
|
def test_camel_to_snake_dict(self):
|
|
data = {"userId": 1, "userInfo": {"realName": "张三"}}
|
|
assert convert_camel_to_snake(data) == {"user_id": 1, "user_info": {"real_name": "张三"}}
|
|
|
|
def test_snake_to_camel_dict(self):
|
|
data = {"user_id": 1, "user_info": {"real_name": "张三"}}
|
|
assert convert_snake_to_camel(data) == {"userId": 1, "userInfo": {"realName": "张三"}}
|
|
|
|
def test_roundtrip(self):
|
|
source = {"user_id": 1, "role_ids": [1, 2]}
|
|
camel = convert_snake_to_camel(source)
|
|
assert camel == {"userId": 1, "roleIds": [1, 2]}
|
|
assert convert_camel_to_snake(camel) == source
|