Skip to content

Commit 8df408f

Browse files
committed
added test for the partial key
1 parent b3a7a78 commit 8df408f

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

tests/test_parser_partial.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
from jsonparse.parser import Parser
2+
import pytest
3+
4+
class TestParserComplexJSON:
5+
6+
@pytest.fixture
7+
def parser(self):
8+
9+
return Parser(stack_trace=False, queue_trace=False)
10+
11+
@pytest.fixture
12+
def complex_json(self):
13+
14+
return [
15+
{
16+
"id": "0001",
17+
"type": "donut",
18+
"exists": True,
19+
"ppu": 0.55,
20+
"batters": {
21+
"batter": [
22+
{"id": "1001", "type": "Reg"},
23+
{"id": "1002", "type": "Chocolate"},
24+
{"id": "1003", "type": "Blueberry"},
25+
{"id": "1004", "type": "Devil's Food"},
26+
{"start": 5, "end": 8}
27+
]
28+
},
29+
"topping": [
30+
{"id": "5001", "ty": "None"},
31+
{"id": "5002", "type": "Glazed"},
32+
{"id": "5003", "type": "Sugar"},
33+
{"id": "5004", "type": "Powdered Sugar"},
34+
{"id": "5005", "type": "Chocolate with Sprinkles"},
35+
{"id": "5006", "type": "Chocolate"},
36+
{"id": "5007", "type": "Maple"}
37+
],
38+
"start": 22,
39+
"end": 99
40+
},
41+
{
42+
"id": "0002",
43+
"type": "donut",
44+
"exists": False,
45+
"ppu": 42,
46+
"batters": {
47+
"batter": [{"id": "1001", "type": "Rul"}]
48+
},
49+
"top_stuff": [
50+
{"id": "5001", "typ": "None"},
51+
{"id": "5002", "type": "Glazed"},
52+
{"id": "5003", "type": "Sugar"},
53+
{"id": "5004", "type": "Chocolate"},
54+
{"id": "5005", "type": "Maple"}
55+
],
56+
"start": 1,
57+
"end": 9
58+
},
59+
{
60+
"id": "0003",
61+
"type": "donut",
62+
"exists": None,
63+
"ppu": 7,
64+
"batters": {
65+
"batter": [
66+
{"id": "1001", "type": "Lar"},
67+
{"id": "1002", "type": "Chocolate"}
68+
]
69+
},
70+
"on_top_thing": [
71+
{"id": "5001", "type": "None"},
72+
{"id": "5002", "type": "Glazed"},
73+
{"id": "5003", "type": "Chocolate"},
74+
{"id": "5004", "type": "Maple"}
75+
],
76+
"start": 4,
77+
"end": 7
78+
}
79+
]
80+
81+
82+
def test_find_key_id(self, parser, complex_json):
83+
"""Find all 'id' keys."""
84+
result = parser.find_key(complex_json, "id", partial=False, case_sensitive=True)
85+
assert result == [
86+
'1001', '1002', '1003', '1004', '5001', '5002', '5003',
87+
'5004', '5005', '5006', '5007', '0001', '1001', '5001',
88+
'5002', '5003', '5004', '5005', '0002', '1001', '1002',
89+
'5001', '5002', '5003', '5004', '0003'
90+
]
91+
92+
def test_find_key_type(self, parser, complex_json):
93+
"""Find all 'type' keys."""
94+
result = parser.find_key(complex_json, "type", partial=False, case_sensitive=True)
95+
print(result)
96+
assert result == [
97+
'Reg', 'Chocolate', 'Blueberry', "Devil's Food", 'Glazed', 'Sugar', 'Powdered Sugar',
98+
'Chocolate with Sprinkles', 'Chocolate', 'Maple', 'donut', 'Rul', 'Glazed', 'Sugar',
99+
'Chocolate', 'Maple', 'donut', 'Lar', 'Chocolate', 'None', 'Glazed', 'Chocolate', 'Maple', 'donut'
100+
]
101+
102+
def test_find_key_start(self, parser, complex_json):
103+
"""Find all 'start' keys."""
104+
result = parser.find_key(complex_json, "start", partial=False, case_sensitive=True)
105+
assert result == [5, 22, 1, 4]
106+
107+
def test_find_key_partial(self, parser, complex_json):
108+
"""Find keys with partial match 'ty' (should match 'type' and 'typ' and 'ty')."""
109+
result = parser.find_key(complex_json, "ty", partial=True, case_sensitive=True)
110+
assert result == [
111+
'Reg', 'Chocolate', 'Blueberry', "Devil's Food", 'None', 'Glazed', 'Sugar', 'Powdered Sugar',
112+
'Chocolate with Sprinkles', 'Chocolate', 'Maple', 'donut', 'Rul', 'None', 'Glazed',
113+
'Sugar', 'Chocolate', 'Maple', 'donut', 'Lar', 'Chocolate', 'None',
114+
'Glazed', 'Chocolate', 'Maple', 'donut'
115+
]
116+
117+
def test_find_key_case_insensitive(self, parser, complex_json):
118+
"""Find keys case-insensitively."""
119+
result = parser.find_key(complex_json, "TYPE", partial=False, case_sensitive=False)
120+
assert all(isinstance(x,str) for x in result)
121+
assert len(result)>10
122+
123+
def test_find_key_nonexistent(self, parser, complex_json):
124+
"""Try to find a non-existent key."""
125+
result = parser.find_key(complex_json, "unknown_key", partial=False, case_sensitive=True)
126+
assert result == []
127+
128+
def test_invalid_data_type(self, parser, complex_json):
129+
"""Pass invalid data type."""
130+
try:
131+
parser.find_key("invalid_data", "id", partial=False, case_sensitive=True)
132+
except TypeError:
133+
assert True
134+
135+
def test_invalid_key_type(self, parser, complex_json):
136+
"""Pass non-string key."""
137+
try:
138+
parser.find_key(complex_json, 1234, partial=False, case_sensitive=True)
139+
except TypeError:
140+
assert True
141+
142+
143+
def test_empty_key(self, parser, complex_json):
144+
"""Pass empty key."""
145+
try:
146+
parser.find_key(complex_json, "", partial=False, case_sensitive=True)
147+
except ValueError:
148+
assert True
149+
150+
151+

0 commit comments

Comments
 (0)