File: test_unpack_variants.py

package info (click to toggle)
dbus-fast 3.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,804 kB
  • sloc: python: 9,997; xml: 39; makefile: 29; sh: 5
file content (58 lines) | stat: -rw-r--r-- 1,679 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
"""Test unpack variants."""

import pytest

from dbus_fast.signature import Variant
from dbus_fast.unpack import unpack_variants


@pytest.mark.asyncio
async def test_dictionary():
    """Test variants unpacked from dictionary."""
    assert unpack_variants(
        {
            "string": Variant("s", "test"),
            "boolean": Variant("b", True),
            "int": Variant("u", 1),
            "object": Variant("o", "/test/path"),
            "array": Variant("as", ["test", "value"]),
            "tuple": Variant("(su)", ["test", 1]),
            "bytes": Variant("ay", b"\0x62\0x75\0x66"),
        }
    ) == {
        "string": "test",
        "boolean": True,
        "int": 1,
        "object": "/test/path",
        "array": ["test", "value"],
        "tuple": ["test", 1],
        "bytes": b"\0x62\0x75\0x66",
    }


@pytest.mark.asyncio
async def test_output_list():
    """Test variants unpacked from multiple outputs."""
    assert unpack_variants(
        [{"hello": Variant("s", "world")}, {"boolean": Variant("b", True)}, 1]
    ) == [{"hello": "world"}, {"boolean": True}, 1]


@pytest.mark.asyncio
async def test_nested_variants():
    """Test unpack variants handles nesting."""
    assert unpack_variants(
        {
            "dict": Variant("a{sv}", {"hello": Variant("s", "world")}),
            "array": Variant(
                "aa{sv}",
                [
                    {"hello": Variant("s", "world")},
                    {"bytes": Variant("ay", b"\0x62\0x75\0x66")},
                ],
            ),
        }
    ) == {
        "dict": {"hello": "world"},
        "array": [{"hello": "world"}, {"bytes": b"\0x62\0x75\0x66"}],
    }