File: test_utils.py

package info (click to toggle)
python-marshmallow 3.26.1-0.2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,280 kB
  • sloc: python: 11,513; makefile: 11; sh: 8
file content (283 lines) | stat: -rw-r--r-- 7,688 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import datetime as dt
from copy import copy, deepcopy
from functools import partial
from typing import NamedTuple

import pytest

from marshmallow import Schema, fields, utils
from tests.base import assert_date_equal, assert_time_equal, central


def test_missing_singleton_copy():
    assert copy(utils.missing) is utils.missing
    assert deepcopy(utils.missing) is utils.missing


class PointNT(NamedTuple):
    x: int
    y: int


class PointClass:
    def __init__(self, x, y):
        self.x = x
        self.y = y


class PointDict(dict):
    def __init__(self, x, y):
        super().__init__({"x": x})
        self.y = y


@pytest.mark.parametrize(
    "obj", [PointNT(24, 42), PointClass(24, 42), PointDict(24, 42), {"x": 24, "y": 42}]
)
def test_get_value_from_object(obj):
    assert utils.get_value(obj, "x") == 24
    assert utils.get_value(obj, "y") == 42


def test_get_value_from_namedtuple_with_default():
    p = PointNT(x=42, y=None)
    # Default is only returned if key is not found
    assert utils.get_value(p, "z", default=123) == 123
    # since 'y' is an attribute, None is returned instead of the default
    assert utils.get_value(p, "y", default=123) is None


class Triangle:
    def __init__(self, p1, p2, p3):
        self.p1 = p1
        self.p2 = p2
        self.p3 = p3
        self.points = [p1, p2, p3]


def test_get_value_for_nested_object():
    tri = Triangle(p1=PointClass(1, 2), p2=PointNT(3, 4), p3={"x": 5, "y": 6})
    assert utils.get_value(tri, "p1.x") == 1
    assert utils.get_value(tri, "p2.x") == 3
    assert utils.get_value(tri, "p3.x") == 5


# regression test for https://github.com/marshmallow-code/marshmallow/issues/62
def test_get_value_from_dict():
    d = dict(items=["foo", "bar"], keys=["baz", "quux"])
    assert utils.get_value(d, "items") == ["foo", "bar"]
    assert utils.get_value(d, "keys") == ["baz", "quux"]


def test_get_value():
    lst = [1, 2, 3]
    assert utils.get_value(lst, 1) == 2

    class MyInt(int):
        pass

    assert utils.get_value(lst, MyInt(1)) == 2


def test_set_value():
    d = {}
    utils.set_value(d, "foo", 42)
    assert d == {"foo": 42}

    d = {}
    utils.set_value(d, "foo.bar", 42)
    assert d == {"foo": {"bar": 42}}

    d = {"foo": {}}
    utils.set_value(d, "foo.bar", 42)
    assert d == {"foo": {"bar": 42}}

    d = {"foo": 42}
    with pytest.raises(ValueError):
        utils.set_value(d, "foo.bar", 42)


def test_is_keyed_tuple():
    p = PointNT(24, 42)
    assert utils.is_keyed_tuple(p) is True
    t = (24, 42)
    assert utils.is_keyed_tuple(t) is False
    d = {"x": 42, "y": 24}
    assert utils.is_keyed_tuple(d) is False
    s = "xy"
    assert utils.is_keyed_tuple(s) is False
    lst = [24, 42]
    assert utils.is_keyed_tuple(lst) is False


def test_is_collection():
    assert utils.is_collection([1, "foo", {}]) is True
    assert utils.is_collection(("foo", 2.3)) is True
    assert utils.is_collection({"foo": "bar"}) is False


@pytest.mark.parametrize(
    ("value", "expected"),
    [
        (dt.datetime(2013, 11, 10, 1, 23, 45), "Sun, 10 Nov 2013 01:23:45 -0000"),
        (
            dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=dt.timezone.utc),
            "Sun, 10 Nov 2013 01:23:45 +0000",
        ),
        (
            dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=central),
            "Sun, 10 Nov 2013 01:23:45 -0600",
        ),
    ],
)
def test_rfc_format(value, expected):
    assert utils.rfcformat(value) == expected


