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_clone_accepts_reverse(): f = Field(name='x', mapping=Mapping(int, str)) assert f('5') == 5 assert f.dump('5') == '5' g = f.clone(reverse=True) assert g(5) == '5' assert g.dump('5') == 5
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_primitive_type_mappings_are_none_aware(primitive_type): mapping = getattr(Mappings, primitive_type.__name__) assert mapping.load(None) is None assert mapping.dump(None) is None f = Field(name='f', mapping=mapping) assert f.load(None) is None assert f.dump(None) is None g = Field(name='g', mapping=mapping, nullable=False) with pytest.raises(Field.Invalid): g.load(None)
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]