File: test_convert_key.py

package info (click to toggle)
dacite 1.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 332 kB
  • sloc: python: 1,870; makefile: 8
file content (19 lines) | stat: -rw-r--r-- 541 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
from dataclasses import dataclass
from dacite import Config, from_dict


def test_convert_key():
    def to_camel_case(key: str) -> str:
        first_part, *remaining_parts = key.split("_")
        return first_part + "".join(part.title() for part in remaining_parts)

    @dataclass
    class Person:
        first_name: str
        last_name: str

    data = {"firstName": "John", "lastName": "Doe"}

    result = from_dict(Person, data, Config(convert_key=to_camel_case))

    assert result == Person(first_name="John", last_name="Doe")