def test_to_dict_undefined_types(): """ Execute the test. """ obj = UndefinedTypes() obj.value = [1, 2, 3] with raises(UndefinedTypeError, match="Type 'set' is not defined."): to_dict(obj)
def test_to_dict_several_types(): """ Execute the test. """ obj = SeveralDataTypes() obj.date = date(2019, 3, 6) obj.time = time(15, 36, 21) obj.datetime = datetime(2019, 3, 6, 15, 36, 21) obj.decimal = Decimal('21.0') obj.integer = 12 obj.floating = 13.5 obj.string = 'qwerty' result = to_dict(obj) assert result == { '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' }
def test_to_dict_list(): """ Execute the test. """ obj = PersonCatalog() obj.people = list() obj_0 = Person() obj_0.name = 'Fernando Cicconeto' obj_0.age = 29 obj.people.append(obj_0) obj_1 = Person() obj_1.name = 'Paulo da Silva' obj_1.age = 40 obj.people.append(obj_1) result = to_dict(obj) assert result == { 'people': [{ 'name': 'Fernando Cicconeto', 'age': 29 }, { 'name': 'Paulo da Silva', 'age': 40 }] }
def test_to_dict_ignore_non_existent(): """ Execute the test. """ obj = Person() obj.name = 'Fernando Cicconeto' result = to_dict(obj) assert result == {'name': 'Fernando Cicconeto'}
def to_dict(self, source): """ Converts to a dictionary data. """ result = list() for item in source: result.append(py_jobject.to_dict(item, self.type_)) return result
def test_to_dict_basic(): """ Execute the test. """ obj = Person() obj.name = 'Fernando Cicconeto' obj.age = 29 result = to_dict(obj) assert result == {'name': 'Fernando Cicconeto', 'age': 29}
def test_to_dict_convert_str_to_int(): """ Execute the test. """ obj = Person() obj.name = 'Fernando Cicconeto' obj.age = '29' result = to_dict(obj) assert result == { 'name': 'Fernando Cicconeto', 'age': 29 }
def test_to_dict_accepts_date_string(): """ Execute the test. """ obj = SeveralDataTypes() obj.date = '2019-03-06' obj.time = '15:36:21' obj.datetime = '2019-03-06 15:36:21' result = to_dict(obj) assert result == { 'date': '2019-03-06', 'time': '15:36:21', 'datetime': '2019-03-06 15:36:21' }
def to_dict(self, source): """ Converts to a dictionary data. """ result = {} for name, value in source.__dict__.items(): value_type = self.type_.__annotations__.get(name) if value_type is not None: value_result = py_jobject.to_dict(value, value_type) else: value_result = value result[name] = value_result return result
def test_to_dict_error_proof(): """ Execute the test. """ obj = SeveralDataTypes() obj.date = 'dgafh' obj.time = 'fadhaf' obj.datetime = 'dgad' obj.decimal = 'fhjsg' obj.integer = 'rhar' obj.floating = 'gjaobre' result = to_dict(obj) assert result == { 'date': None, 'time': None, 'datetime': None, 'decimal': None, 'integer': None, 'floating': None }