File: t1_serialization.py

package info (click to toggle)
s3ql 2.21%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,776 kB
  • ctags: 2,427
  • sloc: python: 14,869; makefile: 128; sh: 33; ansic: 22
file content (66 lines) | stat: -rwxr-xr-x 1,565 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
'''
t1_serialization.py - this file is part of S3QL.

Copyright © 2014 Nikolaus Rath <Nikolaus@rath.org>

This work can be distributed under the terms of the GNU GPLv3.
'''

if __name__ == '__main__':
    import pytest
    import sys
    sys.exit(pytest.main([__file__] + sys.argv[1:]))

from s3ql.common import ThawError, freeze_basic_mapping, thaw_basic_mapping
from s3ql.backends.common import checksum_basic_mapping
import pytest
from collections import OrderedDict


def test_simple():
    d = { 'hello': 42,
          'world': True,
          'data': b'fooxyz',
          'str': 'nice' }

    assert thaw_basic_mapping(freeze_basic_mapping(d)) == d

def test_wrong_key():
    d = { 'hello': 42,
          True: False }

    with pytest.raises(ValueError):
        freeze_basic_mapping(d)

def test_cmplx_value():
    d = { 'hello': [1,2] }

    with pytest.raises(ValueError):
        freeze_basic_mapping(d)

def test_thaw_errors():
    buf = freeze_basic_mapping({ 'hello': 'world' })

    for s in (buf[1:], buf[:-1],
              b'"foo"[2]', b'open("/dev/null", "r")',
              b'"foo".__class__'):
        with pytest.raises(ThawError):
            thaw_basic_mapping(s)

def test_checksum():
    d1 = OrderedDict()
    d2 = OrderedDict()

    d1['foo'] = 1
    d1['bar'] = None

    d2['bar'] = None
    d2['foo'] = 1

    assert list(d1.keys()) != list(d2.keys())
    assert checksum_basic_mapping(d1) == checksum_basic_mapping(d2)

    d2['foo'] += 1

    assert checksum_basic_mapping(d1) != checksum_basic_mapping(d2)