summaryrefslogtreecommitdiff
path: root/tests/test_json.py
blob: 96675f1e451b810126177384dbc654a057733075 (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import pytest
import redis
from redis.commands.json.path import Path
from .conftest import skip_ifmodversion_lt


@pytest.fixture
def client(modclient):
    modclient.flushdb()
    return modclient


@pytest.mark.json
def test_json_setbinarykey(client):
    d = {"hello": "world", b"some": "value"}
    with pytest.raises(TypeError):
        client.json().set("somekey", Path.rootPath(), d)
    assert client.json().set("somekey", Path.rootPath(), d, decode_keys=True)


@pytest.mark.json
def test_json_setgetdeleteforget(client):
    assert client.json().set("foo", Path.rootPath(), "bar")
    assert client.json().get("foo") == "bar"
    assert client.json().get("baz") is None
    assert client.json().delete("foo") == 1
    assert client.json().forget("foo") == 0  # second delete
    assert client.exists("foo") == 0


@pytest.mark.json
def test_justaget(client):
    client.json().set("foo", Path.rootPath(), "bar")
    assert client.json().get("foo") == "bar"


@pytest.mark.json
def test_json_get_jset(client):
    assert client.json().set("foo", Path.rootPath(), "bar")
    assert "bar" == client.json().get("foo")
    assert client.json().get("baz") is None
    assert 1 == client.json().delete("foo")
    assert client.exists("foo") == 0


@pytest.mark.json
def test_nonascii_setgetdelete(client):
    assert client.json().set("notascii", Path.rootPath(),
                             "hyvää-élève") is True
    assert "hyvää-élève" == client.json().get("notascii", no_escape=True)
    assert 1 == client.json().delete("notascii")
    assert client.exists("notascii") == 0


@pytest.mark.json
def test_jsonsetexistentialmodifiersshouldsucceed(client):
    obj = {"foo": "bar"}
    assert client.json().set("obj", Path.rootPath(), obj)

    # Test that flags prevent updates when conditions are unmet
    assert client.json().set("obj", Path("foo"), "baz", nx=True) is None
    assert client.json().set("obj", Path("qaz"), "baz", xx=True) is None

    # Test that flags allow updates when conditions are met
    assert client.json().set("obj", Path("foo"), "baz", xx=True)
    assert client.json().set("obj", Path("qaz"), "baz", nx=True)

    # Test that flags are mutually exlusive
    with pytest.raises(Exception):
        client.json().set("obj", Path("foo"), "baz", nx=True, xx=True)


@pytest.mark.json
def test_mgetshouldsucceed(client):
    client.json().set("1", Path.rootPath(), 1)
    client.json().set("2", Path.rootPath(), 2)
    r = client.json().mget(Path.rootPath(), "1", "2")
    e = [1, 2]
    assert e == r


@pytest.mark.json
@skip_ifmodversion_lt("99.99.99", "ReJSON")  # todo: update after the release
def test_clearShouldSucceed(client):
    client.json().set("arr", Path.rootPath(), [0, 1, 2, 3, 4])
    assert 1 == client.json().clear("arr", Path.rootPath())
    assert [] == client.json().get("arr")


@pytest.mark.json
def test_typeshouldsucceed(client):
    client.json().set("1", Path.rootPath(), 1)
    assert b"integer" == client.json().type("1")


@pytest.mark.json
def test_numincrbyshouldsucceed(client):
    client.json().set("num", Path.rootPath(), 1)
    assert 2 == client.json().numincrby("num", Path.rootPath(), 1)
    assert 2.5 == client.json().numincrby("num", Path.rootPath(), 0.5)
    assert 1.25 == client.json().numincrby("num", Path.rootPath(), -1.25)


@pytest.mark.json
def test_nummultbyshouldsucceed(client):
    client.json().set("num", Path.rootPath(), 1)
    assert 2 == client.json().nummultby("num", Path.rootPath(), 2)
    assert 5 == client.json().nummultby("num", Path.rootPath(), 2.5)
    assert 2.5 == client.json().nummultby("num", Path.rootPath(), 0.5)


@pytest.mark.json
@skip_ifmodversion_lt("99.99.99", "ReJSON")  # todo: update after the release
def test_toggleShouldSucceed(client):
    client.json().set("bool", Path.rootPath(), False)
    assert client.json().toggle("bool", Path.rootPath())
    assert not client.json().toggle("bool", Path.rootPath())
    # check non-boolean value
    client.json().set("num", Path.rootPath(), 1)
    with pytest.raises(redis.exceptions.ResponseError):
        client.json().toggle("num", Path.rootPath())


@pytest.mark.json
def test_strappendshouldsucceed(client):
    client.json().set("str", Path.rootPath(), "foo")
    assert 6 == client.json().strappend("str", "bar", Path.rootPath())
    assert "foobar" == client.json().get("str", Path.rootPath())


@pytest.mark.json
def test_debug(client):
    client.json().set("str", Path.rootPath(), "foo")
    assert 24 == client.json().debug("str", Path.rootPath())


@pytest.mark.json
def test_strlenshouldsucceed(client):
    client.json().set("str", Path.rootPath(), "foo")
    assert 3 == client.json().strlen("str", Path.rootPath())
    client.json().strappend("str", "bar", Path.rootPath())
    assert 6 == client.json().strlen("str", Path.rootPath())


@pytest.mark.json
def test_arrappendshouldsucceed(client):
    client.json().set("arr", Path.rootPath(), [1])
    assert 2 == client.json().arrappend("arr", Path.rootPath(), 2)
    assert 4 == client.json().arrappend("arr", Path.rootPath(), 3, 4)
    assert 7 == client.json().arrappend("arr", Path.rootPath(), *[5, 6, 7])


@pytest.mark.json
def testArrIndexShouldSucceed(client):
    client.json().set("arr", Path.rootPath(), [0, 1, 2, 3, 4])
    assert 1 == client.json().arrindex("arr", Path.rootPath(), 1)
    assert -1 == client.json().arrindex("arr", Path.rootPath(), 1, 2)


@pytest.mark.json
def test_arrinsertshouldsucceed(client):
    client.json().set("arr", Path.rootPath(), [0, 4])
    assert 5 - -client.json().arrinsert(
        "arr",
        Path.rootPath(),
        1,
        *[
            1,
            2,
            3,
        ]
    )
    assert [0, 1, 2, 3, 4] == client.json().get("arr")


@pytest.mark.json
def test_arrlenshouldsucceed(client):
    client.json().set("arr", Path.rootPath(), [0, 1, 2, 3, 4])
    assert 5 == client.json().arrlen("arr", Path.rootPath())


@pytest.mark.json
def test_arrpopshouldsucceed(client):
    client.json().set("arr", Path.rootPath(), [0, 1, 2, 3, 4])
    assert 4 == client.json().arrpop("arr", Path.rootPath(), 4)
    assert 3 == client.json().arrpop("arr", Path.rootPath(), -1)
    assert 2 == client.json().arrpop("arr", Path.rootPath())
    assert 0 == client.json().arrpop("arr", Path.rootPath(), 0)
    assert [1] == client.json().get("arr")


@pytest.mark.json
def test_arrtrimshouldsucceed(client):
    client.json().set("arr", Path.rootPath(), [0, 1, 2, 3, 4])
    assert 3 == client.json().arrtrim("arr", Path.rootPath(), 1, 3)
    assert [1, 2, 3] == client.json().get("arr")


@pytest.mark.json
def test_respshouldsucceed(client):
    obj = {"foo": "bar", "baz": 1, "qaz": True}
    client.json().set("obj", Path.rootPath(), obj)
    assert b"bar" == client.json().resp("obj", Path("foo"))
    assert 1 == client.json().resp("obj", Path("baz"))
    assert client.json().resp("obj", Path("qaz"))


@pytest.mark.json
def test_objkeysshouldsucceed(client):
    obj = {"foo": "bar", "baz": "qaz"}
    client.json().set("obj", Path.rootPath(), obj)
    keys = client.json().objkeys("obj", Path.rootPath())
    keys.sort()
    exp = list(obj.keys())
    exp.sort()
    assert exp == keys


@pytest.mark.json
def test_objlenshouldsucceed(client):
    obj = {"foo": "bar", "baz": "qaz"}
    client.json().set("obj", Path.rootPath(), obj)
    assert len(obj) == client.json().objlen("obj", Path.rootPath())


# @pytest.mark.pipeline
# @pytest.mark.json
# def test_pipelineshouldsucceed(client):
#     p = client.json().pipeline()
#     p.set("foo", Path.rootPath(), "bar")
#     p.get("foo")
#     p.delete("foo")
#     assert [True, "bar", 1] == p.execute()
#     assert client.keys() == []
#     assert client.get("foo") is None