github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/database/error_test.go (about)

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