def test__object_to_dict(self): mapper = Mapper( year=IntegerProperty(), month=IntegerProperty(), day=IntegerProperty(), ) d = date(year=1997, month=4, day=12) value = mapper.get(d) self.assertEqual( dict(year=d.year, month=d.month, day=d.day), dict(value), )
def test__object_mapper__init(): mapper = ObjectMapper( init=Person, args=CompoundListProperty( IntegerProperty('id'), StringProperty('first_name'), StringProperty('last_name'), ), kwargs=CompoundProperty( middle_name=StringProperty(required=False), ), ) first, middle, last = 'Iosif Aleksandrovich Brodsky'.split() person = mapper.get( dict( id=1940, first_name=first, middle_name=middle, last_name=last, )) assert isinstance(person, Person) assert person.id == 1940 assert person.first_name == first assert person.middle_name == middle assert person.last_name == last
class PersonMapper(ObjectMapper): init = Person args = CompoundListProperty( IntegerProperty('id'), StringProperty('first_name'), StringProperty('last_name'), ) kwargs = CompoundProperty( middle_name=StringProperty(required=False), )
class PersonMapper(ObjectMapper): init = Person args = CompoundListProperty( IntegerProperty('id'), StringProperty('first_name'), StringProperty('last_name'), StringProperty('middle_name', required=False), skip_none=True, )
def test__init__args(self): person_mapper = ObjectMapper( init=Person, args=CompoundListProperty( IntegerProperty('id'), StringProperty('first_name'), StringProperty('last_name'), StringProperty('middle_name', required=False), skip_none=True, ), ) self.assertEqual(self.dostoevsky, person_mapper.get(self.dostoevsky_dict)) self.assertEqual( self.dostoevsky_no_middle_name, person_mapper.get(self.dostoevsky_no_middle_name_dict))
def test__get_value__from_object(self): first, middle, last = 'Alexander Sergeyevich Pushkin'.split() pushkin = Person(1799, first, last, middle) prop = IntegerProperty( 'id', get_value=getattr, get_value_exc=AttributeError) self.assertEqual(1799, prop.get(pushkin)) prop = StringProperty( 'first_name', get_value=getattr, get_value_exc=AttributeError) self.assertEqual(first, prop.get(pushkin)) prop = StringProperty( 'middle_name', get_value=getattr, get_value_exc=AttributeError) self.assertEqual(middle, prop.get(pushkin)) prop = StringProperty( 'last_name', get_value=getattr, get_value_exc=AttributeError) self.assertEqual(last, prop.get(pushkin)) prop = StringProperty( 'unknown', get_value=getattr, get_value_exc=AttributeError) with self.assertRaises(PropertyNotResolved): prop.get(pushkin)