首次提交
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
"""
|
||||
测试字典工具。
|
||||
无外部依赖,可离线运行。
|
||||
"""
|
||||
|
||||
from paste.util import udict
|
||||
|
||||
|
||||
class TestUdict:
|
||||
"""字典工具测试"""
|
||||
|
||||
def test_get_by_path_simple(self):
|
||||
"""简单路径读取"""
|
||||
data = {"a": 1, "b": 2}
|
||||
assert udict.get_by_path(data, "a") == 1
|
||||
|
||||
def test_get_by_path_nested(self):
|
||||
"""嵌套路径读取"""
|
||||
data = {"a": {"b": {"c": 123}}}
|
||||
assert udict.get_by_path(data, "a.b.c") == 123
|
||||
|
||||
def test_get_by_path_missing(self):
|
||||
"""缺失路径处理"""
|
||||
data = {"a": 1}
|
||||
assert udict.get_by_path(data, "b.c.d", "default") == "default"
|
||||
|
||||
def test_get_by_path_none_default(self):
|
||||
"""缺失路径无默认值"""
|
||||
data = {"a": 1}
|
||||
assert udict.get_by_path(data, "b") is None
|
||||
|
||||
def test_get_with_default_existing(self):
|
||||
"""存在的键读取"""
|
||||
data = {"key": "value"}
|
||||
assert udict.get_with_default(data, "key", "fallback") == "value"
|
||||
|
||||
def test_get_with_default_missing(self):
|
||||
"""缺失键使用默认值"""
|
||||
data = {"key": "value"}
|
||||
assert udict.get_with_default(data, "missing", "fallback") == "fallback"
|
||||
|
||||
def test_get_with_default_none_value(self):
|
||||
"""值为 None 时使用默认值"""
|
||||
data = {"key": None}
|
||||
assert udict.get_with_default(data, "key", "fallback") == "fallback"
|
||||
Reference in New Issue
Block a user