Skip to content

Commit

Permalink
Add test to reproduce actgardner/gogen-avro issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sixstone-qq committed Nov 14, 2022
1 parent 8e29d38 commit 6be8d61
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 5 deletions.
86 changes: 81 additions & 5 deletions singledecoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
)

//go:generate avrogo testschema1.avsc
//go:generate avrogo testschema2.avsc

func TestSingleDecoder(t *testing.T) {
func TestSingleDecoder_CompabilitySameRecord(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
dec := avro.NewSingleDecoder(memRegistry{
1: mustParseType(`{
"name": "TestRecord",
Expand All @@ -28,6 +30,17 @@ func TestSingleDecoder(t *testing.T) {
"type": {
"type": "int"
}
}, {
"name": "C",
"type": [
"null",
{
"name": "EnumC",
"type": "enum",
"symbols": ["x", "y", "z"]
}
],
"default": "null"
}]
}`),
2: mustParseType(`{
Expand Down Expand Up @@ -69,23 +82,86 @@ func TestSingleDecoder(t *testing.T) {
// 1: the schema id
// 40: B=20 (zig-zag encoded)
// 80: A=40 (ditto)
_, err = dec.Unmarshal(context.Background(), []byte{1, 40, 80}, &x)
// 0: C=null Choose null value
_, err = dec.Unmarshal(ctx, []byte{1, 40, 80, 0}, &x)
c.Assert(err, qt.Equals, nil)
c.Assert(x, qt.Equals, TestRecord{A: 40, B: 20})
c.Assert(x, qt.DeepEquals, TestRecord{A: 40, B: 20})

// Check the record compatibility stuff is working by reading from a
// record written with less fields (note: the default value for A is 42).
var x1 TestRecord
_, err = dec.Unmarshal(context.Background(), []byte{2, 80}, &x1)
_, err = dec.Unmarshal(ctx, []byte{2, 80}, &x1)
c.Assert(err, qt.Equals, nil)
c.Assert(x1, qt.Equals, TestRecord{A: 42, B: 40})

// There's no default value for A, so it doesn't work that way around.
var x2 TestRecord
_, err = dec.Unmarshal(context.Background(), []byte{3, 80}, &x2)
_, err = dec.Unmarshal(ctx, []byte{3, 80}, &x2)
c.Assert(err, qt.ErrorMatches, `cannot unmarshal: cannot create decoder: Incompatible schemas: field B in reader is not present in writer and has no default value`)
}

func TestSingleDecoder_CompabilityDifferentRecord(t *testing.T) {
c := qt.New(t)
ctx := context.Background()
dec := avro.NewSingleDecoder(memRegistry{
1: mustParseType(`{
"name": "TestRecord",
"type": "record",
"fields": [{
"name": "B",
"type": {
"type": "int"
}
}, {
"name": "A",
"type": {
"type": "int"
}
}]
}`),
2: mustParseType(`{
"name": "TestRecord",
"type": "record",
"fields": [{
"name": "B",
"type": {
"type": "int"
}
}, {
"name": "A",
"type": {
"type": "int"
}
}, {
"name": "C",
"type": [
"null",
{
"name": "EnumC",
"type": "enum",
"symbols": ["x", "y", "z"]
}
],
"default": "null"
}]
}`),
}, nil)
cVal := EnumCY
data, _, err := avro.Marshal(TestNewRecord{A: 40, B: 20, C: &cVal})
c.Assert(err, qt.IsNil)
c.Logf("data: %d", data)
var x TestRecord
// In the byte slice below:
// 1: the schema id
// 40: B=20 (zig-zag encoded)
// 80: A=40 (ditto)
// 2: C=Choose EnumC type
// 2 C="y"
_, err = dec.Unmarshal(ctx, []byte{2, 40, 80, 2, 2}, &x)
c.Assert(err, qt.IsNil)
c.Assert(x, qt.Equals, TestRecord{A: 40, B: 20})
}

// memRegistry implements DecodingRegistry and EncodingRegistry by associating a single-byte
// schema ID with schemas.
type memRegistry map[int64]*avro.Type
Expand Down
27 changes: 27 additions & 0 deletions testschema2.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "TestNewRecord",
"type": "record",
"fields": [{
"name": "A",
"type": {
"type": "int"
},
"default": 42
}, {
"name": "B",
"type": {
"type": "int"
}
}, {
"name": "C",
"type": [
"null",
{
"name": "EnumC",
"type": "enum",
"symbols": ["x", "y", "z"]
}
],
"default": null
}]
}
74 changes: 74 additions & 0 deletions testschema2_gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6be8d61

Please sign in to comment.