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

     1  package avro_test
     2  
     3  import (
     4  	"bytes"
     5  	"math/big"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hamba/avro"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestDecoder_NativeInvalidType(t *testing.T) {
    14  	defer ConfigTeardown()
    15  
    16  	data := []byte{0x01}
    17  	schema := "boolean"
    18  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    19  	assert.NoError(t, err)
    20  
    21  	var want *string
    22  	err = dec.Decode(&want)
    23  
    24  	assert.Error(t, err)
    25  }
    26  
    27  func TestDecoder_Bool(t *testing.T) {
    28  	defer ConfigTeardown()
    29  
    30  	data := []byte{0x01}
    31  	schema := "boolean"
    32  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    33  	assert.NoError(t, err)
    34  
    35  	var b bool
    36  	err = dec.Decode(&b)
    37  
    38  	assert.NoError(t, err)
    39  	assert.True(t, b)
    40  }
    41  
    42  func TestDecoder_BoolInvalidSchema(t *testing.T) {
    43  	defer ConfigTeardown()
    44  
    45  	data := []byte{0x01}
    46  	schema := "string"
    47  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    48  	assert.NoError(t, err)
    49  
    50  	var b bool
    51  	err = dec.Decode(&b)
    52  
    53  	assert.Error(t, err)
    54  }
    55  
    56  func TestDecoder_Int(t *testing.T) {
    57  	defer ConfigTeardown()
    58  
    59  	data := []byte{0x36}
    60  	schema := "int"
    61  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    62  	assert.NoError(t, err)
    63  
    64  	var i int
    65  	err = dec.Decode(&i)
    66  
    67  	assert.NoError(t, err)
    68  	assert.Equal(t, 27, i)
    69  }
    70  
    71  func TestDecoder_IntInvalidSchema(t *testing.T) {
    72  	defer ConfigTeardown()
    73  
    74  	data := []byte{0x36}
    75  	schema := "string"
    76  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    77  	assert.NoError(t, err)
    78  
    79  	var i int
    80  	err = dec.Decode(&i)
    81  
    82  	assert.Error(t, err)
    83  }
    84  
    85  func TestDecoder_Int8(t *testing.T) {
    86  	defer ConfigTeardown()
    87  
    88  	data := []byte{0x36}
    89  	schema := "int"
    90  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
    91  	assert.NoError(t, err)
    92  
    93  	var i int8
    94  	err = dec.Decode(&i)
    95  
    96  	assert.NoError(t, err)
    97  	assert.Equal(t, int8(27), i)
    98  }
    99  
   100  func TestDecoder_Int8InvalidSchema(t *testing.T) {
   101  	defer ConfigTeardown()
   102  
   103  	data := []byte{0x36}
   104  	schema := "string"
   105  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   106  	assert.NoError(t, err)
   107  
   108  	var i int8
   109  	err = dec.Decode(&i)
   110  
   111  	assert.Error(t, err)
   112  }
   113  
   114  func TestDecoder_Int16(t *testing.T) {
   115  	defer ConfigTeardown()
   116  
   117  	data := []byte{0x36}
   118  	schema := "int"
   119  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   120  	assert.NoError(t, err)
   121  
   122  	var i int16
   123  	err = dec.Decode(&i)
   124  
   125  	assert.NoError(t, err)
   126  	assert.Equal(t, int16(27), i)
   127  }
   128  
   129  func TestDecoder_Int16InvalidSchema(t *testing.T) {
   130  	defer ConfigTeardown()
   131  
   132  	data := []byte{0x36}
   133  	schema := "string"
   134  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   135  	assert.NoError(t, err)
   136  
   137  	var i int16
   138  	err = dec.Decode(&i)
   139  
   140  	assert.Error(t, err)
   141  }
   142  
   143  func TestDecoder_Int32(t *testing.T) {
   144  	defer ConfigTeardown()
   145  
   146  	data := []byte{0x36}
   147  	schema := "int"
   148  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   149  	assert.NoError(t, err)
   150  
   151  	var i int32
   152  	err = dec.Decode(&i)
   153  
   154  	assert.NoError(t, err)
   155  	assert.Equal(t, int32(27), i)
   156  }
   157  
   158  func TestDecoder_Int32InvalidSchema(t *testing.T) {
   159  	defer ConfigTeardown()
   160  
   161  	data := []byte{0x36}
   162  	schema := "string"
   163  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   164  	assert.NoError(t, err)
   165  
   166  	var i int32
   167  	err = dec.Decode(&i)
   168  
   169  	assert.Error(t, err)
   170  }
   171  
   172  func TestDecoder_Int64(t *testing.T) {
   173  	defer ConfigTeardown()
   174  
   175  	data := []byte{0x36}
   176  	schema := "long"
   177  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   178  	assert.NoError(t, err)
   179  
   180  	var i int64
   181  	err = dec.Decode(&i)
   182  
   183  	assert.NoError(t, err)
   184  	assert.Equal(t, int64(27), i)
   185  }
   186  
   187  func TestDecoder_Int64InvalidSchema(t *testing.T) {
   188  	defer ConfigTeardown()
   189  
   190  	data := []byte{0x36}
   191  	schema := "string"
   192  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   193  	assert.NoError(t, err)
   194  
   195  	var i int64
   196  	err = dec.Decode(&i)
   197  
   198  	assert.Error(t, err)
   199  }
   200  
   201  func TestDecoder_Float32(t *testing.T) {
   202  	defer ConfigTeardown()
   203  
   204  	data := []byte{0x33, 0x33, 0x93, 0x3F}
   205  	schema := "float"
   206  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   207  	assert.NoError(t, err)
   208  
   209  	var i float32
   210  	err = dec.Decode(&i)
   211  
   212  	assert.NoError(t, err)
   213  	assert.Equal(t, float32(1.15), i)
   214  }
   215  
   216  func TestDecoder_Float32InvalidSchema(t *testing.T) {
   217  	defer ConfigTeardown()
   218  
   219  	data := []byte{0x33, 0x33, 0x93, 0x3F}
   220  	schema := "string"
   221  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   222  	assert.NoError(t, err)
   223  
   224  	var i float32
   225  	err = dec.Decode(&i)
   226  
   227  	assert.Error(t, err)
   228  }
   229  
   230  func TestDecoder_Float64(t *testing.T) {
   231  	defer ConfigTeardown()
   232  
   233  	data := []byte{0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xF2, 0x3F}
   234  	schema := "double"
   235  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   236  	assert.NoError(t, err)
   237  
   238  	var i float64
   239  	err = dec.Decode(&i)
   240  
   241  	assert.NoError(t, err)
   242  	assert.Equal(t, float64(1.15), i)
   243  }
   244  
   245  func TestDecoder_Float64InvalidSchema(t *testing.T) {
   246  	defer ConfigTeardown()
   247  
   248  	data := []byte{0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xF2, 0x3F}
   249  	schema := "string"
   250  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   251  	assert.NoError(t, err)
   252  
   253  	var i float64
   254  	err = dec.Decode(&i)
   255  
   256  	assert.Error(t, err)
   257  }
   258  
   259  func TestDecoder_String(t *testing.T) {
   260  	defer ConfigTeardown()
   261  
   262  	data := []byte{0x06, 0x66, 0x6F, 0x6F}
   263  	schema := "string"
   264  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   265  	assert.NoError(t, err)
   266  
   267  	var str string
   268  	err = dec.Decode(&str)
   269  
   270  	assert.NoError(t, err)
   271  	assert.Equal(t, "foo", str)
   272  }
   273  
   274  func TestDecoder_StringInvalidSchema(t *testing.T) {
   275  	defer ConfigTeardown()
   276  
   277  	data := []byte{0x06, 0x66, 0x6F, 0x6F}
   278  	schema := "int"
   279  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   280  	assert.NoError(t, err)
   281  
   282  	var str string
   283  	err = dec.Decode(&str)
   284  
   285  	assert.Error(t, err)
   286  }
   287  
   288  func TestDecoder_Bytes(t *testing.T) {
   289  	defer ConfigTeardown()
   290  
   291  	data := []byte{0x08, 0xEC, 0xAB, 0x44, 0x00}
   292  	schema := "bytes"
   293  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   294  	assert.NoError(t, err)
   295  
   296  	var b []byte
   297  	err = dec.Decode(&b)
   298  
   299  	assert.NoError(t, err)
   300  	assert.Equal(t, []byte{0xEC, 0xAB, 0x44, 0x00}, b)
   301  }
   302  
   303  func TestDecoder_BytesInvalidSchema(t *testing.T) {
   304  	defer ConfigTeardown()
   305  
   306  	data := []byte{0x08, 0xEC, 0xAB, 0x44, 0x00}
   307  	schema := "int"
   308  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   309  	assert.NoError(t, err)
   310  
   311  	var b []byte
   312  	err = dec.Decode(&b)
   313  
   314  	assert.Error(t, err)
   315  }
   316  
   317  func TestDecoder_Time_Date(t *testing.T) {
   318  	defer ConfigTeardown()
   319  
   320  	data := []byte{0xCA, 0xAD, 0x2A}
   321  	schema := `{"type":"int","logicalType":"date"}`
   322  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   323  	assert.NoError(t, err)
   324  
   325  	var got time.Time
   326  	err = dec.Decode(&got)
   327  
   328  	assert.NoError(t, err)
   329  	assert.Equal(t, time.Date(2920, 1, 2, 0, 0, 0, 0, time.UTC), got)
   330  }
   331  
   332  func TestDecoder_Time_TimestampMillis(t *testing.T) {
   333  	defer ConfigTeardown()
   334  
   335  	data := []byte{0x90, 0xB2, 0xAE, 0xC3, 0xEC, 0x5B}
   336  	schema := `{"type":"long","logicalType":"timestamp-millis"}`
   337  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   338  	assert.NoError(t, err)
   339  
   340  	var got time.Time
   341  	err = dec.Decode(&got)
   342  
   343  	assert.NoError(t, err)
   344  	assert.Equal(t, time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC), got)
   345  }
   346  
   347  func TestDecoder_Time_TimestampMillisZero(t *testing.T) {
   348  	defer ConfigTeardown()
   349  
   350  	data := []byte{0xff, 0xdf, 0xe6, 0xa2, 0xe2, 0xa0, 0x1c}
   351  	schema := `{"type":"long","logicalType":"timestamp-millis"}`
   352  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   353  	assert.NoError(t, err)
   354  
   355  	var got time.Time
   356  	err = dec.Decode(&got)
   357  
   358  	assert.NoError(t, err)
   359  	assert.Equal(t, time.Time{}, got)
   360  }
   361  
   362  func TestDecoder_Time_TimestampMillisOneMillis(t *testing.T) {
   363  	defer ConfigTeardown()
   364  
   365  	data := []byte{0x02}
   366  	schema := `{"type":"long","logicalType":"timestamp-millis"}`
   367  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   368  	assert.NoError(t, err)
   369  
   370  	var got time.Time
   371  	err = dec.Decode(&got)
   372  
   373  	assert.NoError(t, err)
   374  	assert.Equal(t, time.Date(1970, 1, 1, 0, 0, 0, 1e6, time.UTC), got)
   375  }
   376  
   377  func TestDecoder_Time_TimestampMicros(t *testing.T) {
   378  	defer ConfigTeardown()
   379  
   380  	data := []byte{0x80, 0xCD, 0xB7, 0xA2, 0xEE, 0xC7, 0xCD, 0x05}
   381  	schema := `{"type":"long","logicalType":"timestamp-micros"}`
   382  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   383  	assert.NoError(t, err)
   384  
   385  	var got time.Time
   386  	err = dec.Decode(&got)
   387  
   388  	assert.NoError(t, err)
   389  	assert.Equal(t, time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC), got)
   390  }
   391  
   392  func TestDecoder_Time_TimestampMicrosZero(t *testing.T) {
   393  	defer ConfigTeardown()
   394  
   395  	data := []byte{0xff, 0xff, 0xdd, 0xf2, 0xdf, 0xff, 0xdf, 0xdc, 0x1}
   396  	schema := `{"type":"long","logicalType":"timestamp-micros"}`
   397  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   398  	assert.NoError(t, err)
   399  
   400  	var got time.Time
   401  	err = dec.Decode(&got)
   402  
   403  	assert.NoError(t, err)
   404  	assert.Equal(t, time.Time{}, got)
   405  }
   406  
   407  func TestDecoder_Time_TimestampMillisOneMicros(t *testing.T) {
   408  	defer ConfigTeardown()
   409  
   410  	data := []byte{0x02}
   411  	schema := `{"type":"long","logicalType":"timestamp-micros"}`
   412  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   413  	assert.NoError(t, err)
   414  
   415  	var got time.Time
   416  	err = dec.Decode(&got)
   417  
   418  	assert.NoError(t, err)
   419  	assert.Equal(t, time.Date(1970, 1, 1, 0, 0, 0, 1e3, time.UTC), got)
   420  }
   421  
   422  func TestDecoder_TimeInvalidSchema(t *testing.T) {
   423  	defer ConfigTeardown()
   424  
   425  	data := []byte{0x80, 0xCD, 0xB7, 0xA2, 0xEE, 0xC7, 0xCD, 0x05}
   426  	schema := `{"type":"long"}`
   427  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   428  	assert.NoError(t, err)
   429  
   430  	var got time.Time
   431  	err = dec.Decode(&got)
   432  
   433  	assert.Error(t, err)
   434  }
   435  
   436  func TestDecoder_Duration_TimeMillis(t *testing.T) {
   437  	defer ConfigTeardown()
   438  
   439  	data := []byte{0xAA, 0xB4, 0xDE, 0x75}
   440  	schema := `{"type":"int","logicalType":"time-millis"}`
   441  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   442  	assert.NoError(t, err)
   443  
   444  	var got time.Duration
   445  	err = dec.Decode(&got)
   446  
   447  	assert.NoError(t, err)
   448  	assert.Equal(t, 123456789*time.Millisecond, got)
   449  }
   450  
   451  func TestDecoder_Duration_TimeMicros(t *testing.T) {
   452  	defer ConfigTeardown()
   453  
   454  	data := []byte{0x86, 0xEA, 0xC8, 0xE9, 0x97, 0x07}
   455  	schema := `{"type":"long","logicalType":"time-micros"}`
   456  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   457  	assert.NoError(t, err)
   458  
   459  	var got time.Duration
   460  	err = dec.Decode(&got)
   461  
   462  	assert.NoError(t, err)
   463  	assert.Equal(t, 123456789123*time.Microsecond, got)
   464  }
   465  
   466  func TestDecoder_DurationInvalidSchema(t *testing.T) {
   467  	defer ConfigTeardown()
   468  
   469  	data := []byte{0x86, 0xEA, 0xC8, 0xE9, 0x97, 0x07}
   470  	schema := `{"type":"string"}`
   471  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   472  	assert.NoError(t, err)
   473  
   474  	var got time.Duration
   475  	err = dec.Decode(&got)
   476  
   477  	assert.Error(t, err)
   478  }
   479  
   480  func TestDecoder_BytesRat_Positive(t *testing.T) {
   481  	defer ConfigTeardown()
   482  
   483  	data := []byte{0x6, 0x00, 0x87, 0x78}
   484  	schema := `{"type":"bytes","logicalType":"decimal","precision":4,"scale":2}`
   485  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   486  	assert.NoError(t, err)
   487  
   488  	got := &big.Rat{}
   489  	err = dec.Decode(got)
   490  
   491  	assert.NoError(t, err)
   492  	assert.Equal(t, big.NewRat(1734, 5), got)
   493  }
   494  
   495  func TestDecoder_BytesRat_Negative(t *testing.T) {
   496  	defer ConfigTeardown()
   497  
   498  	data := []byte{0x6, 0xFF, 0x78, 0x88}
   499  	schema := `{"type":"bytes","logicalType":"decimal","precision":4,"scale":2}`
   500  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   501  	assert.NoError(t, err)
   502  
   503  	got := &big.Rat{}
   504  	err = dec.Decode(got)
   505  
   506  	assert.NoError(t, err)
   507  	assert.Equal(t, big.NewRat(-1734, 5), got)
   508  }
   509  
   510  func TestDecoder_BytesRat_Zero(t *testing.T) {
   511  	defer ConfigTeardown()
   512  
   513  	data := []byte{0x02, 0x00}
   514  	schema := `{"type":"bytes","logicalType":"decimal","precision":4,"scale":2}`
   515  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   516  	assert.NoError(t, err)
   517  
   518  	got := &big.Rat{}
   519  	err = dec.Decode(got)
   520  
   521  	assert.NoError(t, err)
   522  	assert.Equal(t, big.NewRat(0, 1), got)
   523  }
   524  
   525  func TestDecoder_BytesRatInvalidSchema(t *testing.T) {
   526  	defer ConfigTeardown()
   527  
   528  	data := []byte{0x02, 0x00}
   529  	schema := `{"type":"string"}`
   530  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   531  	assert.NoError(t, err)
   532  
   533  	got := &big.Rat{}
   534  	err = dec.Decode(got)
   535  
   536  	assert.Error(t, err)
   537  }
   538  
   539  func TestDecoder_BytesRatInvalidLogicalSchema(t *testing.T) {
   540  	defer ConfigTeardown()
   541  
   542  	data := []byte{0x02, 0x00}
   543  	schema := `{"type":"string","logicalType":"uuid"}`
   544  	dec, err := avro.NewDecoder(schema, bytes.NewReader(data))
   545  	assert.NoError(t, err)
   546  
   547  	got := &big.Rat{}
   548  	err = dec.Decode(got)
   549  
   550  	assert.Error(t, err)
   551  }