File: t1_serialization.py

package info (click to toggle)
s3ql 5.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,416 kB
  • sloc: python: 19,027; cpp: 408; makefile: 147; sh: 133
file content (78 lines) | stat: -rwxr-xr-x 1,689 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
#!/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 sys

    import pytest

    sys.exit(pytest.main([__file__] + sys.argv[1:]))

from collections import OrderedDict

import pytest

from s3ql.backends.common import checksum_basic_mapping
from s3ql.common import ThawError, freeze_basic_mapping, thaw_basic_mapping


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)


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

    d1['foo'] = b'foo'
    d2['foo'] = b'f0o'

    assert checksum_basic_mapping(d1) != checksum_basic_mapping(d2)