github.com/decred/dcrlnd@v0.7.6/kvdb/bolt_fixture.go (about)

     1  package kvdb
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/btcsuite/btcwallet/walletdb"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  type boltFixture struct {
    14  	t       *testing.T
    15  	tempDir string
    16  }
    17  
    18  func NewBoltFixture(t *testing.T) *boltFixture {
    19  	tempDir, err := ioutil.TempDir("", "test")
    20  	require.NoError(t, err)
    21  
    22  	return &boltFixture{
    23  		t:       t,
    24  		tempDir: tempDir,
    25  	}
    26  }
    27  
    28  func (b *boltFixture) Cleanup() {
    29  	os.RemoveAll(b.tempDir)
    30  }
    31  
    32  func (b *boltFixture) NewBackend() walletdb.DB {
    33  	dbPath := filepath.Join(b.tempDir)
    34  
    35  	db, err := GetBoltBackend(&BoltBackendConfig{
    36  		DBPath:         dbPath,
    37  		DBFileName:     "test.db",
    38  		NoFreelistSync: true,
    39  		DBTimeout:      DefaultDBTimeout,
    40  	})
    41  	require.NoError(b.t, err)
    42  
    43  	return db
    44  }