File: test_hooks.py

package info (click to toggle)
dataclass-wizard 0.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,924 kB
  • sloc: python: 17,189; makefile: 126; javascript: 23
file content (73 lines) | stat: -rw-r--r-- 1,817 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
from __future__ import annotations

import pytest

from dataclasses import dataclass
from ipaddress import IPv4Address

from dataclass_wizard import JSONWizard, LoadMeta
from dataclass_wizard.errors import ParseError
from dataclass_wizard import DumpMixin, LoadMixin


def test_register_type_ipv4address_roundtrip():

    @dataclass
    class Foo(JSONWizard):
        s: str | None = None
        c: IPv4Address | None = None

    Foo.register_type(IPv4Address)

    data = {"c": "127.0.0.1", "s": "foobar"}

    foo = Foo.from_dict(data)
    assert foo.c == IPv4Address("127.0.0.1")

    assert foo.to_dict() == data
    assert Foo.from_dict(foo.to_dict()).to_dict() == data


def test_ipv4address_without_hook_raises_parse_error():

    @dataclass
    class Foo(JSONWizard):
        c: IPv4Address | None = None

    data = {"c": "127.0.0.1"}

    with pytest.raises(ParseError) as e:
        Foo.from_dict(data)

    assert e.value.phase == 'load'

    msg = str(e.value)
    # assert "field `c`" in msg
    assert "not currently supported" in msg
    assert "IPv4Address" in msg
    assert "load" in msg.lower()


def test_ipv4address_hooks_with_load_and_dump_mixins_roundtrip():
    @dataclass
    class Foo(JSONWizard, DumpMixin, LoadMixin):
        c: IPv4Address | None = None

        @classmethod
        def load_to_ipv4_address(cls, o, *_):
            return IPv4Address(o)

        @classmethod
        def dump_from_ipv4_address(cls, o, *_):
            return str(o)

    Foo.register_load_hook(IPv4Address, Foo.load_to_ipv4_address)
    Foo.register_dump_hook(IPv4Address, Foo.dump_from_ipv4_address)

    data = {"c": "127.0.0.1"}

    foo = Foo.from_dict(data)
    assert foo.c == IPv4Address("127.0.0.1")

    assert foo.to_dict() == data
    assert Foo.from_dict(foo.to_dict()).to_dict() == data