github.com/hamba/avro@v1.8.0/decoder_array_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_ArrayInvalidType(t *testing.T) {
    12  	defer ConfigTeardown()
    13  
    14  	data := []byte{0x04, 0x36, 0x38, 0x0}
    15  	schema := `{"type":"array", "items": "int"}`
    16  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    17  	assert.NoError(t, err)
    18  
    19  	var str string
    20  	err = dec.Decode(&str)
    21  
    22  	assert.Error(t, err)
    23  }
    24  
    25  func TestDecoder_ArraySlice(t *testing.T) {
    26  	defer ConfigTeardown()
    27  
    28  	data := []byte{0x04, 0x36, 0x38, 0x0}
    29  	schema := `{"type":"array", "items": "int"}`
    30  	dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
    31  
    32  	var got []int
    33  	err := dec.Decode(&got)
    34  
    35  	assert.NoError(t, err)
    36  	assert.Equal(t, []int{27, 28}, got)
    37  }
    38  
    39  func TestDecoder_ArraySliceOfStruct(t *testing.T) {
    40  	defer ConfigTeardown()
    41  
    42  	data := []byte{0x04, 0x36, 0x06, 0x66, 0x6f, 0x6f, 0x36, 0x06, 0x66, 0x6f, 0x6f, 0x0}
    43  	schema := `{"type":"array", "items": {"type": "record", "name": "test", "fields" : [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}}`
    44  	dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
    45  
    46  	var got []TestRecord
    47  	err := dec.Decode(&got)
    48  
    49  	assert.NoError(t, err)
    50  	assert.Equal(t, []TestRecord{{A: 27, B: "foo"}, {A: 27, B: "foo"}}, got)
    51  }
    52  
    53  func TestDecoder_ArraySliceError(t *testing.T) {
    54  	defer ConfigTeardown()
    55  
    56  	data := []byte{0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0xAD, 0xE2, 0xA2, 0xF3, 0xAD, 0xAD}
    57  	schema := `{"type":"array", "items": "int"}`
    58  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    59  	assert.NoError(t, err)
    60  
    61  	var got []int
    62  	err = dec.Decode(&got)
    63  
    64  	assert.Error(t, err)
    65  }