Skip to content

Commit

Permalink
test: codec for plain map as nullable union
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjac committed Oct 8, 2024
1 parent a1ed0ac commit 4d8a8e2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
36 changes: 36 additions & 0 deletions decoder_union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,42 @@ func TestDecoder_UnionPtrReversedNull(t *testing.T) {
assert.Nil(t, got)
}

func TestDecoder_UnionNullableMap(t *testing.T) {
tt := []struct {
name string
data []byte
want map[string]string
}{{
name: "WithData",
data: []byte{0x02, 0x01, 0x10, 0x06, 0x66, 0x6F, 0x6F, 0x06, 0x66, 0x6F, 0x6F, 0x00},
want: map[string]string{"foo": "foo"},
}, {
name: "Empty",
data: []byte{0x02, 0x00},
want: map[string]string{},
}, {
name: "Null",
data: []byte{0x00},
want: nil,
}}

schema := `["null", {"type":"map", "values": "string"}]`

for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
defer ConfigTeardown()

dec, _ := avro.NewDecoder(schema, bytes.NewReader(test.data))

var got map[string]string
err := dec.Decode(&got)

require.NoError(t, err)
assert.Equal(t, test.want, got)
})
}
}

func TestDecoder_UnionNullableSlice(t *testing.T) {
defer ConfigTeardown()

Expand Down
37 changes: 37 additions & 0 deletions encoder_union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,43 @@ func TestEncoder_UnionPtrNotNullable(t *testing.T) {
assert.Error(t, err)
}

func TestEncoder_UnionNullableMap(t *testing.T) {
tt := []struct {
name string
data map[string]string
want []byte
}{{
name: "WithData",
data: map[string]string{"foo": "foo"},
want: []byte{0x02, 0x01, 0x10, 0x06, 0x66, 0x6F, 0x6F, 0x06, 0x66, 0x6F, 0x6F, 0x00},
}, {
name: "Empty",
data: map[string]string{},
want: []byte{0x02, 0x00},
}, {
name: "Null",
data: nil,
want: []byte{0x00},
}}

schema := `["null", {"type":"map", "values": "string"}]`

for _, test := range tt {
t.Run(test.name, func(t *testing.T) {
defer ConfigTeardown()

buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
require.NoError(t, err)

err = enc.Encode(test.data)

require.NoError(t, err)
assert.Equal(t, test.want, buf.Bytes())
})
}
}

func TestEncoder_UnionNullableSlice(t *testing.T) {
defer ConfigTeardown()

Expand Down

0 comments on commit 4d8a8e2

Please sign in to comment.