diff --git a/API.md b/API.md index 4c4a233..c2dfb81 100644 --- a/API.md +++ b/API.md @@ -252,6 +252,19 @@ Gets the objects stored as a JSON values under ``path`` from keys ``args`` +### jsonmgetl +```py + +def jsonmgetl(self, keys=[], path=Path.rootPath()) + +``` + + + +Gets the objects stored as a JSON values under ``path`` from +key list ``keys`` + + ### jsonnumincrby ```py diff --git a/rejson/client.py b/rejson/client.py index 0974e68..54d04f7 100644 --- a/rejson/client.py +++ b/rejson/client.py @@ -120,15 +120,26 @@ def jsonmget(self, path, *args): """ pieces = [] pieces.extend(args) + if not path: + path=Path.rootPath() pieces.append(str_path(path)) return self.execute_command('JSON.MGET', *pieces) + def jsonmgetl(self, keys=[], path=Path.rootPath()): + """ + Gets the objects stored as a JSON values under ``path`` from + key list ``keys`` + """ + return self.jsonmget(path, *keys) + def jsonset(self, name, path, obj, nx=False, xx=False): """ Set the JSON value at key ``name`` under the ``path`` to ``obj`` ``nx`` if set to True, set ``value`` only if it does not exist ``xx`` if set to True, set ``value`` only if it exists """ + if not path: + path=Path.rootPath() pieces = [name, str_path(path), self._encode(obj)] # Handle existential modifiers @@ -152,6 +163,8 @@ def jsonnumincrby(self, name, path, number): Increments the numeric (integer or floating point) JSON value under ``path`` at key ``name`` by the provided ``number`` """ + if not path: + path=Path.rootPath() return self.execute_command('JSON.NUMINCRBY', name, str_path(path), self._encode(number)) def jsonnummultby(self, name, path, number): @@ -159,6 +172,8 @@ def jsonnummultby(self, name, path, number): Multiplies the numeric (integer or floating point) JSON value under ``path`` at key ``name`` with the provided ``number`` """ + if not path: + path=Path.rootPath() return self.execute_command('JSON.NUMMULTBY', name, str_path(path), self._encode(number)) def jsonstrappend(self, name, string, path=Path.rootPath()): @@ -175,11 +190,13 @@ def jsonstrlen(self, name, path=Path.rootPath()): """ return self.execute_command('JSON.STRLEN', name, str_path(path)) - def jsonarrappend(self, name, path=Path.rootPath(), *args): + def jsonarrappend(self, name, path, *args): """ Appends the objects ``args`` to the array under the ``path` in key ``name`` """ + if not path: + path=Path.rootPath() pieces = [name, str_path(path)] for o in args: pieces.append(self._encode(o)) @@ -191,6 +208,8 @@ def jsonarrindex(self, name, path, scalar, start=0, stop=-1): ``name``. The search can be limited using the optional inclusive ``start`` and exclusive ``stop`` indices. """ + if not path: + path=Path.rootPath() return self.execute_command('JSON.ARRINDEX', name, str_path(path), self._encode(scalar), start, stop) def jsonarrinsert(self, name, path, index, *args): @@ -198,6 +217,8 @@ def jsonarrinsert(self, name, path, index, *args): Inserts the objects ``args`` to the array at index ``index`` under the ``path` in key ``name`` """ + if not path: + path=Path.rootPath() pieces = [name, str_path(path), index] for o in args: pieces.append(self._encode(o)) @@ -222,6 +243,8 @@ def jsonarrtrim(self, name, path, start, stop): Trim the array JSON value under ``path`` at key ``name`` to the inclusive range given by ``start`` and ``stop`` """ + if not path: + path=Path.rootPath() return self.execute_command('JSON.ARRTRIM', name, str_path(path), start, stop) def jsonobjkeys(self, name, path=Path.rootPath()): diff --git a/tests/test_rejson.py b/tests/test_rejson.py index 629eb01..a68e43d 100644 --- a/tests/test_rejson.py +++ b/tests/test_rejson.py @@ -46,10 +46,13 @@ def testMGetShouldSucceed(self): "Test JSONMGet" rj.jsonset('1', Path.rootPath(), 1) - rj.jsonset('2', Path.rootPath(), 2) - r = rj.jsonmget(Path.rootPath(), '1', '2') + rj.jsonset('2', None, 2) + r1 = rj.jsonmget(None, '1', '2') + r2 = rj.jsonmgetl(keys=['1', '2']) e = [1, 2] - self.assertListEqual(e, r) + self.assertListEqual(e, r1) + self.assertListEqual(e, r2) + def testTypeShouldSucceed(self): "Test JSONType" @@ -62,7 +65,7 @@ def testNumIncrByShouldSucceed(self): rj.jsonset('num', Path.rootPath(), 1) self.assertEqual(2, rj.jsonnumincrby('num', Path.rootPath(), 1)) - self.assertEqual(2.5, rj.jsonnumincrby('num', Path.rootPath(), 0.5)) + self.assertEqual(2.5, rj.jsonnumincrby('num', None, 0.5)) self.assertEqual(1.25, rj.jsonnumincrby('num', Path.rootPath(), -1.25)) def testNumMultByShouldSucceed(self): @@ -70,7 +73,7 @@ def testNumMultByShouldSucceed(self): rj.jsonset('num', Path.rootPath(), 1) self.assertEqual(2, rj.jsonnummultby('num', Path.rootPath(), 2)) - self.assertEqual(5, rj.jsonnummultby('num', Path.rootPath(), 2.5)) + self.assertEqual(5, rj.jsonnummultby('num', None, 2.5)) self.assertEqual(2.5, rj.jsonnummultby('num', Path.rootPath(), 0.5)) def testStrAppendShouldSucceed(self): @@ -78,7 +81,8 @@ def testStrAppendShouldSucceed(self): rj.jsonset('str', Path.rootPath(), 'foo') self.assertEqual(6, rj.jsonstrappend('str', 'bar', Path.rootPath())) - self.assertEqual('foobar', rj.jsonget('str', Path.rootPath())) + self.assertEqual(9, rj.jsonstrappend('str', 'baz')) + self.assertEqual('foobarbaz', rj.jsonget('str', Path.rootPath())) def testStrLenShouldSucceed(self): "Test JSONStrLen" @@ -86,27 +90,28 @@ def testStrLenShouldSucceed(self): rj.jsonset('str', Path.rootPath(), 'foo') self.assertEqual(3, rj.jsonstrlen('str', Path.rootPath())) rj.jsonstrappend('str', 'bar', Path.rootPath()) - self.assertEqual(6, rj.jsonstrlen('str', Path.rootPath())) + self.assertEqual(6, rj.jsonstrlen('str')) def testArrAppendShouldSucceed(self): "Test JSONSArrAppend" rj.jsonset('arr', Path.rootPath(), [1]) self.assertEqual(2, rj.jsonarrappend('arr', Path.rootPath(), 2)) + self.assertEqual(3, rj.jsonarrappend('arr', None, 3)) def testArrIndexShouldSucceed(self): "Test JSONSArrIndex" rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4]) self.assertEqual(1, rj.jsonarrindex('arr', Path.rootPath(), 1)) - self.assertEqual(-1, rj.jsonarrindex('arr', Path.rootPath(), 1, 2)) + self.assertEqual(-1, rj.jsonarrindex('arr', None, 1, 2)) def testArrInsertShouldSucceed(self): "Test JSONSArrInsert" rj.jsonset('arr', Path.rootPath(), [0, 4]) self.assertEqual(5, rj.jsonarrinsert('arr', - Path.rootPath(), 1, *[1, 2, 3, ])) + None, 1, *[1, 2, 3, ])) self.assertListEqual([0, 1, 2, 3, 4], rj.jsonget('arr')) def testArrLenShouldSucceed(self): @@ -114,13 +119,14 @@ def testArrLenShouldSucceed(self): rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4]) self.assertEqual(5, rj.jsonarrlen('arr', Path.rootPath())) + self.assertEqual(5, rj.jsonarrlen('arr')) def testArrPopShouldSucceed(self): "Test JSONSArrPop" rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4]) self.assertEqual(4, rj.jsonarrpop('arr', Path.rootPath(), 4)) - self.assertEqual(3, rj.jsonarrpop('arr', Path.rootPath(), -1)) + self.assertEqual(3, rj.jsonarrpop('arr', index=-1)) self.assertEqual(2, rj.jsonarrpop('arr', Path.rootPath())) self.assertEqual(0, rj.jsonarrpop('arr', Path.rootPath(), 0)) self.assertListEqual([1], rj.jsonget('arr')) @@ -129,7 +135,7 @@ def testArrTrimShouldSucceed(self): "Test JSONSArrPop" rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4]) - self.assertEqual(3, rj.jsonarrtrim('arr', Path.rootPath(), 1, 3)) + self.assertEqual(3, rj.jsonarrtrim('arr', None, 1, 3)) self.assertListEqual([1, 2, 3], rj.jsonget('arr')) def testObjKeysShouldSucceed(self): @@ -137,18 +143,20 @@ def testObjKeysShouldSucceed(self): obj = {'foo': 'bar', 'baz': 'qaz'} rj.jsonset('obj', Path.rootPath(), obj) - keys = rj.jsonobjkeys('obj', Path.rootPath()) + keys = rj.jsonobjkeys('obj') keys.sort() exp = [k for k in six.iterkeys(obj)] exp.sort() self.assertListEqual(exp, keys) + # TODO: maybe add a test for subobjects here def testObjLenShouldSucceed(self): "Test JSONSObjLen" obj = {'foo': 'bar', 'baz': 'qaz'} rj.jsonset('obj', Path.rootPath(), obj) - self.assertEqual(len(obj), rj.jsonobjlen('obj', Path.rootPath())) + self.assertEqual(len(obj), rj.jsonobjlen('obj')) + # TODO: maybe add a test for subobjects here def testPipelineShouldSucceed(self): "Test pipeline"