Example #1
0
def test_from_dict_undefined_types():
    """
    Execute the test.
    """

    source = {
        'value': [1, 2, 3]
    }

    with raises(UndefinedTypeError, match="Type 'set' is not defined."):
        from_dict(source, UndefinedTypes)
Example #2
0
def test_from_dict_list():
    """
    Execute the test.
    """

    source = {
        'people': [{
            'name': 'Fernando Cicconeto',
            'age': 29
        }, {
            'name': 'Paulo da Silva',
            'age': 40
        }]
    }

    result = from_dict(source, PersonCatalog)

    assert isinstance(result, PersonCatalog)
    assert isinstance(result.people, list)
    assert len(result.people) == 2

    assert isinstance(result.people[0], Person)
    assert result.people[0].name == 'Fernando Cicconeto'
    assert result.people[0].age == 29

    assert isinstance(result.people[1], Person)
    assert result.people[1].name == 'Paulo da Silva'
    assert result.people[1].age == 40
Example #3
0
def test_from_dict_several_types():
    """
    Execute the test.
    """

    source = {
        'date': '2019-03-06',
        'time': '15:36:21',
        'datetime': '2019-03-06 15:36:21',
        'decimal': '21.0',
        'integer': '12',
        'floating': '13.5',
        'string': 'qwerty'
    }

    result = from_dict(source, SeveralDataTypes)

    assert isinstance(result, SeveralDataTypes)
    assert result.date == date(2019, 3, 6)
    assert result.time == time(15, 36, 21)
    assert result.datetime == datetime(2019, 3, 6, 15, 36, 21)
    assert result.decimal == Decimal('21.0')
    assert result.integer == 12
    assert result.floating == 13.5
    assert result.string == 'qwerty'
Example #4
0
    def from_dict(self, source):
        """
        Converts from a dictionary data.
        """

        result = list()

        for item in source:
            result.append(py_jobject.from_dict(item, self.type_))

        return result
def test_from_dict_convert_str_to_int():
    """
    Execute the test.
    """

    source = {'name': 'Fernando Cicconeto', 'age': '29'}

    result = from_dict(source, Person)

    assert isinstance(result, Person)
    assert result.name == 'Fernando Cicconeto'
    assert result.age == 29
def test_from_dict_basic():
    """
    Execute the test.
    """

    source = {'name': 'Fernando Cicconeto', 'age': 29}

    result = from_dict(source, Person)

    assert isinstance(result, Person)
    assert result.name == 'Fernando Cicconeto'
    assert result.age == 29
def test_from_dict_extras():
    """
    Execute the test.
    """

    source = {'name': 'Fernando Cicconeto', 'age': 29, 'city': 'Caxias do Sul'}

    result = from_dict(source, Person)

    assert isinstance(result, Person)
    assert result.name == 'Fernando Cicconeto'
    assert result.age == 29
    assert result.city == 'Caxias do Sul'
Example #8
0
    def from_dict(self, source):
        """
        Converts from a dictionary data.
        """

        result = self.type_()

        for name, value in source.items():
            value_type = self.type_.__annotations__.get(name)

            if value_type is not None:
                value_result = py_jobject.from_dict(value, value_type)
            else:
                value_result = value

            setattr(result, name, value_result)

        return result
Example #9
0
def test_from_dict_error_proof():
    """
    Execute the test.
    """

    source = {
        'date': '0000-00-00',
        'time': 'aaa',
        'datetime': '0000-00-00 00:00:00',
        'decimal': 'b',
        'integer': '4j',
        'floating': 'fhasfh'
    }

    result = from_dict(source, SeveralDataTypes)

    assert isinstance(result, SeveralDataTypes)
    assert result.date is None
    assert result.time is None
    assert result.datetime is None
    assert result.decimal is None
    assert result.integer is None
    assert result.floating is None