Skip to content

Commit

Permalink
SNOW-859547: Refactor tests to create a new test for each test case (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pfus authored Aug 16, 2023
1 parent 0e01460 commit 4bc6cd4
Show file tree
Hide file tree
Showing 17 changed files with 483 additions and 403 deletions.
22 changes: 12 additions & 10 deletions azure_storage_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ func TestExtractContainerNameAndPath(t *testing.T) {
{"sfc-dev1-regression///", "sfc-dev1-regression", "//"},
}
for _, test := range testcases {
azureLoc, err := azureUtil.extractContainerNameAndPath(test.in)
if err != nil {
t.Error(err)
}
if azureLoc.containerName != test.bucket {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.bucket, azureLoc.containerName)
}
if azureLoc.path != test.path {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.path, azureLoc.path)
}
t.Run(test.in, func(t *testing.T) {
azureLoc, err := azureUtil.extractContainerNameAndPath(test.in)
if err != nil {
t.Error(err)
}
if azureLoc.containerName != test.bucket {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.bucket, azureLoc.containerName)
}
if azureLoc.path != test.path {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.path, azureLoc.path)
}
})
}
}

Expand Down
41 changes: 20 additions & 21 deletions bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,37 @@ func TestBindingFloat64(t *testing.T) {
var out float64
var rows *RowsExtended
for _, v := range types {
dbt.mustExec(fmt.Sprintf("CREATE OR REPLACE TABLE test (id int, value %v)", v))
dbt.mustExec("INSERT INTO test VALUES (1, ?)", expected)
rows = dbt.mustQuery("SELECT value FROM test WHERE id = ?", 1)
defer rows.Close()
if rows.Next() {
rows.Scan(&out)
if expected != out {
dbt.Errorf("%s: %g != %g", v, expected, out)
t.Run(v, func(t *testing.T) {
dbt.mustExec(fmt.Sprintf("CREATE OR REPLACE TABLE test (id int, value %v)", v))
dbt.mustExec("INSERT INTO test VALUES (1, ?)", expected)
rows = dbt.mustQuery("SELECT value FROM test WHERE id = ?", 1)
defer rows.Close()
if rows.Next() {
rows.Scan(&out)
if expected != out {
dbt.Errorf("%s: %g != %g", v, expected, out)
}
} else {
dbt.Errorf("%s: no data", v)
}
} else {
dbt.Errorf("%s: no data", v)
}
dbt.mustExec("DROP TABLE IF EXISTS test")
})
}
dbt.mustExec("DROP TABLE IF EXISTS test")
})
}

// TestBindingUint64 tests uint64 binding. Should fail as unit64 is not a
// supported binding value by Go's sql package.
func TestBindingUint64(t *testing.T) {
runDBTest(t, func(dbt *DBTest) {
types := []string{"INTEGER"}
expected := uint64(18446744073709551615)
for _, v := range types {
dbt.mustExec(fmt.Sprintf("CREATE OR REPLACE TABLE test (id int, value %v)", v))
if _, err := dbt.exec("INSERT INTO test VALUES (1, ?)", expected); err == nil {
dbt.Fatal("should fail as uint64 values with high bit set are not supported.")
} else {
logger.Infof("expected err: %v", err)
}
dbt.mustExec("DROP TABLE IF EXISTS test")
dbt.mustExec("CREATE OR REPLACE TABLE test (id int, value INTEGER)")
if _, err := dbt.exec("INSERT INTO test VALUES (1, ?)", expected); err == nil {
dbt.Fatal("should fail as uint64 values with high bit set are not supported.")
} else {
logger.Infof("expected err: %v", err)
}
dbt.mustExec("DROP TABLE IF EXISTS test")
})
}

Expand Down
84 changes: 48 additions & 36 deletions converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ func TestGoTypeToSnowflake(t *testing.T) {
{in: nil, tmode: nullType, out: unSupportedType},
}
for _, test := range testcases {
a := goTypeToSnowflake(test.in, test.tmode)
if a != test.out {
t.Errorf("failed. in: %v, tmode: %v, expected: %v, got: %v", test.in, test.tmode, test.out, a)
}
t.Run(fmt.Sprintf("%v_%v_%v", test.in, test.out, test.tmode), func(t *testing.T) {
a := goTypeToSnowflake(test.in, test.tmode)
if a != test.out {
t.Errorf("failed. in: %v, tmode: %v, expected: %v, got: %v", test.in, test.tmode, test.out, a)
}
})
}
}

