From 91e76dc05cbfb8d14d0bffe66671fb9a80804121 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Sat, 16 Dec 2023 13:20:41 -0800 Subject: [PATCH] Add more tests. --- lib/typing/redshift.go | 1 - lib/typing/redshift_test.go | 84 +++++++++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/lib/typing/redshift.go b/lib/typing/redshift.go index 8b49c6f9c..a932afc5e 100644 --- a/lib/typing/redshift.go +++ b/lib/typing/redshift.go @@ -14,7 +14,6 @@ func RedshiftTypeToKind(rawType string, stringPrecision string) KindDetails { return ParseNumeric(defaultPrefix, rawType) } - // TODO: Add test if strings.Contains(rawType, "character varying") { var strPrecision *int precision, err := strconv.Atoi(stringPrecision) diff --git a/lib/typing/redshift_test.go b/lib/typing/redshift_test.go index ac255d217..b3db6e5de 100644 --- a/lib/typing/redshift_test.go +++ b/lib/typing/redshift_test.go @@ -3,63 +3,101 @@ package typing import ( "testing" + "github.com/artie-labs/transfer/lib/ptr" + "github.com/stretchr/testify/assert" ) func TestRedshiftTypeToKind(t *testing.T) { + type rawTypeAndPrecision struct { + rawType string + precision string + } + type _testCase struct { name string - rawTypes []string + rawTypes []rawTypeAndPrecision expectedKd KindDetails } testCases := []_testCase{ { - name: "Integer", - rawTypes: []string{"integer", "bigint", "INTEGER"}, + name: "Integer", + rawTypes: []rawTypeAndPrecision{ + {rawType: "integer"}, + {rawType: "bigint"}, + {rawType: "INTEGER"}, + }, expectedKd: Integer, }, { - name: "String", - rawTypes: []string{"character varying"}, - expectedKd: String, - }, - { - name: "String", - rawTypes: []string{"character varying"}, + name: "String w/o precision", + rawTypes: []rawTypeAndPrecision{ + {rawType: "character varying"}, + {rawType: "character varying(65535)"}, + { + rawType: "character varying", + precision: "not a number", + }, + }, expectedKd: String, }, { - name: "String", - rawTypes: []string{"character varying(65535)"}, - expectedKd: String, + name: "String w/ precision", + rawTypes: []rawTypeAndPrecision{ + { + rawType: "character varying", + precision: "65535", + }, + }, + expectedKd: KindDetails{ + Kind: String.Kind, + OptionalRedshiftStrPrecision: ptr.ToInt(65535), + }, }, { - name: "Double Precision", - rawTypes: []string{"double precision", "DOUBLE precision"}, + name: "Double Precision", + rawTypes: []rawTypeAndPrecision{ + {rawType: "double precision"}, + {rawType: "DOUBLE precision"}, + }, expectedKd: Float, }, { - name: "Time", - rawTypes: []string{"timestamp with time zone", "timestamp without time zone", "time without time zone", "date"}, + name: "Time", + rawTypes: []rawTypeAndPrecision{ + {rawType: "timestamp with time zone"}, + {rawType: "timestamp without time zone"}, + {rawType: "time without time zone"}, + {rawType: "date"}, + }, expectedKd: ETime, }, { - name: "Boolean", - rawTypes: []string{"boolean"}, + name: "Boolean", + rawTypes: []rawTypeAndPrecision{ + {rawType: "boolean"}, + }, expectedKd: Boolean, }, { - name: "numeric", - rawTypes: []string{"numeric(5,2)", "numeric(5,5)"}, + name: "numeric", + rawTypes: []rawTypeAndPrecision{ + {rawType: "numeric(5,2)"}, + {rawType: "numeric(5,5)"}, + }, expectedKd: EDecimal, }, } for _, testCase := range testCases { - for _, rawType := range testCase.rawTypes { - kd := RedshiftTypeToKind(rawType, "") + for _, rawTypeAndPrec := range testCase.rawTypes { + kd := RedshiftTypeToKind(rawTypeAndPrec.rawType, rawTypeAndPrec.precision) assert.Equal(t, testCase.expectedKd.Kind, kd.Kind, testCase.name) + + if kd.OptionalRedshiftStrPrecision != nil { + assert.Equal(t, *testCase.expectedKd.OptionalRedshiftStrPrecision, *kd.OptionalRedshiftStrPrecision, testCase.name) + } } } }