def test_script_query2_set_(self): path = "store.books.*[?('isbn' in @)].title" result = querypath.set_(self.obj, path, self.value, False) for value in querypath.get(result, path): self.assertEqual(value, self.value) for value in querypath.get(result, "store.books.*[?('isbn' not in @)].title"): self.assertNotEqual(value, self.value) self.assertIs(result, self.obj)
def test_script_query_set_(self): path = "store.books.*[?(@.price<10)].title" result = querypath.set_(self.obj, path, self.value, False) for value in querypath.get(result, path): self.assertEqual(value, self.value) for value in querypath.get(result, "store.books.*[?(@.price>=10)].title"): self.assertNotEqual(value, self.value) self.assertIs(result, self.obj)
def test_script_query_set_tocopy(self): path = "store.books.*[?(@.price<10)].title" original = copy.deepcopy(self.obj) result = querypath.set_(self.obj, path, self.value, True) for value in querypath.get(result, path): self.assertEqual(value, self.value) for value in querypath.get(result, "store.books.*[?(@.price>=10)].title"): self.assertNotEqual(value, self.value) self.assertEqual(original, self.obj) self.assertIsNot(result, self.obj)
def test_script_query2_set_tocopy(self): path = "store.books.*[?('isbn' in @.__dict__)].title" original = copy.deepcopy(self.obj) result = querypath.set_(self.obj, path, self.value, True) for value in querypath.get(result, path): self.assertEqual(value, self.value) for value in querypath.get(result, "store.books.*[?('isbn' not in @.__dict__)].title"): self.assertNotEqual(value, self.value) self.assertEqual(original, self.obj) self.assertIsNot(result, self.obj)
def test_wildcard_test(self): for path in ( "store.books.*.price", "['store']['books'][*]['price']", "['store']['books']['*']['price']", ): self.assertTrue(querypath.get(self.obj, path))
def test_numeric_get(self): for path in ( "store.books.0.author", "store.books[0].author", ): result = list(querypath.get(self.obj, path)) expected = ['Nigel Rees'] self.assertItemsEqual(result, expected)
def test_script_index_get(self): for path in ( "store.books[(@.__len__()-1)].isbn", "..['books'][(@.__len__()-1)]['isbn']", ): result = list(querypath.get(self.obj, path)) expected = ["0-395-19395-8"] self.assertItemsEqual(result, expected)
def test_pipe_union_empty(self): for path in ( "|..bicycle.color", "..bicycle.color|", ): result = list(querypath.get(self.obj, path)) expected = [self.obj, 'red'] self.assertItemsEqual(result, expected)
def test_recursive_descent_set_tocopy(self): path = "store..[?('price' in @)].price" original = copy.deepcopy(self.obj) result = querypath.set_(self.obj, path, self.value, True) for value in querypath.get(result, path): self.assertEqual(value, self.value) self.assertEqual(original, self.obj) self.assertIsNot(result, self.obj)
def test_recursive_descent_test(self): for path in ( "store..price", "$['store']..['price']", "$..price", "$..price", ): self.assertTrue(querypath.get(self.obj, path))
def test_comma_union_get(self): for path in ( "store.books,bicycle,car..price", "$['store']['books,bicycle']..['price']", ): result = list(querypath.get(self.obj, path)) expected = [8.95, 12.99, 8.99, 22.99, 19.95] self.assertItemsEqual(result, expected)
def test_slice_begin_test(self): for path in ( "store.books[2:].price", "store.books.2:.price", "['store']['books']['2:']['price']", "['store']['books'][2:]['price']", ): self.assertTrue(querypath.get(self.obj, path))
def test_single_get(self): for path in ( "store.bicycle.color", "$.store.bicycle.color", "$['store']['bicycle']['color']", ): result = list(querypath.get(self.obj, path)) expected = ["red"] self.assertItemsEqual(result, expected)
def test_wildcard_get(self): for path in ( "store.books.*.price", "['store']['books'][*]['price']", "['store']['books']['*']['price']", ): result = list(querypath.get(self.obj, path)) expected = [8.95, 12.99, 8.99, 22.99] self.assertItemsEqual(result, expected)
def test_slice_end_get(self): for path in ( "store.books[:-1].price", "store.books.:-1.price", "['store']['books'][':-1']['price']", "['store']['books'][:-1]['price']", ): result = list(querypath.get(self.obj, path)) expected = [8.95, 12.99, 8.99] self.assertItemsEqual(result, expected)
def test_recursive_descent_get(self): for path in ( "store..price", "['store']..['price']", "$..price", "..price", ): result = list(querypath.get(self.obj, path)) expected = [8.95, 12.99, 8.99, 22.99, 19.95] self.assertItemsEqual(result, expected)
def test_slice_step_get(self): for path in ( "store.books[::2].price", "store.books.::2.price", "['store']['books']['::2']['price']", "['store']['books'][::2]['price']", ): result = list(querypath.get(self.obj, path)) expected = [8.95, 8.99] self.assertItemsEqual(result, expected)
def test_slice_begin_get(self): for path in ( "store.books[2:].price", "store.books.2:.price", "['store']['books']['2:']['price']", "['store']['books'][2:]['price']", ): result = list(querypath.get(self.obj, path)) expected = [8.99, 22.99] self.assertItemsEqual(result, expected)
def test_empty_result_get(self): for path in ( "folder", "$.folder", "$[folder]", "['folder']", "store.books.7", ): result = list(querypath.get(self.obj, path)) expected = [] self.assertItemsEqual(result, expected)
def test_script_query2_get(self): for path in ( "store.books.*[?(@.isbn)].title", "store.books.*[?('isbn' in @.__dict__)].title", ): result = list(querypath.get(self.obj, path)) expected = [ 'Moby Dick', 'The Lord of the Rings', ] self.assertItemsEqual(result, expected)
def test_pipe_union_get(self): for path in ( "store.books.*.category|..bicycle.color", "store.books.*.category |..bicycle.color", "store.books.*.category| ..bicycle.color", "store.books.*.category | ..bicycle.color", "[store][books][:][category]|[store][bicycle][color]", "store.books.*.category|store.bicycle.color", ): result = list(querypath.get(self.obj, path)) expected = [ 'reference', 'fiction', 'fiction', 'fiction', 'red', ] self.assertItemsEqual(result, expected)
def test_valid_set_(self): original = copy.deepcopy(self.obj) for path in ( "store.bicycle.color", "store.books.0.author", "store.books.*.price", "store.books[:].price", "store.books[2:].price", "store.books[:-1].price", "store.books[::2].price", "store.bicycle.color|store.book[0].title", "store.books[(@.__len__()-1)].isbn", ): self.obj = copy.deepcopy(original) result = querypath.set_(self.obj, path, self.value, False) for value in querypath.get(result, path): self.assertEqual(value, self.value) self.assertIs(result, self.obj)
def test_all_items(self): class A: def __init__(self, a=None): self.i = 1 self.f = 0.1 self.s = "str" self.l = [1,2,3] self.a = a def __eq__(self, o): return isinstance(o,A) \ and self.i==o.i and self.f==o.f and self.s==o.s and self.l==o.l obj = A(A()) result = querypath.get(obj, "..*") expected = [ 1, 0.1, "str", "s", "t", "r", "s", "t", "r", [1, 2, 3], 1, 2, 3, A(), 1, 0.1, "str", "s", "t", "r", "s", "t", "r", [1, 2, 3], 1, 2, 3, None, ] self.assertItemsEqual(result, expected)
def test_all_items(self): obj = { "i": 1, "f": 0.1, "s": "str", "l": [1, 2, 3], "d": { "i": 1, "f": 0.1, "s": "str", }, } result = querypath.get(obj, "..*") expected = [ 1, 0.1, "str", "s", "t", "r", "s", "t", "r", [1, 2, 3], 1, 2, 3, { "i": 1, "f": 0.1, "s": "str", }, 1, 0.1, "str", "s", "t", "r", "s", "t", "r", ] self.assertItemsEqual(result, expected)
def test_comma_union_test(self): for path in ( "store.books,bicycle,car..price", "$['store']['books,bicycle']..['price']", ): self.assertTrue(querypath.get(self.obj, path))
def test_script_query_get(self): result = querypath.get(self.obj, "store.books.*[?(@.price<10)].title") expected = ['Sayings of the Century', 'Moby Dick'] self.assertItemsEqual(result, expected)
def test_pipe_intersection_get(self): result = querypath.get(self.obj, "..price|store.books[0].price") expected = [8.95, 12.99, 8.99, 22.99, 19.95] self.assertItemsEqual(result, expected)
def test_recursive_descent_set_(self): path = "store..[?('price' in @)].price" result = querypath.set_(self.obj, path, self.value, False) for value in querypath.get(result, path): self.assertEqual(value, self.value) self.assertIs(result, self.obj)
def test_script_index_test(self): for path in ( "store.books[(@.__len__()-1)].isbn", "..['books'][(@.__len__()-1)]['isbn']", ): self.assertTrue(querypath.get(self.obj, path))
def test_script_query2_test(self): for path in ( "store.books.*[?('isbn' in @)].title", "store.books.*[?(@['isbn'])].title", ): self.assertTrue(querypath.get(self.obj, path))