Example #1
0
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)
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.rootPath(), root) == result
    assert client.json().get(jsonfile.rsplit(".")[0]) == {"hello": "world"}
Example #3
0
def test_fields_as_name(client):
    # create index
    SCHEMA = (
        TextField("$.name", sortable=True, as_name="name"),
        NumericField("$.age", as_name="just_a_number"),
    )
    definition = IndexDefinition(index_type=IndexType.JSON)
    client.ft().create_index(SCHEMA, definition=definition)

    # insert json data
    res = client.json().set("doc:1", Path.rootPath(), {
        "name": "Jon",
        "age": 25
    })
    assert res

    total = client.ft().search(
        Query("Jon").return_fields("name", "just_a_number")).docs
    assert 1 == len(total)
    assert "doc:1" == total[0].id
    assert "Jon" == total[0].name
    assert "25" == total[0].just_a_number
def test_clear(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")
def test_mgetshouldsucceed(client):
    client.json().set("1", Path.rootPath(), 1)
    client.json().set("2", Path.rootPath(), 2)
    assert client.json().mget(["1"], Path.rootPath()) == [1]

    assert client.json().mget([1, 2], Path.rootPath()) == [1, 2]
def test_nonascii_setgetdelete(client):
    assert client.json().set("notascii", Path.rootPath(), "hyvää-élève")
    assert "hyvää-élève" == client.json().get("notascii", no_escape=True)
    assert 1 == client.json().delete("notascii")
    assert client.exists("notascii") == 0
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
def test_jsonget(client):
    client.json().set("foo", Path.rootPath(), "bar")
    assert client.json().get("foo") == "bar"
def test_arrlen(client):
    client.json().set("arr", Path.rootPath(), [0, 1, 2, 3, 4])
    assert 5 == client.json().arrlen("arr", Path.rootPath())
    assert 5 == client.json().arrlen("arr")
    assert client.json().arrlen("fakekey") is None
Example #10
0
def test_arrindex(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)
Example #11
0
def test_arrappend(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])
Example #12
0
def test_strlen(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())
    assert 6 == client.json().strlen("str")
Example #13
0
def test_strappend(client):
    client.json().set("jsonkey", Path.rootPath(), "foo")
    assert 6 == client.json().strappend("jsonkey", "bar")
    assert "foobar" == client.json().get("jsonkey", Path.rootPath())
Example #14
0
def test_numincrby(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)
Example #15
0
def test_type(client):
    client.json().set("1", Path.rootPath(), 1)
    assert "integer" == client.json().type("1", Path.rootPath())
    assert "integer" == client.json().type("1")
Example #16
0
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)
Example #17
0
        with jsonX methods.
        """
        def __init__(self, r):
            self._r = r

        def __getattr__(self, attr):
            if attr.startswith('json'):
                return getattr(self._r.json(), attr[4:])
            return getattr(self._r, attr)

        def pipeline(self):
            return RejsonCompat(self._r.pipeline())

    def make_redis_client(host='localhost', port=6379, db=0):
        """Return plain redis client + rejson client"""
        r = redis.Redis(host, port=port, db=db)
        j = RejsonCompat(redis.Redis(host, port=port, db=db))
        return r, j
else:
    #versions < 4.0.0 don't have rejson built in
    import rejson
    from rejson import Path

    ROOT_PATH = Path.rootPath()

    def make_redis_client(host='localhost', port=6379, db=0):
        """Return plain redis client + rejson client"""
        r = rejson.Client(host=host, port=port, db=db, decode_responses=False)
        j = rejson.Client(host=host, port=port, db=db, decode_responses=True)
        return r, j