github.com/blend/go-sdk@v1.20220411.3/testutil/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 testutil_test
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/db"
    15  	"github.com/blend/go-sdk/logger"
    16  	"github.com/blend/go-sdk/testutil"
    17  )
    18  
    19  func TestMain(m *testing.M) {
    20  	testutil.MarkUpdateGoldenFlag(testutil.OptUpdateGoldenFlag(testUpdateGoldenFlag))
    21  
    22  	testutil.New(
    23  		m,
    24  		testutil.OptLog(logger.All()),
    25  		ensureConnectionOption(),
    26  	).Run()
    27  }
    28  
    29  func ensureConnectionOption() testutil.Option {
    30  	return func(s *testutil.Suite) {
    31  		s.Before = append(s.Before, ensureConnection)
    32  	}
    33  }
    34  
    35  // ensureConnection makes sure that a valid database connection exists
    36  // before running tests. It uses the helpers **from this package** to
    37  // configure and validation the connection, then closes it since further
    38  // usage will not be needed.
    39  func ensureConnection(ctx context.Context) error {
    40  	c := db.Config{}
    41  	err := testutil.ResolveDBConfig(ctx, &c)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	pool, err := db.New(db.OptConfig(c))
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	err = testutil.ValidatePool(ctx, pool, "")
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	return pool.Close()
    57  }