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
def test__date_format(self): prop = Operation( StringProperty('year', default='2019'), StringProperty('month'), StringProperty('day'), func='.'.join, ) data = {'month': 8, 'day': 12} self.assertEqual('2019.8.12', prop.get(data))
class PersonMapper(ObjectMapper): init = Person args = CompoundListProperty( IntegerProperty('id'), StringProperty('first_name'), StringProperty('last_name'), StringProperty('middle_name', required=False), skip_none=True, )
class PersonMapper(ObjectMapper): init = Person args = CompoundListProperty( IntegerProperty('id'), StringProperty('first_name'), StringProperty('last_name'), ) kwargs = CompoundProperty( middle_name=StringProperty(required=False), )
def test(self): prop = CompoundProperty( first_name=StringProperty(['name']), last_name=StringProperty(['surname']), ) name, surname = 'Pauline Gvozd'.split() self.assertEqual( dict(first_name=name, last_name=surname), prop.get(data=dict(name=name, surname=surname)), )
def test__properties_no_sources(self): prop = CompoundProperty( first_name=StringProperty(), last_name=StringProperty(), ) first_name, last_name = 'Pauline Gvozd'.split() self.assertEqual( dict(first_name=first_name, last_name=last_name), prop.get(data=dict(first_name=first_name, last_name=last_name)), )
def test__count_not_none(self): def count_not_none(args): return sum(1 for a in args if a is not None) prop = Operation(func=count_not_none) self.assertEqual(0, prop.get({})) prop = Operation(prop, func=count_not_none) self.assertEqual(1, prop.get({})) name = StringProperty('name', 'first_name', default=None) surname = StringProperty('surname', 'family_name', default=None) prop = Operation(name, surname, func=count_not_none) self.assertEqual(0, prop.get({}))
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)
class PersonId(Mapper): first_name = StringProperty('first_name', 'name') last_name = StringProperty('last_name', 'surname', 'family_name')
class MyMapper(Mapper): name = StringProperty()
def test__numbers(self): prop = StringProperty(['n']) self.assertEqual(prop.get(dict(n=1)), '1')
def test__str(self): self.assertEqual('Property([])', str(Property([]))) self.assertEqual("Property('x')", str(Property('x'))) self.assertEqual("Property('x', 'y')", str(Property('x', 'y'))) self.assertEqual('StringProperty()', str(StringProperty()))