File: test_tool.py

package info (click to toggle)
cbor2 5.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 648 kB
  • sloc: ansic: 5,522; python: 3,884; makefile: 19; sh: 8
file content (138 lines) | stat: -rw-r--r-- 4,200 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import binascii
import json
from io import BytesIO, TextIOWrapper
from pathlib import Path

import pytest

import cbor2.tool


@pytest.mark.parametrize(
    "value, expected",
    [
        ((1, 2, 3), [1, 2, 3]),
        ({b"\x01\x02\x03": "b"}, {"\x01\x02\x03": "b"}),
        ({"dict": {"b": 17}}, {"dict": {"b": 17}}),
    ],
    ids=["tuple", "byte_key", "recursion"],
)
def test_key_to_str(value, expected):
    assert cbor2.tool.key_to_str(value) == expected


def test_default():
    with pytest.raises(TypeError):
        json.dumps(BytesIO(b""), cls=cbor2.tool.DefaultEncoder)


@pytest.mark.parametrize(
    "payload",
    ["D81CA16162D81CA16161D81D00", "d81c81d81c830102d81d00"],
    ids=["dict", "list"],
)
def test_self_referencing(payload):
    decoded = cbor2.loads(binascii.unhexlify(payload))
    with pytest.raises(ValueError, match="Cannot convert self-referential data to JSON"):
        cbor2.tool.key_to_str(decoded)


def test_nonrecursive_ref():
    payload = "d81c83d81ca26162d81ca16161016163d81d02d81d01d81d01"
    decoded = cbor2.loads(binascii.unhexlify(payload))
    result = cbor2.tool.key_to_str(decoded)
    expected = [
        {"b": {"a": 1}, "c": {"a": 1}},
        {"b": {"a": 1}, "c": {"a": 1}},
        {"b": {"a": 1}, "c": {"a": 1}},
    ]
    assert result == expected


def test_stdin(monkeypatch, tmpdir):
    f = tmpdir.join("outfile")
    argv = ["-o", str(f)]
    inbuf = TextIOWrapper(BytesIO(binascii.unhexlify("02")))
    with monkeypatch.context() as m:
        m.setattr("sys.argv", [""] + argv)
        m.setattr("sys.stdin", inbuf)
        cbor2.tool.main()
        assert f.read() == "2\n"


def test_stdout(monkeypatch, tmpdir):
    argv = ["-o", "-"]
    inbuf = TextIOWrapper(BytesIO(binascii.unhexlify("02")))
    outbuf = BytesIO()
    with monkeypatch.context() as m:
        m.setattr("sys.argv", [""] + argv)
        m.setattr("sys.stdin", inbuf)
        m.setattr("sys.stdout", outbuf)
        cbor2.tool.main()


def test_readfrom(monkeypatch, tmpdir):
    f = tmpdir.join("infile")
    outfile = tmpdir.join("outfile")
    f.write_binary(binascii.unhexlify("02"))
    argv = ["-o", str(outfile), str(f)]
    with monkeypatch.context() as m:
        m.setattr("sys.argv", [""] + argv)
        cbor2.tool.main()
        assert outfile.read() == "2\n"


def test_b64(monkeypatch, tmpdir):
    f = tmpdir.join("outfile")
    argv = ["-d", "-o", str(f)]
    inbuf = TextIOWrapper(BytesIO(b"oQID"))
    with monkeypatch.context() as m:
        m.setattr("sys.argv", [""] + argv)
        m.setattr("sys.stdin", inbuf)
        cbor2.tool.main()
        assert f.read() == '{"2": 3}\n'


def test_stream(monkeypatch, tmpdir):
    f = tmpdir.join("outfile")
    argv = ["--sequence", "-o", str(f)]
    inbuf = TextIOWrapper(BytesIO(binascii.unhexlify("0203")))
    with monkeypatch.context() as m:
        m.setattr("sys.argv", [""] + argv)
        m.setattr("sys.stdin", inbuf)
        cbor2.tool.main()
        assert f.read() == "2\n3\n"


def test_embed_bytes(monkeypatch, tmpdir):
    f = tmpdir.join("outfile")
    argv = ["-o", str(f)]
    inbuf = TextIOWrapper(BytesIO(binascii.unhexlify("42C2C2")))
    with monkeypatch.context() as m:
        m.setattr("sys.argv", [""] + argv)
        m.setattr("sys.stdin", inbuf)
        cbor2.tool.main()
        assert f.read() == '"\\\\xc2\\\\xc2"\n'


def test_dtypes_from_file(monkeypatch, tmpdir):
    infile = Path(__file__).with_name("examples.cbor.b64")
    expected = Path(__file__).with_name("examples.json").read_text()
    outfile = tmpdir.join("outfile.json")
    argv = ["--sort-keys", "--pretty", "-d", "-o", str(outfile), str(infile)]
    with monkeypatch.context() as m:
        m.setattr("sys.argv", [""] + argv)
        cbor2.tool.main()
        assert outfile.read() == expected


def test_ignore_tag(monkeypatch, tmpdir):
    f = tmpdir.join("outfile")
    argv = ["-o", str(f), "-i", "6000"]
    inbuf = TextIOWrapper(BytesIO(binascii.unhexlify("D917706548656C6C6F")))
    expected = '"Hello"\n'
    with monkeypatch.context() as m:
        m.setattr("sys.argv", [""] + argv)
        m.setattr("sys.stdin", inbuf)
        cbor2.tool.main()
        assert f.read() == expected