diff options
Diffstat (limited to 'tests/test_json.py')
-rw-r--r-- | tests/test_json.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/test_json.py b/tests/test_json.py index a99547d..8bd1d77 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -1392,3 +1392,42 @@ def test_custom_decoder(client): assert client.exists("foo") == 0 assert not isinstance(cj.__encoder__, json.JSONEncoder) assert not isinstance(cj.__decoder__, json.JSONDecoder) + + +@pytest.mark.redismod +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.rootPath(), jsonfile.name) + assert client.json().get("test") == obj + with pytest.raises(json.JSONDecodeError): + client.json().set_file("test2", Path.rootPath(), nojsonfile.name) + + +@pytest.mark.redismod +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} + print(result) + assert client.json().set_path(Path.rootPath(), root) == result + assert client.json().get(jsonfile.rsplit(".")[0]) == {"hello": "world"} |