def test_list_of_dates_serialization(): list_of_dates = [ dt.datetime(2018, 1, 1), dt.datetime(2018, 2, 1), dt.datetime(2018, 3, 1) ] mapping = Field(name='dates', mapping=Mappings.list(Mappings.date())) assert mapping.dump(list_of_dates) == [ '2018-01-01', '2018-02-01', '2018-03-01' ]
def test_list_of_dates(): list_field = Field(name='dates', mapping=Mappings.list(Mappings.date())) assert list_field.dump( [dt.datetime(2018, 1, 1), dt.datetime(2018, 12, 31, 12, 55)]) == [ '2018-01-01', '2018-12-31', ] assert list_field.dump(None) is None assert list_field.dump([]) == [] assert list_field.dump([None]) == [None]
def test_reversal_of_mappings(): str_to_date = Mappings.date('%d.%m.%Y') assert str_to_date('31.12.2017') == dt.datetime(2017, 12, 31) date_to_str = str_to_date.reverse() assert date_to_str(dt.datetime(2017, 12, 31, 12, 55)) == '31.12.2017' list_of_dates = Mappings.list(Mappings.date('%d.%m.%Y')) assert list_of_dates(['01.01.2018', '02.01.2018']) == [dt.datetime(2018, 1, 1), dt.datetime(2018, 1, 2)] list_serializer = list_of_dates.reverse() assert list_serializer([dt.datetime(2018, 1, 1), dt.datetime(2018, 1, 2)]) == ['01.01.2018', '02.01.2018']
def test_list_of_dicts_field(): user = Schema( Field('username'), Field('password', default=None), ) list_of_users = Field(name='users', mapping=Mappings.list(user)) payload = { 'users': [ {'username': '******'}, {'username': '******', 'password': '******'}, {'username': '******', 'email': '*****@*****.**'}, ] } schema = Schema(list_of_users) assert schema.load(payload) == { 'users': [ {'username': '******', 'password': None}, {'username': '******', 'password': '******'}, {'username': '******', 'password': None}, ] }
def test_primitive_list_field(): numbers = Field(name='numbers', mapping=Mappings.list(int)) assert Schema(numbers).load({'numbers': ['1', 2, '3']}) == {'numbers': [1, 2, 3]}
def test_list_of_dicts(): user = Schema(Field(name='username'), Field(name='password', default=None)) list_of_users = Field(name='users', mapping=Mappings.list(user)) obj = [{'username': '******'}, {'username': '******', 'password': '******'}] assert list_of_users.dump(obj) == obj
def test_primitive_list(): list_field = Field(name='numbers', mapping=Mappings.list(int)) assert list_field.dump([1, 2, 3]) == [1, 2, 3] assert list_field.dump(None) is None assert list_field.dump([]) == [] assert list_field.dump([None]) == [None]