File: test_json.py

package info (click to toggle)
python-aioshelly 13.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 732 kB
  • sloc: python: 6,867; makefile: 7; sh: 3
file content (71 lines) | stat: -rw-r--r-- 1,879 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
"""Tests for JSON helper."""

import pytest

from aioshelly.json import json_bytes, json_dumps


def test_json_bytes_with_dict() -> None:
    """Test json_bytes with a dict and non-str keys."""
    data = {"id": 1, "method": "Shelly.GetDeviceInfo", 3: "test"}
    result = json_bytes(data)

    assert isinstance(result, bytes)
    assert result == b'{"id":1,"method":"Shelly.GetDeviceInfo","3":"test"}'


def test_json_bytes_with_set() -> None:
    """Test json_bytes with a set."""
    data = {1, 2}
    result = json_bytes(data)

    assert isinstance(result, bytes)
    assert result in [b"[1,2]", b"[2,1]"]


def test_json_bytes_with_tuple() -> None:
    """Test json_bytes with a tuple."""
    data = (1, 2, 3)
    result = json_bytes(data)

    assert isinstance(result, bytes)
    assert result == b"[1,2,3]"


def test_json_bytes_with_non_serializable() -> None:
    """Test json_bytes with non-serializable object."""
    with pytest.raises(TypeError):
        json_bytes(object())


def test_json_dumps_with_dict() -> None:
    """Test json_dumps with a dict and non-str keys."""
    data = {"id": 1, "method": "Shelly.GetDeviceInfo", 3: "test"}
    result = json_dumps(data)

    assert isinstance(result, str)
    assert result == '{"id":1,"method":"Shelly.GetDeviceInfo","3":"test"}'


def test_json_dumps_with_set() -> None:
    """Test json_dumps with a set."""
    data = {1, 2}
    result = json_dumps(data)

    assert isinstance(result, str)
    assert result in ["[1,2]", "[2,1]"]


def test_json_dumps_with_tuple() -> None:
    """Test json_dumps with a tuple."""
    data = (1, 2, 3)
    result = json_dumps(data)

    assert isinstance(result, str)
    assert result == "[1,2,3]"


def test_json_dumps_with_non_serializable() -> None:
    """Test json_dumps with non-serializable object."""
    with pytest.raises(TypeError):
        json_dumps(object())