44from typing import List
55
66import jsonpath
7+ from jsonpath import Projection
8+
9+
10+ def test_select_from_primitive () -> None :
11+ expr = "$[0].a"
12+ data = [{"a" : 1 , "b" : 1 }, {"a" : 2 , "b" : 2 }, {"b" : 3 , "a" : 3 }]
13+ projection = ("nosuchthing" ,)
14+ it = jsonpath .query (expr , data ).select (* projection )
15+ assert list (it ) == []
716
817
918def test_top_level_array () -> None :
@@ -14,6 +23,14 @@ def test_top_level_array() -> None:
1423 assert list (it ) == [{"a" : 1 }, {"a" : 2 }, {"a" : 3 }]
1524
1625
26+ def test_top_level_array_flat_projection () -> None :
27+ expr = "$.*"
28+ data = [{"a" : 1 , "b" : 1 }, {"a" : 2 , "b" : 2 }, {"b" : 3 , "a" : 3 }]
29+ projection = ("a" ,)
30+ it = jsonpath .query (expr , data ).select (* projection , projection = Projection .FLAT )
31+ assert list (it ) == [[1 ], [2 ], [3 ]]
32+
33+
1734def test_top_level_array_partial_existence () -> None :
1835 expr = "$.*"
1936 data = [{"a" : 1 , "b" : 1 }, {"b" : 2 }, {"b" : 3 , "a" : 3 }]
@@ -76,3 +93,19 @@ def test_select_nested_objects() -> None:
7693 projection = ("foo.bar" ,)
7794 it = jsonpath .query (expr , data ).select (* projection )
7895 assert list (it ) == [{"foo" : {"bar" : 42 }}]
96+
97+
98+ def test_select_nested_objects_root_projection () -> None :
99+ expr = "$.a"
100+ data = {"a" : {"foo" : {"bar" : 42 }, "bar" : 7 }, "b" : 1 }
101+ projection = ("foo.bar" ,)
102+ it = jsonpath .query (expr , data ).select (* projection , projection = Projection .ROOT )
103+ assert list (it ) == [{"a" : {"foo" : {"bar" : 42 }}}]
104+
105+
106+ def test_select_nested_objects_flat_projection () -> None :
107+ expr = "$.a"
108+ data = {"a" : {"foo" : {"bar" : 42 }, "bar" : 7 }, "b" : 1 }
109+ projection = ("foo.bar" ,)
110+ it = jsonpath .query (expr , data ).select (* projection , projection = Projection .FLAT )
111+ assert list (it ) == [[42 ]]
0 commit comments