Example #1
0
def test_from_dict_different_casing_raises_error() -> None:
    # given
    dic = {"Name": "Liz", "Age": 42}

    # when - then
    with pytest.raises(DeserializationError):
        _ = from_dict(data_class=_TestDataClass, data=dic)
Example #2
0
def test_from_dict_missing_required_field_raises_error() -> None:
    # given
    dic = {"name": "Liz", "hobby": "papercut"}

    # when - then
    with pytest.raises(DeserializationError):
        _ = from_dict(data_class=_TestDataClass, data=dic)
Example #3
0
def test_from_dict_only_required_fields_success() -> None:
    # given
    dic = {"name": "Liz", "age": 42}

    # when
    obj = from_dict(data_class=_TestDataClass, data=dic)

    # then
    assert obj is not None
    assert obj.name == "Liz"
    assert obj.age == 42
    assert obj.hobby is None
Example #4
0
def test_from_dict_required_and_optional_fields_success() -> None:
    # given
    dic = {"name": "Liz", "age": 42, "hobby": "papercut"}

    # when
    obj = from_dict(data_class=_TestDataClass, data=dic)

    # then
    assert obj is not None
    assert obj.name == "Liz"
    assert obj.age == 42
    assert obj.hobby == "papercut"
Example #5
0
def test_from_dict_required_optional_extra_fields_success() -> None:
    """ Unexpected dictionary fields should just be ignored """

    # given
    dic = {
        "name": "Liz",
        "age": 42,
        "hobby": "papercut",
        "origin": "Sierra Leone"
    }

    # when
    obj = from_dict(data_class=_TestDataClass, data=dic)

    # then
    assert obj is not None
    assert obj.name == "Liz"
    assert obj.age == 42
    assert obj.hobby == "papercut"
 def from_json(cls, json_string):
     dic = from_json(class_name=cls.__name__,
                     json_string=json_string,
                     root="site")
     return cls(site=from_dict(data_class=CreateResponse._Site, data=dic))
 def from_dict(cls, dic: Dict[str, Any]):
     return cls(site=from_dict(data_class=GetResponse._Site, data=dic))
 def from_dict(cls, dic: Dict[str, Any]):
     # recreate GET/POST/PUT response payload: fill all available fields
     return from_dict(data_class=cls, data=dic)
 def from_dict(cls, dic: Dict[str, Any]):
     return from_dict(data_class=cls, data=dic)
Example #10
0
 def from_json(cls, json_string):
     dic = from_json(class_name=cls.__name__, json_string=json_string)
     return from_dict(data_class=cls, data=dic)