Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit f5db374

Browse files
authored
test: use subtests for finer-grained test output (#16)
1 parent 44af73e commit f5db374

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

engine_test.go

+40-37
Original file line numberDiff line numberDiff line change
@@ -15,81 +15,84 @@ const (
1515
driverName = "engine_tests"
1616
)
1717

18-
func TestEngine_Query(t *testing.T) {
19-
e := newEngine(t)
20-
gosql.Register(driverName, e)
21-
testQuery(t, e,
18+
func TestQueries(t *testing.T) {
19+
testQuery(t,
2220
"SELECT i FROM mytable;",
2321
[][]interface{}{{int64(1)}, {int64(2)}, {int64(3)}},
2422
)
2523

26-
testQuery(t, e,
24+
testQuery(t,
2725
"SELECT i FROM mytable WHERE i = 2;",
2826
[][]interface{}{{int64(2)}},
2927
)
3028

31-
testQuery(t, e,
29+
testQuery(t,
3230
"SELECT i FROM mytable ORDER BY i DESC;",
3331
[][]interface{}{{int64(3)}, {int64(2)}, {int64(1)}},
3432
)
3533

36-
testQuery(t, e,
34+
testQuery(t,
3735
"SELECT i FROM mytable WHERE s = 'a' ORDER BY i DESC;",
3836
[][]interface{}{{int64(1)}},
3937
)
4038

41-
testQuery(t, e,
39+
testQuery(t,
4240
"SELECT i FROM mytable WHERE s = 'a' ORDER BY i DESC LIMIT 1;",
4341
[][]interface{}{{int64(1)}},
4442
)
4543

46-
testQuery(t, e,
44+
testQuery(t,
4745
"SELECT COUNT(*) FROM mytable;",
4846
[][]interface{}{{int64(3)}},
4947
)
5048

51-
testQuery(t, e,
49+
testQuery(t,
5250
"SELECT COUNT(*) AS c FROM mytable;",
5351
[][]interface{}{{int64(3)}},
5452
)
5553
}
5654

57-
func testQuery(t *testing.T, e *sqle.Engine, q string, r [][]interface{}) {
58-
assert := require.New(t)
55+
func testQuery(t *testing.T, q string, r [][]interface{}) {
56+
t.Run(q, func(t *testing.T) {
57+
assert := require.New(t)
5958

60-
db, err := gosql.Open(driverName, "")
61-
assert.NoError(err)
62-
defer func() { assert.NoError(db.Close()) }()
59+
e := newEngine(t)
60+
sqle.DefaultEngine = e
6361

64-
res, err := db.Query(q)
65-
assert.NoError(err)
66-
defer func() { assert.NoError(res.Close()) }()
62+
db, err := gosql.Open(sqle.DriverName, "")
63+
assert.NoError(err)
64+
defer func() { assert.NoError(db.Close()) }()
6765

68-
cols, err := res.Columns()
69-
assert.NoError(err)
70-
assert.Equal(len(r[0]), len(cols))
66+
res, err := db.Query(q)
67+
assert.NoError(err)
68+
defer func() { assert.NoError(res.Close()) }()
7169

72-
vals := make([]interface{}, len(cols))
73-
valPtrs := make([]interface{}, len(cols))
74-
for i := 0; i < len(cols); i++ {
75-
valPtrs[i] = &vals[i]
76-
}
70+
cols, err := res.Columns()
71+
assert.NoError(err)
72+
assert.Equal(len(r[0]), len(cols))
7773

78-
i := 0
79-
for {
80-
if !res.Next() {
81-
break
74+
vals := make([]interface{}, len(cols))
75+
valPtrs := make([]interface{}, len(cols))
76+
for i := 0; i < len(cols); i++ {
77+
valPtrs[i] = &vals[i]
8278
}
8379

84-
err := res.Scan(valPtrs...)
85-
assert.NoError(err)
80+
i := 0
81+
for {
82+
if !res.Next() {
83+
break
84+
}
85+
86+
err := res.Scan(valPtrs...)
87+
assert.NoError(err)
8688

87-
assert.Equal(r[i], vals)
88-
i++
89-
}
89+
assert.Equal(r[i], vals)
90+
i++
91+
}
9092

91-
assert.NoError(res.Err())
92-
assert.Equal(len(r), i)
93+
assert.NoError(res.Err())
94+
assert.Equal(len(r), i)
95+
})
9396
}
9497

9598
func newEngine(t *testing.T) *sqle.Engine {

sql/parse/parse_test.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,14 @@ var fixtures = map[string]sql.Node{
176176
}
177177

178178
func TestParse(t *testing.T) {
179-
assert := assert.New(t)
180179
for query, expectedPlan := range fixtures {
181-
p, err := Parse(query)
182-
assert.Nil(err, "error for query '%s'", query)
183-
assert.Exactly(expectedPlan, p,
184-
"plans do not match for query '%s'", query)
180+
t.Run(query, func(t *testing.T) {
181+
assert := assert.New(t)
182+
p, err := Parse(query)
183+
assert.Nil(err, "error for query '%s'", query)
184+
assert.Exactly(expectedPlan, p,
185+
"plans do not match for query '%s'", query)
186+
})
187+
185188
}
186189
}

0 commit comments

Comments
 (0)