|
1 | | -from jsonpath import JSONPath |
| 1 | +import pytest |
| 2 | + |
| 3 | +from jsonpath import JSONPath, JSONPathTypeError |
2 | 4 |
|
3 | 5 |
|
4 | 6 | def test_issue_17(): |
@@ -64,3 +66,38 @@ def test_issue_15_bracket_notation_in_filter(): |
64 | 66 | # Case 2: Dot notation in filter (already working, but good to keep) |
65 | 67 | expr_dot = "$.tool.books.[*].properties.[*].[?(@.name=='moq')].value" |
66 | 68 | assert JSONPath(expr_dot).parse(data) == ["x", "q"] |
| 69 | + |
| 70 | + |
| 71 | +def test_issue_10_mixed_type_sorting(): |
| 72 | + # Case 1: Mixed types (int and str) - Should raise JSONPathTypeError |
| 73 | + data1 = [{"code": "N"}, {"code": 0}] |
| 74 | + with pytest.raises(JSONPathTypeError): |
| 75 | + JSONPath("$./(code)").parse(data1) |
| 76 | + |
| 77 | + # Case 2: Numbers (numerical sort) |
| 78 | + data2 = [{"code": 10}, {"code": 2}] |
| 79 | + result2 = JSONPath("$./(code)").parse(data2) |
| 80 | + assert result2 == [{"code": 2}, {"code": 10}] |
| 81 | + |
| 82 | + # Case 3: Strings (lexicographical sort) |
| 83 | + data3 = [{"code": "b"}, {"code": "a"}] |
| 84 | + result3 = JSONPath("$./(code)").parse(data3) |
| 85 | + assert result3 == [{"code": "a"}, {"code": "b"}] |
| 86 | + |
| 87 | + # Case 4: Strings that look like numbers (converted to numbers by _getattr) |
| 88 | + data4 = [{"code": "10"}, {"code": "2"}] |
| 89 | + result4 = JSONPath("$./(code)").parse(data4) |
| 90 | + # "2" -> 2, "10" -> 10. 2 < 10. |
| 91 | + assert result4 == [{"code": "2"}, {"code": "10"}] |
| 92 | + |
| 93 | + # Case 5: Mixed numbers and strings that look like numbers |
| 94 | + data5 = [{"code": 10}, {"code": "2"}] |
| 95 | + result5 = JSONPath("$./(code)").parse(data5) |
| 96 | + # "2" -> 2. 2 < 10. |
| 97 | + assert result5 == [{"code": "2"}, {"code": 10}] |
| 98 | + |
| 99 | + # Case 6: Missing keys (None) |
| 100 | + # None vs int comparison in Python 3 raises TypeError |
| 101 | + data6 = [{"code": 1}, {"other": 2}] |
| 102 | + with pytest.raises(JSONPathTypeError): |
| 103 | + JSONPath("$./(code)").parse(data6) |
0 commit comments