def test_it(): debug = False for i in xrange(0, len(tests)): if debug: print(">>>", i, tests[i]) v = jsonpath.jsonpath(json, tests[i]) assert jsonmatch.jsonmatch(v, values[i]) p = jsonpath.jsonpath(json, tests[i], "PATH") assert jsonmatch.jsonmatch(p, paths[i]) if debug: print("OK") print(len(tests), "tests passed")
def test_array_retrieval(): def check(expr, exp_len): result = jsonpath.jsonpath(test, expr) assert result != 0 assert len(result) == exp_len ok() return result check('$.store.book[*].author', 4) check('$..author', 4) result = check('$..book[?(@.price<10)]', 2) expected = [{ "category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, { "category": "fiction", "author": "Herman Melville", "title": "Moby Dick", "isbn": "0-553-21311-3", "price": 8.99 }] assert jsonmatch.jsonmatch(result, expected) for book in result: assert 'category' in book assert 'author' in book assert 'title' in book assert 'price' in book if 'isbn' in book: # moby dick assert 8.99 == book['price'] assert 'Moby Dick' == book['title'] else: #sayings of the century assert 8.95 == book['price'] assert 'Sayings of the Century' == book['title'] check('$.store.*', 2) #book array and one bicycle result = check('$.store..price', 5) #the price of everything assert jsonmatch.jsonmatch(result, [8.95, 12.99, 8.99, 22.99, 19.95]) check('$..book[0,1]', 2) check('$..book[:2]', 2) check('$..book[?(@.isbn)]', 2) check('$.store.!', 2) #the keys in the store hash check('$..*', 27)
def check_array(expr, exp_len, expected=None): result = jsonpath.jsonpath(test, expr) assert result != 0 assert len(result) == exp_len if expected is not None: assert jsonmatch.jsonmatch(result, expected) return result
def test_array_retrieval(): def check(expr, exp_len): result = jsonpath.jsonpath(test, expr) assert result != 0 assert len(result) == exp_len ok() return result check('$.store.book[*].author', 4) check('$..author', 4) result = check('$..book[?(@.price<10)]', 2) expected = [ {"category":"reference", "author":"Nigel Rees", "title":"Sayings of the Century", "price":8.95}, {"category":"fiction", "author":"Herman Melville", "title":"Moby Dick", "isbn":"0-553-21311-3", "price":8.99} ] assert jsonmatch.jsonmatch(result, expected) for book in result: assert 'category' in book assert 'author' in book assert 'title' in book assert 'price' in book if 'isbn' in book: # moby dick assert 8.99 == book['price'] assert 'Moby Dick' == book['title'] else: #sayings of the century assert 8.95 == book['price'] assert 'Sayings of the Century' == book['title'] check('$.store.*', 2) #book array and one bicycle result = check('$.store..price', 5) #the price of everything assert jsonmatch.jsonmatch(result, [ 8.95, 12.99, 8.99, 22.99, 19.95 ]) check('$..book[0,1]', 2) check('$..book[:2]', 2) check('$..book[?(@.isbn)]', 2) check('$.store.!', 2) #the keys in the store hash check('$..*', 27)
"$['store']['book'][0]['author']", "$['store']['book'][1]['author']", "$['store']['book'][2]['author']", "$['store']['book'][3]['author']" ], ["$['store']['book']", "$['store']['bicycle']"], [ "$['store']['book'][0]['price']", "$['store']['book'][1]['price']", "$['store']['book'][2]['price']", "$['store']['book'][3]['price']", "$['store']['bicycle']['price']" ], ["$['store']['book'][3]"], ["$['store']['book'][3]"], ["$['store']['book'][0]", "$['store']['book'][1]"], ["$['store']['book'][0]", "$['store']['book'][1]"], ["$['store']['book'][2]", "$['store']['book'][3]"], ["$['store']['book'][0]", "$['store']['book'][2]"], # ["$['store']","$['store']['book']","$['store']['bicycle']","$['store']['book'][0]","$['store']['book'][1]","$['store']['book'][2]","$['store']['book'][3]","$['store']['book'][0]['category']","$['store']['book'][0]['author']","$['store']['book'][0]['title']","$['store']['book'][0]['price']","$['store']['book'][1]['category']","$['store']['book'][1]['author']","$['store']['book'][1]['title']","$['store']['book'][1]['price']","$['store']['book'][2]['category']","$['store']['book'][2]['author']","$['store']['book'][2]['title']","$['store']['book'][2]['isbn']","$['store']['book'][2]['price']","$['store']['book'][3]['category']","$['store']['book'][3]['author']","$['store']['book'][3]['title']","$['store']['book'][3]['isbn']","$['store']['book'][3]['price']","$['store']['bicycle']['color']","$['store']['bicycle']['price']"] ] assert len(tests) == len(values) assert len(tests) == len(paths) debug = False for i in xrange(0, len(tests)): if debug: print ">>>", i, tests[i] v = jsonpath.jsonpath(json, tests[i]) assert jsonmatch.jsonmatch(v, values[i]) p = jsonpath.jsonpath(json, tests[i], "PATH") assert jsonmatch.jsonmatch(p, paths[i]) if debug: print "OK" print len(tests), "tests passed"
# 10 # [{"book":[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],"bicycle":{"color":"red","price":19.95}},[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],{"color":"red","price":19.95},{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century","price":8.95},{"category":"fiction","author":"Evelyn Waugh","title":"Sword of Honour","price":12.99},{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":8.99},{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99},"reference","Nigel Rees","Sayings of the Century",8.95,"fiction","Evelyn Waugh","Sword of Honour",12.99,"fiction","Herman Melville","Moby Dick","0-553-21311-3",8.99,"fiction","J. R. R. Tolkien","The Lord of the Rings","0-395-19395-8",22.99,"red",19.95] ] paths = [ ["$['store']['book'][0]['author']","$['store']['book'][1]['author']","$['store']['book'][2]['author']","$['store']['book'][3]['author']"], ["$['store']['book'][0]['author']","$['store']['book'][1]['author']","$['store']['book'][2]['author']","$['store']['book'][3]['author']"], ["$['store']['book']","$['store']['bicycle']"], ["$['store']['book'][0]['price']","$['store']['book'][1]['price']","$['store']['book'][2]['price']","$['store']['book'][3]['price']","$['store']['bicycle']['price']"], ["$['store']['book'][3]"], ["$['store']['book'][3]"], ["$['store']['book'][0]","$['store']['book'][1]"], ["$['store']['book'][0]","$['store']['book'][1]"], ["$['store']['book'][2]","$['store']['book'][3]"], ["$['store']['book'][0]","$['store']['book'][2]"], # ["$['store']","$['store']['book']","$['store']['bicycle']","$['store']['book'][0]","$['store']['book'][1]","$['store']['book'][2]","$['store']['book'][3]","$['store']['book'][0]['category']","$['store']['book'][0]['author']","$['store']['book'][0]['title']","$['store']['book'][0]['price']","$['store']['book'][1]['category']","$['store']['book'][1]['author']","$['store']['book'][1]['title']","$['store']['book'][1]['price']","$['store']['book'][2]['category']","$['store']['book'][2]['author']","$['store']['book'][2]['title']","$['store']['book'][2]['isbn']","$['store']['book'][2]['price']","$['store']['book'][3]['category']","$['store']['book'][3]['author']","$['store']['book'][3]['title']","$['store']['book'][3]['isbn']","$['store']['book'][3]['price']","$['store']['bicycle']['color']","$['store']['bicycle']['price']"] ] assert len(tests) == len(values) assert len(tests) == len(paths) debug = False for i in xrange(0, len(tests)): if debug: print ">>>", i, tests[i] v = jsonpath.jsonpath(json, tests[i]) assert jsonmatch.jsonmatch(v, values[i]) p = jsonpath.jsonpath(json, tests[i], "PATH") assert jsonmatch.jsonmatch(p, paths[i]) if debug: print "OK" print len(tests), "tests passed"