From 4bd885ed82aa0938e799f9dc45ab3d01ebb73845 Mon Sep 17 00:00:00 2001 From: Nathan <148575555+nathan-artie@users.noreply.github.com> Date: Thu, 13 Jun 2024 17:49:55 -0700 Subject: [PATCH] More tests --- clients/bigquery/storagewrite.go | 4 +- clients/bigquery/storagewrite_test.go | 71 +++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/clients/bigquery/storagewrite.go b/clients/bigquery/storagewrite.go index 4a2765af2..de31b417c 100644 --- a/clients/bigquery/storagewrite.go +++ b/clients/bigquery/storagewrite.go @@ -40,7 +40,7 @@ func columnToTableFieldSchema(column columns.Column) (*storagepb.TableFieldSchem case ext.DateTimeKindType: fieldType = storagepb.TableFieldSchema_TIMESTAMP default: - return nil, fmt.Errorf("unsupported extended time details type: %s", column.KindDetails.ExtendedTimeDetails.Type) + return nil, fmt.Errorf("unsupported extended time details type: %q", column.KindDetails.ExtendedTimeDetails.Type) } case typing.Struct.Kind: fieldType = storagepb.TableFieldSchema_STRING @@ -48,7 +48,7 @@ func columnToTableFieldSchema(column columns.Column) (*storagepb.TableFieldSchem fieldType = storagepb.TableFieldSchema_STRING mode = storagepb.TableFieldSchema_REPEATED default: - return nil, fmt.Errorf("unsupported column kind: %s", column.KindDetails.Kind) + return nil, fmt.Errorf("unsupported column kind: %q", column.KindDetails.Kind) } return &storagepb.TableFieldSchema{ diff --git a/clients/bigquery/storagewrite_test.go b/clients/bigquery/storagewrite_test.go index 967a6020b..53c83a268 100644 --- a/clients/bigquery/storagewrite_test.go +++ b/clients/bigquery/storagewrite_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "cloud.google.com/go/bigquery/storage/apiv1/storagepb" "github.com/artie-labs/transfer/lib/typing" "github.com/artie-labs/transfer/lib/typing/columns" "github.com/artie-labs/transfer/lib/typing/decimal" @@ -14,6 +15,76 @@ import ( "google.golang.org/protobuf/encoding/protojson" ) +func TestColumnToTableFieldSchema(t *testing.T) { + { + // Boolean: + fieldSchema, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.Boolean)) + assert.NoError(t, err) + assert.Equal(t, "foo", fieldSchema.Name) + assert.Equal(t, storagepb.TableFieldSchema_NULLABLE, fieldSchema.Mode) + assert.Equal(t, storagepb.TableFieldSchema_BOOL, fieldSchema.Type) + } + { + // Integer: + fieldSchema, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.Integer)) + assert.NoError(t, err) + assert.Equal(t, storagepb.TableFieldSchema_INT64, fieldSchema.Type) + } + { + // Float: + fieldSchema, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.Float)) + assert.NoError(t, err) + assert.Equal(t, storagepb.TableFieldSchema_DOUBLE, fieldSchema.Type) + } + { + // EDecimal: + fieldSchema, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.EDecimal)) + assert.NoError(t, err) + assert.Equal(t, storagepb.TableFieldSchema_STRING, fieldSchema.Type) + } + { + // ETime - Time: + fieldSchema, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.NewKindDetailsFromTemplate(typing.ETime, ext.TimeKindType))) + assert.NoError(t, err) + assert.Equal(t, storagepb.TableFieldSchema_TIME, fieldSchema.Type) + } + { + // ETime - Date: + fieldSchema, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateKindType))) + assert.NoError(t, err) + assert.Equal(t, storagepb.TableFieldSchema_DATE, fieldSchema.Type) + } + { + // ETime - DateTime: + fieldSchema, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateTimeKindType))) + assert.NoError(t, err) + assert.Equal(t, storagepb.TableFieldSchema_TIMESTAMP, fieldSchema.Type) + } + { + // ETime - Invalid: + _, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.NewKindDetailsFromTemplate(typing.ETime, ""))) + assert.ErrorContains(t, err, "unsupported extended time details type:") + } + { + // Struct: + fieldSchema, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.Struct)) + assert.NoError(t, err) + assert.Equal(t, storagepb.TableFieldSchema_STRING, fieldSchema.Type) + } + { + // Array: + fieldSchema, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.Array)) + assert.NoError(t, err) + assert.Equal(t, storagepb.TableFieldSchema_STRING, fieldSchema.Type) + assert.Equal(t, storagepb.TableFieldSchema_REPEATED, fieldSchema.Mode) + } + { + // Invalid: + _, err := columnToTableFieldSchema(columns.NewColumn("foo", typing.KindDetails{})) + assert.ErrorContains(t, err, "unsupported column kind: ") + } +} + func TestEncodePacked64TimeMicros(t *testing.T) { epoch := time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)