Skip to content

Commit 4bc6cd4

Browse files
authored
SNOW-859547: Refactor tests to create a new test for each test case (#877)
1 parent 0e01460 commit 4bc6cd4

17 files changed

+483
-403
lines changed

azure_storage_client_test.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ func TestExtractContainerNameAndPath(t *testing.T) {
2828
{"sfc-dev1-regression///", "sfc-dev1-regression", "//"},
2929
}
3030
for _, test := range testcases {
31-
azureLoc, err := azureUtil.extractContainerNameAndPath(test.in)
32-
if err != nil {
33-
t.Error(err)
34-
}
35-
if azureLoc.containerName != test.bucket {
36-
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.bucket, azureLoc.containerName)
37-
}
38-
if azureLoc.path != test.path {
39-
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.path, azureLoc.path)
40-
}
31+
t.Run(test.in, func(t *testing.T) {
32+
azureLoc, err := azureUtil.extractContainerNameAndPath(test.in)
33+
if err != nil {
34+
t.Error(err)
35+
}
36+
if azureLoc.containerName != test.bucket {
37+
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.bucket, azureLoc.containerName)
38+
}
39+
if azureLoc.path != test.path {
40+
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.path, azureLoc.path)
41+
}
42+
})
4143
}
4244
}
4345

bindings_test.go

+20-21
Original file line numberDiff line numberDiff line change
@@ -43,38 +43,37 @@ func TestBindingFloat64(t *testing.T) {
4343
var out float64
4444
var rows *RowsExtended
4545
for _, v := range types {
46-
dbt.mustExec(fmt.Sprintf("CREATE OR REPLACE TABLE test (id int, value %v)", v))
47-
dbt.mustExec("INSERT INTO test VALUES (1, ?)", expected)
48-
rows = dbt.mustQuery("SELECT value FROM test WHERE id = ?", 1)
49-
defer rows.Close()
50-
if rows.Next() {
51-
rows.Scan(&out)
52-
if expected != out {
53-
dbt.Errorf("%s: %g != %g", v, expected, out)
46+
t.Run(v, func(t *testing.T) {
47+
dbt.mustExec(fmt.Sprintf("CREATE OR REPLACE TABLE test (id int, value %v)", v))
48+
dbt.mustExec("INSERT INTO test VALUES (1, ?)", expected)
49+
rows = dbt.mustQuery("SELECT value FROM test WHERE id = ?", 1)
50+
defer rows.Close()
51+
if rows.Next() {
52+
rows.Scan(&out)
53+
if expected != out {
54+
dbt.Errorf("%s: %g != %g", v, expected, out)
55+
}
56+
} else {
57+
dbt.Errorf("%s: no data", v)
5458
}
55-
} else {
56-
dbt.Errorf("%s: no data", v)
57-
}
58-
dbt.mustExec("DROP TABLE IF EXISTS test")
59+
})
5960
}
61+
dbt.mustExec("DROP TABLE IF EXISTS test")
6062
})
6163
}
6264

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

converter_test.go

+48-36
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ func TestGoTypeToSnowflake(t *testing.T) {
128128
{in: nil, tmode: nullType, out: unSupportedType},
129129
}
130130
for _, test := range testcases {
131-
a := goTypeToSnowflake(test.in, test.tmode)
132-
if a != test.out {
133-
t.Errorf("failed. in: %v, tmode: %v, expected: %v, got: %v", test.in, test.tmode, test.out, a)
134-
}
131+
t.Run(fmt.Sprintf("%v_%v_%v", test.in, test.out, test.tmode), func(t *testing.T) {
132+
a := goTypeToSnowflake(test.in, test.tmode)
133+
if a != test.out {
134+
t.Errorf("failed. in: %v, tmode: %v, expected: %v, got: %v", test.in, test.tmode, test.out, a)
135+
}
136+
})
135137
}
136138
}
137139

@@ -160,11 +162,13 @@ func TestSnowflakeTypeToGo(t *testing.T) {
160162
{in: sliceType, scale: 0, out: reflect.TypeOf("")},
161163
}
162164
for _, test := range testcases {
163-
a := snowflakeTypeToGo(test.in, test.scale)
164-
if a != test.out {
165-
t.Errorf("failed. in: %v, scale: %v, expected: %v, got: %v",
166-
test.in, test.scale, test.out, a)
167-
}
165+
t.Run(fmt.Sprintf("%v_%v", test.in, test.out), func(t *testing.T) {
166+
a := snowflakeTypeToGo(test.in, test.scale)
167+
if a != test.out {
168+
t.Errorf("failed. in: %v, scale: %v, expected: %v, got: %v",
169+
test.in, test.scale, test.out, a)
170+
}
171+
})
168172
}
169173
}
170174

