github.com/alloyzeus/go-azfl@v0.0.0-20231220071816-9740126a2d07/errors/operation_test.go (about)

     1  package errors
     2  
     3  import "testing"
     4  
     5  func TestOpEmpty(t *testing.T) {
     6  	var err error = Op("")
     7  
     8  	assert(t, "operation error", err.Error())
     9  	assert(t, nil, Unwrap(err))
    10  
    11  	opErr, ok := err.(OperationError)
    12  	assert(t, true, ok)
    13  	assertNotEqual(t, nil, opErr)
    14  	assert(t, "", opErr.OperationName())
    15  }
    16  
    17  func TestOpJustName(t *testing.T) {
    18  	var err error = Op("DeleteAccount")
    19  
    20  	assert(t, "DeleteAccount error", err.Error())
    21  	assert(t, nil, Unwrap(err))
    22  
    23  	opErr, ok := err.(OperationError)
    24  	assert(t, true, ok)
    25  	assertNotEqual(t, nil, opErr)
    26  	assert(t, "DeleteAccount", opErr.OperationName())
    27  }
    28  
    29  func TestOpJustWrapped(t *testing.T) {
    30  	var err error = Op("").Wrap(ErrAccessForbidden)
    31  
    32  	assert(t, "operation forbidden", err.Error())
    33  
    34  	inner := Unwrap(err)
    35  	assertNotEqual(t, nil, inner)
    36  	assert(t, ErrAccessForbidden, inner)
    37  
    38  	opErr, ok := err.(OperationError)
    39  	assert(t, true, ok)
    40  	assertNotEqual(t, nil, opErr)
    41  	assert(t, "", opErr.OperationName())
    42  	assert(t, ErrAccessForbidden, opErr.Unwrap())
    43  }
    44  
    45  func TestOpNameAndWrapped(t *testing.T) {
    46  	var err error = Op("DropDatabase").Wrap(ErrAccessForbidden)
    47  
    48  	assert(t, "DropDatabase: forbidden", err.Error())
    49  
    50  	inner := Unwrap(err)
    51  	assertNotEqual(t, nil, inner)
    52  	assert(t, ErrAccessForbidden, inner)
    53  
    54  	opErr, ok := err.(OperationError)
    55  	assert(t, true, ok)
    56  	assertNotEqual(t, nil, opErr)
    57  	assert(t, "DropDatabase", opErr.OperationName())
    58  	assert(t, ErrAccessForbidden, opErr.Unwrap())
    59  }
    60  
    61  func TestOpParamsBasic(t *testing.T) {
    62  	var err error = Op("LaunchNuke").Params(N("authorizationCode").Desc(ErrValueMismatch))
    63  
    64  	assert(t, "LaunchNuke error. authorizationCode: mismatch", err.Error())
    65  }
    66  
    67  func TestOpParamsRes(t *testing.T) {
    68  	var err error = Op("Fetch").Params(N("part").Val("HEAD")).Wrap(ErrEntityUnreachable)
    69  
    70  	//TODO: param values are quoted when necessary
    71  	assert(t, "Fetch: unreachable. part=HEAD", err.Error())
    72  }
    73  
    74  func TestOpParamsNoWrap(t *testing.T) {
    75  	var err error = Op("Fetch").Params(N("part").Val("HEAD"))
    76  
    77  	assert(t, "Fetch error. part=HEAD", err.Error())
    78  }
    79  
    80  func TestOpNoNameParamsNoWrap(t *testing.T) {
    81  	var err error = Op("").Params(N("part").Val("HEAD"))
    82  
    83  	assert(t, "operation error. part=HEAD", err.Error())
    84  }
    85  
    86  func TestOpHintWrap(t *testing.T) {
    87  	var err error = Op("SwimmingRevolution").Params(N("location").Val("Pacific Ocean")).
    88  		Wrap(Msg("sun is shining")).Hint("Please check the water temperature first")
    89  
    90  	assert(t, `SwimmingRevolution: sun is shining. location="Pacific Ocean". Please check the water temperature first`, err.Error())
    91  }
    92  
    93  func TestOpHintNoWrap(t *testing.T) {
    94  	var err error = Op("SwimmingRevolution").Params(N("location").Val("Pacific Ocean")).
    95  		Hint("Please check the water temperature first")
    96  
    97  	assert(t, `SwimmingRevolution error. location="Pacific Ocean". Please check the water temperature first`, err.Error())
    98  }