MCPcopy Create free account
hub / github.com/apache/fory / test_simple_struct

Function test_simple_struct

python/pyfory/tests/xlang_test_main.py:463–501  ·  view source on GitHub ↗

Test simple struct serialization.

()

Source from the content-addressed store, hash-verified

461
462
463def test_simple_struct():
464 """Test simple struct serialization."""
465 data_file = get_data_file()
466 with open(data_file, "rb") as f:
467 data_bytes = f.read()
468
469 fory = pyfory.Fory(xlang=True, compatible=True)
470 fory.register_type(Color, type_id=101)
471 fory.register_type(Item, type_id=102)
472 fory.register_type(SimpleStruct, type_id=103)
473
474 expected = SimpleStruct(
475 f1={1: 1.0, 2: 2.0},
476 f2=39,
477 f3=Item(name="item"),
478 f4="f4",
479 f5=Color.White,
480 f6=["f6"],
481 f7=40,
482 f8=41,
483 last=42,
484 )
485
486 debug_print(f"Java bytes length: {len(data_bytes)}")
487 debug_print(f"Java bytes (first 50): {data_bytes[:50].hex()}")
488
489 obj = fory.deserialize(data_bytes)
490 debug_print(f"Deserialized: {obj}")
491 assert obj == expected, f"Mismatch: {obj} != {expected}"
492
493 new_bytes = fory.serialize(obj)
494 debug_print(f"Python bytes length: {len(new_bytes)}")
495 debug_print(f"Python bytes (first 50): {new_bytes[:50].hex()}")
496 debug_print(f"Bytes match: {data_bytes == new_bytes}")
497 new_value = fory.deserialize(new_bytes)
498 assert new_value == expected, f"new_value: {new_value},\n expected: {expected}"
499
500 with open(data_file, "wb") as f:
501 f.write(new_bytes)
502
503
504def test_named_simple_struct():

Callers

nothing calls this directly

Calls 11

register_typeMethod · 0.95
deserializeMethod · 0.95
serializeMethod · 0.95
hexMethod · 0.80
get_data_fileFunction · 0.70
SimpleStructClass · 0.70
ItemClass · 0.70
debug_printFunction · 0.70
readMethod · 0.65
writeMethod · 0.65
ForyMethod · 0.45

Tested by

no test coverage detected