From e9a0f43703572a7b936d7a1d705d4c7e677e3d5f Mon Sep 17 00:00:00 2001 From: Ludovic Pourrat <52542520+ludovic-pourrat@users.noreply.github.com> Date: Fri, 17 May 2024 08:34:15 +0200 Subject: [PATCH] Serialize schemas feature #400 --- schema_test.go | 84 ++++++++++++++++++++------------------------------ 1 file changed, 34 insertions(+), 50 deletions(-) diff --git a/schema_test.go b/schema_test.go index 7922e11..4aebb67 100644 --- a/schema_test.go +++ b/schema_test.go @@ -2,6 +2,7 @@ package avro_test import ( "encoding/json" + "fmt" "testing" "github.com/hamba/avro/v2" @@ -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") @@ -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()) }) } } @@ -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()) } }) } @@ -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()) } }) @@ -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) }) @@ -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()) } @@ -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 != "" { @@ -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) @@ -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) @@ -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()) } }) } @@ -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) }) @@ -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{} { @@ -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) @@ -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)