aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvan Jones <evan.jones@datadoghq.com>2021-10-25 11:24:46 -0400
committerGitHub <noreply@github.com>2021-10-26 00:24:46 +0900
commita4fc68a6cbf6144b492b97440471ce8d4ab42b66 (patch)
tree80b2f7d3958ac80c1698867c2b4ca17478191f4b
parentchange angle bracket import to quotes (#868) (diff)
downloadgolite-a4fc68a6cbf6144b492b97440471ce8d4ab42b66.tar.gz
golite-a4fc68a6cbf6144b492b97440471ce8d4ab42b66.tar.xz
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.
-rw-r--r--sqlite3_test.go60
1 files changed, 38 insertions, 22 deletions
diff --git a/sqlite3_test.go b/sqlite3_test.go
index ba4c26f..c5deb3e 100644
--- a/sqlite3_test.go
+++ b/sqlite3_test.go
@@ -27,7 +27,7 @@ import (
"time"
)
-func TempFilename(t *testing.T) string {
+func TempFilename(t testing.TB) string {
f, err := ioutil.TempFile("", "go-sqlite3-test-")
if err != nil {
t.Fatal(err)
@@ -1888,28 +1888,21 @@ func BenchmarkCustomFunctions(b *testing.B) {
}
func TestSuite(t *testing.T) {
- tempFilename := TempFilename(t)
- defer os.Remove(tempFilename)
- d, err := sql.Open("sqlite3", tempFilename+"?_busy_timeout=99999")
- if err != nil {
- t.Fatal(err)
- }
- defer d.Close()
+ initializeTestDB(t)
+ defer freeTestDB()
- db = &TestDB{t, d, SQLITE, sync.Once{}}
- ok := testing.RunTests(func(string, string) (bool, error) { return true, nil }, tests)
- if !ok {
- t.Fatal("A subtest failed")
+ for _, test := range tests {
+ t.Run(test.Name, test.F)
}
+}
- if !testing.Short() {
- for _, b := range benchmarks {
- fmt.Printf("%-20s", b.Name)
- r := testing.Benchmark(b.F)
- fmt.Printf("%10d %10.0f req/s\n", r.N, float64(r.N)/r.T.Seconds())
- }
+func BenchmarkSuite(b *testing.B) {
+ initializeTestDB(b)
+ defer freeTestDB()
+
+ for _, benchmark := range benchmarks {
+ b.Run(benchmark.Name, benchmark.F)
}
- db.tearDown()
}
// Dialect is a type of dialect of databases.
@@ -1924,14 +1917,37 @@ const (
// DB provide context for the tests
type TestDB struct {
- *testing.T
+ testing.TB
*sql.DB
- dialect Dialect
- once sync.Once
+ dialect Dialect
+ once sync.Once
+ tempFilename string
}
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
var testTables = []string{"foo", "bar", "t", "bench"}