github.com/decred/dcrlnd@v0.7.6/channeldb/migration/create_tlb_test.go (about)

     1  package migration_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/decred/dcrlnd/channeldb/migration"
     8  	"github.com/decred/dcrlnd/channeldb/migtest"
     9  	"github.com/decred/dcrlnd/kvdb"
    10  )
    11  
    12  // TestCreateTLB asserts that a CreateTLB properly initializes a new top-level
    13  // bucket, and that it succeeds even if the bucket already exists. It would
    14  // probably be better if the latter failed, but the kvdb abstraction doesn't
    15  // support this.
    16  func TestCreateTLB(t *testing.T) {
    17  	newBucket := []byte("hello")
    18  
    19  	tests := []struct {
    20  		name            string
    21  		beforeMigration func(kvdb.RwTx) error
    22  		shouldFail      bool
    23  	}{
    24  		{
    25  			name: "already exists",
    26  			beforeMigration: func(tx kvdb.RwTx) error {
    27  				_, err := tx.CreateTopLevelBucket(newBucket)
    28  				return err
    29  			},
    30  			shouldFail: true,
    31  		},
    32  		{
    33  			name:            "does not exist",
    34  			beforeMigration: func(_ kvdb.RwTx) error { return nil },
    35  			shouldFail:      false,
    36  		},
    37  	}
    38  
    39  	for _, test := range tests {
    40  		test := test
    41  		t.Run(test.name, func(t *testing.T) {
    42  			migtest.ApplyMigration(
    43  				t,
    44  				test.beforeMigration,
    45  				func(tx kvdb.RwTx) error {
    46  					if tx.ReadBucket(newBucket) != nil {
    47  						return nil
    48  					}
    49  					return fmt.Errorf("bucket \"%s\" not "+
    50  						"created", newBucket)
    51  				},
    52  				migration.CreateTLB(newBucket),
    53  				test.shouldFail,
    54  			)
    55  		})
    56  	}
    57  }