File: test_toml_codec.py

package info (click to toggle)
python-mashumaro 3.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,408 kB
  • sloc: python: 19,981; sh: 16; makefile: 5
file content (61 lines) | stat: -rw-r--r-- 1,466 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
from datetime import date
from typing import Dict, List

import tomli_w

from mashumaro.codecs.toml import (
    TOMLDecoder,
    TOMLEncoder,
    toml_decode,
    toml_encode,
)
from mashumaro.dialect import Dialect


class MyDialect(Dialect):
    serialization_strategy = {
        date: {
            "serialize": date.toordinal,
            "deserialize": date.fromordinal,
        },
    }


def test_toml_decode():
    data = tomli_w.dumps({"x": [date(2023, 9, 22), date(2023, 9, 23)]})
    assert toml_decode(data, Dict[str, List[date]]) == {
        "x": [
            date(2023, 9, 22),
            date(2023, 9, 23),
        ]
    }


def test_toml_encode():
    data = tomli_w.dumps({"x": [date(2023, 9, 22), date(2023, 9, 23)]})
    assert (
        toml_encode(
            {"x": [date(2023, 9, 22), date(2023, 9, 23)]},
            Dict[str, List[date]],
        )
        == data
    )


def test_decoder_with_default_dialect():
    data = tomli_w.dumps({"x": [738785, 738786]})
    decoder = TOMLDecoder(Dict[str, List[date]], default_dialect=MyDialect)
    assert decoder.decode(data) == {
        "x": [
            date(2023, 9, 22),
            date(2023, 9, 23),
        ]
    }


def test_encoder_with_default_dialect():
    data = tomli_w.dumps({"x": [738785, 738786]})
    encoder = TOMLEncoder(Dict[str, List[date]], default_dialect=MyDialect)
    assert (
        encoder.encode({"x": [date(2023, 9, 22), date(2023, 9, 23)]}) == data
    )