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