github.com/hamba/avro@v1.8.0/decoder_dynamic_test.go (about)

     1  package avro_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/hamba/avro"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestDecoder_Interface(t *testing.T) {
    12  	tests := []struct {
    13  		name   string
    14  		data   []byte
    15  		schema string
    16  		got    interface{}
    17  		want   interface{}
    18  	}{
    19  		{
    20  			name:   "Empty Interface",
    21  			data:   []byte{0x36, 0x06, 0x66, 0x6f, 0x6f},
    22  			schema: `{"type": "record", "name": "test", "fields" : [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}`,
    23  			got:    nil,
    24  			want:   map[string]interface{}{"a": int64(27), "b": "foo"},
    25  		},
    26  		{
    27  			name:   "Interface Non-Ptr",
    28  			data:   []byte{0x36, 0x06, 0x66, 0x6f, 0x6f},
    29  			schema: `{"type": "record", "name": "test", "fields": [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}`,
    30  			got:    TestRecord{},
    31  			want:   map[string]interface{}{"a": int64(27), "b": "foo"},
    32  		},
    33  		{
    34  			name:   "Interface Nil Ptr",
    35  			data:   []byte{0x36, 0x06, 0x66, 0x6f, 0x6f},
    36  			schema: `{"type": "record", "name": "test", "fields" : [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}`,
    37  			got:    (*TestRecord)(nil),
    38  			want:   &TestRecord{A: 27, B: "foo"},
    39  		},
    40  		{
    41  			name:   "Interface Ptr",
    42  			data:   []byte{0x36, 0x06, 0x66, 0x6f, 0x6f},
    43  			schema: `{"type": "record", "name": "test", "fields": [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}`,
    44  			got:    &TestRecord{},
    45  			want:   &TestRecord{A: 27, B: "foo"},
    46  		},
    47  	}
    48  
    49  	for _, tt := range tests {
    50  		t.Run(tt.name, func(t *testing.T) {
    51  			defer ConfigTeardown()
    52  
    53  			dec, _ := avro.NewDecoder(tt.schema, bytes.NewReader(tt.data))
    54  
    55  			err := dec.Decode(&tt.got)
    56  
    57  			assert.NoError(t, err)
    58  			assert.Equal(t, tt.want, tt.got)
    59  		})
    60  	}
    61  }