Skip to content

Commit 6966ca0

Browse files
committed
sqlite3_test.go: Fix go test -run=...: Use standard sub-tests
Selecting only some tests with go test -run=... does not work, because some of the tests are executed using testing.RunTests(). That function is documented as "an internal function". This changes TestSuite to use the testing subtests feature instead. This has a behaviour change: the benchmarks now need to be selected at the command line with the standard go test -bench=. flag. This will also set up the test database twice when running benchmarks, rather than once.
1 parent 92f580b commit 6966ca0

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

sqlite3_test.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"time"
2727
)
2828

29-
func TempFilename(t *testing.T) string {
29+
func TempFilename(t testing.TB) string {
3030
f, err := ioutil.TempFile("", "go-sqlite3-test-")
3131
if err != nil {
3232
t.Fatal(err)
@@ -1850,28 +1850,21 @@ func BenchmarkCustomFunctions(b *testing.B) {
18501850
}
18511851

18521852
func TestSuite(t *testing.T) {
1853-
tempFilename := TempFilename(t)
1854-
defer os.Remove(tempFilename)
1855-
d, err := sql.Open("sqlite3", tempFilename+"?_busy_timeout=99999")
1856-
if err != nil {
1857-
t.Fatal(err)
1858-
}
1859-
defer d.Close()
1853+
initializeTestDB(t)
1854+
defer freeTestDB()
18601855

1861-
db = &TestDB{t, d, SQLITE, sync.Once{}}
1862-
ok := testing.RunTests(func(string, string) (bool, error) { return true, nil }, tests)
1863-
if !ok {
1864-
t.Fatal("A subtest failed")
1856+
for _, test := range tests {
1857+
t.Run(test.Name, test.F)
18651858
}
1859+
}
18661860

1867-
if !testing.Short() {
1868-
for _, b := range benchmarks {
1869-
fmt.Printf("%-20s", b.Name)
1870-
r := testing.Benchmark(b.F)
1871-
fmt.Printf("%10d %10.0f req/s\n", r.N, float64(r.N)/r.T.Seconds())
1872-
}
1861+
func BenchmarkSuite(b *testing.B) {
1862+
initializeTestDB(b)
1863+
defer freeTestDB()
1864+
1865+
for _, benchmark := range benchmarks {
1866+
b.Run(benchmark.Name, benchmark.F)
18731867
}
1874-
db.tearDown()
18751868
}
18761869

18771870
// Dialect is a type of dialect of databases.
@@ -1886,14 +1879,37 @@ const (
18861879

18871880
// DB provide context for the tests
18881881
type TestDB struct {
1889-
*testing.T
1882+
testing.TB
18901883
*sql.DB
1891-
dialect Dialect
1892-
once sync.Once
1884+
dialect Dialect
1885+
once sync.Once
1886+
tempFilename string
18931887
}
18941888

18951889
var db *TestDB
18961890

1891+
func initializeTestDB(t testing.TB) {
1892+
tempFilename := TempFilename(t)
1893+
d, err := sql.Open("sqlite3", tempFilename+"?_busy_timeout=99999")
1894+
if err != nil {
1895+
os.Remove(tempFilename)
1896+
t.Fatal(err)
1897+
}
1898+
1899+
db = &TestDB{t, d, SQLITE, sync.Once{}, tempFilename}
1900+
}
1901+
1902+
func freeTestDB() {
1903+
err := db.DB.Close()
1904+
if err != nil {
1905+
panic(err)
1906+
}
1907+
err = os.Remove(db.tempFilename)
1908+
if err != nil {
1909+
panic(err)
1910+
}
1911+
}
1912+
18971913
// the following tables will be created and dropped during the test
18981914
var testTables = []string{"foo", "bar", "t", "bench"}
18991915

0 commit comments

Comments
 (0)