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

     1  package avro_test
     2  
     3  import (
     4  	"bytes"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/hamba/avro"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestDecoder_FixedInvalidType(t *testing.T) {
    13  	defer ConfigTeardown()
    14  
    15  	data := []byte{0x66, 0x6F, 0x6F, 0x66, 0x6F, 0x6F}
    16  	schema := `{"type":"fixed", "name": "test", "size": 6}`
    17  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    18  	assert.NoError(t, err)
    19  
    20  	var i [6]int
    21  	err = dec.Decode(&i)
    22  
    23  	assert.Error(t, err)
    24  }
    25  
    26  func TestDecoder_Fixed(t *testing.T) {
    27  	defer ConfigTeardown()
    28  
    29  	data := []byte{0x66, 0x6F, 0x6F, 0x66, 0x6F, 0x6F}
    30  	schema := `{"type":"fixed", "name": "test", "size": 6}`
    31  	dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))
    32  
    33  	var got [6]byte
    34  	err := dec.Decode(&got)
    35  
    36  	assert.NoError(t, err)
    37  	assert.Equal(t, [6]byte{'f', 'o', 'o', 'f', 'o', 'o'}, got)
    38  }
    39  
    40  func TestDecoder_FixedRat_Positive(t *testing.T) {
    41  	defer ConfigTeardown()
    42  
    43  	data := []byte{0x00, 0x00, 0x00, 0x00, 0x87, 0x78}
    44  	schema := `{"type":"fixed", "name": "test", "size": 6,"logicalType":"decimal","precision":4,"scale":2}`
    45  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    46  	assert.NoError(t, err)
    47  
    48  	got := &big.Rat{}
    49  	err = dec.Decode(got)
    50  
    51  	assert.NoError(t, err)
    52  	assert.Equal(t, big.NewRat(1734, 5), got)
    53  }
    54  
    55  func TestDecoder_FixedRat_Negative(t *testing.T) {
    56  	defer ConfigTeardown()
    57  
    58  	data := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0x78, 0x88}
    59  	schema := `{"type":"fixed", "name": "test", "size": 6, "logicalType":"decimal","precision":4,"scale":2}`
    60  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    61  	assert.NoError(t, err)
    62  
    63  	got := &big.Rat{}
    64  	err = dec.Decode(got)
    65  
    66  	assert.NoError(t, err)
    67  	assert.Equal(t, big.NewRat(-1734, 5), got)
    68  }
    69  
    70  func TestDecoder_FixedRat_Zero(t *testing.T) {
    71  	defer ConfigTeardown()
    72  
    73  	data := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    74  	schema := `{"type":"fixed", "name": "test", "size": 6,"logicalType":"decimal","precision":4,"scale":2}`
    75  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    76  	assert.NoError(t, err)
    77  
    78  	got := &big.Rat{}
    79  	err = dec.Decode(got)
    80  
    81  	assert.NoError(t, err)
    82  	assert.Equal(t, big.NewRat(0, 1), got)
    83  }
    84  
    85  func TestDecoder_FixedRatInvalidLogicalSchema(t *testing.T) {
    86  	defer ConfigTeardown()
    87  
    88  	data := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
    89  	schema := `{"type":"fixed", "name": "test", "size": 6}`
    90  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    91  	assert.NoError(t, err)
    92  
    93  	got := &big.Rat{}
    94  	err = dec.Decode(got)
    95  
    96  	assert.Error(t, err)
    97  }