2020from copy import copy
2121from enum import Enum
2222from tempfile import TemporaryDirectory
23- from typing import Any
23+ from typing import Any , List
2424
2525import pytest
2626from fastavro import reader
2929from pyiceberg .io .pyarrow import PyArrowFileIO
3030from pyiceberg .manifest import DataFile , write_manifest
3131from pyiceberg .table import Table
32+ from pyiceberg .typedef import Record
3233from pyiceberg .utils .lazydict import LazyDict
3334
3435
3536# helper function to serialize our objects to dicts to enable
3637# direct comparison with the dicts returned by fastavro
37- def todict (obj : Any ) -> Any :
38+ def todict (obj : Any , spec_keys : List [str ]) -> Any :
39+ if type (obj ) is Record :
40+ return {key : obj [pos ] for key , pos in zip (spec_keys , range (len (obj )))}
3841 if isinstance (obj , dict ) or isinstance (obj , LazyDict ):
3942 data = []
4043 for k , v in obj .items ():
@@ -43,9 +46,13 @@ def todict(obj: Any) -> Any:
4346 elif isinstance (obj , Enum ):
4447 return obj .value
4548 elif hasattr (obj , "__iter__" ) and not isinstance (obj , str ) and not isinstance (obj , bytes ):
46- return [todict (v ) for v in obj ]
49+ return [todict (v , spec_keys ) for v in obj ]
4750 elif hasattr (obj , "__dict__" ):
48- return {key : todict (value ) for key , value in inspect .getmembers (obj ) if not callable (value ) and not key .startswith ("_" )}
51+ return {
52+ key : todict (value , spec_keys )
53+ for key , value in inspect .getmembers (obj )
54+ if not callable (value ) and not key .startswith ("_" )
55+ }
4956 else :
5057 return obj
5158
@@ -102,7 +109,7 @@ def test_write_sample_manifest(table_test_all_types: Table) -> None:
102109 )
103110 wrapped_entry_v2 = copy (entry )
104111 wrapped_entry_v2 .data_file = wrapped_data_file_v2_debug
105- wrapped_entry_v2_dict = todict (wrapped_entry_v2 )
112+ wrapped_entry_v2_dict = todict (wrapped_entry_v2 , [ field . name for field in test_spec . fields ] )
106113
107114 with TemporaryDirectory () as tmpdir :
108115 tmp_avro_file = tmpdir + "/test_write_manifest.avro"
0 commit comments