TestDotDict.test_req()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 1
dl 0
loc 12
rs 9.9
c 0
b 0
f 0
1
# SPDX-FileCopyrightText: Copyright 2020-2023, Contributors to pocketutils
2
# SPDX-PackageHomePage: https://github.com/dmyersturnbull/pocketutils
3
# SPDX-License-Identifier: Apache-2.0
4
from datetime import date
5
from typing import Self
6
7
import pytest
8
from pocketutils.core.dot_dict import NestedDotDict
9
10
11
class TestDotDict:
12
    def test_keys(self: Self) -> None:
13
        t = NestedDotDict({"a": "0", "b": 1, "c": {"c1": 8, "c2": ["abc", "xyz"]}})
14
        assert list(t.keys()) == ["a", "b", "c"]
15
16
    def test_bad(self: Self) -> None:
17
        with pytest.raises(ValueError):
18
            NestedDotDict({"a.b": "c"})
19
        with pytest.raises(ValueError):
20
            NestedDotDict({"a": {"b.c": "d"}})
21
        with pytest.raises(TypeError):
22
            # noinspection PyTypeChecker
23
            NestedDotDict({1: 2})
24
        with pytest.raises(ValueError):
25
            NestedDotDict({"a.b": {5: 2}})
26
27
    def test_iter(self: Self) -> None:
28
        t = NestedDotDict({"a": {"b": 555}})
29
        assert len(t) == 1
30
        assert list(iter(t)) == ["a"]
31
        assert list(t.items()) == [("a", {"b": 555})]
32
33
    def test_get(self: Self) -> None:
34
        t = NestedDotDict({"a": {"b": {"c": "hello"}}})
35
        assert t.get("a") == {"b": {"c": "hello"}}
36
        assert t.get("a.b") == {"c": "hello"}
37
        assert t.get("a.b.c") == "hello"
38
        assert t["a"] == {"b": {"c": "hello"}}
39
        assert t["a.b"] == {"c": "hello"}
40
        assert t["a.b.c"] == "hello"
41
        assert t.get("b") is None
42
        assert t.get("a.x") is None
43
        assert t.get("a.b.x") is None
44
        assert t.get("a.b.c.x") is None
45
        assert t.get("b", default="Q") == "Q"
46
        with pytest.raises(LookupError):
47
            # noinspection PyStatementEffect
48
            t["b"]
49
        with pytest.raises(LookupError):
50
            # noinspection PyStatementEffect
51
            t["a.x"]
52
        with pytest.raises(LookupError):
53
            # noinspection PyStatementEffect
54
            t["a.b.c.x"]
55
        t = NestedDotDict({"a": {"1": "hello", "2": "ell", "3": "elle"}})
56
        assert t["a.1"] == "hello"
57
        assert t["a.2"] == "ell"
58
        assert t["a.3"] == "elle"
59
60
    def test_get_as(self: Self) -> None:
61
        t = NestedDotDict(
62
            {
63
                "animal": "kitten",
64
                "size": 4.3,
65
                "number": 8,
66
                "birthday": "2020-01-17",
67
                "birthdayzulu": "2020-01-17Z",
68
                "birthdatetime": "2020-01-17T15:22:11",
69
                "birthdatetimezulu": "2020-01-17T15:22:11Z",
70
            },
71
        )
72
        assert t.get_as("animal", str) == "kitten"
73
        assert t.get_as("size", float) == 4.3
74
        with pytest.raises(TypeError):
75
            t.get_as("size", str)
76
        assert t.get_as("number", int) == 8
77
        with pytest.raises(TypeError):
78
            t.get_as("birthday", date)
79
80
    def test_get_list_as(self: Self) -> None:
81
        t = NestedDotDict({"kittens": ["dory", "johnson", "robin", "jack"], "ages": [3, 5, 7, 11]})
82
        assert t.get_list_as("kittens", str) == ["dory", "johnson", "robin", "jack"]
83
        assert t.get_list_as("ages", int) == [3, 5, 7, 11]
84
        with pytest.raises(TypeError):
85
            t.get_list_as("ages", str)
86
        assert t.get_list_as("hobbies", str) == []
87
        with pytest.raises(TypeError):
88
            t.get_list_as("kittens", float)
89
90
    def test_req(self: Self) -> None:
91
        t = NestedDotDict(
92
            {
93
                "kittens": ["dory", "johnson", "robin", "jack"],
94
                "ages": [3, 5, 7, 11],
95
                "carrots": "yes",
96
            },
97
        )
98
        assert t.req_as("carrots", str) == "yes"
99
        assert t.req_list_as("ages", int) == [3, 5, 7, 11]
100
        with pytest.raises(LookupError):
101
            t.req_list_as("hobbies", str)
102
103
    def test_nested(self: Self) -> None:
104
        t = NestedDotDict(
105
            {"kittens": {"names": ["dory", "johnson", "robin", "jack"], "ages": [3, 5, 7, 11]}},
106
        )
107
        assert isinstance(t.get("kittens"), dict)
108
        assert t.get("kittens.ages") == [3, 5, 7, 11]
109
        assert t.get_list("kittens.ages") == [3, 5, 7, 11]
110
        assert t.get_list_as("kittens.ages", int) == [3, 5, 7, 11]
111
112
    def test_leaves(self: Self) -> None:
113
        t = NestedDotDict({"a": {"b": 1}, "b": 2, "c": {"a": {"a": 3}}})
114
        assert t.leaves() == {"a.b": 1, "b": 2, "c.a.a": 3}
115
116
    def test_size(self: Self) -> None:
117
        t = NestedDotDict({})
118
        assert len(t.nodes()) == 0
119
        t = NestedDotDict({"x": "y"})
120
        assert len(t.nodes()) == 2
121
        t = NestedDotDict({"a": {"b": 1}, "b": 2, "c": {"a": {"a": 3}}})
122
        assert len(t.nodes()) == 6
123
        t = NestedDotDict({"a": {"b": 1}, "b": [1, 2, 3], "c": {"a": {"a": 3}}})
124
        assert len(t.nodes()) == 6
125
126
    def test_req_as(self: Self) -> None:
127
        t = NestedDotDict({"zoo": {"animals": "jackets"}, "what": 0.1})
128
        assert t.req_as("zoo.animals", str) == "jackets"
129
        assert t.req_as("what", float) == 0.1
130
        with pytest.raises(TypeError):
131
            t.req_as("what", str)
132
133
134
if __name__ == "__main__":
135
    pytest.main()
136