加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
e03_dict_test.py 11.75 KB
一键复制 编辑 原始数据 按行查看 历史
import pytest
import e03_dict_quiz as q
@pytest.mark.parametrize(
"dct, expected",
[
[
{"open": 45.70, "high": 47.00, "low": 45.49, "close": 46.83},
{"OPEN": 3.8221, "HIGH": 3.8501, "LOW": 3.8175, "CLOSE": 3.8465},
],
[
{"open": 6.61, "high": 6.63, "low": 6.45, "close": 6.48},
{"OPEN": 1.8886, "HIGH": 1.8916, "LOW": 1.8641, "CLOSE": 1.8687},
],
],
)
def test_001(dct, expected):
dct_bak = dct.copy()
res = q.f001(dct)
assert dct == dct_bak
assert res == expected
@pytest.mark.parametrize(
"dct, expected",
[
[
{"a": [3, 9], "b": [2, 2, 7, 7], "c": [5]},
{"a": 6.0, "b": 4.5, "c": 5.0},
],
[
{"d": [1, 1, 1], "e": [3, 2, 3, 3], "f": [0, 0]},
{"d": 1.0, "e": 2.8, "f": 0.0},
],
],
)
def test_002(dct, expected):
dct_bak = dct.copy()
res = q.f002(dct)
assert dct == dct_bak
assert res == expected
@pytest.mark.parametrize(
"lst, expected",
[
[
[(0.3, 0.4), (0.8, -0.8), (0.9, 0.4)],
{(0.3, 0.4): 0.5, (0.9, 0.4): 0.98},
],
[
[(-0.5, -0.5), (-1, 1), (-0.2, 0.9), (0.7, 0.7)],
{(-0.5, -0.5): 0.71, (-0.2, 0.9): 0.92, (0.7, 0.7): 0.99},
],
],
)
def test_003(lst, expected):
lst_bak = lst.copy()
res = q.f003(lst)
assert lst == lst_bak
assert res == expected
@pytest.mark.parametrize(
"keys, vals, expected",
[
[
["aaa", "bbb", "ccc"],
[5.982, 4.221, 9.475],
{"aaa": 5.982, "bbb": 4.221, "ccc": 9.475},
],
[
["ddd", "eee", "fff", "ggg"],
[5.982, 4.221, 9.475, 6.685],
{"ddd": 5.982, "eee": 4.221, "fff": 9.475, "ggg": 6.685},
],
],
)
def test_004(keys, vals, expected):
res = q.f004(keys, vals)
assert res == expected
@pytest.mark.parametrize(
"keys, cols, expected",
[
[
["aaa", "bbb", "ccc"],
[
[5.982, 2.904],
[4.221, 3.434],
[9.475, 6.549],
],
{
"aaa": [5.982, 2.904],
"bbb": [4.221, 3.434],
"ccc": [9.475, 6.549],
},
],
[
["ddd", "eee", "fff", "ggg"],
[
[5.982, 8.853, 5.923],
[4.221, 3.123, 8.121],
[9.475, 6.659, 7.492],
[6.685, 4.129, 2.223],
],
{
"ddd": [5.982, 8.853, 5.923],
"eee": [4.221, 3.123, 8.121],
"fff": [9.475, 6.659, 7.492],
"ggg": [6.685, 4.129, 2.223],
},
],
],
)
def test_005(keys, cols, expected):
res = q.f005(keys, cols)
assert res == expected
@pytest.mark.parametrize(
"keys, rows, expected",
[
[
["aaa", "bbb", "ccc"],
[
[5.982, 4.221, 9.475],
[2.904, 3.434, 6.549],
],
{
"aaa": [5.982, 2.904],
"bbb": [4.221, 3.434],
"ccc": [9.475, 6.549],
},
],
[
["ddd", "eee", "fff", "ggg"],
[
[5.982, 4.221, 9.475, 6.685],
[8.853, 3.123, 6.659, 4.129],
[5.923, 8.121, 7.492, 2.223],
],
{
"ddd": [5.982, 8.853, 5.923],
"eee": [4.221, 3.123, 8.121],
"fff": [9.475, 6.659, 7.492],
"ggg": [6.685, 4.129, 2.223],
},
],
],
)
def test_006(keys, rows, expected):
res = q.f006(keys, rows)
assert res == expected
@pytest.mark.parametrize(
"keys, rows, expected",
[
[
["aaa", "bbb", "ccc"],
[
[5.982, 4.221, 9.475],
[2.904, 3.434, 6.549],
],
[
{"aaa": 5.982, "bbb": 4.221, "ccc": 9.475},
{"aaa": 2.904, "bbb": 3.434, "ccc": 6.549},
],
],
[
["ddd", "eee", "fff", "ggg"],
[
[5.982, 4.221, 9.475, 6.685],
[8.853, 3.123, 6.659, 4.129],
[5.923, 8.121, 7.492, 2.223],
],
[
{"ddd": 5.982, "eee": 4.221, "fff": 9.475, "ggg": 6.685},
{"ddd": 8.853, "eee": 3.123, "fff": 6.659, "ggg": 4.129},
{"ddd": 5.923, "eee": 8.121, "fff": 7.492, "ggg": 2.223},
],
],
],
)
def test_007(keys, rows, expected):
res = q.f007(keys, rows)
assert res == expected
@pytest.mark.parametrize(
"keys, cols, expected",
[
[
["aaa", "bbb", "ccc"],
[
[5.982, 2.904],
[4.221, 3.434],
[9.475, 6.549],
],
[
{"aaa": 5.982, "bbb": 4.221, "ccc": 9.475},
{"aaa": 2.904, "bbb": 3.434, "ccc": 6.549},
],
],
[
["ddd", "eee", "fff", "ggg"],
[
[5.982, 8.853, 5.923],
[4.221, 3.123, 8.121],
[9.475, 6.659, 7.492],
[6.685, 4.129, 2.223],
],
[
{"ddd": 5.982, "eee": 4.221, "fff": 9.475, "ggg": 6.685},
{"ddd": 8.853, "eee": 3.123, "fff": 6.659, "ggg": 4.129},
{"ddd": 5.923, "eee": 8.121, "fff": 7.492, "ggg": 2.223},
],
],
],
)
def test_008(keys, cols, expected):
res = q.f008(keys, cols)
assert res == expected
@pytest.mark.parametrize(
"lst, expected",
[
[
[
{"aaa": 4.287, "bbb": 9.821, "ccc": 1.934},
{"aaa": 2.194, "bbb": 5.338, "ccc": 6.574},
],
{
"aaa": [4.287, 2.194],
"bbb": [9.821, 5.338],
"ccc": [1.934, 6.574],
},
],
[
[
{"ddd": 8.764, "eee": 4.329},
{"ddd": 0.926, "eee": 3.927},
{"ddd": 5.551, "eee": 7.452},
],
{
"ddd": [8.764, 0.926, 5.551],
"eee": [4.329, 3.927, 7.452],
},
],
],
)
def test_009(lst, expected):
res = q.f009(lst)
assert res == expected
@pytest.mark.parametrize(
"dct, expected",
[
[
{
"aaa": [4.287, 2.194],
"bbb": [9.821, 5.338],
"ccc": [1.934, 6.574],
},
[
{"aaa": 4.287, "bbb": 9.821, "ccc": 1.934},
{"aaa": 2.194, "bbb": 5.338, "ccc": 6.574},
],
],
[
{
"ddd": [8.764, 0.926, 5.551],
"eee": [4.329, 3.927, 7.452],
},
[
{"ddd": 8.764, "eee": 4.329},
{"ddd": 0.926, "eee": 3.927},
{"ddd": 5.551, "eee": 7.452},
],
],
],
)
def test_010(dct, expected):
res = q.f010(dct)
assert res == expected
@pytest.mark.parametrize(
"dct, newk, newv, expected",
[
[{"a": 3, "b": 1, "c": 2}, "d", 5, {"a": 3, "b": 1, "c": 2, "d": 5}],
[{"e": 3.14, "f": 2.74}, "g", 0.25, {"e": 3.14, "f": 2.74, "g": 0.25}],
],
)
def test_011(dct, newk, newv, expected):
res = q.f011(dct, newk, newv)
assert res is None
assert dct == expected
@pytest.mark.parametrize(
"dct, newk, newv, expected",
[
[{"a": 3, "b": 1, "c": 2}, "d", 5, {"a": 3, "b": 1, "c": 2, "d": 5}],
[{"e": 3.14, "f": 2.74}, "g", 0.25, {"e": 3.14, "f": 2.74, "g": 0.25}],
],
)
def test_012(dct, newk, newv, expected):
dct_bak = dct.copy()
res = q.f012(dct, newk, newv)
assert res == expected
assert list(res) == list(expected)
assert dct == dct_bak
@pytest.mark.parametrize(
"dct, newk, newv, expected",
[
[{"a": 3, "b": 1, "c": 2}, "d", 5, {"d": 5, "a": 3, "b": 1, "c": 2}],
[{"e": 3.14, "f": 2.74}, "g", 0.25, {"g": 0.25, "e": 3.14, "f": 2.74}],
],
)
def test_013(dct, newk, newv, expected):
dct_bak = dct.copy()
res = q.f013(dct, newk, newv)
assert res == expected
assert list(res) == list(expected)
assert dct == dct_bak
def test_014_1():
my_dct = {"a": 3, "b": 1, "c": 2}
res = q.f014(my_dct, "b")
assert res is None
assert my_dct == {"a": 3, "c": 2}
def test_014_2():
my_dct = {"a": 3, "b": 1, "c": 2}
with pytest.raises(KeyError):
q.f014(my_dct, "d")
def test_015_1():
my_dct = {"a": 3, "b": 1, "c": 2}
res = q.f015(my_dct, "b")
assert res == 1
assert my_dct == {"a": 3, "c": 2}
def test_015_2():
my_dct = {"a": 3, "b": 1, "c": 2}
with pytest.raises(KeyError):
q.f015(my_dct, "d")
def test_016():
my_dct = {"a": 3, "b": 1, "c": 2}
res = q.f016(my_dct, "b")
assert res == 1
assert my_dct == {"a": 3, "c": 2}
my_dct = {"a": 3, "b": 1, "c": 2}
res = q.f016(my_dct, "d")
assert res is None
assert my_dct == {"a": 3, "b": 1, "c": 2}
@pytest.mark.parametrize(
"dct, newk, newv, expected_res, expected_dct",
[
[{"a": 3, "b": 1, "c": 2}, "c", 5, 2, {"a": 3, "b": 1, "c": 2}],
[{"a": 3, "c": 2}, "b", 5, 5, {"a": 3, "c": 2, "b": 5}],
],
)
def test_017(dct, newk, newv, expected_res, expected_dct):
res = q.f017(dct, newk, newv)
assert res == expected_res
assert dct == expected_dct
assert list(dct) == list(expected_dct)
@pytest.mark.parametrize(
"dct, kvs, expected",
[
[{"a": 3, "b": 1, "c": 2}, {"a": 5, "c": 4}, {"a": 5, "b": 1, "c": 4}],
[
{"a": 3, "b": 1, "c": 2},
{"a": 5, "e": 4, "d": 0},
{"a": 5, "b": 1, "c": 2, "e": 4, "d": 0},
],
],
)
def test_018(dct, kvs, expected):
res = q.f018(dct, **kvs)
assert res is None
assert dct == expected
assert list(dct) == list(expected)
@pytest.mark.parametrize(
"dct, kvs, expected",
[
[{"a": 3, "b": 1, "c": 2}, {"a": 5, "c": 4}, {"a": 5, "b": 1, "c": 4}],
[
{"a": 3, "b": 1, "c": 2},
{"a": 5, "e": 4, "d": 0},
{"a": 5, "b": 1, "c": 2, "e": 4, "d": 0},
],
],
)
def test_019(dct, kvs, expected):
dct_bak = dct.copy()
res = q.f019(dct, **kvs)
assert res == expected
assert list(res) == list(expected)
assert dct == dct_bak
@pytest.mark.parametrize(
"dct1, dct2, exp1, exp2",
[
[
{"a": 3, "b": 1, "c": 2},
{"d": 5, "e": 4},
{"d": 5, "e": 4},
{"a": 3, "b": 1, "c": 2},
],
[
{"d": 5, "c": 1},
{"b": 3, "e": 4, "a": 2},
{"b": 3, "e": 4, "a": 2},
{"d": 5, "c": 1},
],
],
)
def test_020(dct1, dct2, exp1, exp2):
res = q.f020(dct1, dct2)
assert res is None
assert dct1 == exp1
assert dct2 == exp2
def test_021_1():
my_dct = {"a": 3, "b": 1, "c": 2}
res = q.f021(my_dct, "c")
assert res == 2
def test_021_2():
my_dct = {"a": 3, "b": 1, "c": 2}
with pytest.raises(KeyError):
q.f021(my_dct, "e")
def test_022():
my_dct = {"a": 3, "b": 1, "c": 2}
res = q.f022(my_dct, "c")
assert res == 2
res = q.f022(my_dct, "e")
assert res is None
def test_023():
dct1 = {"a": 3, "b": 1, "c": 2}
dct2 = {"c": 5, "d": 4}
res = q.f023("c", dct1, dct2)
assert res == 2
res = q.f023("c", dct2, dct1)
assert res == 5
res = q.f023("d", dct1, dct2)
assert res == 4
res = q.f023("e", dct1, dct2)
assert res is None
res = q.f023("e", dct1, dct2, {"e": 7})
assert res == 7
res = q.f023("c")
assert res is None
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化