|
| 1 | +import io |
| 2 | +import json |
| 3 | +from unittest import TestCase, skipUnless |
| 4 | +import json_stream |
| 5 | + |
| 6 | +try: |
| 7 | + import json_stream_rs_tokenizer |
| 8 | + HAS_RS_TOKENIZER = hasattr(json_stream_rs_tokenizer, 'RustTokenizer') |
| 9 | +except ImportError: |
| 10 | + HAS_RS_TOKENIZER = False |
| 11 | + |
| 12 | +class TestBinaryResumption(TestCase): |
| 13 | + @skipUnless(HAS_RS_TOKENIZER, 'Rust tokenizer not available') |
| 14 | + def test_json_then_binary(self): |
| 15 | + json_header = json.dumps({"header": "info"}) |
| 16 | + binary_data = b'\x00\x01\x02\x03' |
| 17 | + test_data = json_header.encode('utf-8') + binary_data |
| 18 | + test_file = io.BytesIO(test_data) |
| 19 | + |
| 20 | + # Load with correct_cursor=True |
| 21 | + header = json_stream.load(test_file, correct_cursor=True) |
| 22 | + |
| 23 | + # Consume all data from header |
| 24 | + header.read_all() |
| 25 | + |
| 26 | + # Signal that we are done with JSON and want to resume binary read |
| 27 | + header.tokenizer.park_cursor() |
| 28 | + |
| 29 | + # Verify file cursor position |
| 30 | + self.assertEqual(test_file.tell(), len(json_header)) |
| 31 | + |
| 32 | + # Verify binary data |
| 33 | + remaining = test_file.read() |
| 34 | + self.assertEqual(remaining, binary_data) |
| 35 | + |
| 36 | + @skipUnless(HAS_RS_TOKENIZER, 'Rust tokenizer not available') |
| 37 | + def test_binary_then_json(self): |
| 38 | + binary_data = b'binary_start' |
| 39 | + json_data = b'{"a": 1}' |
| 40 | + test_data = binary_data + json_data |
| 41 | + test_file = io.BytesIO(test_data) |
| 42 | + |
| 43 | + # Read binary |
| 44 | + read_binary = test_file.read(len(binary_data)) |
| 45 | + self.assertEqual(read_binary, binary_data) |
| 46 | + |
| 47 | + # Load JSON |
| 48 | + data = json_stream.load(test_file) |
| 49 | + self.assertEqual(dict(data.items()), {"a": 1}) |
| 50 | + |
| 51 | + @skipUnless(HAS_RS_TOKENIZER, 'Rust tokenizer not available') |
| 52 | + def test_json_then_binary_then_json(self): |
| 53 | + json_1 = b'{"first": true}' |
| 54 | + binary_middle = b'middle_binary' |
| 55 | + json_2 = b'{"second": false}' |
| 56 | + test_data = json_1 + binary_middle + json_2 |
| 57 | + test_file = io.BytesIO(test_data) |
| 58 | + |
| 59 | + # Load first JSON |
| 60 | + data1 = json_stream.load(test_file, correct_cursor=True) |
| 61 | + self.assertEqual(dict(data1.items()), {"first": True}) |
| 62 | + data1.read_all() |
| 63 | + data1.tokenizer.park_cursor() |
| 64 | + self.assertEqual(test_file.tell(), len(json_1)) |
| 65 | + |
| 66 | + # Read middle binary |
| 67 | + read_middle = test_file.read(len(binary_middle)) |
| 68 | + self.assertEqual(read_middle, binary_middle) |
| 69 | + |
| 70 | + # Load second JSON |
| 71 | + data2 = json_stream.load(test_file) |
| 72 | + self.assertEqual(dict(data2.items()), {"second": False}) |
| 73 | + |
| 74 | + @skipUnless(HAS_RS_TOKENIZER, 'Rust tokenizer not available') |
| 75 | + def test_load_many_then_binary(self): |
| 76 | + json_1 = '{"a": 1}' |
| 77 | + json_2 = '{"b": 2}' |
| 78 | + binary_data = b'binary' |
| 79 | + test_data = json_1.encode('utf-8') + json_2.encode('utf-8') + binary_data |
| 80 | + |
| 81 | + test_file = io.BytesIO(test_data) |
| 82 | + |
| 83 | + loader = json_stream.load_many(test_file, correct_cursor=True) |
| 84 | + |
| 85 | + # Read first JSON |
| 86 | + doc1 = next(loader) |
| 87 | + doc1.read_all() |
| 88 | + |
| 89 | + # Read second JSON |
| 90 | + doc2 = next(loader) |
| 91 | + doc2.read_all() |
| 92 | + |
| 93 | + # Now park cursor |
| 94 | + doc2.tokenizer.park_cursor() |
| 95 | + |
| 96 | + self.assertEqual(test_file.tell(), len(json_1) + len(json_2)) |
| 97 | + self.assertEqual(test_file.read(), binary_data) |
0 commit comments