github.com/btcsuite/btcd@v0.24.0/database/error_test.go (about)

     1  // Copyright (c) 2015-2016 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package database_test
     6  
     7  import (
     8  	"errors"
     9  	"testing"
    10  
    11  	"github.com/btcsuite/btcd/database"
    12  )
    13  
    14  // TestErrorCodeStringer tests the stringized output for the ErrorCode type.
    15  func TestErrorCodeStringer(t *testing.T) {
    16  	tests := []struct {
    17  		in   database.ErrorCode
    18  		want string
    19  	}{
    20  		{database.ErrDbTypeRegistered, "ErrDbTypeRegistered"},
    21  		{database.ErrDbUnknownType, "ErrDbUnknownType"},
    22  		{database.ErrDbDoesNotExist, "ErrDbDoesNotExist"},
    23  		{database.ErrDbExists, "ErrDbExists"},
    24  		{database.ErrDbNotOpen, "ErrDbNotOpen"},
    25  		{database.ErrDbAlreadyOpen, "ErrDbAlreadyOpen"},
    26  		{database.ErrInvalid, "ErrInvalid"},
    27  		{database.ErrCorruption, "ErrCorruption"},
    28  		{database.ErrTxClosed, "ErrTxClosed"},
    29  		{database.ErrTxNotWritable, "ErrTxNotWritable"},
    30  		{database.ErrBucketNotFound, "ErrBucketNotFound"},
    31  		{database.ErrBucketExists, "ErrBucketExists"},
    32  		{database.ErrBucketNameRequired, "ErrBucketNameRequired"},
    33  		{database.ErrKeyRequired, "ErrKeyRequired"},
    34  		{database.ErrKeyTooLarge, "ErrKeyTooLarge"},
    35  		{database.ErrValueTooLarge, "ErrValueTooLarge"},
    36  		{database.ErrIncompatibleValue, "ErrIncompatibleValue"},
    37  		{database.ErrBlockNotFound, "ErrBlockNotFound"},
    38  		{database.ErrBlockExists, "ErrBlockExists"},
    39  		{database.ErrBlockRegionInvalid, "ErrBlockRegionInvalid"},
    40  		{database.ErrDriverSpecific, "ErrDriverSpecific"},
    41  
    42  		{0xffff, "Unknown ErrorCode (65535)"},
    43  	}
    44  
    45  	// Detect additional error codes that don't have the stringer added.
    46  	if len(tests)-1 != int(database.TstNumErrorCodes) {
    47  		t.Errorf("It appears an error code was added without adding " +
    48  			"an associated stringer test")
    49  	}
    50  
    51  	t.Logf("Running %d tests", len(tests))
    52  	for i, test := range tests {
    53  		result := test.in.String()
    54  		if result != test.want {
    55  			t.Errorf("String #%d\ngot: %s\nwant: %s", i, result,
    56  				test.want)
    57  			continue
    58  		}
    59  	}
    60  }
    61  
    62  // TestError tests the error output for the Error type.
    63  func TestError(t *testing.T) {
    64  	t.Parallel()
    65  
    66  	tests := []struct {
    67  		in   database.Error
    68  		want string
    69  	}{
    70  		{
    71  			database.Error{Description: "some error"},
    72  			"some error",
    73  		},
    74  		{
    75  			database.Error{Description: "human-readable error"},
    76  			"human-readable error",
    77  		},
    78  		{
    79  			database.Error{
    80  				ErrorCode:   database.ErrDriverSpecific,
    81  				Description: "some error",
    82  				Err:         errors.New("driver-specific error"),
    83  			},
    84  			"some error: driver-specific error",
    85  		},
    86  	}
    87  
    88  	t.Logf("Running %d tests", len(tests))
    89  	for i, test := range tests {
    90  		result := test.in.Error()
    91  		if result != test.want {
    92  			t.Errorf("Error #%d\n got: %s want: %s", i, result,
    93  				test.want)
    94  			continue
    95  		}
    96  	}
    97  }