|
1 | 1 | """Test cases for the fluent API.""" |
2 | 2 | import pytest |
3 | 3 |
|
| 4 | +from jsonpath import JSONPathMatch |
4 | 5 | from jsonpath import query |
5 | 6 |
|
6 | 7 |
|
@@ -141,6 +142,14 @@ def test_query_head() -> None: |
141 | 142 | assert [m.obj for m in matches] == [0, 1] |
142 | 143 |
|
143 | 144 |
|
| 145 | +def test_query_first() -> None: |
| 146 | + """Test that we can limit the number of matches with `first`.""" |
| 147 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).first(2) |
| 148 | + matches = list(it) |
| 149 | + assert len(matches) == 2 # noqa: PLR2004 |
| 150 | + assert [m.obj for m in matches] == [0, 1] |
| 151 | + |
| 152 | + |
144 | 153 | def test_query_tail() -> None: |
145 | 154 | """Test that we can get the last _n_ matches.""" |
146 | 155 | it = query("$.some.*", {"some": [0, 1, 2, 3]}).tail(2) |
@@ -177,3 +186,57 @@ def test_query_tail_negative() -> None: |
177 | 186 | """Test that we get an exception if tail is given a negative integer.""" |
178 | 187 | with pytest.raises(ValueError, match="can't select a negative number of matches"): |
179 | 188 | query("$.some.*", {"some": [0, 1, 2, 3]}).tail(-1) |
| 189 | + |
| 190 | + |
| 191 | +def test_query_last() -> None: |
| 192 | + """Test that we can get the last _n_ matches with `last`.""" |
| 193 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).last(2) |
| 194 | + matches = list(it) |
| 195 | + assert len(matches) == 2 # noqa: PLR2004 |
| 196 | + assert [m.obj for m in matches] == [2, 3] |
| 197 | + |
| 198 | + |
| 199 | +def test_query_first_one() -> None: |
| 200 | + """Test that we can get the first match from a query iterator.""" |
| 201 | + maybe_match = query("$.some.*", {"some": [0, 1, 2, 3]}).first_one() |
| 202 | + assert isinstance(maybe_match, JSONPathMatch) |
| 203 | + assert maybe_match.value == 0 |
| 204 | + |
| 205 | + |
| 206 | +def test_query_first_one_of_empty_iterator() -> None: |
| 207 | + """Test that `first_one` returns `None` if the iterator is empty.""" |
| 208 | + maybe_match = query("$.nosuchthing.*", {"some": [0, 1, 2, 3]}).first_one() |
| 209 | + assert maybe_match is None |
| 210 | + |
| 211 | + |
| 212 | +def test_query_one() -> None: |
| 213 | + """Test that we can get the first match from a query iterator with `one`.""" |
| 214 | + maybe_match = query("$.some.*", {"some": [0, 1, 2, 3]}).one() |
| 215 | + assert isinstance(maybe_match, JSONPathMatch) |
| 216 | + assert maybe_match.value == 0 |
| 217 | + |
| 218 | + |
| 219 | +def test_query_last_one() -> None: |
| 220 | + """Test that we can get the last match from a query iterator.""" |
| 221 | + maybe_match = query("$.some.*", {"some": [0, 1, 2, 3]}).last_one() |
| 222 | + assert isinstance(maybe_match, JSONPathMatch) |
| 223 | + assert maybe_match.value == 3 # noqa: PLR2004 |
| 224 | + |
| 225 | + |
| 226 | +def test_query_last_of_empty_iterator() -> None: |
| 227 | + """Test that `last_one` returns `None` if the iterator is empty.""" |
| 228 | + maybe_match = query("$.nosuchthing.*", {"some": [0, 1, 2, 3]}).last_one() |
| 229 | + assert maybe_match is None |
| 230 | + |
| 231 | + |
| 232 | +def test_query_tee() -> None: |
| 233 | + """Test that we can tee a query iterator.""" |
| 234 | + it1, it2 = query("$.some.*", {"some": [0, 1, 2, 3]}).tee() |
| 235 | + |
| 236 | + rv1 = it1.skip(1).one() |
| 237 | + assert rv1 is not None |
| 238 | + assert rv1.value == 1 |
| 239 | + |
| 240 | + rv2 = it2.skip(2).one() |
| 241 | + assert rv2 is not None |
| 242 | + assert rv2.value == 2 # noqa: PLR2004 |
0 commit comments