summaryrefslogtreecommitdiff
path: root/tests/test_helpers.py
blob: 359582909f77d35b6c86810675cc85a119658ba1 (plain)
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
import string

from redis.commands.helpers import (
    delist,
    list_or_args,
    nativestr,
    parse_to_dict,
    parse_to_list,
    quote_string,
    random_string,
)


def test_list_or_args():
    k = ["hello, world"]
    a = ["some", "argument", "list"]
    assert list_or_args(k, a) == k + a

    for i in ["banana", b"banana"]:
        assert list_or_args(i, a) == [i] + a


def test_parse_to_list():
    assert parse_to_list(None) == []
    r = ["hello", b"my name", "45", "555.55", "is simon!", None]
    assert parse_to_list(r) == ["hello", "my name", 45, 555.55, "is simon!", None]


def test_parse_to_dict():
    assert parse_to_dict(None) == {}
    r = [
        ["Some number", "1.0345"],
        ["Some string", "hello"],
        [
            "Child iterators",
            [
                "Time",
                "0.2089",
                "Counter",
                3,
                "Child iterators",
                ["Type", "bar", "Time", "0.0729", "Counter", 3],
                ["Type", "barbar", "Time", "0.058", "Counter", 3],
            ],
        ],
    ]
    assert parse_to_dict(r) == {
        "Child iterators": {
            "Child iterators": [
                {"Counter": 3.0, "Time": 0.0729, "Type": "bar"},
                {"Counter": 3.0, "Time": 0.058, "Type": "barbar"},
            ],
            "Counter": 3.0,
            "Time": 0.2089,
        },
        "Some number": 1.0345,
        "Some string": "hello",
    }


def test_nativestr():
    assert nativestr("teststr") == "teststr"
    assert nativestr(b"teststr") == "teststr"
    assert nativestr("null") is None


def test_delist():
    assert delist(None) is None
    assert delist([b"hello", "world", b"banana"]) == ["hello", "world", "banana"]


def test_random_string():
    assert len(random_string()) == 10
    assert len(random_string(15)) == 15
    for a in random_string():
        assert a in string.ascii_lowercase


def test_quote_string():
    assert quote_string("hello world!") == '"hello world!"'
    assert quote_string("") == '""'
    assert quote_string("hello world!") == '"hello world!"'