github.com/hamba/avro@v1.8.0/decoder_enum_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_EnumInvalidType(t *testing.T) {
    12  	defer ConfigTeardown()
    13  
    14  	data := []byte{0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0xAD}
    15  	schema := `{"type":"enum", "name": "test", "symbols": ["foo", "bar"]}`
    16  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    17  	assert.NoError(t, err)
    18  
    19  	var str int
    20  	err = dec.Decode(&str)
    21  
    22  	assert.Error(t, err)
    23  }
    24  
    25  func TestDecoder_Enum(t *testing.T) {
    26  	defer ConfigTeardown()
    27  
    28  	data := []byte{0x02}
    29  	schema := `{"type":"enum", "name": "test", "symbols": ["foo", "bar"]}`
    30  	dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
    31  
    32  	var got string
    33  	err := dec.Decode(&got)
    34  
    35  	assert.NoError(t, err)
    36  	assert.Equal(t, "bar", got)
    37  }
    38  
    39  func TestDecoder_EnumInvalidSymbol(t *testing.T) {
    40  	defer ConfigTeardown()
    41  
    42  	data := []byte{0x04}
    43  	schema := `{"type":"enum", "name": "test", "symbols": ["foo", "bar"]}`
    44  	dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
    45  
    46  	var got string
    47  	err := dec.Decode(&got)
    48  
    49  	assert.Error(t, err)
    50  }
    51  
    52  func TestDecoder_EnumError(t *testing.T) {
    53  	defer ConfigTeardown()
    54  
    55  	data := []byte{0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0xAD}
    56  	schema := `{"type":"enum", "name": "test", "symbols": ["foo", "bar"]}`
    57  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    58  	assert.NoError(t, err)
    59  
    60  	var got string
    61  	err = dec.Decode(&got)
    62  
    63  	assert.Error(t, err)
    64  }