github.com/hamba/avro@v1.8.0/decoder_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 TestNewDecoder_SchemaError(t *testing.T) {
    12  	defer ConfigTeardown()
    13  
    14  	schema := "{}"
    15  	_, err := avro.NewDecoder(schema, nil)
    16  
    17  	assert.Error(t, err)
    18  }
    19  
    20  func TestDecoder_DecodeUnsupportedTypeError(t *testing.T) {
    21  	defer ConfigTeardown()
    22  
    23  	data := []byte{0x01}
    24  	schema := avro.NewPrimitiveSchema(avro.Type("test"), nil)
    25  	dec := avro.NewDecoderForSchema(schema, bytes.NewReader(data))
    26  
    27  	var b bool
    28  	err := dec.Decode(&b)
    29  
    30  	assert.Error(t, err)
    31  }
    32  
    33  func TestDecoder_DecodeEmptyReader(t *testing.T) {
    34  	defer ConfigTeardown()
    35  
    36  	data := []byte{}
    37  	schema := "boolean"
    38  	dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
    39  
    40  	var b bool
    41  	err := dec.Decode(b)
    42  
    43  	assert.Error(t, err)
    44  }
    45  
    46  func TestDecoder_DecodeNonPtr(t *testing.T) {
    47  	defer ConfigTeardown()
    48  
    49  	data := []byte{0x01}
    50  	schema := "boolean"
    51  	dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
    52  
    53  	var b bool
    54  	err := dec.Decode(b)
    55  
    56  	assert.Error(t, err)
    57  }
    58  
    59  func TestDecoder_DecodeNilPtr(t *testing.T) {
    60  	defer ConfigTeardown()
    61  
    62  	data := []byte{0x01}
    63  	schema := "boolean"
    64  	dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
    65  
    66  	err := dec.Decode((*bool)(nil))
    67  
    68  	assert.Error(t, err)
    69  }
    70  
    71  func TestDecoder_DecodeEOFDoesntReturnError(t *testing.T) {
    72  	defer ConfigTeardown()
    73  
    74  	data := []byte{0xE2}
    75  	schema := "int"
    76  	dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
    77  
    78  	var i int
    79  	err := dec.Decode(&i)
    80  
    81  	assert.NoError(t, err)
    82  }
    83  
    84  func TestUnmarshal(t *testing.T) {
    85  	defer ConfigTeardown()
    86  
    87  	schema := avro.MustParse("int")
    88  
    89  	var i int
    90  	err := avro.Unmarshal(schema, []byte{0xE2}, &i)
    91  
    92  	assert.NoError(t, err)
    93  }
    94  
    95  func TestUnmarshal_Ptr(t *testing.T) {
    96  	defer ConfigTeardown()
    97  
    98  	schema := avro.MustParse("boolean")
    99  
   100  	var b bool
   101  	err := avro.Unmarshal(schema, []byte{0x01}, b)
   102  
   103  	assert.Error(t, err)
   104  }
   105  
   106  func TestUnmarshal_NilPtr(t *testing.T) {
   107  	defer ConfigTeardown()
   108  
   109  	schema := avro.MustParse("boolean")
   110  
   111  	err := avro.Unmarshal(schema, []byte{0x01}, (*bool)(nil))
   112  
   113  	assert.Error(t, err)
   114  }