Skip to content

Commit 58d383f

Browse files
committed
fixing tests and lint
1 parent aa0c809 commit 58d383f

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

redis/commands/json/commands.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import os
2+
from json import JSONDecodeError, loads
3+
14
from deprecated import deprecated
25

36
from redis.exceptions import DataError
47

58
from .decoders import decode_dict_keys
69
from .path import Path
7-
from json import loads, JSONDecodeError
8-
import os
910

1011

1112
class JSONCommands:
@@ -217,31 +218,37 @@ def set(self, name, path, obj, nx=False, xx=False, decode_keys=False):
217218

218219
def set_file(self, name, path, file_name, nx=False, xx=False, decode_keys=False):
219220
"""
220-
Set the JSON value at key ``name`` under the ``path`` to the contents of the json file ``file_name``.
221+
Set the JSON value at key ``name`` under the ``path`` to the content
222+
of the json file ``file_name``.
221223
222224
``nx`` if set to True, set ``value`` only if it does not exist.
223225
``xx`` if set to True, set ``value`` only if it exists.
224226
``decode_keys`` If set to True, the keys of ``obj`` will be decoded
225227
with utf-8.
226228
227229
"""
228-
try:
229-
file_content = loads(file_name)
230-
except JSONDecodeError:
231-
raise JSONDecodeError("Inappropriate file type, set_file() requires json file")
232-
230+
231+
with open(file_name, "r") as fp:
232+
file_content = loads(fp.read())
233+
233234
return self.set(name, path, file_content, nx, xx, decode_keys)
234235

236+
def set_path(self, json_path, root_folder, nx=False, xx=False, decode_keys=False):
237+
"""
238+
Iterate over ``root_folder`` and set each JSON file to a value
239+
under ``json_path`` with the file name as the key.
235240
236-
def set_path(self, json_path, root_directory , nx=False, xx=False, decode_keys=False):
237-
"""
241+
``nx`` if set to True, set ``value`` only if it does not exist.
242+
``xx`` if set to True, set ``value`` only if it exists.
243+
``decode_keys`` If set to True, the keys of ``obj`` will be decoded
244+
with utf-8.
238245
239246
"""
240247
set_files_result = {}
241-
for root, dirs, files in os.walk(root_directory):
248+
for root, dirs, files in os.walk(root_folder):
242249
for file in files:
243250
try:
244-
file_name = file.rsplit('.')[0]
251+
file_name = os.path.join(root, file).rsplit(".")[0]
245252
file_path = os.path.join(root, file)
246253
self.set_file(file_name, json_path, file_path, nx, xx, decode_keys)
247254
set_files_result[os.path.join(root, file)] = True
@@ -250,8 +257,6 @@ def set_path(self, json_path, root_directory , nx=False, xx=False, decode_keys=F
250257

251258
return set_files_result
252259

253-
254-
255260
def strlen(self, name, path=None):
256261
"""Return the length of the string JSON value under ``path`` at key
257262
``name``.

tests/test_json.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,11 +1396,38 @@ def test_custom_decoder(client):
13961396

13971397
@pytest.mark.redismod
13981398
def test_set_file(client):
1399-
import tempfile
14001399
import json
1400+
import tempfile
14011401

1402+
obj = {"hello": "world"}
14021403
jsonfile = tempfile.NamedTemporaryFile(suffix=".json")
1403-
with open(jsonfile.name, 'w+') as fp:
1404+
with open(jsonfile.name, "w+") as fp:
1405+
fp.write(json.dumps(obj))
1406+
1407+
nojsonfile = tempfile.NamedTemporaryFile()
1408+
nojsonfile.write(b"Hello World")
1409+
1410+
assert client.json().set_file("test", Path.rootPath(), jsonfile.name)
1411+
assert client.json().get("test") == obj
1412+
with pytest.raises(json.JSONDecodeError):
1413+
client.json().set_file("test2", Path.rootPath(), nojsonfile.name)
1414+
1415+
1416+
@pytest.mark.redismod
1417+
def test_set_path(client):
1418+
import json
1419+
import os
1420+
import tempfile
1421+
1422+
root = tempfile.mkdtemp()
1423+
sub = tempfile.mkdtemp(dir=root)
1424+
ospointer, jsonfile = tempfile.mkstemp(suffix=".json", dir=sub)
1425+
ospointer2, nojsonfile = tempfile.mkstemp(dir=root)
1426+
1427+
with open(jsonfile, "w+") as fp:
14041428
fp.write(json.dumps({"hello": "world"}))
1429+
open(nojsonfile, "a+").write("hello")
14051430

1406-
assert client.json().set("test", Path.rootPath(), jsonfile.name)
1431+
result = {"/private" + jsonfile: True, "/private" + nojsonfile: False}
1432+
assert client.json().set_path(Path.rootPath(), os.path.realpath(root)) == result
1433+
assert client.json().get("/private" + jsonfile.rsplit(".")[0]) == {"hello": "world"}

0 commit comments

Comments
 (0)