@pytest.mark.parametrize(
    ("value", "expected"),
    [
        (dt.datetime(2013, 11, 10, 1, 23, 45), "2013-11-10T01:23:45"),
        (
            dt.datetime(2013, 11, 10, 1, 23, 45, 123456, tzinfo=dt.timezone.utc),
            "2013-11-10T01:23:45.123456+00:00",
        ),
        (
            dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=dt.timezone.utc),
            "2013-11-10T01:23:45+00:00",
        ),
        (
            dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=central),
            "2013-11-10T01:23:45-06:00",
        ),
    ],
)
def test_isoformat(value, expected):
    assert utils.isoformat(value) == expected


@pytest.mark.parametrize(
    ("value", "expected"),
    [
        ("Sun, 10 Nov 2013 01:23:45 -0000", dt.datetime(2013, 11, 10, 1, 23, 45)),
        (
            "Sun, 10 Nov 2013 01:23:45 +0000",
            dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=dt.timezone.utc),
        ),
        (
            "Sun, 10 Nov 2013 01:23:45 -0600",
            dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=central),
        ),
    ],
)
def test_from_rfc(value, expected):
    result = utils.from_rfc(value)
    assert type(result) is dt.datetime
    assert result == expected


@pytest.mark.parametrize(
    ("value", "expected"),
    [
        ("2013-11-10T01:23:45", dt.datetime(2013, 11, 10, 1, 23, 45)),
        (
            "2013-11-10T01:23:45+00:00",
            dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=dt.timezone.utc),
        ),
        (
            # Regression test for https://github.com/marshmallow-code/marshmallow/issues/1251
            "2013-11-10T01:23:45.123+00:00",
            dt.datetime(2013, 11, 10, 1, 23, 45, 123000, tzinfo=dt.timezone.utc),
        ),
        (
            "2013-11-10T01:23:45.123456+00:00",
            dt.datetime(2013, 11, 10, 1, 23, 45, 123456, tzinfo=dt.timezone.utc),
        ),
        (
            "2013-11-10T01:23:45-06:00",
            dt.datetime(2013, 11, 10, 1, 23, 45, tzinfo=central),
        ),
    ],
)
def test_from_iso_datetime(value, expected):
    result = utils.from_iso_datetime(value)
    assert type(result) is dt.datetime
    assert result == expected


def test_from_iso_time_with_microseconds():
    t = dt.time(1, 23, 45, 6789)
    formatted = t.isoformat()
    result = utils.from_iso_time(formatted)
    assert type(result) is dt.time
    assert_time_equal(result, t)


def test_from_iso_time_without_microseconds():
    t = dt.time(1, 23, 45)
    formatted = t.isoformat()
    result = utils.from_iso_time(formatted)
    assert type(result) is dt.time
    assert_time_equal(result, t)


def test_from_iso_date():
    d = dt.date(2014, 8, 21)
    iso_date = d.isoformat()
    result = utils.from_iso_date(iso_date)
    assert type(result) is dt.date
    assert_date_equal(result, d)


@pytest.mark.parametrize(
    ("value", "expected"),
    [
        (1676386740, dt.datetime(2023, 2, 14, 14, 59, 00)),
        (1676386740.58, dt.datetime(2023, 2, 14, 14, 59, 00, 580000)),
    ],
)
def test_from_timestamp(value, expected):
    result = utils.from_timestamp(value)
    assert type(result) is dt.datetime
    assert result == expected


def test_from_timestamp_with_negative_value():
    value = -10
    with pytest.raises(ValueError, match=r"Not a valid POSIX timestamp"):
        utils.from_timestamp(value)


def test_from_timestamp_with_overflow_value():
    value = 9223372036854775
    with pytest.raises(ValueError, match="out of range"):
        utils.from_timestamp(value)


def test_get_func_args():
    def f1(foo, bar):
        pass

    f2 = partial(f1, "baz")

    class F3:
        def __call__(self, foo, bar):
            pass

    f3 = F3()

    for func in [f1, f2, f3]:
        assert utils.get_func_args(func) == ["foo", "bar"]


# Regression test for https://github.com/marshmallow-code/marshmallow/issues/540
def test_function_field_using_type_annotation():
    def get_split_words(value: str):
        return value.split(";")

    class MySchema(Schema):
        friends = fields.Function(deserialize=get_split_words)

    data = {"friends": "Clark;Alfred;Robin"}
    result = MySchema().load(data)
    assert result == {"friends": ["Clark", "Alfred", "Robin"]}