def testMatchBad(self): data = ( # Objects ("/3", '{"1": "foo", "2": "bar"}', False), ("/a", '{"1": "foo", "2": "bar"}', False), ("/-", '{"1": "foo", "2": "bar"}', False), ("/1/3", '{"1": {"1.1": "foo", "1.2": "bar"}}', False), ("/1/a", '{"1": {"1.1": "foo", "1.2": "bar"}}', False), ("/1/-", '{"1": {"1.1": "foo", "1.2": "bar"}}', False), # Arrays ("/2", '["1", "2"]', False), ("/0/2", '[["1", "2"]]', False), # Both ("/1/3", '{"1": ["foo", "bar"]}', False), ("/0/3", '[{"1.1": "foo", "1.2": "bar"}]', False), ("/0/a", '[{"1.1": "foo", "1.2": "bar"}]', False), ("/0/-", '[{"1.1": "foo", "1.2": "bar"}]', False), # Wrong Object ("/1/0", '{"1": "foo"}', True), ("/1/0", '{"1": 1}', True), ("/1/0", '{"1": true}', True), ("/1/0", '{"1": null}', True), ) for pointer, jobj, willRaise in data: j = JSONMatcher(pointer) if willRaise: self.assertRaises(JSONPointerMatchError, j.matchs, jobj) else: self.assertEqual(j.matchs(jobj), [], "Failed test: %s" % (pointer,))
def testMatchingOK(self): data = ( ( "/", '{"1":"foo", "2": "bar"}', [{"1": "foo", "2": "bar"}, ], ), ( "/.", '{"1":"foo", "2": "bar"}', ["foo", "bar", ], ), ( "/./0", '{"1":["foo1", "foo2"], "2": ["bar1", "bar2"]}', ["foo1", "bar1", ], ), ( "/./1", '{"1":["foo1", "foo2"], "2": ["bar1", "bar2"]}', ["foo2", "bar2", ], ), ( "/./-", '{"1":["foo1", "foo2"], "2": ["bar1", "bar2"]}', ["foo2", "bar2", ], ), ( "/./2", '{"1":["foo1", "foo2"], "2": ["bar1", "bar2"]}', [], ), ( "/./foo1", '{"1":{"foo1": "bar1", "foo2": "bar2"}, "2": {"foo1": "bar3", "foo4": "bar4"}}', ["bar1", "bar3", ], ), ( "/./foo2", '{"1":{"foo1": "bar1", "foo2": "bar2"}, "2": {"foo1": "bar3", "foo4": "bar4"}}', ["bar2", ], ), ( "/./foo4", '{"1":{"foo1": "bar1", "foo2": "bar2"}, "2": {"foo1": "bar3", "foo4": "bar4"}}', ["bar4", ], ), ( "/./foo3", '{"1":{"foo1": "bar1", "foo2": "bar2"}, "2": {"foo1": "bar3", "foo4": "bar4"}}', [], ), ) for pointer, jobj, result in data: j = JSONMatcher(pointer) self.assertEqual(j.matchs(jobj), result, "Failed test: %s" % (pointer,))
def testMatchOK(self): data = ( # Objects ("/", '{"1": "foo"}', [{"1": "foo"}]), ("/1", '{"1": "foo", "2": "bar"}', ["foo"]), ("/2", '{"1": "foo", "2": "bar"}', ["bar"]), ("/1", '{"1": {"1.1": "foo"}}', [{"1.1": "foo"}]), ("/1/1.1", '{"1": {"1.1": "foo", "1.2": "bar"}}', ["foo"]), ("/1/1.2", '{"1": {"1.1": "foo", "1.2": "bar"}}', ["bar"]), # Arrays ("/", '["1", "2"]', [["1", "2"]]), ("/0", '["1", "2"]', ["1"]), ("/1", '["1", "2"]', ["2"]), ("/-", '["1", "2"]', ["2"]), ("/0", '[["1", "2"]]', [["1", "2"]]), ("/-", '[["1", "2"]]', [["1", "2"]]), ("/0/0", '[["1", "2"]]', ["1"]), ("/0/1", '[["1", "2"]]', ["2"]), ("/0/-", '[["1", "2"]]', ["2"]), # Both ("/", '{"1": ["foo", "bar"]}', [{"1": ["foo", "bar"]}]), ("/1", '{"1": ["foo", "bar"]}', [["foo", "bar"]]), ("/1/0", '{"1": ["foo", "bar"]}', ["foo"]), ("/1/1", '{"1": ["foo", "bar"]}', ["bar"]), ("/1/-", '{"1": ["foo", "bar"]}', ["bar"]), ("/", '[{"1.1": "foo", "1.2": "bar"}]', [[{"1.1": "foo", "1.2": "bar"}]]), ("/0", '[{"1.1": "foo", "1.2": "bar"}]', [{"1.1": "foo", "1.2": "bar"}]), ("/-", '[{"1.1": "foo", "1.2": "bar"}]', [{"1.1": "foo", "1.2": "bar"}]), ("/0/1.1", '[{"1.1": "foo", "1.2": "bar"}]', ["foo"]), ("/0/1.2", '[{"1.1": "foo", "1.2": "bar"}]', ["bar"]), ) for pointer, jobj, result in data: j = JSONMatcher(pointer) self.assertEqual(j.matchs(jobj), result, "Failed test: %s" % (pointer,))