From 325746cbd8a0cf8536c6d7797f33772bd7f556d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfred=20Gunnarg=C3=A5rd?= Date: Tue, 27 Apr 2021 07:39:34 +0200 Subject: [PATCH] chore: add tests for jsonbq.MarshalSchema using more or less same tests as for protobq.InferSchema --- encoding/jsonbq/marshal_test.go | 191 ++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 encoding/jsonbq/marshal_test.go diff --git a/encoding/jsonbq/marshal_test.go b/encoding/jsonbq/marshal_test.go new file mode 100644 index 0000000..5718332 --- /dev/null +++ b/encoding/jsonbq/marshal_test.go @@ -0,0 +1,191 @@ +package jsonbq + +import ( + "encoding/json" + "testing" + + "cloud.google.com/go/bigquery" + "gotest.tools/v3/assert" +) + +func TestMarshalSchema(t *testing.T) { + for _, tt := range []struct { + name string + schema bigquery.Schema + expected []*Field + }{ + { + name: "library.Book", + schema: bigquery.Schema{ + {Name: "name", Type: bigquery.StringFieldType}, + {Name: "author", Type: bigquery.StringFieldType}, + {Name: "title", Type: bigquery.StringFieldType}, + {Name: "read", Type: bigquery.BooleanFieldType}, + }, + expected: []*Field{ + { + Name: "name", + Type: bigquery.StringFieldType, + Mode: ModeNullable, + }, + { + Name: "author", + Type: bigquery.StringFieldType, + Mode: ModeNullable, + }, + { + Name: "title", + Type: bigquery.StringFieldType, + Mode: ModeNullable, + }, + { + Name: "read", + Type: bigquery.BooleanFieldType, + Mode: ModeNullable, + }, + }, + }, + { + name: "library.UpdateBookRequest", + schema: bigquery.Schema{ + { + Name: "book", + Type: bigquery.RecordFieldType, + Schema: bigquery.Schema{ + {Name: "name", Type: bigquery.StringFieldType}, + {Name: "author", Type: bigquery.StringFieldType}, + {Name: "title", Type: bigquery.StringFieldType}, + {Name: "read", Type: bigquery.BooleanFieldType}, + }, + }, + { + Name: "update_mask", + Type: bigquery.RecordFieldType, + Schema: bigquery.Schema{ + {Name: "paths", Type: bigquery.StringFieldType, Repeated: true}, + }, + }, + }, + expected: []*Field{ + { + Name: "book", + Type: bigquery.RecordFieldType, + Mode: ModeNullable, + Fields: []*Field{ + { + Name: "name", + Type: bigquery.StringFieldType, + Mode: ModeNullable, + }, + { + Name: "author", + Type: bigquery.StringFieldType, + Mode: ModeNullable, + }, + { + Name: "title", + Type: bigquery.StringFieldType, + Mode: ModeNullable, + }, + { + Name: "read", + Type: bigquery.BooleanFieldType, + Mode: ModeNullable, + }, + }, + }, + { + Name: "update_mask", + Type: bigquery.RecordFieldType, + Mode: ModeNullable, + Fields: []*Field{ + { + Name: "paths", + Type: bigquery.StringFieldType, + Mode: ModeRepeated, + }, + }, + }, + }, + }, + { + name: "examplev1.ExampleDateTime (no offset)", + schema: bigquery.Schema{ + {Name: "date_time", Type: bigquery.DateTimeFieldType}, + }, + expected: []*Field{ + { + Name: "date_time", + Type: bigquery.DateTimeFieldType, + Mode: ModeNullable, + }, + }, + }, + { + name: "examplev1.ExampleDateTime (with offset)", + schema: bigquery.Schema{ + { + Name: "date_time", + Type: bigquery.RecordFieldType, + Schema: bigquery.Schema{ + {Name: "datetime", Type: bigquery.DateTimeFieldType}, + {Name: "utc_offset", Type: bigquery.FloatFieldType}, + { + Name: "time_zone", + Type: bigquery.RecordFieldType, + Schema: bigquery.Schema{ + {Name: "id", Type: bigquery.StringFieldType}, + {Name: "version", Type: bigquery.StringFieldType}, + }, + }, + }, + }, + }, + expected: []*Field{ + { + Name: "date_time", + Type: bigquery.RecordFieldType, + Mode: ModeNullable, + Fields: []*Field{ + { + Name: "datetime", + Type: bigquery.DateTimeFieldType, + Mode: ModeNullable, + }, + { + Name: "utc_offset", + Type: bigquery.FloatFieldType, + Mode: ModeNullable, + }, + { + Name: "time_zone", + Type: bigquery.RecordFieldType, + Mode: ModeNullable, + Fields: []*Field{ + { + Name: "id", + Type: bigquery.StringFieldType, + Mode: ModeNullable, + }, + { + Name: "version", + Type: bigquery.StringFieldType, + Mode: ModeNullable, + }, + }, + }, + }, + }, + }, + }, + } { + tt := tt + t.Run(tt.name, func(t *testing.T) { + got, err := MarshalSchema(tt.schema) + assert.NilError(t, err) + want, err := json.Marshal(tt.expected) + assert.NilError(t, err) + assert.Equal(t, string(got), string(want)) + }) + } +}