github.com/hamba/avro@v1.8.0/decoder_map_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_MapInvalidType(t *testing.T) { 12 defer ConfigTeardown() 13 14 data := []byte{0x02, 0x06, 0x66, 0x6F, 0x6F, 0x06, 0x66, 0x6F, 0x6F, 0x00} 15 schema := `{"type":"map", "values": "string"}` 16 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 17 assert.NoError(t, err) 18 19 var str string 20 err = dec.Decode(&str) 21 22 assert.Error(t, err) 23 } 24 25 func TestDecoder_MapMap(t *testing.T) { 26 defer ConfigTeardown() 27 28 data := []byte{0x02, 0x06, 0x66, 0x6F, 0x6F, 0x06, 0x66, 0x6F, 0x6F, 0x00} 29 schema := `{"type":"map", "values": "string"}` 30 dec, _ := avro.NewDecoder(schema, bytes.NewReader(data)) 31 32 var got map[string]string 33 err := dec.Decode(&got) 34 35 assert.NoError(t, err) 36 assert.Equal(t, map[string]string{"foo": "foo"}, got) 37 } 38 39 func TestDecoder_MapMapOfStruct(t *testing.T) { 40 defer ConfigTeardown() 41 42 data := []byte{0x02, 0x06, 0x66, 0x6F, 0x6F, 0x36, 0x06, 0x66, 0x6f, 0x6f, 0x0} 43 schema := `{"type":"map", "values": {"type": "record", "name": "test", "fields" : [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}}` 44 dec, _ := avro.NewDecoder(schema, bytes.NewReader(data)) 45 46 var got map[string]TestRecord 47 err := dec.Decode(&got) 48 49 assert.NoError(t, err) 50 assert.Equal(t, map[string]TestRecord{"foo": {A: 27, B: "foo"}}, got) 51 } 52 53 func TestDecoder_MapMapError(t *testing.T) { 54 defer ConfigTeardown() 55 56 data := []byte{0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0xAD, 0xE2, 0xA2, 0xF3, 0xAD, 0xAD} 57 schema := `{"type":"map", "values": "string"}` 58 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 59 assert.NoError(t, err) 60 61 var got map[string]string 62 err = dec.Decode(&got) 63 64 assert.Error(t, err) 65 } 66 67 func TestDecoder_MapInvalidKeyType(t *testing.T) { 68 defer ConfigTeardown() 69 70 data := []byte{0x02, 0x06, 0x66, 0x6F, 0x6F, 0x06, 0x66, 0x6F, 0x6F, 0x00} 71 schema := `{"type":"map", "values": "string"}` 72 dec, _ := avro.NewDecoder(schema, bytes.NewReader(data)) 73 74 var got map[int]string 75 err := dec.Decode(&got) 76 77 assert.Error(t, err) 78 }