Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Dec 16, 2023
1 parent cbecb98 commit 91e76dc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 24 deletions.
1 change: 0 additions & 1 deletion lib/typing/redshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
84 changes: 61 additions & 23 deletions lib/typing/redshift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

0 comments on commit 91e76dc

Please sign in to comment.