Esempio n. 1
0
 class MyRecord(Record):
     v = dict_of(
         int,
         int,
         coerce=lambda v: {int(i): int(i)
                           for i in v},
     )
Esempio n. 2
0
 class MyRecord(Record):
     v = dict_of(
         int,
         int,
         nullable=True,
         default={
             1: 1,
             2: 2,
             3: 3
         },
     )
Esempio n. 3
0
 class MyRecord(Record):
     v = dict_of(text_type, text_type)
Esempio n. 4
0
    (class_name, cls, value, nullable_or_not)
    for class_name, cls, non_null_val in (
        ('bytes', bytes_type, b'\xE2\x9C\x93\'\\\"\xE2\x9C\x93'),
        ('text', text_type, 'Herv\u00E9\'\\\"Herv\u00E9'),
        ('ascii-only text', text_type, 'ASCII'),
    ) + tuple(
        (t.__name__, t, t(42))
        for t in integer_types
    ) + (
        ('float', float, 0.3),
        ('bool', bool, True),
        ('sequence (nonempty)', seq_of(int), (1, 2, 3)),
        ('sequence (empty)', seq_of(int), []),
        ('set (nonempty)', set_of(int), (1, 2, 3)),
        ('set (empty)', set_of(int), []),
        ('dict (nonempty)', dict_of(text_type, int), {'one':1, 'two':2}),
        ('dict (empty)', dict_of(text_type, int), []),
        ('datetime', datetime, datetime(2016, 4, 15, 10, 1, 59)),
        ('timedelta', timedelta, timedelta(days=1, hours=2, minutes=3, seconds=4)),
        (lambda R2: ('other record', R2, R2(2)))(_other_record()),
    )
    for nullable_or_not, vals in (
        (lambda f: f, (non_null_val,)),
        (nullable, (non_null_val, None)),
    )
    for value in vals
)
def _(class_name, cls, value, nullable_or_not):

    @test('Record with {}{} field (set to {!r}) -> PODS -> Record'.format(
        'nullable ' if nullable_or_not is nullable else '',
Esempio n. 5
0
 class MyRecord(Record):
     v = dict_of(MyClass1, MyClass2)
Esempio n. 6
0
 class MyRecord2(Record):
     v = dict_of(int, int, nullable=False)
Esempio n. 7
0
 class MyRecord1(Record):
     v = dict_of(int, int, nullable=True)
Esempio n. 8
0
    class MyRecord(Record):
        v = one_of('a', 'b', 'c', coerce=text_type.lower)

    assert_eq(MyRecord('C').v, 'c')


#----------------------------------------------------------------------------------------------------------------------------------
# nonempty


@foreach((
    (bytes_type, 'byte strings', b''),
    (text_type, 'text strings', ''),
    (seq_of(int), 'seqeuence fields', ()),
    (set_of(int), 'set fields', ()),
    (dict_of(int, int), 'dict fields', {}),
))
def _(ftype, ftype_name, empty_val):
    @test('in general {} fields can be empty'.format(ftype_name))
    def _():
        class MyRecord(Record):
            v = ftype

        assert_eq(len(MyRecord(empty_val).v), 0)

    @test("nonempty {} can't be empty".format(ftype_name))
    def _():
        class MyRecord(Record):
            v = nonempty(ftype)

        with assert_raises(FieldValueError):
Esempio n. 9
0
 class MyRecord(Record):
     v = dict_of(Field(int), Field(int))
Esempio n. 10
0
 class MyRecord(Record):
     elems = dict_of(
         text_type,
         pair_of(int),
     )
Esempio n. 11
0
 class MyRecord(Record):
     v = nullable(dict_of(int, int))
Esempio n. 12
0
 class MyRecord(Record):
     elems = dict_of(int, MyClass1)
Esempio n. 13
0
 class Record2(Record):
     elems = dict_of(int, MyClass2)
Esempio n. 14
0
 class Record1(Record):
     elems = dict_of(int, MyClass1)
Esempio n. 15
0
 class MyRecord(Record):
     children = dict_of(text_type, RecursiveType)
Esempio n. 16
0
 class MyRecord(Record):
     v = dict_of(
         int,
         int,
         check=lambda s: len(s) == 3,
     )
Esempio n. 17
0
 class MyRecord(Record):
     elems = dict_of(text_type, text_type)
Esempio n. 18
0
 class MyRecord(Record):
     mydict = dict_of(int, int)