golang.org/x/build@v0.0.0-20240506185731-218518f32b70/perfdata/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 7 package dbtest 8 9 import ( 10 "testing" 11 12 "golang.org/x/build/perfdata/db" 13 _ "golang.org/x/build/perfdata/db/sqlite3" 14 ) 15 16 // NewDB makes a connection to a testing database, either sqlite3 or 17 // Cloud SQL depending on the -cloud flag. cleanup must be called when 18 // done with the testing database, instead of calling db.Close() 19 func NewDB(t *testing.T) (*db.DB, func()) { 20 driverName, dataSourceName, cloudCleanup := createEmptyDB(t) 21 d, err := db.OpenSQL(driverName, dataSourceName) 22 if err != nil { 23 if cloudCleanup != nil { 24 cloudCleanup() 25 } 26 t.Fatalf("open database: %v", err) 27 } 28 29 cleanup := func() { 30 if cloudCleanup != nil { 31 cloudCleanup() 32 } 33 d.Close() 34 } 35 // Make sure the database really is empty. 36 uploads, err := d.CountUploads() 37 if err != nil { 38 cleanup() 39 t.Fatal(err) 40 } 41 if uploads != 0 { 42 cleanup() 43 t.Fatalf("found %d row(s) in Uploads, want 0", uploads) 44 } 45 return d, cleanup 46 }