@@ -85,3 +85,95 @@ def test_query_drop() -> None:
8585 matches = list (it )
8686 assert len (matches ) == 2 # noqa: PLR2004
8787 assert [m .obj for m in matches ] == [2 , 3 ]
88+
89+
90+ def test_query_limit () -> None :
91+ """Test that we can limit the number of matches."""
92+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).limit (2 )
93+ matches = list (it )
94+ assert len (matches ) == 2 # noqa: PLR2004
95+ assert [m .obj for m in matches ] == [0 , 1 ]
96+
97+
98+ def test_query_limit_zero () -> None :
99+ """Test that we can call limit with zero."""
100+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).limit (0 )
101+ matches = list (it )
102+ assert len (matches ) == 0 # noqa: PLR2004
103+ assert [m .obj for m in matches ] == []
104+
105+
106+ def test_query_limit_more () -> None :
107+ """Test that we can give limit a number greater than the number of matches."""
108+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).limit (5 )
109+ matches = list (it )
110+ assert len (matches ) == 4 # noqa: PLR2004
111+ assert [m .obj for m in matches ] == [0 , 1 , 2 , 3 ]
112+
113+
114+ def test_query_limit_all () -> None :
115+ """Test limit is number of matches."""
116+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).limit (4 )
117+ matches = list (it )
118+ assert len (matches ) == 4 # noqa: PLR2004
119+ assert [m .obj for m in matches ] == [0 , 1 , 2 , 3 ]
120+
121+
122+ def test_query_limit_negative () -> None :
123+ """Test that we get an exception if limit is negative."""
124+ with pytest .raises (ValueError , match = "can't take a negative number of matches" ):
125+ query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).limit (- 1 )
126+
127+
128+ def test_query_take () -> None :
129+ """Test that we can limit the number of matches with `take`."""
130+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).take (2 )
131+ matches = list (it )
132+ assert len (matches ) == 2 # noqa: PLR2004
133+ assert [m .obj for m in matches ] == [0 , 1 ]
134+
135+
136+ def test_query_head () -> None :
137+ """Test that we can limit the number of matches with `head`."""
138+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).head (2 )
139+ matches = list (it )
140+ assert len (matches ) == 2 # noqa: PLR2004
141+ assert [m .obj for m in matches ] == [0 , 1 ]
142+
143+
144+ def test_query_tail () -> None :
145+ """Test that we can get the last _n_ matches."""
146+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).tail (2 )
147+ matches = list (it )
148+ assert len (matches ) == 2 # noqa: PLR2004
149+ assert [m .obj for m in matches ] == [2 , 3 ]
150+
151+
152+ def test_query_tail_zero () -> None :
153+ """Test that we can call `tail` with zero."""
154+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).tail (0 )
155+ matches = list (it )
156+ assert len (matches ) == 0 # noqa: PLR2004
157+ assert [m .obj for m in matches ] == []
158+
159+
160+ def test_query_tail_all () -> None :
161+ """Test tail is the same as the number of matches."""
162+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).tail (4 )
163+ matches = list (it )
164+ assert len (matches ) == 4 # noqa: PLR2004
165+ assert [m .obj for m in matches ] == [0 , 1 , 2 , 3 ]
166+
167+
168+ def test_query_tail_more () -> None :
169+ """Test tail is more than the number of matches."""
170+ it = query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).tail (5 )
171+ matches = list (it )
172+ assert len (matches ) == 4 # noqa: PLR2004
173+ assert [m .obj for m in matches ] == [0 , 1 , 2 , 3 ]
174+
175+
176+ def test_query_tail_negative () -> None :
177+ """Test that we get an exception if tail is given a negative integer."""
178+ with pytest .raises (ValueError , match = "can't select a negative number of matches" ):
179+ query ("$.some.*" , {"some" : [0 , 1 , 2 , 3 ]}).tail (- 1 )
0 commit comments