github.com/hamba/avro@v1.8.0/encoder_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 TestEncoder_EnumInvalidType(t *testing.T) {
    12  	defer ConfigTeardown()
    13  
    14  	schema := `{"type":"enum", "name": "test", "symbols": ["foo", "bar"]}`
    15  	buf := bytes.NewBuffer([]byte{})
    16  	enc, err := avro.NewEncoder(schema, buf)
    17  	assert.NoError(t, err)
    18  
    19  	err = enc.Encode(27)
    20  
    21  	assert.Error(t, err)
    22  }
    23  
    24  func TestEncoder_Enum(t *testing.T) {
    25  	defer ConfigTeardown()
    26  
    27  	schema := `{"type":"enum", "name": "test", "symbols": ["foo", "bar"]}`
    28  	buf := bytes.NewBuffer([]byte{})
    29  	enc, err := avro.NewEncoder(schema, buf)
    30  	assert.NoError(t, err)
    31  
    32  	err = enc.Encode("bar")
    33  
    34  	assert.NoError(t, err)
    35  	assert.Equal(t, []byte{0x02}, buf.Bytes())
    36  }
    37  
    38  func TestEncoder_EnumInvalidSymbol(t *testing.T) {
    39  	defer ConfigTeardown()
    40  
    41  	schema := `{"type":"enum", "name": "test", "symbols": ["foo", "bar"]}`
    42  	buf := bytes.NewBuffer([]byte{})
    43  	enc, err := avro.NewEncoder(schema, buf)
    44  	assert.NoError(t, err)
    45  
    46  	err = enc.Encode("baz")
    47  
    48  	assert.Error(t, err)
    49  }