|
| 1 | +"""Test cases for the fluent API.""" |
| 2 | +import pytest |
| 3 | + |
| 4 | +from jsonpath import query |
| 5 | + |
| 6 | + |
| 7 | +def test_iter_query() -> None: |
| 8 | + """Test that `query` result is iterable, just like `finditer`.""" |
| 9 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}) |
| 10 | + for i, match in enumerate(it): |
| 11 | + assert match.value == i |
| 12 | + |
| 13 | + assert [m.obj for m in query("$.some.*", {"some": [0, 1, 2, 3]})] == [0, 1, 2, 3] |
| 14 | + |
| 15 | + |
| 16 | +def test_query_values() -> None: |
| 17 | + """Test that we can get an iterable of values from a query.""" |
| 18 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).values() |
| 19 | + assert list(it) == [0, 1, 2, 3] |
| 20 | + |
| 21 | + |
| 22 | +def test_query_locations() -> None: |
| 23 | + """Test that we can get an iterable of paths from a query.""" |
| 24 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).locations() |
| 25 | + assert list(it) == [ |
| 26 | + "$['some'][0]", |
| 27 | + "$['some'][1]", |
| 28 | + "$['some'][2]", |
| 29 | + "$['some'][3]", |
| 30 | + ] |
| 31 | + |
| 32 | + |
| 33 | +def test_query_items() -> None: |
| 34 | + """Test that we can get an iterable of values and paths from a query.""" |
| 35 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).items() |
| 36 | + assert list(it) == [ |
| 37 | + ("$['some'][0]", 0), |
| 38 | + ("$['some'][1]", 1), |
| 39 | + ("$['some'][2]", 2), |
| 40 | + ("$['some'][3]", 3), |
| 41 | + ] |
| 42 | + |
| 43 | + |
| 44 | +def test_query_skip() -> None: |
| 45 | + """Test that we can skip matches from the start of a query iterable.""" |
| 46 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).skip(2) |
| 47 | + matches = list(it) |
| 48 | + assert len(matches) == 2 # noqa: PLR2004 |
| 49 | + assert [m.obj for m in matches] == [2, 3] |
| 50 | + |
| 51 | + |
| 52 | +def test_query_skip_zero() -> None: |
| 53 | + """Test that we can skip zero matches from the start of a query iterable.""" |
| 54 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).skip(0) |
| 55 | + matches = list(it) |
| 56 | + assert len(matches) == 4 # noqa: PLR2004 |
| 57 | + assert [m.obj for m in matches] == [0, 1, 2, 3] |
| 58 | + |
| 59 | + |
| 60 | +def test_query_skip_negative() -> None: |
| 61 | + """Test that we get an exception when skipping a negative value.""" |
| 62 | + with pytest.raises(ValueError, match="can't drop a negative number of matches"): |
| 63 | + query("$.some.*", {"some": [0, 1, 2, 3]}).skip(-1) |
| 64 | + |
| 65 | + |
| 66 | +def test_query_skip_all() -> None: |
| 67 | + """Test that we can skip all matches from the start of a query iterable.""" |
| 68 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).skip(4) |
| 69 | + matches = list(it) |
| 70 | + assert len(matches) == 0 # noqa: PLR2004 |
| 71 | + assert [m.obj for m in matches] == [] |
| 72 | + |
| 73 | + |
| 74 | +def test_query_skip_more() -> None: |
| 75 | + """Test that we can skip more results than there are matches.""" |
| 76 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).skip(5) |
| 77 | + matches = list(it) |
| 78 | + assert len(matches) == 0 # noqa: PLR2004 |
| 79 | + assert [m.obj for m in matches] == [] |
| 80 | + |
| 81 | + |
| 82 | +def test_query_drop() -> None: |
| 83 | + """Test that we can skip matches with `drop`.""" |
| 84 | + it = query("$.some.*", {"some": [0, 1, 2, 3]}).drop(2) |
| 85 | + matches = list(it) |
| 86 | + assert len(matches) == 2 # noqa: PLR2004 |
| 87 | + assert [m.obj for m in matches] == [2, 3] |
0 commit comments