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

Function test_polymorphic_map

python/pyfory/tests/xlang_test_main.py:709–753  ·  view source on GitHub ↗

Test polymorphic map serialization.

()

Source from the content-addressed store, hash-verified

707
708
709def test_polymorphic_map():
710 """Test polymorphic map serialization."""
711 data_file = get_data_file()
712 with open(data_file, "rb") as f:
713 data_bytes = f.read()
714 buffer = pyfory.Buffer(data_bytes)
715
716 fory = pyfory.Fory(xlang=True, compatible=True)
717 fory.register_type(Dog, type_id=302)
718 fory.register_type(Cat, type_id=303)
719 fory.register_type(AnimalMapHolder, type_id=305)
720
721 # Part 1: Read Map<String, Animal> with polymorphic values
722 animal_map = fory.deserialize(buffer)
723 assert len(animal_map) == 2
724
725 dog1 = animal_map.get("dog1")
726 assert isinstance(dog1, Dog)
727 assert dog1.name == "Rex"
728 assert dog1.age == 2
729
730 cat1 = animal_map.get("cat1")
731 assert isinstance(cat1, Cat)
732 assert cat1.lives == 9
733 assert cat1.age == 4
734
735 # Part 2: Read AnimalMapHolder
736 holder = fory.deserialize(buffer)
737 assert len(holder.animal_map) == 2
738
739 my_dog = holder.animal_map.get("myDog")
740 assert isinstance(my_dog, Dog)
741 assert my_dog.name == "Fido"
742
743 my_cat = holder.animal_map.get("myCat")
744 assert isinstance(my_cat, Cat)
745 assert my_cat.lives == 8
746
747 # Write back
748 new_buffer = pyfory.Buffer.allocate(256)
749 fory.serialize(animal_map, buffer=new_buffer)
750 fory.serialize(holder, buffer=new_buffer)
751
752 with open(data_file, "wb") as f:
753 f.write(new_buffer.get_bytes(0, new_buffer.get_writer_index()))
754
755
756def test_one_string_field_schema():

Callers

nothing calls this directly

Calls 11

register_typeMethod · 0.95
deserializeMethod · 0.95
serializeMethod · 0.95
get_bytesMethod · 0.80
get_data_fileFunction · 0.70
readMethod · 0.65
getMethod · 0.65
allocateMethod · 0.65
writeMethod · 0.65
BufferMethod · 0.45
ForyMethod · 0.45

Tested by

no test coverage detected