Exemplo n.º 1
0
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)
Exemplo n.º 2
0
def test_field_min_max():
    f = Field(name='name', min=1, max=10, mapping=int)

    with pytest.raises(Field.Invalid):
        f.load('-1')

    with pytest.raises(Field.Invalid):
        f.load('11')

    assert f.load(10) == 10
    assert f.load('10') == 10
    assert f.load(1) == 1
    assert f.load('1') == 1
Exemplo n.º 3
0
def test_field_regex():
    f = Field(name='name', regex=r'^[a-z]+$', nullable=True)

    assert f.load('abc') == 'abc'
    assert f.load(None) is None

    with pytest.raises(Field.Invalid):
        f.load('Abc')

    with pytest.raises(Field.Invalid):
        f.load(0)
Exemplo n.º 4
0
def test_field_min_len_max_len():
    f1 = Field(name='name', min_len=3, max_len=8)

    assert f1.load('123') == '123'
    assert f1.load('12345678') == '12345678'

    with pytest.raises(Field.Invalid):
        f1.load('12')

    with pytest.raises(Field.Invalid):
        f1.load('123456789')

    # auto_trim=True
    assert Field(name='name', min_len=3, max_len=8, auto_trim=True).load('123456789') == '12345678'
Exemplo n.º 5
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)