Skip to content

Commit

Permalink
Serialize schemas feature hamba#400
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovic-pourrat authored May 17, 2024
1 parent dc715a4 commit e9a0f43
Showing 1 changed file with 34 additions and 50 deletions.
84 changes: 34 additions & 50 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package avro_test

import (
"encoding/json"
"fmt"
"testing"

"github.com/hamba/avro/v2"
Expand Down Expand Up @@ -41,6 +42,16 @@ func TestParseFiles(t *testing.T) {
assert.Equal(t, avro.String, s.Type())
}

func TestSerialize(t *testing.T) {
s, err := avro.ParseFiles("testdata/superhero-part1.avsc", "testdata/superhero-part2.avsc")

data := avro.Serialize(s)
fmt.Println(data)
n, err := avro.Parse(data)
require.NoError(t, err)
assert.Equal(t, avro.Record, n.Type())
}

func TestParseFiles_FileDoesntExist(t *testing.T) {
_, err := avro.ParseFiles("test.something")

Expand Down Expand Up @@ -150,13 +161,11 @@ func TestPrimitiveSchema(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.schema, func(t *testing.T) {
t.Parallel()

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
s, err := avro.Parse(test.schema)

require.NoError(t, err)
assert.Equal(t, test.want, schema.Type())
assert.Equal(t, test.wantFingerprint, schema.Fingerprint())
assert.Equal(t, test.want, s.Type())
assert.Equal(t, test.wantFingerprint, s.Fingerprint())
})
}
}
Expand Down Expand Up @@ -259,13 +268,11 @@ func TestRecordSchema(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
s, err := avro.Parse(test.schema)

test.wantErr(t, err)
if schema != nil {
assert.Equal(t, avro.Record, schema.Type())
if s != nil {
assert.Equal(t, avro.Record, s.Type())
}
})
}
Expand Down Expand Up @@ -319,14 +326,13 @@ func TestErrorRecordSchema(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
s, err := avro.Parse(test.schema)

test.wantErr(t, err)
if test.wantSchema {
assert.Equal(t, avro.Record, schema.Type())
recSchema := schema.(*avro.RecordSchema)
assert.Equal(t, avro.Record, s.Type())
recSchema := s.(*avro.RecordSchema)
assert.True(t, recSchema.IsError())
}
})
Expand Down Expand Up @@ -434,9 +440,7 @@ func TestRecordSchema_ValidatesDefault(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

_, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
_, err := avro.Parse(test.schema)

test.wantErr(t, err)
})
Expand Down Expand Up @@ -484,13 +488,11 @@ func TestRecordSchema_ValidatesOrder(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
s, err := avro.Parse(test.schema)

test.wantErr(t, err)
if test.want != "" {
rs := schema.(*avro.RecordSchema)
rs := s.(*avro.RecordSchema)
require.Len(t, rs.Fields(), 1)
assert.Equal(t, test.want, rs.Fields()[0].Order())
}
Expand Down Expand Up @@ -675,9 +677,7 @@ func TestEnumSchema(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
schema, err := avro.Parse(test.schema)

test.wantErr(t, err)
if test.wantName != "" {
Expand Down Expand Up @@ -730,9 +730,7 @@ func TestArraySchema(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

got, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
got, err := avro.Parse(test.schema)

test.wantErr(t, err)
assert.Equal(t, test.want, got)
Expand Down Expand Up @@ -778,9 +776,7 @@ func TestMapSchema(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

got, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
got, err := avro.Parse(test.schema)

test.wantErr(t, err)
assert.Equal(t, test.want, got)
Expand Down Expand Up @@ -842,14 +838,12 @@ func TestUnionSchema(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
s, err := avro.Parse(test.schema)

test.wantErr(t, err)
if test.wantFingerprint != [32]byte{} {
assert.Equal(t, avro.Union, schema.Type())
assert.Equal(t, test.wantFingerprint, schema.Fingerprint())
assert.Equal(t, avro.Union, s.Type())
assert.Equal(t, test.wantFingerprint, s.Fingerprint())
}
})
}
Expand Down Expand Up @@ -881,12 +875,10 @@ func TestUnionSchema_Indices(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
s, err := avro.Parse(test.schema)

require.NoError(t, err)
null, typ := schema.(*avro.UnionSchema).Indices()
null, typ := s.(*avro.UnionSchema).Indices()
assert.Equal(t, test.want[0], null)
assert.Equal(t, test.want[1], typ)
})
Expand Down Expand Up @@ -948,9 +940,7 @@ func TestFixedSchema(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
schema, err := avro.Parse(test.schema)

test.wantErr(t, err)
if test.wantFingerprint != [32]byte{} {
Expand Down Expand Up @@ -1150,11 +1140,9 @@ func TestSchema_LogicalTypes(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
schema, err := avro.Parse(test.schema)

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
require.NoError(t, err)

assert.Equal(t, test.wantType, schema.Type())

lts, ok := schema.(avro.LogicalTypeSchema)
Expand Down Expand Up @@ -1251,11 +1239,7 @@ func TestSchema_FingerprintUsing(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

schema, err := avro.ParseWithCache(test.schema, "", &avro.SchemaCache{})
require.NoError(t, err)

schema := avro.MustParse(test.schema)
got, err := schema.FingerprintUsing(test.typ)

require.NoError(t, err)
Expand Down

0 comments on commit e9a0f43

Please sign in to comment.