github.com/jgbaldwinbrown/perf@v0.1.1/storage/db/dbtest/dbtest.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build cgo 6 // +build cgo 7 8 package dbtest 9 10 import ( 11 "testing" 12 13 "golang.org/x/perf/storage/db" 14 _ "golang.org/x/perf/storage/db/sqlite3" 15 ) 16 17 // NewDB makes a connection to a testing database, either sqlite3 or 18 // Cloud SQL depending on the -cloud flag. cleanup must be called when 19 // done with the testing database, instead of calling db.Close() 20 func NewDB(t *testing.T) (*db.DB, func()) { 21 driverName, dataSourceName, cloudCleanup := createEmptyDB(t) 22 d, err := db.OpenSQL(driverName, dataSourceName) 23 if err != nil { 24 if cloudCleanup != nil { 25 cloudCleanup() 26 } 27 t.Fatalf("open database: %v", err) 28 } 29 30 cleanup := func() { 31 if cloudCleanup != nil { 32 cloudCleanup() 33 } 34 d.Close() 35 } 36 // Make sure the database really is empty. 37 uploads, err := d.CountUploads() 38 if err != nil { 39 cleanup() 40 t.Fatal(err) 41 } 42 if uploads != 0 { 43 cleanup() 44 t.Fatalf("found %d row(s) in Uploads, want 0", uploads) 45 } 46 return d, cleanup 47 }