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

Function test_data_class_serializer_xlang

python/pyfory/tests/test_struct.py:818–873  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

816
817
818def test_data_class_serializer_xlang():
819 fory = Fory(xlang=True, compatible=False, ref=True)
820 fory.register_type(ComplexObject, name="example.ComplexObject")
821 fory.register_type(DataClassObject, name="example.TestDataClassObject")
822
823 complex_data = ComplexObject(
824 f1="nested_str",
825 f5=100,
826 f8=3.14,
827 f10={10: 1.0, 20: 2.0},
828 )
829 obj_original = DataClassObject(
830 f_int=123,
831 f_float=45.67,
832 f_str="hello xlang",
833 f_bool=True,
834 f_list=[1, 2, 3, 4, 5],
835 f_dict={"a": 1.1, "b": 2.2},
836 f_any="any_value",
837 f_complex=complex_data,
838 )
839
840 obj_deserialized = ser_de(fory, obj_original)
841
842 assert obj_deserialized == obj_original
843 assert obj_deserialized.f_int == obj_original.f_int
844 assert obj_deserialized.f_float == obj_original.f_float
845 assert obj_deserialized.f_str == obj_original.f_str
846 assert obj_deserialized.f_bool == obj_original.f_bool
847 assert obj_deserialized.f_list == obj_original.f_list
848 assert obj_deserialized.f_dict == obj_original.f_dict
849 assert obj_deserialized.f_any == obj_original.f_any
850 assert obj_deserialized.f_complex == obj_original.f_complex
851 assert type(fory.type_resolver.get_serializer(DataClassObject)) is pyfory.DataClassSerializer
852 # Ensure it's using xlang mode indirectly, by checking no JIT methods if possible,
853 # or by ensuring it was registered with _register_xtype which now uses DataClassSerializer.
854 # For now, the registration path check is implicit via xlang=True usage.
855 # We can also check if the hash is non-zero if it was computed,
856 # or if the _serializers attribute exists.
857 serializer_instance = fory.type_resolver.get_serializer(DataClassObject)
858 assert hasattr(serializer_instance, "_serializers")
859 assert not hasattr(serializer_instance, "_xlang")
860
861 # Test with None for a complex field
862 obj_with_none_complex = DataClassObject(
863 f_int=789,
864 f_float=12.34,
865 f_str="another string",
866 f_bool=False,
867 f_list=[10, 20],
868 f_dict={"x": 7.7, "y": 8.8},
869 f_any=None,
870 f_complex=None,
871 )
872 obj_deserialized_none = ser_de(fory, obj_with_none_complex)
873 assert obj_deserialized_none == obj_with_none_complex
874
875

Callers

nothing calls this directly

Calls 6

register_typeMethod · 0.95
ForyClass · 0.90
ComplexObjectClass · 0.85
DataClassObjectClass · 0.85
get_serializerMethod · 0.80
ser_deFunction · 0.70

Tested by

no test coverage detected