def test_date_and_datetime_fields(): assert Field(name='datetime', mapping=Mappings.datetime()).dump( dt.datetime(2018, 12, 31, 16, 55, 33)) == '2018-12-31 16:55:33' assert Field(name='date', mapping=Mappings.date()).dump(dt.datetime(2018, 12, 31)) == '2018-12-31' assert Field(name='date', mapping=Mappings.date()).dump(None) is None assert Field(name='date', mapping=Mappings.datetime()).dump(None) is 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_reverse(): person = Schema( Field('name'), Field('date_of_birth', source_name='dob', mapping=Mappings.date()), Field('weight', mapping=int), ) serialized = {'name': 'A', 'dob': '2000-01-01', 'weight': '60'} deserialized = { 'name': 'A', 'date_of_birth': dt.datetime(2000, 1, 1), 'weight': 60 } assert person.load(serialized) == person(serialized) == deserialized serializer = person.reverse() assert serializer.f.name assert serializer.f.dob assert serializer.f.weight with pytest.raises(AttributeError): assert not serializer.f.date_of_birth assert serializer(deserialized) == { 'name': 'A', 'dob': '2000-01-01', 'weight': 60 } assert serializer.f.name.dump('A') == 'A' assert serializer.f.dob.dump('2000-01-01') == dt.datetime(2000, 1, 1) assert serializer.f.weight.dump('60') == 60 assert serializer.dump(serialized) == deserialized
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_custom_date_and_datetime_formats(): date_field = Field(name='date', mapping=Mappings.date('%d.%m.%Y.')) assert date_field.load('01.03.2018.') == dt.datetime(2018, 3, 1) datetime_field = Field(name='datetime', mapping=Mappings.datetime('%d.%m.%Y. %H:%M')) assert datetime_field.load('01.03.2018. 12:30') == dt.datetime(2018, 3, 1, 12, 30) with pytest.raises(Field.Invalid): date_field.load('01/03/2018') with pytest.raises(Field.Invalid): datetime_field.load('01/03/2018 12:30') assert date_field.load(dt.datetime(2018, 1, 1, 12, 55)) == dt.datetime(2018, 1, 1) assert date_field.load(dt.date(2018, 1, 1)) == dt.datetime(2018, 1, 1) assert datetime_field.load(dt.datetime(2018, 1, 1, 12, 55)) == dt.datetime(2018, 1, 1, 12, 55) assert datetime_field.load(dt.date(2018, 1, 1)) == dt.datetime(2018, 1, 1) date_field2 = Field(name='date', mapping=Mappings.date('%d/%m/%Y', '%d.%m.%Y.')) assert date_field2.load('01.03.2018.') == dt.datetime(2018, 3, 1) assert date_field2.load('01/03/2018') == dt.datetime(2018, 3, 1)
def test_instance_factory_on_the_fly(): class Person: def __init__(self, name=None, date_of_birth=None): self.name = name self.date_of_birth = date_of_birth person_schema = Schema(Field('name'), Field('date_of_birth', mapping=Mappings.date()), instance_factory=Person) person = person_schema.load({'date_of_birth': '1995-10-11'}) assert isinstance(person, Person) assert person.name is None assert person.date_of_birth == dt.datetime(1995, 10, 11)
class UserSchema(Schema, FlaskRequestSchemaMixin): fields = ( Field('username', required=True), Field('password', source_name='Password'), Field('dob', mapping=Mappings.date(), default=None) )