Expand Down Expand Up @@ -160,11 +162,13 @@ func TestSnowflakeTypeToGo(t *testing.T) {
{in: sliceType, scale: 0, out: reflect.TypeOf("")},
}
for _, test := range testcases {
a := snowflakeTypeToGo(test.in, test.scale)
if a != test.out {
t.Errorf("failed. in: %v, scale: %v, expected: %v, got: %v",
test.in, test.scale, test.out, a)
}
t.Run(fmt.Sprintf("%v_%v", test.in, test.out), func(t *testing.T) {
a := snowflakeTypeToGo(test.in, test.scale)
if a != test.out {
t.Errorf("failed. in: %v, scale: %v, expected: %v, got: %v",
test.in, test.scale, test.out, a)
}
})
}
}

Expand Down Expand Up @@ -263,12 +267,14 @@ func TestStringToValue(t *testing.T) {
}

for _, tt := range types {
rowType = &execResponseRowType{
Type: tt,
}
if err = stringToValue(&dest, *rowType, &source, nil); err == nil {
t.Errorf("should raise error. type: %v, value:%v", tt, source)
}
t.Run(tt, func(t *testing.T) {
rowType = &execResponseRowType{
Type: tt,
}
if err = stringToValue(&dest, *rowType, &source, nil); err == nil {
t.Errorf("should raise error. type: %v, value:%v", tt, source)
}
})
}

sources := []string{
Expand All @@ -282,12 +288,14 @@ func TestStringToValue(t *testing.T) {

for _, ss := range sources {
for _, tt := range types {
rowType = &execResponseRowType{
Type: tt,
}
if err = stringToValue(&dest, *rowType, &ss, nil); err == nil {
t.Errorf("should raise error. type: %v, value:%v", tt, source)
}
t.Run(ss+tt, func(t *testing.T) {
rowType = &execResponseRowType{
Type: tt,
}
if err = stringToValue(&dest, *rowType, &ss, nil); err == nil {
t.Errorf("should raise error. type: %v, value:%v", tt, source)
}
})
}
}

Expand Down Expand Up @@ -318,15 +326,17 @@ func TestArrayToString(t *testing.T) {
{in: driver.NamedValue{Value: &stringArray{"foo", "bar", "baz"}}, typ: textType, out: []string{"foo", "bar", "baz"}},
}
for _, test := range testcases {
s, a := snowflakeArrayToString(&test.in, false)
if s != test.typ {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.typ, s)
}
for i, v := range a {
if *v != test.out[i] {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.out[i], a)
t.Run(strings.Join(test.out, "_"), func(t *testing.T) {
s, a := snowflakeArrayToString(&test.in, false)
if s != test.typ {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.typ, s)
}
}
for i, v := range a {
if *v != test.out[i] {
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.out[i], a)
}
}
})
}
}

Expand Down Expand Up @@ -1377,12 +1387,14 @@ func TestTimeTypeValueToString(t *testing.T) {
}

for _, tc := range testcases {
output, err := timeTypeValueToString(tc.in, tc.tsmode)
if err != nil {
t.Error(err)
}
if strings.Compare(tc.out, *output) != 0 {
t.Errorf("failed to convert time %v of type %v. expected: %v, received: %v", tc.in, tc.tsmode, tc.out, *output)
}
t.Run(tc.out, func(t *testing.T) {
output, err := timeTypeValueToString(tc.in, tc.tsmode)
if err != nil {
t.Error(err)
}
if strings.Compare(tc.out, *output) != 0 {
t.Errorf("failed to convert time %v of type %v. expected: %v, received: %v", tc.in, tc.tsmode, tc.out, *output)
}
})
}
}
26 changes: 14 additions & 12 deletions datatype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ func TestDataTypeMode(t *testing.T) {
err: fmt.Errorf(errMsgInvalidByteArray, 123)},
}
for _, ts := range testcases {
tmode, err := dataTypeMode(ts.tp)
if ts.err == nil {
if err != nil {
t.Errorf("failed to get datatype mode: %v", err)
t.Run(fmt.Sprintf("%v_%v", ts.tp, ts.tmode), func(t *testing.T) {
tmode, err := dataTypeMode(ts.tp)
if ts.err == nil {
if err != nil {
t.Errorf("failed to get datatype mode: %v", err)
}
if tmode != ts.tmode {
t.Errorf("wrong data type: %v", tmode)
}
} else {
if err == nil {
t.Errorf("should raise an error: %v", ts.err)
}
}
if tmode != ts.tmode {
t.Errorf("wrong data type: %v", tmode)
}
} else {
if err == nil {
t.Errorf("should raise an error: %v", ts.err)
}
}
})
}
}

Expand Down
Loading

0 comments on commit 4bc6cd4

Please sign in to comment.