@@ -263,12 +267,14 @@ func TestStringToValue(t *testing.T) {
263267
}
264268

265269
for _, tt := range types {
266-
rowType = &execResponseRowType{
267-
Type: tt,
268-
}
269-
if err = stringToValue(&dest, *rowType, &source, nil); err == nil {
270-
t.Errorf("should raise error. type: %v, value:%v", tt, source)
271-
}
270+
t.Run(tt, func(t *testing.T) {
271+
rowType = &execResponseRowType{
272+
Type: tt,
273+
}
274+
if err = stringToValue(&dest, *rowType, &source, nil); err == nil {
275+
t.Errorf("should raise error. type: %v, value:%v", tt, source)
276+
}
277+
})
272278
}
273279

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

283289
for _, ss := range sources {
284290
for _, tt := range types {
285-
rowType = &execResponseRowType{
286-
Type: tt,
287-
}
288-
if err = stringToValue(&dest, *rowType, &ss, nil); err == nil {
289-
t.Errorf("should raise error. type: %v, value:%v", tt, source)
290-
}
291+
t.Run(ss+tt, func(t *testing.T) {
292+
rowType = &execResponseRowType{
293+
Type: tt,
294+
}
295+
if err = stringToValue(&dest, *rowType, &ss, nil); err == nil {
296+
t.Errorf("should raise error. type: %v, value:%v", tt, source)
297+
}
298+
})
291299
}
292300
}
293301

@@ -318,15 +326,17 @@ func TestArrayToString(t *testing.T) {
318326
{in: driver.NamedValue{Value: &stringArray{"foo", "bar", "baz"}}, typ: textType, out: []string{"foo", "bar", "baz"}},
319327
}
320328
for _, test := range testcases {
321-
s, a := snowflakeArrayToString(&test.in, false)
322-
if s != test.typ {
323-
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.typ, s)
324-
}
325-
for i, v := range a {
326-
if *v != test.out[i] {
327-
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.out[i], a)
329+
t.Run(strings.Join(test.out, "_"), func(t *testing.T) {
330+
s, a := snowflakeArrayToString(&test.in, false)
331+
if s != test.typ {
332+
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.typ, s)
328333
}
329-
}
334+
for i, v := range a {
335+
if *v != test.out[i] {
336+
t.Errorf("failed. in: %v, expected: %v, got: %v", test.in, test.out[i], a)
337+
}
338+
}
339+
})
330340
}
331341
}
332342

@@ -1377,12 +1387,14 @@ func TestTimeTypeValueToString(t *testing.T) {
13771387
}
13781388

13791389
for _, tc := range testcases {
1380-
output, err := timeTypeValueToString(tc.in, tc.tsmode)
1381-
if err != nil {
1382-
t.Error(err)
1383-
}
1384-
if strings.Compare(tc.out, *output) != 0 {
1385-
t.Errorf("failed to convert time %v of type %v. expected: %v, received: %v", tc.in, tc.tsmode, tc.out, *output)
1386-
}
1390+
t.Run(tc.out, func(t *testing.T) {
1391+
output, err := timeTypeValueToString(tc.in, tc.tsmode)
1392+
if err != nil {
1393+
t.Error(err)
1394+
}
1395+
if strings.Compare(tc.out, *output) != 0 {
1396+
t.Errorf("failed to convert time %v of type %v. expected: %v, received: %v", tc.in, tc.tsmode, tc.out, *output)
1397+
}
1398+
})
13871399
}
13881400
}

datatype_test.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,21 @@ func TestDataTypeMode(t *testing.T) {
3030
err: fmt.Errorf(errMsgInvalidByteArray, 123)},
3131
}
3232
for _, ts := range testcases {
33-
tmode, err := dataTypeMode(ts.tp)
34-
if ts.err == nil {
35-
if err != nil {
36-
t.Errorf("failed to get datatype mode: %v", err)
33+
t.Run(fmt.Sprintf("%v_%v", ts.tp, ts.tmode), func(t *testing.T) {
34+
tmode, err := dataTypeMode(ts.tp)
35+
if ts.err == nil {
36+
if err != nil {
37+
t.Errorf("failed to get datatype mode: %v", err)
38+
}
39+
if tmode != ts.tmode {
40+
t.Errorf("wrong data type: %v", tmode)
41+
}
42+
} else {
43+
if err == nil {
44+
t.Errorf("should raise an error: %v", ts.err)
45+
}
3746
}
38-
if tmode != ts.tmode {
39-
t.Errorf("wrong data type: %v", tmode)
40-
}
41-
} else {
42-
if err == nil {
43-
t.Errorf("should raise an error: %v", ts.err)
44-
}
45-
}
47+
})
4648
}
4749
}
4850

0 commit comments

Comments
 (0)