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
)
|