github.com/hamba/avro@v1.8.0/codec_skip_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 TestDecoder_SkipBool(t *testing.T) { 12 defer ConfigTeardown() 13 14 data := []byte{0x01, 0x06, 0x66, 0x6f, 0x6f} 15 schema := `{ 16 "type": "record", 17 "name": "test", 18 "fields" : [ 19 {"name": "a", "type": "boolean"}, 20 {"name": "b", "type": "string"} 21 ] 22 }` 23 24 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 25 assert.NoError(t, err) 26 27 var got TestPartialRecord 28 err = dec.Decode(&got) 29 30 assert.NoError(t, err) 31 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 32 } 33 34 func TestDecoder_SkipInt(t *testing.T) { 35 defer ConfigTeardown() 36 37 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 38 schema := `{ 39 "type": "record", 40 "name": "test", 41 "fields" : [ 42 {"name": "a", "type": "int"}, 43 {"name": "b", "type": "string"} 44 ] 45 }` 46 47 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 48 assert.NoError(t, err) 49 50 var got TestPartialRecord 51 err = dec.Decode(&got) 52 53 assert.NoError(t, err) 54 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 55 } 56 57 func TestDecoder_SkipLong(t *testing.T) { 58 defer ConfigTeardown() 59 60 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 61 schema := `{ 62 "type": "record", 63 "name": "test", 64 "fields" : [ 65 {"name": "a", "type": "long"}, 66 {"name": "b", "type": "string"} 67 ] 68 }` 69 70 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 71 assert.NoError(t, err) 72 73 var got TestPartialRecord 74 err = dec.Decode(&got) 75 76 assert.NoError(t, err) 77 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 78 } 79 80 func TestDecoder_SkipFloat(t *testing.T) { 81 data := []byte{0x0, 0x0, 0x0, 0x0, 0x06, 0x66, 0x6f, 0x6f} 82 schema := `{ 83 "type": "record", 84 "name": "test", 85 "fields" : [ 86 {"name": "a", "type": "float"}, 87 {"name": "b", "type": "string"} 88 ] 89 }` 90 91 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 92 assert.NoError(t, err) 93 94 var got TestPartialRecord 95 err = dec.Decode(&got) 96 97 assert.NoError(t, err) 98 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 99 } 100 101 func TestDecoder_SkipDouble(t *testing.T) { 102 defer ConfigTeardown() 103 104 data := []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x06, 0x66, 0x6f, 0x6f} 105 schema := `{ 106 "type": "record", 107 "name": "test", 108 "fields" : [ 109 {"name": "a", "type": "double"}, 110 {"name": "b", "type": "string"} 111 ] 112 }` 113 114 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 115 assert.NoError(t, err) 116 117 var got TestPartialRecord 118 err = dec.Decode(&got) 119 120 assert.NoError(t, err) 121 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 122 } 123 124 func TestDecoder_SkipBytes(t *testing.T) { 125 defer ConfigTeardown() 126 127 data := []byte{0x02, 0x36, 0x06, 0x66, 0x6f, 0x6f} 128 schema := `{ 129 "type": "record", 130 "name": "test", 131 "fields" : [ 132 {"name": "a", "type": "bytes"}, 133 {"name": "b", "type": "string"} 134 ] 135 }` 136 137 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 138 assert.NoError(t, err) 139 140 var got TestPartialRecord 141 err = dec.Decode(&got) 142 143 assert.NoError(t, err) 144 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 145 } 146 147 func TestDecoder_SkipString(t *testing.T) { 148 defer ConfigTeardown() 149 150 data := []byte{0x02, 0x66, 0x06, 0x66, 0x6f, 0x6f} 151 schema := `{ 152 "type": "record", 153 "name": "test", 154 "fields" : [ 155 {"name": "a", "type": "string"}, 156 {"name": "b", "type": "string"} 157 ] 158 }` 159 160 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 161 assert.NoError(t, err) 162 163 var got TestPartialRecord 164 err = dec.Decode(&got) 165 166 assert.NoError(t, err) 167 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 168 } 169 170 func TestDecoder_SkipRecord(t *testing.T) { 171 defer ConfigTeardown() 172 173 data := []byte{0x02, 0x66, 0x06, 0x66, 0x6f, 0x6f} 174 schema := `{ 175 "type": "record", 176 "name": "test", 177 "fields" : [ 178 {"name": "a", "type": {"type": "record", "name": "test2", "fields": [{"name": "c", "type": "string"}]}}, 179 {"name": "b", "type": "string"} 180 ] 181 }` 182 183 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 184 assert.NoError(t, err) 185 186 var got TestPartialRecord 187 err = dec.Decode(&got) 188 189 assert.NoError(t, err) 190 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 191 } 192 193 func TestDecoder_SkipRef(t *testing.T) { 194 defer ConfigTeardown() 195 196 data := []byte{0x02, 0x66, 0x06, 0x66, 0x6f, 0x6f} 197 schema := `{ 198 "type": "record", 199 "name": "test", 200 "fields" : [ 201 {"name": "a", "type": {"type": "record", "name": "test2", "fields": [{"name": "c", "type": "string"}]}}, 202 {"name": "b", "type": "string"}, 203 {"name": "c", "type": "test2"} 204 ] 205 }` 206 207 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 208 assert.NoError(t, err) 209 210 var got TestPartialRecord 211 err = dec.Decode(&got) 212 213 assert.NoError(t, err) 214 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 215 } 216 217 func TestDecoder_SkipEnum(t *testing.T) { 218 defer ConfigTeardown() 219 220 data := []byte{0x02, 0x06, 0x66, 0x6f, 0x6f} 221 schema := `{ 222 "type": "record", 223 "name": "test", 224 "fields" : [ 225 {"name": "a", "type": {"type": "enum", "name": "test2", "symbols": ["sym1", "sym2"]}}, 226 {"name": "b", "type": "string"} 227 ] 228 }` 229 230 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 231 assert.NoError(t, err) 232 233 var got TestPartialRecord 234 err = dec.Decode(&got) 235 236 assert.NoError(t, err) 237 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 238 } 239 240 func TestDecoder_SkipArray(t *testing.T) { 241 defer ConfigTeardown() 242 243 data := []byte{0x04, 0x36, 0x36, 0x0, 0x06, 0x66, 0x6f, 0x6f} 244 schema := `{ 245 "type": "record", 246 "name": "test", 247 "fields" : [ 248 {"name": "a", "type": {"type": "array", "items": "int"}}, 249 {"name": "b", "type": "string"} 250 ] 251 }` 252 253 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 254 assert.NoError(t, err) 255 256 var got TestPartialRecord 257 err = dec.Decode(&got) 258 259 assert.NoError(t, err) 260 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 261 } 262 263 func TestDecoder_SkipArrayBlocks(t *testing.T) { 264 defer ConfigTeardown() 265 266 data := []byte{0x03, 0x04, 0x36, 0x36, 0x0, 0x06, 0x66, 0x6f, 0x6f} 267 schema := `{ 268 "type": "record", 269 "name": "test", 270 "fields" : [ 271 {"name": "a", "type": {"type": "array", "items": "int"}}, 272 {"name": "b", "type": "string"} 273 ] 274 }` 275 276 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 277 assert.NoError(t, err) 278 279 var got TestPartialRecord 280 err = dec.Decode(&got) 281 282 assert.NoError(t, err) 283 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 284 } 285 286 func TestDecoder_SkipMap(t *testing.T) { 287 defer ConfigTeardown() 288 289 data := []byte{0x04, 0x02, 0x66, 0x36, 0x02, 0x6f, 0x36, 0x0, 0x06, 0x66, 0x6f, 0x6f} 290 schema := `{ 291 "type": "record", 292 "name": "test", 293 "fields" : [ 294 {"name": "a", "type": {"type": "map", "values": "int"}}, 295 {"name": "b", "type": "string"} 296 ] 297 }` 298 299 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 300 assert.NoError(t, err) 301 302 var got TestPartialRecord 303 err = dec.Decode(&got) 304 305 assert.NoError(t, err) 306 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 307 } 308 309 func TestDecoder_SkipMapBlocks(t *testing.T) { 310 defer ConfigTeardown() 311 312 data := []byte{0x03, 0x0C, 0x02, 0x66, 0x36, 0x02, 0x6f, 0x36, 0x0, 0x06, 0x66, 0x6f, 0x6f} 313 schema := `{ 314 "type": "record", 315 "name": "test", 316 "fields" : [ 317 {"name": "a", "type": {"type": "map", "values": "int"}}, 318 {"name": "b", "type": "string"} 319 ] 320 }` 321 322 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 323 assert.NoError(t, err) 324 325 var got TestPartialRecord 326 err = dec.Decode(&got) 327 328 assert.NoError(t, err) 329 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 330 } 331 332 func TestDecoder_SkipUnion(t *testing.T) { 333 defer ConfigTeardown() 334 335 data := []byte{0x02, 0x02, 0x66, 0x06, 0x66, 0x6f, 0x6f} 336 schema := `{ 337 "type": "record", 338 "name": "test", 339 "fields" : [ 340 {"name": "a", "type": ["null", "string"]}, 341 {"name": "b", "type": "string"} 342 ] 343 }` 344 345 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 346 assert.NoError(t, err) 347 348 var got TestPartialRecord 349 err = dec.Decode(&got) 350 351 assert.NoError(t, err) 352 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 353 } 354 355 func TestDecoder_SkipUnionNull(t *testing.T) { 356 defer ConfigTeardown() 357 358 data := []byte{0x00, 0x06, 0x66, 0x6f, 0x6f} 359 schema := `{ 360 "type": "record", 361 "name": "test", 362 "fields" : [ 363 {"name": "a", "type": ["null", "string"]}, 364 {"name": "b", "type": "string"} 365 ] 366 }` 367 368 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 369 assert.NoError(t, err) 370 371 var got TestPartialRecord 372 err = dec.Decode(&got) 373 374 assert.NoError(t, err) 375 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 376 } 377 378 func TestDecoder_SkipUnionInvalidSchema(t *testing.T) { 379 defer ConfigTeardown() 380 381 data := []byte{0x03, 0x06, 0x66, 0x6f, 0x6f} 382 schema := `{ 383 "type": "record", 384 "name": "test", 385 "fields" : [ 386 {"name": "a", "type": ["null", "string"]}, 387 {"name": "b", "type": "string"} 388 ] 389 }` 390 391 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 392 assert.NoError(t, err) 393 394 var got TestPartialRecord 395 err = dec.Decode(&got) 396 397 assert.Error(t, err) 398 } 399 400 func TestDecoder_SkipFixed(t *testing.T) { 401 defer ConfigTeardown() 402 403 data := []byte{0x04, 0x02, 0x66, 0x36, 0x02, 0x6f, 0x36, 0x06, 0x66, 0x6f, 0x6f} 404 schema := `{ 405 "type": "record", 406 "name": "test", 407 "fields" : [ 408 {"name": "a", "type": {"type": "fixed", "name": "test2", "size": 7}}, 409 {"name": "b", "type": "string"} 410 ] 411 }` 412 413 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 414 assert.NoError(t, err) 415 416 var got TestPartialRecord 417 err = dec.Decode(&got) 418 419 assert.NoError(t, err) 420 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 421 }