2019-01-08 00:37:16 +00:00
package influxdb_test
2018-08-27 17:09:17 +00:00
import (
"encoding/json"
"errors"
"fmt"
"testing"
2018-09-12 11:25:17 +00:00
2019-01-08 00:37:16 +00:00
platform "github.com/influxdata/influxdb"
2018-08-27 17:09:17 +00:00
)
const EFailedToGetStorageHost = "failed to get the storage host"
func TestErrorMsg ( t * testing . T ) {
cases := [ ] struct {
name string
err error
msg string
} {
{
name : "simple error" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error { Code : platform . ENotFound } ,
2018-08-27 17:09:17 +00:00
msg : "<not found>" ,
} ,
{
name : "with op" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error {
Code : platform . ENotFound ,
2018-08-27 17:09:17 +00:00
Op : "bolt.FindAuthorizationByID" ,
} ,
msg : "bolt.FindAuthorizationByID: <not found>" ,
} ,
{
name : "with op and value" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error {
Code : platform . ENotFound ,
2018-08-27 17:09:17 +00:00
Op : "bolt.FindAuthorizationByID" ,
Msg : fmt . Sprintf ( "with ID %d" , 323 ) ,
} ,
msg : "bolt.FindAuthorizationByID: <not found> with ID 323" ,
} ,
{
name : "with a third party error" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error {
2018-08-27 17:09:17 +00:00
Code : EFailedToGetStorageHost ,
Op : "cmd/fluxd.injectDeps" ,
Err : errors . New ( "empty value" ) ,
} ,
msg : "cmd/fluxd.injectDeps: empty value" ,
} ,
{
name : "with a internal error" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error {
2018-08-27 17:09:17 +00:00
Code : EFailedToGetStorageHost ,
Op : "cmd/fluxd.injectDeps" ,
2018-09-12 11:25:17 +00:00
Err : & platform . Error { Code : platform . EEmptyValue , Op : "cmd/fluxd.getStrList" } ,
2018-08-27 17:09:17 +00:00
} ,
msg : "cmd/fluxd.injectDeps: cmd/fluxd.getStrList: <empty value>" ,
} ,
}
for _ , c := range cases {
if c . msg != c . err . Error ( ) {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s failed, want %s, got %s" , c . name , c . msg , c . err . Error ( ) )
2018-08-27 17:09:17 +00:00
}
}
}
func TestErrorMessage ( t * testing . T ) {
cases := [ ] struct {
name string
err error
want string
} {
{
name : "nil error" ,
} ,
2019-01-04 16:02:26 +00:00
{
name : "nil error of type *platform.Error" ,
err : ( * platform . Error ) ( nil ) ,
} ,
2018-08-27 17:09:17 +00:00
{
name : "simple error" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error { Msg : "simple error" } ,
2018-08-27 17:09:17 +00:00
want : "simple error" ,
} ,
{
2019-04-17 20:30:22 +00:00
name : "embedded error" ,
err : & platform . Error { Err : & platform . Error { Msg : "embedded error" } } ,
want : "embedded error" ,
2018-08-27 17:09:17 +00:00
} ,
{
name : "default error" ,
err : errors . New ( "s" ) ,
want : "An internal error has occurred." ,
} ,
}
for _ , c := range cases {
2018-09-12 11:25:17 +00:00
if result := platform . ErrorMessage ( c . err ) ; c . want != result {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s failed, want %s, got %s" , c . name , c . want , result )
2018-08-27 17:09:17 +00:00
}
}
}
2018-11-07 18:55:52 +00:00
func TestErrorOp ( t * testing . T ) {
cases := [ ] struct {
name string
err error
want string
} {
{
name : "nil error" ,
} ,
2019-01-04 16:02:26 +00:00
{
name : "nil error of type *platform.Error" ,
err : ( * platform . Error ) ( nil ) ,
} ,
2018-11-07 18:55:52 +00:00
{
name : "simple error" ,
err : & platform . Error { Op : "op1" } ,
want : "op1" ,
} ,
{
2019-04-17 20:30:22 +00:00
name : "embedded error" ,
2018-11-07 18:55:52 +00:00
err : & platform . Error { Op : "op1" , Err : & platform . Error { Code : platform . EInvalid } } ,
want : "op1" ,
} ,
2018-11-07 18:55:52 +00:00
{
2019-04-17 20:30:22 +00:00
name : "embedded error without op in root level" ,
2018-11-07 18:55:52 +00:00
err : & platform . Error { Err : & platform . Error { Code : platform . EInvalid , Op : "op2" } } ,
want : "op2" ,
} ,
2018-11-07 18:55:52 +00:00
{
name : "default error" ,
err : errors . New ( "s" ) ,
want : "" ,
} ,
}
for _ , c := range cases {
if result := platform . ErrorOp ( c . err ) ; c . want != result {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s failed, want %s, got %s" , c . name , c . want , result )
2018-11-07 18:55:52 +00:00
}
}
}
2018-08-27 17:09:17 +00:00
func TestErrorCode ( t * testing . T ) {
cases := [ ] struct {
name string
err error
want string
} {
{
name : "nil error" ,
} ,
2019-01-04 16:02:26 +00:00
{
name : "nil error of type *platform.Error" ,
err : ( * platform . Error ) ( nil ) ,
} ,
2018-08-27 17:09:17 +00:00
{
name : "simple error" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error { Code : platform . ENotFound } ,
want : platform . ENotFound ,
2018-08-27 17:09:17 +00:00
} ,
{
2019-04-17 20:30:22 +00:00
name : "embedded error" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error { Code : platform . ENotFound , Err : & platform . Error { Code : platform . EInvalid } } ,
want : platform . ENotFound ,
2018-08-27 17:09:17 +00:00
} ,
2018-11-07 18:55:52 +00:00
{
2019-04-17 20:30:22 +00:00
name : "embedded error with root level code" ,
2018-11-07 18:55:52 +00:00
err : & platform . Error { Err : & platform . Error { Code : platform . EInvalid } } ,
want : platform . EInvalid ,
} ,
2018-08-27 17:09:17 +00:00
{
name : "default error" ,
err : errors . New ( "s" ) ,
2018-09-12 11:25:17 +00:00
want : platform . EInternal ,
2018-08-27 17:09:17 +00:00
} ,
}
for _ , c := range cases {
2018-09-12 11:25:17 +00:00
if result := platform . ErrorCode ( c . err ) ; c . want != result {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s failed, want %s, got %s" , c . name , c . want , result )
2018-08-27 17:09:17 +00:00
}
}
}
func TestJSON ( t * testing . T ) {
cases := [ ] struct {
2018-12-17 15:05:23 +00:00
name string
err * platform . Error
encoded string
2018-08-27 17:09:17 +00:00
} {
{
2018-12-17 15:05:23 +00:00
name : "simple error" ,
err : & platform . Error { Code : platform . ENotFound } ,
encoded : ` { "code":"not found"} ` ,
2018-08-27 17:09:17 +00:00
} ,
{
name : "with op" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error {
Code : platform . ENotFound ,
2018-08-27 17:09:17 +00:00
Op : "bolt.FindAuthorizationByID" ,
} ,
2018-12-17 15:05:23 +00:00
encoded : ` { "code":"not found","op":"bolt.FindAuthorizationByID"} ` ,
2018-08-27 17:09:17 +00:00
} ,
{
name : "with op and value" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error {
Code : platform . ENotFound ,
2018-12-17 15:05:23 +00:00
Op : "bolt/FindAuthorizationByID" ,
2018-08-27 17:09:17 +00:00
Msg : fmt . Sprintf ( "with ID %d" , 323 ) ,
} ,
2018-12-21 13:44:52 +00:00
encoded : ` { "code":"not found","message":"with ID 323","op":"bolt/FindAuthorizationByID"} ` ,
2018-08-27 17:09:17 +00:00
} ,
{
name : "with a third party error" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error {
2018-08-27 17:09:17 +00:00
Code : EFailedToGetStorageHost ,
Op : "cmd/fluxd.injectDeps" ,
Err : errors . New ( "empty value" ) ,
} ,
2018-12-21 13:44:52 +00:00
encoded : ` { "code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","error":"empty value"} ` ,
2018-08-27 17:09:17 +00:00
} ,
{
name : "with a internal error" ,
2018-09-12 11:25:17 +00:00
err : & platform . Error {
2018-08-27 17:09:17 +00:00
Code : EFailedToGetStorageHost ,
Op : "cmd/fluxd.injectDeps" ,
2018-09-12 11:25:17 +00:00
Err : & platform . Error { Code : platform . EEmptyValue , Op : "cmd/fluxd.getStrList" } ,
2018-08-27 17:09:17 +00:00
} ,
2018-12-21 13:44:52 +00:00
encoded : ` { "code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","error": { "code":"empty value","op":"cmd/fluxd.getStrList"}} ` ,
2018-12-17 15:05:23 +00:00
} ,
{
name : "with a deep internal error" ,
err : & platform . Error {
Code : EFailedToGetStorageHost ,
Op : "cmd/fluxd.injectDeps" ,
Err : & platform . Error {
Code : platform . EInvalid ,
Op : "cmd/fluxd.getStrList" ,
Err : & platform . Error {
Code : platform . EEmptyValue ,
Err : errors . New ( "an err" ) ,
} ,
} ,
} ,
2018-12-21 13:44:52 +00:00
encoded : ` { "code":"failed to get the storage host","op":"cmd/fluxd.injectDeps","error": { "code":"invalid","op":"cmd/fluxd.getStrList","error": { "code":"empty value","error":"an err"}}} ` ,
2018-08-27 17:09:17 +00:00
} ,
}
for _ , c := range cases {
result , err := json . Marshal ( c . err )
// encode testing
if err != nil {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s encode failed, want err: %v, should be nil" , c . name , err )
2018-08-27 17:09:17 +00:00
}
2018-12-17 15:05:23 +00:00
if string ( result ) != c . encoded {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s encode failed, want result: %s, got %s" , c . name , c . encoded , string ( result ) )
2018-12-17 15:05:23 +00:00
}
2018-08-27 17:09:17 +00:00
// decode testing
2018-09-12 11:25:17 +00:00
got := new ( platform . Error )
2018-12-17 15:05:23 +00:00
err = json . Unmarshal ( result , got )
2018-08-27 17:09:17 +00:00
if err != nil {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s decode failed, want err: %v, should be nil" , c . name , err )
2018-08-27 17:09:17 +00:00
}
decodeEqual ( t , c . err , got , "decode: " + c . name )
}
}
2018-09-12 11:25:17 +00:00
func decodeEqual ( t * testing . T , want , result * platform . Error , caseName string ) {
2018-08-27 17:09:17 +00:00
if want . Code != result . Code {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s code failed, want %s, got %s" , caseName , want . Code , result . Code )
2018-08-27 17:09:17 +00:00
}
if want . Op != result . Op {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s op failed, want %s, got %s" , caseName , want . Op , result . Op )
2018-08-27 17:09:17 +00:00
}
if want . Msg != result . Msg {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s msg failed, want %s, got %s" , caseName , want . Msg , result . Msg )
2018-08-27 17:09:17 +00:00
}
if want . Err != nil {
2018-09-12 11:25:17 +00:00
if _ , ok := want . Err . ( * platform . Error ) ; ok {
decodeEqual ( t , want . Err . ( * platform . Error ) , result . Err . ( * platform . Error ) , caseName )
2018-08-27 17:09:17 +00:00
} else {
if want . Err . Error ( ) != result . Err . Error ( ) {
2019-01-04 16:02:26 +00:00
t . Errorf ( "%s Err failed, want %s, got %s" , caseName , want . Err . Error ( ) , result . Err . Error ( ) )
2018-08-27 17:09:17 +00:00
}
}
}
}