forked from adafruit/Adafruit_CircuitPython_JSON_Stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_json_stream.py
762 lines (591 loc) · 19.9 KB
/
test_json_stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# SPDX-FileCopyrightText: 2025 Justin Myers
#
# SPDX-License-Identifier: Unlicense
import json
import math
import pytest
import adafruit_json_stream
# pylint: disable=invalid-name,pointless-statement,redefined-outer-name
# ---------------
# Helpers
# ---------------
class BytesChunkIO:
def __init__(self, data=b"", chunk_size=10):
self.chunk_size = chunk_size
self.chunks_read = 0
self.data = data
self.data_len = len(self.data)
self.position = 0
def __iter__(self):
return self
def __next__(self):
if self.position > self.data_len:
raise StopIteration
end = self.chunk_size
if self.position + end > self.data_len:
end = self.data_len
chunk = self.data[self.position : self.position + self.chunk_size]
self.chunks_read += 1
self.position += self.chunk_size
return chunk
def get_chunks_read(self):
return self.chunks_read
# ---------------
# Fixtures
# ---------------
@pytest.fixture
def dict_with_all_types():
return """
{
"_check": "{\\\"a\\\": 1, \\\"b\\\": [2,3]}",
"bool": true,
"dict": {"key": "value"},
"float": 1.1,
"int": 1,
"list": [1,2,3],
"null": null,
"string": "string"
}
"""
@pytest.fixture
def list_with_bad_strings():
return r"""
[
"\"}\"",
"{\"a\": 1, \"b\": [2,3]}",
"\"",
"\\\"",
"\\\\\"",
"\\x40\"",
"[[[{{{",
"]]]}}}"
]
"""
@pytest.fixture
def dict_with_bad_strings():
return r"""
{
"1": "\"}\"",
"2": "{\"a\": 1, \"b\": [2,3]}",
"3": "\"",
"4": "\\\"",
"5": "\\\\\"",
"6": "\\x40\"",
"7": "[[[{{{",
"8": "]]]}}}"
}
"""
@pytest.fixture
def list_with_values():
return """
[
1,
2,
3
]
"""
@pytest.fixture
def dict_with_keys():
return """
{
"field_1": 1,
"field_2": 2,
"field_3": 3
}
"""
@pytest.fixture
def dict_with_list_with_single_entries():
return """
{
"list_1": [
{
"dict_id": 1
},
{
"dict_id": 2
},
{
"dict_id": 3
},
{
"dict_id": 4
}
]
}
"""
@pytest.fixture
def complex_dict():
return """
{
"list_1": [
{
"dict_id": 1,
"dict_name": "one",
"sub_dict": {
"sub_dict_id": 1.1,
"sub_dict_name": "one point one"
},
"sub_list": [
"a",
"b",
"c"
]
},
{
"dict_id": 2,
"dict_name": "two",
"sub_dict": {
"sub_dict_id": 2.1,
"sub_dict_name": "two point one"
},
"sub_list": [
"d",
"e",
"f"
]
}
],
"list_2": [
{
"dict_id": 3,
"dict_name": "three",
"sub_dict": {
"sub_dict_id": 3.1,
"sub_dict_name": "three point one"
},
"sub_list": [
"g",
"h",
"i"
]
},
{
"dict_id": 4,
"dict_name": "four",
"sub_dict": {
"sub_dict_id": 4.1,
"sub_dict_name": "four point one"
},
"sub_list": [
"j",
"k",
"l"
]
}
]
}
"""
# ---------------
# Tests
# ---------------
def test_all_types(dict_with_all_types):
"""Test loading a simple dict all data types."""
assert json.loads(dict_with_all_types)
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_all_types.encode()))
assert stream["bool"] is True
assert stream["dict"]["key"] == "value"
assert stream["float"] == 1.1
assert stream["int"] == 1
assert next(stream["list"]) == 1
assert stream["null"] is None
assert stream["string"] == "string"
def test_simple_dict_with_keys(dict_with_keys):
"""Test loading a simple dict with keys."""
assert json.loads(dict_with_keys)
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_keys.encode()))
for i in range(1, 4):
assert stream[f"field_{i}"] == i
with pytest.raises(KeyError, match="field_4"):
stream["field_4"]
def test_simple_dict_with_grabbing_key_twice_raises(dict_with_keys):
"""Test loading a simple dict with keys twice raises."""
assert json.loads(dict_with_keys)
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_keys.encode()))
assert stream["field_1"] == 1
with pytest.raises(KeyError, match="field_1"):
stream["field_1"]
def test_simple_dict_with_keys_middle_key(dict_with_keys):
"""Test loading a simple dict and grabbing a key in the middle."""
assert json.loads(dict_with_keys)
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_keys.encode()))
assert stream["field_2"] == 2
def test_simple_dict_with_keys_missing_key_raises(dict_with_keys):
"""Test loading a simple dict and grabbing a key that doesn't exist raises."""
assert json.loads(dict_with_keys)
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_keys.encode()))
with pytest.raises(KeyError, match="field_4"):
stream["field_4"]
def test_list_with_values(list_with_values):
"""Test loading a list and iterating over it."""
assert json.loads(list_with_values)
stream = adafruit_json_stream.load(BytesChunkIO(list_with_values.encode()))
counter = 0
for value in stream:
counter += 1
assert value == counter
def test_dict_with_list_of_single_entries(dict_with_list_with_single_entries):
"""Test loading an dict with a list of dicts with one entry each."""
assert json.loads(dict_with_list_with_single_entries)
stream = adafruit_json_stream.load(
BytesChunkIO(dict_with_list_with_single_entries.encode())
)
counter = 0
for obj in stream["list_1"]:
counter += 1
assert obj["dict_id"] == counter
assert counter == 4
def test_complex_dict(complex_dict):
"""Test loading a complex dict."""
assert json.loads(complex_dict)
dict_names = [
"one",
"two",
"three",
"four",
]
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
counter = 0
sub_counter = 0
for obj in stream["list_1"]:
counter += 1
assert obj["dict_id"] == counter
assert obj["dict_name"] == dict_names[counter - 1]
sub_dict = obj["sub_dict"]
assert sub_dict["sub_dict_id"] == counter + 0.1
assert sub_dict["sub_dict_name"] == f"{dict_names[counter-1]} point one"
for item in obj["sub_list"]:
sub_counter += 1
assert item == chr(96 + sub_counter)
assert counter == 2
assert sub_counter == 6
for obj in stream["list_2"]:
counter += 1
assert obj["dict_id"] == counter
assert obj["dict_name"] == dict_names[counter - 1]
sub_dict = obj["sub_dict"]
assert sub_dict["sub_dict_id"] == counter + 0.1
assert sub_dict["sub_dict_name"] == f"{dict_names[counter-1]} point one"
for item in obj["sub_list"]:
sub_counter += 1
assert item == chr(96 + sub_counter)
assert counter == 4
assert sub_counter == 12
def test_bad_strings_in_list(list_with_bad_strings):
"""Test loading different strings that can confuse the parser."""
bad_strings = [
'"}"',
'{"a": 1, "b": [2,3]}',
'"',
'\\"',
'\\\\"',
'\\x40"',
"[[[{{{",
"]]]}}}",
]
assert json.loads(list_with_bad_strings)
# get each separately
stream = adafruit_json_stream.load(BytesChunkIO(list_with_bad_strings.encode()))
for i, item in enumerate(stream):
assert item == bad_strings[i]
def test_bad_strings_in_list_iter(list_with_bad_strings):
"""Test loading different strings that can confuse the parser."""
bad_strings = [
'"}"',
'{"a": 1, "b": [2,3]}',
'"',
'\\"',
'\\\\"',
'\\x40"',
"[[[{{{",
"]]]}}}",
]
assert json.loads(list_with_bad_strings)
# get each separately
stream = adafruit_json_stream.load(BytesChunkIO(list_with_bad_strings.encode()))
for i, item in enumerate(stream):
assert item == bad_strings[i]
def test_bad_strings_in_dict_as_object(dict_with_bad_strings):
"""Test loading different strings that can confuse the parser."""
bad_strings = {
"1": '"}"',
"2": '{"a": 1, "b": [2,3]}',
"3": '"',
"4": '\\"',
"5": '\\\\"',
"6": '\\x40"',
"7": "[[[{{{",
"8": "]]]}}}",
}
# read all at once
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_bad_strings.encode()))
assert stream.as_object() == bad_strings
def test_bad_strings_in_dict_all_keys(dict_with_bad_strings):
"""Test loading different strings that can confuse the parser."""
bad_strings = {
"1": '"}"',
"2": '{"a": 1, "b": [2,3]}',
"3": '"',
"4": '\\"',
"5": '\\\\"',
"6": '\\x40"',
"7": "[[[{{{",
"8": "]]]}}}",
}
# read one after the other with keys
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_bad_strings.encode()))
assert stream["1"] == bad_strings["1"]
assert stream["2"] == bad_strings["2"]
assert stream["3"] == bad_strings["3"]
assert stream["4"] == bad_strings["4"]
assert stream["5"] == bad_strings["5"]
assert stream["6"] == bad_strings["6"]
assert stream["7"] == bad_strings["7"]
assert stream["8"] == bad_strings["8"]
def test_bad_strings_in_dict_skip_some(dict_with_bad_strings):
"""Test loading different strings that can confuse the parser."""
bad_strings = {
"1": '"}"',
"2": '{"a": 1, "b": [2,3]}',
"3": '"',
"4": '\\"',
"5": '\\\\"',
"6": '\\x40"',
"7": "[[[{{{",
"8": "]]]}}}",
}
# read some, skip some
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_bad_strings.encode()))
assert stream["2"] == bad_strings["2"]
assert stream["5"] == bad_strings["5"]
assert stream["8"] == bad_strings["8"]
def test_complex_dict_grabbing(complex_dict):
"""Test loading a complex dict and grabbing specific keys."""
assert json.loads(complex_dict)
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
list_1 = stream["list_1"]
dict_1 = next(list_1)
sub_list = dict_1["sub_list"]
assert next(sub_list) == "a"
list_2 = stream["list_2"]
next(list_2)
dict_2 = next(list_2)
sub_list = dict_2["sub_list"]
assert next(sub_list) == "j"
def test_complex_dict_passed_key_raises(complex_dict):
"""
Test loading a complex dict and attempting to grab a specific key that has been passed raises.
"""
assert json.loads(complex_dict)
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
list_1 = stream["list_1"]
dict_1 = next(list_1)
assert dict_1["dict_name"] == "one"
with pytest.raises(KeyError, match="obects_id"):
stream["obects_id"]
def test_complex_dict_passed_reference_raises(complex_dict):
"""
Test loading a complex dict and attempting to grab a data from a saved reference that has
been passed raises.
"""
assert json.loads(complex_dict)
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
list_1 = stream["list_1"]
dict_1 = next(list_1)
sub_dict = dict_1["sub_dict"]
sub_list = dict_1["sub_list"]
list_2 = stream["list_2"]
next(list_2)
with pytest.raises(KeyError, match="sub_dict_id"):
sub_dict["sub_dict_id"]
with pytest.raises(StopIteration):
next(sub_list)
# complex_dict is 1518 bytes
@pytest.mark.parametrize(
("chunk_size", "expected_chunks"), ((10, 152), (50, 31), (100, 16), (5000, 1))
)
def test_complex_dict_buffer_sizes(chunk_size, complex_dict, expected_chunks):
"""Test loading a complex dict and checking the chunking."""
assert json.loads(complex_dict)
bytes_io_chunk = BytesChunkIO(complex_dict.encode(), chunk_size)
stream = adafruit_json_stream.load(bytes_io_chunk)
list_1 = stream["list_1"]
dict_1 = next(list_1)
sub_list = dict_1["sub_list"]
assert next(sub_list) == "a"
list_2 = stream["list_2"]
next(list_2)
dict_2 = next(list_2)
sub_list = dict_2["sub_list"]
assert next(sub_list) == "j"
for _ in sub_list:
pass
with pytest.raises(KeyError):
stream["list_3"]
assert bytes_io_chunk.get_chunks_read() == expected_chunks
assert math.ceil(len(complex_dict) / chunk_size) == expected_chunks
# complex_dict is 1518 bytes
@pytest.mark.parametrize(
("chunk_size", "expected_chunks"), ((5, 61), (10, 31), (50, 7), (100, 4))
)
def test_complex_dict_not_looking_at_all_data_buffer_sizes(
chunk_size, complex_dict, expected_chunks
):
"""Test loading a complex dict and checking the chunking."""
assert json.loads(complex_dict)
bytes_io_chunk = BytesChunkIO(complex_dict.encode(), chunk_size)
stream = adafruit_json_stream.load(bytes_io_chunk)
list_1 = stream["list_1"]
dict_1 = next(list_1)
sub_list = dict_1["sub_list"]
assert next(sub_list) == "a"
assert bytes_io_chunk.get_chunks_read() == expected_chunks
assert math.ceil(len(complex_dict) / chunk_size) >= (expected_chunks / 4)
def test_incomplete_json_raises():
"""Test incomplete json raises."""
data = """
{
"field_1": 1
"""
with pytest.raises(json.JSONDecodeError):
json.loads(data)
stream = adafruit_json_stream.load(BytesChunkIO(data.encode()))
with pytest.raises(EOFError):
stream["field_2"]
def test_as_object(complex_dict):
"""Test loading a complex dict and grabbing parts as objects."""
assert json.loads(complex_dict)
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
list_1 = stream["list_1"]
dict_1 = next(list_1)
assert dict_1["sub_dict"].as_object() == {
"sub_dict_id": 1.1,
"sub_dict_name": "one point one",
}
assert dict_1["sub_list"].as_object() == ["a", "b", "c"]
dict_2 = next(list_1)
assert dict_2.as_object() == {
"dict_id": 2,
"dict_name": "two",
"sub_dict": {"sub_dict_id": 2.1, "sub_dict_name": "two point one"},
"sub_list": ["d", "e", "f"],
}
assert stream["list_2"].as_object() == [
{
"dict_id": 3,
"dict_name": "three",
"sub_dict": {"sub_dict_id": 3.1, "sub_dict_name": "three point one"},
"sub_list": ["g", "h", "i"],
},
{
"dict_id": 4,
"dict_name": "four",
"sub_dict": {"sub_dict_id": 4.1, "sub_dict_name": "four point one"},
"sub_list": ["j", "k", "l"],
},
]
def test_as_object_stream(dict_with_all_types):
assert json.loads(dict_with_all_types)
stream = adafruit_json_stream.load(BytesChunkIO(dict_with_all_types.encode()))
obj = stream.as_object()
assert obj == {
"_check": '{"a": 1, "b": [2,3]}',
"bool": True,
"dict": {"key": "value"},
"float": 1.1,
"int": 1,
"list": [1, 2, 3],
"null": None,
"string": "string",
}
assert json.loads(obj["_check"]) == {
"a": 1,
"b": [
2,
3,
],
}
def test_as_object_that_is_partially_read_raises(complex_dict):
"""Test loading a complex dict and grabbing partially read raises."""
assert json.loads(complex_dict)
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
list_1 = stream["list_1"]
dict_1 = next(list_1)
assert dict_1["dict_id"] == 1
with pytest.raises(BufferError):
dict_1.as_object()
def test_as_object_grabbing_multiple_subscriptable_levels_twice(complex_dict):
"""Test loading a complex dict and grabbing multiple subscriptable levels twice."""
assert json.loads(complex_dict)
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
list_1 = stream["list_1"]
dict_1 = next(list_1)
assert dict_1["sub_dict"]["sub_dict_id"] == 1.1
assert dict_1["sub_dict"]["sub_dict_name"] == "one point one"
def test_as_object_grabbing_multiple_subscriptable_levels_again_after_passed_raises(
complex_dict,
):
"""
Test loading a complex dict and grabbing multiple subscriptable levels after passing it raises.
"""
assert json.loads(complex_dict)
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
list_1 = stream["list_1"]
dict_1 = next(list_1)
assert dict_1["sub_dict"]["sub_dict_id"] == 1.1
assert next(dict_1["sub_list"]) == "a"
with pytest.raises(KeyError, match="sub_dict"):
dict_1["sub_dict"]["sub_dict_name"]
def test_iterating_keys(dict_with_keys):
"""Iterate through keys of a simple object."""
bytes_io_chunk = BytesChunkIO(dict_with_keys.encode())
stream = adafruit_json_stream.load(bytes_io_chunk)
output = list(stream)
assert output == ["field_1", "field_2", "field_3"]
def test_iterating_keys_get(dict_with_keys):
"""Iterate through keys of a simple object and get values."""
the_dict = json.loads(dict_with_keys)
bytes_io_chunk = BytesChunkIO(dict_with_keys.encode())
stream = adafruit_json_stream.load(bytes_io_chunk)
for key in stream:
value = stream[key]
assert value == the_dict[key]
def test_iterating_items(dict_with_keys):
"""Iterate through items of a simple object."""
bytes_io_chunk = BytesChunkIO(dict_with_keys.encode())
stream = adafruit_json_stream.load(bytes_io_chunk)
output = list(stream.items())
assert output == [("field_1", 1), ("field_2", 2), ("field_3", 3)]
def test_iterating_keys_after_get(dict_with_keys):
"""Iterate through keys of a simple object after an item has already been read."""
bytes_io_chunk = BytesChunkIO(dict_with_keys.encode())
stream = adafruit_json_stream.load(bytes_io_chunk)
assert stream["field_1"] == 1
output = list(stream)
assert output == ["field_2", "field_3"]
def test_iterating_items_after_get(dict_with_keys):
"""Iterate through items of a simple object after an item has already been read."""
bytes_io_chunk = BytesChunkIO(dict_with_keys.encode())
stream = adafruit_json_stream.load(bytes_io_chunk)
assert stream["field_1"] == 1
output = list(stream.items())
assert output == [("field_2", 2), ("field_3", 3)]
def test_iterating_complex_dict(complex_dict):
"""Mix iterating over items of objects in objects in arrays."""
names = ["one", "two", "three", "four"]
sub_values = [None, "two point one", "three point one", None]
stream = adafruit_json_stream.load(BytesChunkIO(complex_dict.encode()))
thing_num = 0
for (index, item) in enumerate(stream.items()):
key, a_list = item
assert key == f"list_{index+1}"
for thing in a_list:
assert thing["dict_name"] == names[thing_num]
for sub_key in thing["sub_dict"]:
# break after getting a key with or without the value
# (testing finish() called from the parent list)
if sub_key == "sub_dict_name":
if thing_num in {1, 2}:
value = thing["sub_dict"][sub_key]
assert value == sub_values[thing_num]
break
thing_num += 1