forked from mirror/go-sqlite3
sqlite3_test.go: Fix go test -run=...: Use standard sub-tests (#881)
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.
This commit is contained in:
parent
2b131e01c1
commit
a4fc68a6cb
|
@ -27,7 +27,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TempFilename(t *testing.T) string {
|
func TempFilename(t testing.TB) string {
|
||||||
f, err := ioutil.TempFile("", "go-sqlite3-test-")
|
f, err := ioutil.TempFile("", "go-sqlite3-test-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -1888,29 +1888,22 @@ func BenchmarkCustomFunctions(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSuite(t *testing.T) {
|
func TestSuite(t *testing.T) {
|
||||||
tempFilename := TempFilename(t)
|
initializeTestDB(t)
|
||||||
defer os.Remove(tempFilename)
|
defer freeTestDB()
|
||||||
d, err := sql.Open("sqlite3", tempFilename+"?_busy_timeout=99999")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
defer d.Close()
|
|
||||||
|
|
||||||
db = &TestDB{t, d, SQLITE, sync.Once{}}
|
for _, test := range tests {
|
||||||
ok := testing.RunTests(func(string, string) (bool, error) { return true, nil }, tests)
|
t.Run(test.Name, test.F)
|
||||||
if !ok {
|
}
|
||||||
t.Fatal("A subtest failed")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !testing.Short() {
|
func BenchmarkSuite(b *testing.B) {
|
||||||
for _, b := range benchmarks {
|
initializeTestDB(b)
|
||||||
fmt.Printf("%-20s", b.Name)
|
defer freeTestDB()
|
||||||
r := testing.Benchmark(b.F)
|
|
||||||
fmt.Printf("%10d %10.0f req/s\n", r.N, float64(r.N)/r.T.Seconds())
|
for _, benchmark := range benchmarks {
|
||||||
|
b.Run(benchmark.Name, benchmark.F)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
db.tearDown()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dialect is a type of dialect of databases.
|
// Dialect is a type of dialect of databases.
|
||||||
type Dialect int
|
type Dialect int
|
||||||
|
@ -1924,14 +1917,37 @@ const (
|
||||||
|
|
||||||
// DB provide context for the tests
|
// DB provide context for the tests
|
||||||
type TestDB struct {
|
type TestDB struct {
|
||||||
*testing.T
|
testing.TB
|
||||||
*sql.DB
|
*sql.DB
|
||||||
dialect Dialect
|
dialect Dialect
|
||||||
once sync.Once
|
once sync.Once
|
||||||
|
tempFilename string
|
||||||
}
|
}
|
||||||
|
|
||||||
var db *TestDB
|
var db *TestDB
|
||||||
|
|
||||||
|
func initializeTestDB(t testing.TB) {
|
||||||
|
tempFilename := TempFilename(t)
|
||||||
|
d, err := sql.Open("sqlite3", tempFilename+"?_busy_timeout=99999")
|
||||||
|
if err != nil {
|
||||||
|
os.Remove(tempFilename)
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
db = &TestDB{t, d, SQLITE, sync.Once{}, tempFilename}
|
||||||
|
}
|
||||||
|
|
||||||
|
func freeTestDB() {
|
||||||
|
err := db.DB.Close()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = os.Remove(db.tempFilename)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// the following tables will be created and dropped during the test
|
// the following tables will be created and dropped during the test
|
||||||
var testTables = []string{"foo", "bar", "t", "bench"}
|
var testTables = []string{"foo", "bar", "t", "bench"}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue