kaspad/database/error_test.go
Ori Newman c88fa1492e [NOD-375] Move to pkg/errors (#447)
* [NOD-375] Move to pkg/errors

* [NOD-375] Fix tests

* [NOD-375] Make AreErrorsEqual a shared function
2019-11-04 11:24:12 +02:00

119 lines
3.0 KiB
Go

// Copyright (c) 2015-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package database
import (
"github.com/pkg/errors"
"testing"
)
// TestErrorCodeStringer tests the stringized output for the ErrorCode type.
func TestErrorCodeStringer(t *testing.T) {
tests := []struct {
in ErrorCode
want string
}{
{ErrDbTypeRegistered, "ErrDbTypeRegistered"},
{ErrDbUnknownType, "ErrDbUnknownType"},
{ErrDbDoesNotExist, "ErrDbDoesNotExist"},
{ErrDbExists, "ErrDbExists"},
{ErrDbNotOpen, "ErrDbNotOpen"},
{ErrDbAlreadyOpen, "ErrDbAlreadyOpen"},
{ErrInvalid, "ErrInvalid"},
{ErrCorruption, "ErrCorruption"},
{ErrTxClosed, "ErrTxClosed"},
{ErrTxNotWritable, "ErrTxNotWritable"},
{ErrBucketNotFound, "ErrBucketNotFound"},
{ErrBucketExists, "ErrBucketExists"},
{ErrBucketNameRequired, "ErrBucketNameRequired"},
{ErrKeyRequired, "ErrKeyRequired"},
{ErrKeyTooLarge, "ErrKeyTooLarge"},
{ErrValueTooLarge, "ErrValueTooLarge"},
{ErrIncompatibleValue, "ErrIncompatibleValue"},
{ErrBlockNotFound, "ErrBlockNotFound"},
{ErrBlockExists, "ErrBlockExists"},
{ErrBlockRegionInvalid, "ErrBlockRegionInvalid"},
{ErrDriverSpecific, "ErrDriverSpecific"},
{0xffff, "Unknown ErrorCode (65535)"},
}
// Detect additional error codes that don't have the stringer added.
if len(tests)-1 != int(TstNumErrorCodes) {
t.Errorf("It appears an error code was added without adding " +
"an associated stringer test")
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.String()
if result != test.want {
t.Errorf("String #%d\ngot: %s\nwant: %s", i, result,
test.want)
continue
}
}
}
// TestError tests the error output for the Error type.
func TestError(t *testing.T) {
t.Parallel()
tests := []struct {
in Error
want string
}{
{
Error{Description: "some error"},
"some error",
},
{
Error{Description: "human-readable error"},
"human-readable error",
},
{
Error{
ErrorCode: ErrDriverSpecific,
Description: "some error",
Err: errors.New("driver-specific error"),
},
"some error: driver-specific error",
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("Error #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}
func TestIsErrorCode(t *testing.T) {
dummyError := errors.New("")
tests := []struct {
err error
code ErrorCode
expectedResult bool
}{
{makeError(ErrBucketExists, "", dummyError), ErrBucketExists, true},
{makeError(ErrBucketExists, "", dummyError), ErrBlockExists, false},
{dummyError, ErrBlockExists, false},
{nil, ErrBlockExists, false},
}
for i, test := range tests {
actualResult := IsErrorCode(test.err, test.code)
if test.expectedResult != actualResult {
t.Errorf("TestIsErrorCode: %d: Expected: %t, but got: %t",
i, test.expectedResult, actualResult)
}
}
}