github.com/hamba/avro@v1.8.0/writer_test.go (about) 1 package avro_test 2 3 import ( 4 "bytes" 5 "errors" 6 "testing" 7 8 "github.com/hamba/avro" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestWriter_Reset(t *testing.T) { 13 var buf bytes.Buffer 14 w := avro.NewWriter(nil, 10) 15 w.Reset(&buf) 16 w.Write([]byte("test")) 17 18 err := w.Flush() 19 20 assert.NoError(t, err) 21 assert.Equal(t, []byte("test"), buf.Bytes()) 22 } 23 24 func TestWriter_Buffer(t *testing.T) { 25 w := avro.NewWriter(nil, 10) 26 w.Write([]byte("test")) 27 28 assert.Equal(t, 4, w.Buffered()) 29 assert.Equal(t, []byte("test"), w.Buffer()) 30 } 31 32 func TestWriter_Flush(t *testing.T) { 33 var buf bytes.Buffer 34 w := avro.NewWriter(&buf, 10) 35 w.Write([]byte("test")) 36 37 err := w.Flush() 38 39 assert.NoError(t, err) 40 assert.Equal(t, []byte("test"), buf.Bytes()) 41 } 42 43 func TestWriter_FlushNoWriter(t *testing.T) { 44 w := avro.NewWriter(nil, 10) 45 w.Write([]byte("test")) 46 47 err := w.Flush() 48 49 assert.NoError(t, err) 50 } 51 52 func TestWriter_FlushReturnsWriterError(t *testing.T) { 53 var buf bytes.Buffer 54 w := avro.NewWriter(&buf, 10) 55 w.Error = errors.New("test") 56 57 err := w.Flush() 58 59 assert.Error(t, err) 60 } 61 62 func TestWriter_FlushReturnsUnderlyingWriterError(t *testing.T) { 63 w := avro.NewWriter(&errorWriter{}, 10) 64 w.Write([]byte("test")) 65 66 err := w.Flush() 67 68 assert.Error(t, err) 69 assert.Error(t, w.Error) 70 } 71 72 func TestWriter_Write(t *testing.T) { 73 w := avro.NewWriter(nil, 50) 74 75 w.Write([]byte{0xBC, 0xDC, 0x06, 0x00, 0x10, 0x0A}) 76 77 assert.Equal(t, []byte{0xBC, 0xDC, 0x06, 0x00, 0x10, 0x0A}, w.Buffer()) 78 } 79 80 func TestWriter_WriteBool(t *testing.T) { 81 tests := []struct { 82 data bool 83 want []byte 84 }{ 85 { 86 data: false, 87 want: []byte{0x00}, 88 }, 89 { 90 data: true, 91 want: []byte{0x01}, 92 }, 93 } 94 95 for _, tt := range tests { 96 w := avro.NewWriter(nil, 50) 97 98 w.WriteBool(tt.data) 99 100 assert.Equal(t, tt.want, w.Buffer()) 101 } 102 } 103 104 func TestWriter_WriteInt(t *testing.T) { 105 tests := []struct { 106 data int32 107 want []byte 108 }{ 109 { 110 data: 27, 111 want: []byte{0x36}, 112 }, 113 { 114 data: -8, 115 want: []byte{0x0F}, 116 }, 117 { 118 data: -1, 119 want: []byte{0x01}, 120 }, 121 { 122 data: 0, 123 want: []byte{0x00}, 124 }, 125 { 126 data: 1, 127 want: []byte{0x02}, 128 }, 129 { 130 data: -64, 131 want: []byte{0x7F}, 132 }, 133 { 134 data: 64, 135 want: []byte{0x80, 0x01}, 136 }, 137 { 138 data: 123456789, 139 want: []byte{0xAA, 0xB4, 0xDE, 0x75}, 140 }, 141 { 142 data: 987654321, 143 want: []byte{0xE2, 0xA2, 0xF3, 0xAD, 0x07}, 144 }, 145 } 146 147 for _, tt := range tests { 148 w := avro.NewWriter(nil, 50) 149 150 w.WriteInt(tt.data) 151 152 assert.Equal(t, tt.want, w.Buffer()) 153 } 154 } 155 156 func TestWriter_WriteLong(t *testing.T) { 157 tests := []struct { 158 data int64 159 want []byte 160 }{ 161 { 162 data: 27, 163 want: []byte{0x36}, 164 }, 165 { 166 data: -8, 167 want: []byte{0x0F}, 168 }, 169 { 170 data: -1, 171 want: []byte{0x01}, 172 }, 173 { 174 data: 0, 175 want: []byte{0x00}, 176 }, 177 { 178 data: 1, 179 want: []byte{0x02}, 180 }, 181 { 182 data: -64, 183 want: []byte{0x7F}, 184 }, 185 { 186 data: 64, 187 want: []byte{0x80, 0x01}, 188 }, 189 { 190 data: 123456789, 191 want: []byte{0xAA, 0xB4, 0xDE, 0x75}, 192 }, 193 { 194 data: 987654321, 195 want: []byte{0xE2, 0xA2, 0xF3, 0xAD, 0x07}, 196 }, 197 { 198 data: 9223372036854775807, 199 want: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01}, 200 }, 201 { 202 data: -9223372036854775808, 203 want: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01}, 204 }, 205 { 206 data: -5468631321897454687, 207 want: []byte{0xBD, 0xB1, 0xAE, 0xD4, 0xD2, 0xCD, 0xBD, 0xE4, 0x97, 0x01}, 208 }, 209 } 210 211 for _, tt := range tests { 212 w := avro.NewWriter(nil, 50) 213 214 w.WriteLong(tt.data) 215 216 assert.Equal(t, tt.want, w.Buffer()) 217 } 218 } 219 220 func TestWriter_WriteFloat(t *testing.T) { 221 tests := []struct { 222 data float32 223 want []byte 224 }{ 225 { 226 data: 0.0, 227 want: []byte{0x00, 0x00, 0x00, 0x00}, 228 }, 229 { 230 data: 1.0, 231 want: []byte{0x00, 0x00, 0x80, 0x3F}, 232 }, 233 { 234 data: 1.15, 235 want: []byte{0x33, 0x33, 0x93, 0x3F}, 236 }, 237 { 238 data: -53.964, 239 want: []byte{0x23, 0xDB, 0x57, 0xC2}, 240 }, 241 { 242 data: -123456789.123, 243 want: []byte{0xA3, 0x79, 0xEB, 0xCC}, 244 }, 245 { 246 data: 987654.111115, 247 want: []byte{0x62, 0x20, 0x71, 0x49}, 248 }, 249 } 250 251 for _, tt := range tests { 252 w := avro.NewWriter(nil, 50) 253 254 w.WriteFloat(tt.data) 255 256 assert.Equal(t, tt.want, w.Buffer()) 257 } 258 } 259 260 func TestWriter_WriteDouble(t *testing.T) { 261 tests := []struct { 262 data float64 263 want []byte 264 }{ 265 { 266 data: 0.0, 267 want: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 268 }, 269 { 270 data: 1.0, 271 want: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F}, 272 }, 273 { 274 data: 1.15, 275 want: []byte{0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xF2, 0x3F}, 276 }, 277 { 278 data: -53.964, 279 want: []byte{0x08, 0xAC, 0x1C, 0x5A, 0x64, 0xFB, 0x4A, 0xC0}, 280 }, 281 { 282 data: -123456789.123, 283 want: []byte{0xB6, 0xF3, 0x7D, 0x54, 0x34, 0x6F, 0x9D, 0xC1}, 284 }, 285 { 286 data: 987654.111115, 287 want: []byte{0xB6, 0x10, 0xE4, 0x38, 0x0C, 0x24, 0x2E, 0x41}, 288 }, 289 { 290 data: 123456789.123456789, 291 want: []byte{0x75, 0x6B, 0x7E, 0x54, 0x34, 0x6F, 0x9D, 0x41}, 292 }, 293 { 294 data: 9999999.99999999999999999999999, 295 want: []byte{0x00, 0x00, 0x00, 0x00, 0xD0, 0x12, 0x63, 0x41}, 296 }, 297 { 298 data: 916734926348163.01973408746523, 299 want: []byte{0x18, 0xFC, 0x1A, 0xDD, 0x1F, 0x0E, 0x0A, 0x43}, 300 }, 301 { 302 data: -267319348967891263.1928357138913857, 303 want: []byte{0x0A, 0x8F, 0xA6, 0x40, 0xAC, 0xAD, 0x8D, 0xC3}, 304 }, 305 } 306 307 for _, tt := range tests { 308 w := avro.NewWriter(nil, 50) 309 310 w.WriteDouble(tt.data) 311 312 assert.Equal(t, tt.want, w.Buffer()) 313 } 314 } 315 316 func TestWriter_WriteBytes(t *testing.T) { 317 tests := []struct { 318 data []byte 319 want []byte 320 }{ 321 { 322 data: []byte{0x02}, 323 want: []byte{0x02, 0x02}, 324 }, 325 { 326 data: []byte{0x03, 0xFF}, 327 want: []byte{0x04, 0x03, 0xFF}, 328 }, 329 { 330 data: []byte{0xEC, 0xAB, 0x44, 0x00}, 331 want: []byte{0x08, 0xEC, 0xAB, 0x44, 0x00}, 332 }, 333 { 334 data: []byte{0xAC, 0xDC, 0x01, 0x00, 0x10, 0x0F}, 335 want: []byte{0x0C, 0xAC, 0xDC, 0x01, 0x00, 0x10, 0x0F}, 336 }, 337 } 338 339 for _, tt := range tests { 340 w := avro.NewWriter(nil, 50) 341 342 w.WriteBytes(tt.data) 343 344 assert.Equal(t, tt.want, w.Buffer()) 345 } 346 } 347 348 func TestWriter_WriteString(t *testing.T) { 349 tests := []struct { 350 data string 351 want []byte 352 }{ 353 { 354 data: "", 355 want: []byte{0x00}, 356 }, 357 { 358 data: "foo", 359 want: []byte{0x06, 0x66, 0x6F, 0x6F}, 360 }, 361 { 362 data: "avro", 363 want: []byte{0x08, 0x61, 0x76, 0x72, 0x6F}, 364 }, 365 { 366 data: "apache", 367 want: []byte{0x0C, 0x61, 0x70, 0x61, 0x63, 0x68, 0x65}, 368 }, 369 { 370 data: "oppan gangnam style!", 371 want: []byte{0x28, 0x6F, 0x70, 0x70, 0x61, 0x6E, 0x20, 0x67, 0x61, 0x6E, 0x67, 0x6E, 0x61, 0x6D, 0x20, 0x73, 0x74, 0x79, 0x6C, 0x65, 0x21}, 372 }, 373 { 374 data: "че-то по русски", 375 want: []byte{0x36, 0xD1, 0x87, 0xD0, 0xB5, 0x2D, 0xD1, 0x82, 0xD0, 0xBE, 0x20, 0xD0, 0xBF, 0xD0, 0xBE, 0x20, 0xD1, 0x80, 0xD1, 0x83, 0xD1, 0x81, 0xD1, 0x81, 0xD0, 0xBA, 0xD0, 0xB8}, 376 }, 377 { 378 data: "世界", 379 want: []byte{0x0C, 0xE4, 0xB8, 0x96, 0xE7, 0x95, 0x8C}, 380 }, 381 { 382 data: "!№;%:?*\"()@#$^&", 383 want: []byte{0x22, 0x21, 0xE2, 0x84, 0x96, 0x3B, 0x25, 0x3A, 0x3F, 0x2A, 0x22, 0x28, 0x29, 0x40, 0x23, 0x24, 0x5E, 0x26}, 384 }, 385 } 386 387 for _, tt := range tests { 388 w := avro.NewWriter(nil, 50) 389 390 w.WriteString(tt.data) 391 392 assert.Equal(t, tt.want, w.Buffer()) 393 } 394 } 395 396 func TestWriter_WriteBlockHeader(t *testing.T) { 397 tests := []struct { 398 len int64 399 size int64 400 want []byte 401 }{ 402 { 403 len: 64, 404 size: 0, 405 want: []byte{0x80, 0x01}, 406 }, 407 { 408 len: 64, 409 size: 64, 410 want: []byte{0x7F, 0x80, 0x01}, 411 }, 412 } 413 414 for _, tt := range tests { 415 w := avro.NewWriter(nil, 50) 416 417 w.WriteBlockHeader(tt.len, tt.size) 418 419 assert.Equal(t, tt.want, w.Buffer()) 420 } 421 } 422 423 func TestWriter_WriteBlockCB(t *testing.T) { 424 w := avro.NewWriter(nil, 50) 425 426 wrote := w.WriteBlockCB(func(w *avro.Writer) int64 { 427 w.WriteString("foo") 428 w.WriteString("avro") 429 430 return 2 431 }) 432 433 assert.Equal(t, []byte{0x03, 0x12, 0x06, 0x66, 0x6F, 0x6F, 0x08, 0x61, 0x76, 0x72, 0x6F}, w.Buffer()) 434 assert.Equal(t, int64(2), wrote) 435 } 436 437 type errorWriter struct{} 438 439 func (errorWriter) Write(p []byte) (n int, err error) { 440 return 0, errors.New("test error") 441 }