Skip to content

Commit f333823

Browse files
committed
More tests and minor fixes
1 parent 1b47d3e commit f333823

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

adafruit_json_stream.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def __getitem__(self, key):
197197
break
198198
if current_key == key:
199199
next_value = self.data.next_value(",")
200-
if self.data.last_char in [ord("}"), ord("]")]:
200+
if self.data.last_char == ord("}"):
201201
self.done = True
202202
if isinstance(next_value, Transient):
203203
self.active_child = next_value

tests/test_json_stream.py

+38-6
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,22 @@ def __init__(self, data=b"", chunk_size=10):
2222
self.chunks_read = 0
2323
self.data = data
2424
self.data_len = len(self.data)
25-
self.poition = 0
25+
self.position = 0
2626

2727
def __iter__(self):
2828
return self
2929

3030
def __next__(self):
31-
if self.poition > self.data_len:
31+
if self.position > self.data_len:
3232
raise StopIteration
3333

3434
end = self.chunk_size
35-
if self.poition + end > self.data_len:
35+
if self.position + end > self.data_len:
3636
end = self.data_len
37-
chunk = self.data[self.poition : self.poition + self.chunk_size]
37+
chunk = self.data[self.position : self.position + self.chunk_size]
3838

3939
self.chunks_read += 1
40-
self.poition += self.chunk_size
40+
self.position += self.chunk_size
4141

4242
return chunk
4343

@@ -54,6 +54,7 @@ def get_chunks_read(self):
5454
def dict_with_all_types():
5555
return """
5656
{
57+
"_check": "{\\\"a\\\": 1, \\\"b\\\": [2,3]}",
5758
"bool": true,
5859
"dict": {"key": "value"},
5960
"float": 1.1,
@@ -341,6 +342,28 @@ def test_complex_dict_passed_key_raises(complex_dict):
341342
stream["obects_id"]
342343

343344

345+
def test_complex_dict_passed_reference_raises(complex_dict):
346+
"""
347+
Test loading a complex dict and attempting to grab a data from a saved reference that has
348+
been passed raises.
349+
"""
350+
351+
assert json.loads(complex_dict)
352+
353+
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
354+
355+
list_1 = stream["list_1"]
356+
dict_1 = next(list_1)
357+
sub_dict = dict_1["sub_dict"]
358+
sub_list = dict_1["sub_list"]
359+
list_2 = stream["list_2"]
360+
next(list_2)
361+
with pytest.raises(KeyError, match="sub_dict_id"):
362+
sub_dict["sub_dict_id"]
363+
with pytest.raises(StopIteration):
364+
next(sub_list)
365+
366+
344367
# complex_dict is 1518 bytes
345368
@pytest.mark.parametrize(
346369
("chunk_size", "expected_chunks"), ((10, 152), (50, 31), (100, 16), (5000, 1))
@@ -456,7 +479,9 @@ def test_as_object_stream(dict_with_all_types):
456479

457480
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_all_types.encode()))
458481

459-
assert stream.as_object() == {
482+
obj = stream.as_object()
483+
assert obj == {
484+
"_check": '{"a": 1, "b": [2,3]}',
460485
"bool": True,
461486
"dict": {"key": "value"},
462487
"float": 1.1,
@@ -465,6 +490,13 @@ def test_as_object_stream(dict_with_all_types):
465490
"null": None,
466491
"string": "string",
467492
}
493+
assert json.loads(obj["_check"]) == {
494+
"a": 1,
495+
"b": [
496+
2,
497+
3,
498+
],
499+
}
468500

469501

470502
def test_as_object_that_is_partially_read_raises(complex_dict):

0 commit comments

Comments
 (0)