Skip to content

Commit e2333a6

Browse files
authored
Fix JsonRecordPacker when bytes field is None (#180)
Als fixed DeprecationWarning: The default datetime adapter is deprecated as of Python 3.12.
1 parent 820113c commit e2333a6

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

flow/record/jsonpacker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def unpack_obj(self, obj: Any) -> RecordDescriptor | Record | Any:
9797
del obj["_type"]
9898
for field_type, field_name in record_descriptor.get_field_tuples():
9999
if field_type == "bytes":
100-
obj[field_name] = base64.b64decode(obj[field_name])
100+
value = obj[field_name]
101+
obj[field_name] = base64.b64decode(value) if value is not None else None
101102
return record_descriptor.recordType(**obj)
102103
if _type == "recorddescriptor":
103104
data = obj["_data"]

tests/adapter/test_sqlite_duckdb.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ def test_read_from_sqlite(tmp_path: Path, db: Database) -> None:
169169
"""
170170
)
171171
for i in range(1, 30):
172+
dt_isoformat = datetime(2023, 10, i, 13, 37, tzinfo=timezone.utc).isoformat()
172173
con.execute(
173174
"""
174175
INSERT INTO 'test/record' VALUES (?, ?, ?, ?)
175176
""",
176-
(f"record{i}", f"foobar{i}".encode(), datetime(2023, 10, i, 13, 37, tzinfo=timezone.utc), 3.14 + i),
177+
(f"record{i}", f"foobar{i}".encode(), dt_isoformat, 3.14 + i),
177178
)
178179

179180
# Read the SQLite database using flow.record

tests/packer/test_json_packer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,29 @@ def test_record_pack_surrogateescape() -> None:
112112

113113
# pack the json string back to a record and make sure it is the same as before
114114
assert packer.unpack(data) == record
115+
116+
117+
def test_json_packer_bytes_type() -> None:
118+
TestRecord = RecordDescriptor(
119+
"test/bytes",
120+
[
121+
("bytes", "data"),
122+
],
123+
)
124+
125+
packer = JsonRecordPacker()
126+
127+
record = TestRecord(b"hello world")
128+
data = packer.pack(record)
129+
assert data.startswith('{"data": "aGVsbG8gd29ybGQ="')
130+
assert packer.unpack(data) == record
131+
132+
record = TestRecord(data=None)
133+
data = packer.pack(record)
134+
assert data.startswith('{"data": null')
135+
assert packer.unpack(data) == record
136+
137+
record = TestRecord(data=b"")
138+
data = packer.pack(record)
139+
assert data.startswith('{"data": ""')
140+
assert packer.unpack(data) == record

0 commit comments

Comments
 (0)