github.com/vc42/parquet-go@v0.0.0-20240320194221-1a9adb5f23f5/convert_test.go (about)

     1  package parquet_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/vc42/parquet-go"
     8  )
     9  
    10  var conversionTests = [...]struct {
    11  	scenario string
    12  	from     interface{}
    13  	to       interface{}
    14  }{
    15  	{
    16  		scenario: "convert between rows which have the same schema",
    17  
    18  		from: AddressBook{
    19  			Owner: "Julien Le Dem",
    20  			OwnerPhoneNumbers: []string{
    21  				"555 123 4567",
    22  				"555 666 1337",
    23  			},
    24  			Contacts: []Contact{
    25  				{
    26  					Name:        "Dmitriy Ryaboy",
    27  					PhoneNumber: "555 987 6543",
    28  				},
    29  				{
    30  					Name: "Chris Aniszczyk",
    31  				},
    32  			},
    33  		},
    34  
    35  		to: AddressBook{
    36  			Owner: "Julien Le Dem",
    37  			OwnerPhoneNumbers: []string{
    38  				"555 123 4567",
    39  				"555 666 1337",
    40  			},
    41  			Contacts: []Contact{
    42  				{
    43  					Name:        "Dmitriy Ryaboy",
    44  					PhoneNumber: "555 987 6543",
    45  				},
    46  				{
    47  					Name: "Chris Aniszczyk",
    48  				},
    49  			},
    50  		},
    51  	},
    52  
    53  	{
    54  		scenario: "missing column",
    55  		from:     struct{ FirstName, LastName string }{FirstName: "Luke", LastName: "Skywalker"},
    56  		to:       struct{ LastName string }{LastName: "Skywalker"},
    57  	},
    58  
    59  	{
    60  		scenario: "missing optional column",
    61  		from: struct {
    62  			FirstName *string
    63  			LastName  string
    64  		}{FirstName: newString("Luke"), LastName: "Skywalker"},
    65  		to: struct{ LastName string }{LastName: "Skywalker"},
    66  	},
    67  
    68  	{
    69  		scenario: "missing repeated column",
    70  		from: struct {
    71  			ID    uint64
    72  			Names []string
    73  		}{ID: 42, Names: []string{"me", "myself", "I"}},
    74  		to: struct{ ID uint64 }{ID: 42},
    75  	},
    76  
    77  	{
    78  		scenario: "extra column",
    79  		from:     struct{ LastName string }{LastName: "Skywalker"},
    80  		to:       struct{ FirstName, LastName string }{LastName: "Skywalker"},
    81  	},
    82  
    83  	{
    84  		scenario: "extra optional column",
    85  		from:     struct{ ID uint64 }{ID: 2},
    86  		to: struct {
    87  			ID      uint64
    88  			Details *struct{ FirstName, LastName string }
    89  		}{ID: 2, Details: nil},
    90  	},
    91  
    92  	{
    93  		scenario: "extra repeated column",
    94  		from:     struct{ ID uint64 }{ID: 1},
    95  		to: struct {
    96  			ID    uint64
    97  			Names []string
    98  		}{ID: 1, Names: []string{}},
    99  	},
   100  }
   101  
   102  func TestConvert(t *testing.T) {
   103  	for _, test := range conversionTests {
   104  		t.Run(test.scenario, func(t *testing.T) {
   105  			to := parquet.SchemaOf(test.to)
   106  			from := parquet.SchemaOf(test.from)
   107  
   108  			conv, err := parquet.Convert(to, from)
   109  			if err != nil {
   110  				t.Fatal(err)
   111  			}
   112  
   113  			row := from.Deconstruct(nil, test.from)
   114  			row, err = conv.Convert(nil, row)
   115  			if err != nil {
   116  				t.Fatal(err)
   117  			}
   118  
   119  			value := reflect.New(reflect.TypeOf(test.to))
   120  			if err := to.Reconstruct(value.Interface(), row); err != nil {
   121  				t.Fatal(err)
   122  			}
   123  
   124  			value = value.Elem()
   125  			if !reflect.DeepEqual(value.Interface(), test.to) {
   126  				t.Errorf("converted value mismatch:\nwant = %+v\ngot  = %+v", test.to, value.Interface())
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func newString(s string) *string { return &s }