github.com/blend/go-sdk@v1.20220411.3/tracing/dbtrace/main_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package dbtrace
     9  
    10  import (
    11  	"os"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/db"
    15  	"github.com/blend/go-sdk/logger"
    16  )
    17  
    18  func TestMain(m *testing.M) {
    19  	conn, err := db.New(db.OptConfigFromEnv())
    20  	if err != nil {
    21  		logger.FatalExit(err)
    22  	}
    23  	err = openDefaultDB(conn)
    24  	if err != nil {
    25  		logger.FatalExit(err)
    26  	}
    27  	defer func() { _ = conn.Close() }()
    28  	os.Exit(m.Run())
    29  }
    30  
    31  var (
    32  	defaultConnection *db.Connection
    33  )
    34  
    35  func setDefaultDB(conn *db.Connection) {
    36  	defaultConnection = conn
    37  }
    38  
    39  func defaultDB() *db.Connection {
    40  	return defaultConnection
    41  }
    42  
    43  func openDefaultDB(conn *db.Connection) error {
    44  	err := conn.Open()
    45  	if err != nil {
    46  		return err
    47  	}
    48  	setDefaultDB(conn)
    49  	return nil
    50  }