def test_nummultby(client): client.json().set("num", Path.root_path(), 1) with pytest.deprecated_call(): assert 2 == client.json().nummultby("num", Path.root_path(), 2) assert 5 == client.json().nummultby("num", Path.root_path(), 2.5) assert 2.5 == client.json().nummultby("num", Path.root_path(), 0.5)
async def test_numincrby(modclient): await modclient.json().set("num", Path.root_path(), 1) assert 2 == await modclient.json().numincrby("num", Path.root_path(), 1) assert 2.5 == await modclient.json().numincrby("num", Path.root_path(), 0.5) assert 1.25 == await modclient.json().numincrby("num", Path.root_path(), -1.25)
async def test_objlen(modclient: redis.Redis): obj = {"foo": "bar", "baz": "qaz"} await modclient.json().set("obj", Path.root_path(), obj) assert len(obj) == await modclient.json().objlen("obj", Path.root_path()) await modclient.json().set("obj", Path.root_path(), obj) assert len(obj) == await modclient.json().objlen("obj")
def test_objlen(client): obj = {"foo": "bar", "baz": "qaz"} client.json().set("obj", Path.root_path(), obj) assert len(obj) == client.json().objlen("obj", Path.root_path()) client.json().set("obj", Path.root_path(), obj) assert len(obj) == client.json().objlen("obj")
async def test_toggle(modclient: redis.Redis): await modclient.json().set("bool", Path.root_path(), False) assert await modclient.json().toggle("bool", Path.root_path()) assert await modclient.json().toggle("bool", Path.root_path()) is False # check non-boolean value await modclient.json().set("num", Path.root_path(), 1) with pytest.raises(exceptions.ResponseError): await modclient.json().toggle("num", Path.root_path())
def test_toggle(client): client.json().set("bool", Path.root_path(), False) assert client.json().toggle("bool", Path.root_path()) assert client.json().toggle("bool", Path.root_path()) is False # check non-boolean value client.json().set("num", Path.root_path(), 1) with pytest.raises(redis.exceptions.ResponseError): client.json().toggle("num", Path.root_path())
async def test_json_setbinarykey(modclient: redis.Redis): d = {"hello": "world", b"some": "value"} with pytest.raises(TypeError): modclient.json().set("somekey", Path.root_path(), d) assert await modclient.json().set("somekey", Path.root_path(), d, decode_keys=True)
def test_arrinsert(client): client.json().set("arr", Path.root_path(), [0, 4]) assert 5 - -client.json().arrinsert("arr", Path.root_path(), 1, *[1, 2, 3]) assert [0, 1, 2, 3, 4] == client.json().get("arr") # test prepends client.json().set("val2", Path.root_path(), [5, 6, 7, 8, 9]) client.json().arrinsert("val2", Path.root_path(), 0, ["some", "thing"]) assert client.json().get("val2") == [["some", "thing"], 5, 6, 7, 8, 9]
async def test_nummultby(modclient: redis.Redis): await modclient.json().set("num", Path.root_path(), 1) with pytest.deprecated_call(): assert 2 == await modclient.json().nummultby("num", Path.root_path(), 2) assert 5 == await modclient.json().nummultby("num", Path.root_path(), 2.5) assert 2.5 == await modclient.json().nummultby("num", Path.root_path(), 0.5)
async def test_arrinsert(modclient: redis.Redis): await modclient.json().set("arr", Path.root_path(), [0, 4]) assert 5 - - await modclient.json().arrinsert("arr", Path.root_path(), 1, * [1, 2, 3]) assert [0, 1, 2, 3, 4] == await modclient.json().get("arr") # test prepends await modclient.json().set("val2", Path.root_path(), [5, 6, 7, 8, 9]) await modclient.json().arrinsert("val2", Path.root_path(), 0, ["some", "thing"]) assert await modclient.json().get("val2") == [["some", "thing"], 5, 6, 7, 8, 9]
def test_objkeys(client): obj = {"foo": "bar", "baz": "qaz"} client.json().set("obj", Path.root_path(), obj) keys = client.json().objkeys("obj", Path.root_path()) keys.sort() exp = list(obj.keys()) exp.sort() assert exp == keys client.json().set("obj", Path.root_path(), obj) keys = client.json().objkeys("obj") assert keys == list(obj.keys()) assert client.json().objkeys("fakekey") is None
async def test_objkeys(modclient: redis.Redis): obj = {"foo": "bar", "baz": "qaz"} await modclient.json().set("obj", Path.root_path(), obj) keys = await modclient.json().objkeys("obj", Path.root_path()) keys.sort() exp = list(obj.keys()) exp.sort() assert exp == keys await modclient.json().set("obj", Path.root_path(), obj) keys = await modclient.json().objkeys("obj") assert keys == list(obj.keys()) assert await modclient.json().objkeys("fakekey") is None
async def test_resp(modclient: redis.Redis): obj = {"foo": "bar", "baz": 1, "qaz": True} await modclient.json().set("obj", Path.root_path(), obj) assert "bar" == await modclient.json().resp("obj", Path("foo")) assert 1 == await modclient.json().resp("obj", Path("baz")) assert await modclient.json().resp("obj", Path("qaz")) assert isinstance(await modclient.json().resp("obj"), list)
async def test_json_setgetdeleteforget(modclient: redis.Redis): assert await modclient.json().set("foo", Path.root_path(), "bar") assert await modclient.json().get("foo") == "bar" assert await modclient.json().get("baz") is None assert await modclient.json().delete("foo") == 1 assert await modclient.json().forget("foo") == 0 # second delete assert await modclient.exists("foo") == 0
def test_json_setgetdeleteforget(client): assert client.json().set("foo", Path.root_path(), "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
def test_set_file(client): import json import tempfile obj = {"hello": "world"} jsonfile = tempfile.NamedTemporaryFile(suffix=".json") with open(jsonfile.name, "w+") as fp: fp.write(json.dumps(obj)) nojsonfile = tempfile.NamedTemporaryFile() nojsonfile.write(b"Hello World") assert client.json().set_file("test", Path.root_path(), jsonfile.name) assert client.json().get("test") == obj with pytest.raises(json.JSONDecodeError): client.json().set_file("test2", Path.root_path(), nojsonfile.name)
async def test_nonascii_setgetdelete(modclient: redis.Redis): assert await modclient.json().set("notascii", Path.root_path(), "hyvää-élève") assert "hyvää-élève" == await modclient.json().get("notascii", no_escape=True) assert 1 == await modclient.json().delete("notascii") assert await modclient.exists("notascii") == 0
def test_resp(client): obj = {"foo": "bar", "baz": 1, "qaz": True} client.json().set("obj", Path.root_path(), obj) assert "bar" == client.json().resp("obj", Path("foo")) assert 1 == client.json().resp("obj", Path("baz")) assert client.json().resp("obj", Path("qaz")) assert isinstance(client.json().resp("obj"), list)
async def test_arrtrim(modclient: redis.Redis): await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 3 == await modclient.json().arrtrim("arr", Path.root_path(), 1, 3) assert [1, 2, 3] == await modclient.json().get("arr") # <0 test, should be 0 equivalent await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 0 == await modclient.json().arrtrim("arr", Path.root_path(), -1, 3) # testing stop > end await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 2 == await modclient.json().arrtrim("arr", Path.root_path(), 3, 99) # start > array size and stop await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 0 == await modclient.json().arrtrim("arr", Path.root_path(), 9, 1) # all larger await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 0 == await modclient.json().arrtrim("arr", Path.root_path(), 9, 11)
def test_arrtrim(client): client.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 3 == client.json().arrtrim("arr", Path.root_path(), 1, 3) assert [1, 2, 3] == client.json().get("arr") # <0 test, should be 0 equivalent client.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 0 == client.json().arrtrim("arr", Path.root_path(), -1, 3) # testing stop > end client.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 2 == client.json().arrtrim("arr", Path.root_path(), 3, 99) # start > array size and stop client.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 0 == client.json().arrtrim("arr", Path.root_path(), 9, 1) # all larger client.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 0 == client.json().arrtrim("arr", Path.root_path(), 9, 11)
def test_json_commands_in_pipeline(client): p = client.json().pipeline() p.set("foo", Path.root_path(), "bar") p.get("foo") p.delete("foo") assert [True, "bar", 1] == p.execute() assert client.keys() == [] assert client.get("foo") is None # now with a true, json object client.flushdb() p = client.json().pipeline() d = {"hello": "world", "oh": "snap"} with pytest.deprecated_call(): p.jsonset("foo", Path.root_path(), d) p.jsonget("foo") p.exists("notarealkey") p.delete("foo") assert [True, d, 0, 1] == p.execute() assert client.keys() == [] assert client.get("foo") is None
def test_custom_decoder(client): import json import ujson cj = client.json(encoder=ujson, decoder=ujson) assert cj.set("foo", Path.root_path(), "bar") assert "bar" == cj.get("foo") assert cj.get("baz") is None assert 1 == cj.delete("foo") assert client.exists("foo") == 0 assert not isinstance(cj.__encoder__, json.JSONEncoder) assert not isinstance(cj.__decoder__, json.JSONDecoder)
def test_arrpop(client): client.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 4 == client.json().arrpop("arr", Path.root_path(), 4) assert 3 == client.json().arrpop("arr", Path.root_path(), -1) assert 2 == client.json().arrpop("arr", Path.root_path()) assert 0 == client.json().arrpop("arr", Path.root_path(), 0) assert [1] == client.json().get("arr") # test out of bounds client.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 4 == client.json().arrpop("arr", Path.root_path(), 99) # none test client.json().set("arr", Path.root_path(), []) assert client.json().arrpop("arr") is None
async def test_arrpop(modclient: redis.Redis): await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 4 == await modclient.json().arrpop("arr", Path.root_path(), 4) assert 3 == await modclient.json().arrpop("arr", Path.root_path(), -1) assert 2 == await modclient.json().arrpop("arr", Path.root_path()) assert 0 == await modclient.json().arrpop("arr", Path.root_path(), 0) assert [1] == await modclient.json().get("arr") # test out of bounds await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 4 == await modclient.json().arrpop("arr", Path.root_path(), 99) # none test await modclient.json().set("arr", Path.root_path(), []) assert await modclient.json().arrpop("arr") is None
def test_jsonsetexistentialmodifiersshouldsucceed(client): obj = {"foo": "bar"} assert client.json().set("obj", Path.root_path(), 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)
def test_set_path(client): import json import tempfile root = tempfile.mkdtemp() sub = tempfile.mkdtemp(dir=root) jsonfile = tempfile.mktemp(suffix=".json", dir=sub) nojsonfile = tempfile.mktemp(dir=root) with open(jsonfile, "w+") as fp: fp.write(json.dumps({"hello": "world"})) open(nojsonfile, "a+").write("hello") result = {jsonfile: True, nojsonfile: False} assert client.json().set_path(Path.root_path(), root) == result assert client.json().get(jsonfile.rsplit(".")[0]) == {"hello": "world"}
async def test_type(modclient: redis.Redis): await modclient.json().set("1", Path.root_path(), 1) assert "integer" == await modclient.json().type("1", Path.root_path()) assert "integer" == await modclient.json().type("1")
async def test_clear(modclient: redis.Redis): await modclient.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4]) assert 1 == await modclient.json().clear("arr", Path.root_path()) assert [] == await modclient.json().get("arr")
async def test_mgetshouldsucceed(modclient: redis.Redis): await modclient.json().set("1", Path.root_path(), 1) await modclient.json().set("2", Path.root_path(), 2) assert await modclient.json().mget(["1"], Path.root_path()) == [1] assert await modclient.json().mget([1, 2], Path.root_path()) == [1, 2]
async def test_json_get_jset(modclient: redis.Redis): assert await modclient.json().set("foo", Path.root_path(), "bar") assert "bar" == await modclient.json().get("foo") assert await modclient.json().get("baz") is None assert 1 == await modclient.json().delete("foo") assert await modclient.exists("foo") == 0