<reponame>alrudolph/attrs-mek<filename>tests/examples/section4_test.py
from datetime import datetime
from typing import List
from attrs_mek import mek, Value

# BEGIN README

response = {
    "people": [
        {"name": "Mek", "age": 0},
        {"name": "Mek2", "age": 10},
    ],
    "status": {"date": "2022/1/1"},
}


@mek
class Person:
    name: str
    age: int
    date: datetime = Value(converter=lambda x: datetime.strptime(x, "%Y/%m/%d"))


@mek
class People:
    people: List[Person] = Value(converter=Person.from_dict)
    # Because a `from_list` is provided, the converter is applied to each list element.

    @people.from_list
    def combiner(dictionary, people_item):
        # Combine the "status" dictionary with each element in "people"
        # For simple cases like this, see :func:`~attrs_mek.Value.from_list_merge`
        return {**dictionary["status"], **people_item}


mek_person = People.from_dict(response)
# People(people=[
#   Person(name='Mek', age=0, date=datetime.datetime(2022, 1, 1, 0, 0)),
#   Person(name='Mek2', age=10, date=datetime.datetime(2022, 1, 1, 0, 0))
# ])                                  (line breaks added for readability)

# END README


def test_section4():
    for mp, p in zip(mek_person.people, response["people"]):
        assert mp.name == p["name"]
        assert mp.age == p["age"]
        assert mp.date.strftime("%Y/%-m/%-d") == response["status"]["date"]

from datetime import datetime
from typing import List
from attrs_mek import mek, Value

# BEGIN README

response = {
    "people": [
        {"name": "Mek", "age": 0},
        {"name": "Mek2", "age": 10},
    ],
    "status": {"date": "2022/1/1"},
}


@mek
class Person:
    name: str
    age: int
    date: datetime = Value(converter=lambda x: datetime.strptime(x, "%Y/%m/%d"))


@mek
class People:
    people: List[Person] = Value(converter=Person.from_dict)
    # Because a `from_list` is provided, the converter is applied to each list element.

    @people.from_list
    def combiner(dictionary, people_item):
        # Combine the "status" dictionary with each element in "people"
        # For simple cases like this, see :func:`~attrs_mek.Value.from_list_merge`
        return {**dictionary["status"], **people_item}


mek_person = People.from_dict(response)
# People(people=[
#   Person(name='Mek', age=0, date=datetime.datetime(2022, 1, 1, 0, 0)),
#   Person(name='Mek2', age=10, date=datetime.datetime(2022, 1, 1, 0, 0))
# ])                                  (line breaks added for readability)

# END README


def test_section4():
    for mp, p in zip(mek_person.people, response["people"]):
        assert mp.name == p["name"]
        assert mp.age == p["age"]
        assert mp.date.strftime("%Y/%-m/%-d") == response["status"]["date"]
# BEGIN README # Because a `from_list` is provided, the converter is applied to each list element. # Combine the "status" dictionary with each element in "people" # For simple cases like this, see :func:`~attrs_mek.Value.from_list_merge` # People(people=[ #   Person(name='Mek', age=0, date=datetime.datetime(2022, 1, 1, 0, 0)), #   Person(name='Mek2', age=10, date=datetime.datetime(2022, 1, 1, 0, 0)) # ])                                  (line breaks added for readability) # END README