示例#1
0
def test_cyclic_array_nosharing():
    """Test that serializing a cyclic structure w/o value sharing will blow up gracefully."""
    a = []
    a.append(a)
    with pytest.raises(ValueError) as exc:
        dumps(a)
        exc.match('cyclic data structure detected but value sharing is disabled')
示例#2
0
def test_cyclic_map_nosharing():
    """Test that serializing a cyclic structure w/o value sharing will fail gracefully."""
    a = {}
    a[0] = a
    with pytest.raises(ValueError) as exc:
        dumps(a)
        exc.match('cyclic data structure detected but value sharing is disabled')
示例#3
0
def test_mime():
    expected = unhexlify(
        'd824787b436f6e74656e742d547970653a20746578742f706c61696e3b20636861727365743d2269736f2d38'
        '3835392d3135220a4d494d452d56657273696f6e3a20312e300a436f6e74656e742d5472616e736665722d456'
        'e636f64696e673a2071756f7465642d7072696e7461626c650a0a48656c6c6f203d413475726f')
    message = MIMEText(u'Hello \u20acuro', 'plain', 'iso-8859-15')
    assert dumps(message) == expected
示例#4
0
def test_canonical_set(frozen):
    value = {u'y', u'x', u'aa', u'a'}
    if frozen:
        value = frozenset(value)

    serialized = dumps(value, canonical=True)
    assert serialized == unhexlify('d9010284616161786179626161')
示例#5
0
def test_set(frozen):
    value = {u'a', u'b', u'c'}
    if frozen:
        value = frozenset(value)

    serialized = dumps(value)
    assert len(serialized) == 10
    assert serialized.startswith(unhexlify('d9010283'))
示例#6
0
def test_default():
    class DummyType(object):
        def __init__(self, state):
            self.state = state

    def default_encoder(encoder, value):
        encoder.encode(value.state)

    expected = unhexlify('820305')
    obj = DummyType([3, 5])
    serialized = dumps(obj, default=default_encoder)
    assert serialized == expected
示例#7
0
def test_default_cyclic():
    class DummyType(object):
        def __init__(self, value=None):
            self.value = value

    @shareable_encoder
    def default_encoder(encoder, value):
        state = encoder.encode_to_bytes(value.value)
        encoder.encode(CBORTag(3000, state))

    expected = unhexlify('D81CD90BB849D81CD90BB843D81D00')
    obj = DummyType()
    obj2 = DummyType(obj)
    obj.value = obj2
    serialized = dumps(obj, value_sharing=True, default=default_encoder)
    assert serialized == expected
示例#8
0
def test_minimal_floats(value, expected):
    expected = unhexlify(expected)
    assert dumps(value, canonical=True) == expected
示例#9
0
def test_ordered_map(value, expected):
    expected = unhexlify(expected)
    assert dumps(value, canonical=True) == expected
示例#10
0
def test_unsupported_type():
    with pytest.raises(ValueError) as exc:
        dumps(lambda: None)
        exc.match('cannot serialize type function')
示例#11
0
def test_string(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected
示例#12
0
def test_datetime(value, as_timestamp, expected):
    expected = unhexlify(expected)
    assert dumps(value, datetime_as_timestamp=as_timestamp, timezone=timezone.utc) == expected
示例#13
0
def test_tuple_key():
    assert dumps({(2, 1): u''}) == unhexlify('a182020160')
示例#14
0
def test_naive_datetime():
    """Test that naive datetimes are gracefully rejected when no timezone has been set."""
    with pytest.raises(ValueError) as exc:
        dumps(datetime(2013, 3, 21))
        exc.match('naive datetime encountered and no default timezone has been set')
示例#15
0
def test_cyclic_array():
    """Test that an array that contains itself can be serialized with value sharing enabled."""
    expected = unhexlify('d81c81d81c81d81d00')
    a = [[]]
    a[0].append(a)
    assert dumps(a, value_sharing=True) == expected
示例#16
0
def test_custom_tag():
    expected = unhexlify('d917706548656c6c6f')
    assert dumps(CBORTag(6000, u'Hello')) == expected
示例#17
0
def test_uuid():
    expected = unhexlify('d825505eaffac8b51e480581277fdcc7842faf')
    assert dumps(UUID(hex='5eaffac8b51e480581277fdcc7842faf')) == expected
示例#18
0
def test_rational():
    expected = unhexlify('d81e820205')
    assert dumps(Fraction(2, 5)) == expected
示例#19
0
def test_regex():
    expected = unhexlify('d8236d68656c6c6f2028776f726c6429')
    assert dumps(re.compile(u'hello (world)')) == expected
示例#20
0
def test_float(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected
示例#21
0
def test_date():
    expected = unhexlify('c074323031332d30332d32315430303a30303a30305a')
    assert dumps(date(2013, 3, 21), timezone=timezone.utc) == expected
示例#22
0
def test_integer(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected
示例#23
0
def test_cyclic_map():
    """Test that a dict that contains itself can be serialized with value sharing enabled."""
    expected = unhexlify('d81ca100d81d00')
    a = {}
    a[0] = a
    assert dumps(a, value_sharing=True) == expected
示例#24
0
def test_bytearray(value, expected):
    expected = unhexlify(expected)
    assert dumps(bytearray(value)) == expected
示例#25
0
def test_not_cyclic_same_object(value_sharing, expected):
    """Test that the same shareable object can be included twice if not in a cyclic structure."""
    expected = unhexlify(expected)
    a = []
    b = [a, a]
    assert dumps(b, value_sharing=value_sharing) == expected
示例#26
0
def test_special(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected
示例#27
0
def test_simple_value(value, expected):
    expected = unhexlify(expected)
    assert dumps(value) == expected