Exemple #1
0
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'
    ]
Exemple #2
0
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
Exemple #3
0
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]
Exemple #4
0
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']
Exemple #5
0
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
Exemple #6
0
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)
Exemple #7
0
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)
Exemple #8
0
def test_chained_mappings():
    create_user = Schema(
        Field('username'),
        Field('password'),
        Field('created_time', source_name='created', mapping=Mappings.datetime()),
    )

    save_user = Schema(
        create_user.f.username.map_as('name'),
        create_user.f.password.map_as('pass'),
        create_user.f.created_time.reverse(),
    )

    input_dict = {'username': '******', 'password': '******', 'created': '2010-01-01 12:00:00'}

    assert save_user(create_user(input_dict)) == {
        'name': 'marcus',
        'pass': '******',
        'created': '2010-01-01 12:00:00',
    }
Exemple #9
0
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},
        ]
    }
class UserSchema(Schema, FlaskRequestSchemaMixin):
    fields = (
        Field('username', required=True),
        Field('password', source_name='Password'),
        Field('dob', mapping=Mappings.date(), default=None)
    )
Exemple #11
0
def test_calling_field_means_invoking_field_loader():
    f = Field(mapping=Mappings.datetime())
    assert f('2017-12-31 23:55:59') == dt.datetime(2017, 12, 31, 23, 55, 59)
Exemple #12
0
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]}
Exemple #13
0
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
Exemple #14
0
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]