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_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_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)
def test_debug(client): client.json().set("str", Path.rootPath(), "foo") assert 24 == client.json().debug("MEMORY", "str", Path.rootPath()) assert 24 == client.json().debug("MEMORY", "str") # technically help is valid assert isinstance(client.json().debug("HELP"), list)
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_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_create_json_with_alias(client): """ Create definition with IndexType.JSON as index type (ON JSON) with two fields with aliases, and use json client to test it. """ definition = IndexDefinition(prefix=["king:"], index_type=IndexType.JSON) client.ft().create_index( (TextField("$.name", as_name="name"), NumericField("$.num", as_name="num")), definition=definition, ) client.json().set("king:1", Path.rootPath(), {"name": "henry", "num": 42}) client.json().set("king:2", Path.rootPath(), { "name": "james", "num": 3.14 }) res = client.ft().search("@name:henry") assert res.docs[0].id == "king:1" assert res.docs[0].json == '{"name":"henry","num":42}' assert res.total == 1 res = client.ft().search("@num:[0 10]") assert res.docs[0].id == "king:2" assert res.docs[0].json == '{"name":"james","num":3.14}' assert res.total == 1 # Tests returns an error if path contain special characters (user should # use an alias) with pytest.raises(Exception): client.ft().search("@$.name:henry")
def test_strlen(client): client.json().set("str", Path.rootPath(), "foo") assert 3 == client.json().strlen("str", Path.rootPath()) import json client.json().strappend("str", json.dumps("bar"), Path.rootPath()) assert 6 == client.json().strlen("str", Path.rootPath()) assert 6 == client.json().strlen("str")
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")
def test_strappend(client): client.json().set("jsonkey", Path.rootPath(), 'foo') import json assert 6 == client.json().strappend("jsonkey", json.dumps('bar')) with pytest.raises(redis.exceptions.ResponseError): assert 6 == client.json().strappend("jsonkey", 'bar') assert "foobar" == client.json().get("jsonkey", Path.rootPath())
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)
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_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]
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
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
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
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_with_jsonpath(client): definition = IndexDefinition(index_type=IndexType.JSON) client.ft().create_index( ( TextField('$["prod:name"]', as_name="name"), TextField("$.prod:name", as_name="name_unsupported"), ), definition=definition, ) client.json().set("doc:1", Path.rootPath(), {"prod:name": "RediSearch"}) # query for a supported field succeeds res = client.ft().search(Query("@name:RediSearch")) assert res.total == 1 assert res.docs[0].id == "doc:1" assert res.docs[0].json == '{"prod:name":"RediSearch"}' # query for an unsupported field fails res = client.ft().search("@name_unsupported:RediSearch") assert res.total == 0 # return of a supported field succeeds res = client.ft().search(Query("@name:RediSearch").return_field("name")) assert res.total == 1 assert res.docs[0].id == "doc:1" assert res.docs[0].name == "RediSearch" # return of an unsupported field fails res = client.ft().search( Query("@name:RediSearch").return_field("name_unsupported")) assert res.total == 1 assert res.docs[0].id == "doc:1" with pytest.raises(Exception): res.docs[0].name_unsupported
def test_json_with_multipath(client): """ Create definition with IndexType.JSON as index type (ON JSON), and use json client to test it. """ definition = IndexDefinition(prefix=["king:"], index_type=IndexType.JSON) client.ft().create_index((TagField("$..name", as_name="name")), definition=definition) client.json().set("king:1", Path.rootPath(), { "name": "henry", "country": { "name": "england" } }) res = client.ft().search("@name:{henry}") assert res.docs[0].id == "king:1" assert res.docs[0].json == '{"name":"henry","country":{"name":"england"}}' assert res.total == 1 res = client.ft().search("@name:{england}") assert res.docs[0].id == "king:1" assert res.docs[0].json == '{"name":"henry","country":{"name":"england"}}' assert res.total == 1
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)
def test_search_return_fields(client): res = client.json().set( "doc:1", Path.rootPath(), { "t": "riceratops", "t2": "telmatosaurus", "n": 9072, "flt": 97.2 }, ) assert res # create index on definition = IndexDefinition(index_type=IndexType.JSON) SCHEMA = ( TextField("$.t"), NumericField("$.flt"), ) client.ft().create_index(SCHEMA, definition=definition) waitForIndex(client, "idx") total = client.ft().search(Query("*").return_field("$.t", as_field="txt")).docs assert 1 == len(total) assert "doc:1" == total[0].id assert "riceratops" == total[0].txt total = client.ft().search( Query("*").return_field("$.t2", as_field="txt")).docs assert 1 == len(total) assert "doc:1" == total[0].id assert "telmatosaurus" == total[0].txt
def test_create_client_definition_json(client): """ Create definition with IndexType.JSON as index type (ON JSON), and use json client to test it. """ definition = IndexDefinition(prefix=["king:"], index_type=IndexType.JSON) client.ft().create_index((TextField("$.name"), ), definition=definition) client.json().set("king:1", Path.rootPath(), {"name": "henry"}) client.json().set("king:2", Path.rootPath(), {"name": "james"}) res = client.ft().search("henry") assert res.docs[0].id == "king:1" assert res.docs[0].payload is None assert res.docs[0].json == '{"name":"henry"}' assert res.total == 1
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_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)
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_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)