github.com/hamba/avro@v1.8.0/encoder_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 TestEncoder_MapInvalidType(t *testing.T) {
    12  	defer ConfigTeardown()
    13  
    14  	schema := `{"type":"map", "values": "string"}`
    15  	buf := bytes.NewBuffer([]byte{})
    16  	enc, err := avro.NewEncoder(schema, buf)
    17  	assert.NoError(t, err)
    18  
    19  	err = enc.Encode("test")
    20  
    21  	assert.Error(t, err)
    22  }
    23  
    24  func TestEncoder_Map(t *testing.T) {
    25  	defer ConfigTeardown()
    26  
    27  	schema := `{"type":"map", "values": "string"}`
    28  	buf := bytes.NewBuffer([]byte{})
    29  	enc, err := avro.NewEncoder(schema, buf)
    30  	assert.NoError(t, err)
    31  
    32  	err = enc.Encode(map[string]string{})
    33  
    34  	assert.NoError(t, err)
    35  	assert.Equal(t, []byte{0x00}, buf.Bytes())
    36  }
    37  
    38  func TestEncoder_MapEmpty(t *testing.T) {
    39  	defer ConfigTeardown()
    40  
    41  	schema := `{"type":"map", "values": "string"}`
    42  	buf := bytes.NewBuffer([]byte{})
    43  	enc, err := avro.NewEncoder(schema, buf)
    44  	assert.NoError(t, err)
    45  
    46  	err = enc.Encode(map[string]string{"foo": "foo"})
    47  
    48  	assert.NoError(t, err)
    49  	assert.Equal(t, []byte{0x01, 0x10, 0x06, 0x66, 0x6F, 0x6F, 0x06, 0x66, 0x6F, 0x6F, 0x00}, buf.Bytes())
    50  }
    51  
    52  func TestEncoder_MapOfStruct(t *testing.T) {
    53  	defer ConfigTeardown()
    54  
    55  	schema := `{"type":"map", "values": {"type": "record", "name": "test", "fields" : [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}}`
    56  	buf := bytes.NewBuffer([]byte{})
    57  	enc, err := avro.NewEncoder(schema, buf)
    58  	assert.NoError(t, err)
    59  
    60  	err = enc.Encode(map[string]TestRecord{"foo": {A: 27, B: "foo"}})
    61  
    62  	assert.NoError(t, err)
    63  	assert.Equal(t, []byte{0x01, 0x12, 0x06, 0x66, 0x6F, 0x6F, 0x36, 0x06, 0x66, 0x6f, 0x6f, 0x0}, buf.Bytes())
    64  }
    65  
    66  func TestEncoder_MapInvalidKeyType(t *testing.T) {
    67  	defer ConfigTeardown()
    68  
    69  	schema := `{"type":"map", "values": "string"}`
    70  	buf := bytes.NewBuffer([]byte{})
    71  	enc, err := avro.NewEncoder(schema, buf)
    72  	assert.NoError(t, err)
    73  
    74  	err = enc.Encode(map[int]string{1: "foo"})
    75  
    76  	assert.Error(t, err)
    77  }
    78  
    79  func TestEncoder_MapError(t *testing.T) {
    80  	defer ConfigTeardown()
    81  
    82  	schema := `{"type":"map", "values": "string"}`
    83  	buf := bytes.NewBuffer([]byte{})
    84  	enc, err := avro.NewEncoder(schema, buf)
    85  	assert.NoError(t, err)
    86  
    87  	err = enc.Encode(map[string]int{"foo": 1})
    88  
    89  	assert.Error(t, err)
    90  }
    91  
    92  func TestEncoder_MapWithMoreThanBlockLengthKeys(t *testing.T) {
    93  	avro.DefaultConfig = avro.Config{
    94  		TagKey:               "avro",
    95  		BlockLength:          1,
    96  		UnionResolutionError: true,
    97  	}.Freeze()
    98  
    99  	schema := `{"type":"map", "values": "int"}`
   100  	buf := bytes.NewBuffer([]byte{})
   101  	enc, err := avro.NewEncoder(schema, buf)
   102  	assert.NoError(t, err)
   103  
   104  	err = enc.Encode(map[string]int{"foo": 1, "bar": 2})
   105  
   106  	assert.NoError(t, err)
   107  	assert.Condition(t, func() bool {
   108  		// {"foo": 1, "bar": 2}
   109  		foobar := bytes.Equal([]byte{0x01, 0x0a, 0x06, 0x66, 0x6F, 0x6F, 0x02, 0x01, 0x0a, 0x06, 0x62, 0x61, 0x72, 0x04, 0x0}, buf.Bytes())
   110  		// {"bar": 2, "foo": 1}
   111  		barfoo := bytes.Equal([]byte{0x01, 0x0a, 0x06, 0x62, 0x61, 0x72, 0x04, 0x01, 0x0a, 0x06, 0x66, 0x6F, 0x6F, 0x02, 0x0}, buf.Bytes())
   112  		return (foobar || barfoo)
   113  	})
   114  }