Ejemplo n.º 1
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
Ejemplo n.º 2
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',
    }
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)