File: test_atomdict.py

package info (click to toggle)
python-atom 0.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,616 kB
  • sloc: cpp: 9,040; python: 6,249; makefile: 123
file content (188 lines) | stat: -rw-r--r-- 5,691 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# --------------------------------------------------------------------------------------
# Copyright (c) 2018-2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# --------------------------------------------------------------------------------------
"""Test the typed dictionary."""

import pytest

from atom.api import Atom, Dict, Int, List, atomdict, atomlist


@pytest.fixture
def atom_dict():
    """Atom with different Dict members."""

    class DictAtom(Atom):
        untyped = Dict()
        keytyped = Dict(Int())
        valuetyped = Dict(value=Int())
        fullytyped = Dict(Int(), Int())
        untyped_default = Dict(default={1: 1})
        keytyped_default = Dict(Int(), default={1: 1})
        valuetyped_default = Dict(value=Int(), default={1: 1})
        fullytyped_default = Dict(Int(), Int(), default={1: 1})

    return DictAtom()


MEMBERS = [
    "untyped",
    "keytyped",
    "valuetyped",
    "fullytyped",
    "untyped_default",
    "keytyped_default",
    "valuetyped_default",
    "fullytyped_default",
]


@pytest.mark.parametrize("member", MEMBERS)
def test_instance(atom_dict, member):
    """Test the repr."""
    assert isinstance(getattr(atom_dict, member), atomdict)


@pytest.mark.parametrize("member", MEMBERS)
def test_repr(atom_dict, member):
    """Test the repr."""
    d = getattr(atom_dict.__class__, member).default_value_mode[1]
    if not d:
        d = {i: i**2 for i in range(10)}
        setattr(atom_dict, member, d)
    assert repr(getattr(atom_dict, member)) == repr(d)


@pytest.mark.parametrize("member", MEMBERS)
def test_len(atom_dict, member):
    """Test the len."""
    d = getattr(atom_dict.__class__, member).default_value_mode[1]
    if not d:
        d = {i: i**2 for i in range(10)}
        setattr(atom_dict, member, d)
    assert len(getattr(atom_dict, member)) == len(d)


@pytest.mark.parametrize("member", MEMBERS)
def test_contains(atom_dict, member):
    """Test __contains__."""
    d = {i: i**2 for i in range(10)}
    setattr(atom_dict, member, d)
    assert 5 in getattr(atom_dict, member)
    del getattr(atom_dict, member)[5]
    assert 5 not in getattr(atom_dict, member)


@pytest.mark.parametrize("member", MEMBERS)
def test_keys(atom_dict, member):
    """Test the keys."""
    d = getattr(atom_dict.__class__, member).default_value_mode[1]
    if not d:
        d = {i: i**2 for i in range(10)}
        setattr(atom_dict, member, d)
    assert getattr(atom_dict, member).keys() == d.keys()


@pytest.mark.parametrize("member", MEMBERS)
def test_copy(atom_dict, member):
    """Test copy."""
    d = getattr(atom_dict.__class__, member).default_value_mode[1]
    if not d:
        d = {i: i**2 for i in range(10)}
        setattr(atom_dict, member, d)
    assert getattr(atom_dict, member).copy() == d


def test_setitem(atom_dict):
    """Test setting items."""
    atom_dict.untyped[""] = 1
    assert atom_dict.untyped[""] == 1

    atom_dict.keytyped[1] = ""
    assert atom_dict.keytyped[1] == ""
    with pytest.raises(TypeError):
        atom_dict.keytyped[""] = 1

    atom_dict.valuetyped[1] = 1
    assert atom_dict.valuetyped[1] == 1
    with pytest.raises(TypeError):
        atom_dict.valuetyped[""] = ""

    atom_dict.fullytyped[1] = 1
    assert atom_dict.fullytyped[1] == 1
    with pytest.raises(TypeError):
        atom_dict.fullytyped[""] = 1
    with pytest.raises(TypeError):
        atom_dict.fullytyped[1] = ""


def test_setdefault(atom_dict):
    """Test using setdefault."""

    assert atom_dict.untyped.setdefault("", 1) == 1
    assert atom_dict.untyped.setdefault("", 2) == 1
    assert atom_dict.untyped[""] == 1

    assert atom_dict.keytyped.setdefault(1, "") == ""
    assert atom_dict.keytyped[1] == ""
    with pytest.raises(TypeError):
        atom_dict.keytyped.setdefault("", 1)

    assert atom_dict.valuetyped.setdefault(1, 1) == 1
    assert atom_dict.valuetyped.setdefault(1, "") == 1
    assert atom_dict.valuetyped[1] == 1
    with pytest.raises(TypeError):
        atom_dict.valuetyped.setdefault(2, "")

    assert atom_dict.fullytyped.setdefault(1, 1) == 1
    assert atom_dict.fullytyped.setdefault(1, "") == 1
    assert atom_dict.fullytyped[1] == 1
    with pytest.raises(TypeError):
        atom_dict.fullytyped.setdefault("", 1)
    with pytest.raises(TypeError):
        atom_dict.fullytyped.setdefault(2, "")


def test_setdefault_coercion():
    class A(Atom):
        d = Dict(int, List(int))

    a = A()
    content = a.d.setdefault(1, [])
    assert isinstance(content, atomlist)
    assert content is a.d[1]


def test_update(atom_dict):
    """Test update a dict."""
    atom_dict.untyped.update({"": 1})
    assert atom_dict.untyped[""] == 1
    atom_dict.untyped.update([("1", 1)])
    assert atom_dict.untyped["1"] == 1

    atom_dict.keytyped.update({1: 1})
    assert atom_dict.keytyped[1] == 1
    atom_dict.keytyped.update([(2, 1)])
    assert atom_dict.keytyped[1] == 1
    with pytest.raises(TypeError):
        atom_dict.keytyped.update({"": 1})

    atom_dict.valuetyped.update({1: 1})
    assert atom_dict.valuetyped[1] == 1
    atom_dict.valuetyped.update([(2, 1)])
    assert atom_dict.valuetyped[1] == 1
    with pytest.raises(TypeError):
        atom_dict.valuetyped.update({"": ""})

    atom_dict.fullytyped.update({1: 1})
    assert atom_dict.fullytyped[1] == 1
    atom_dict.fullytyped.update([(2, 1)])
    assert atom_dict.fullytyped[1] == 1
    with pytest.raises(TypeError):
        atom_dict.fullytyped.update({"": 1})
    with pytest.raises(TypeError):
        atom_dict.fullytyped.update({"": ""})