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

     1  package errors
     2  
     3  import "testing"
     4  
     5  func TestEntityBlank(t *testing.T) {
     6  	var err error = Ent("")
     7  	assert(t, "entity error", err.Error())
     8  
     9  	entErr, ok := err.(EntityError)
    10  	assert(t, true, ok)
    11  	assertNotEqual(t, nil, entErr)
    12  	assert(t, "", entErr.EntityIdentifier())
    13  	assert(t, nil, Unwrap(err))
    14  }
    15  
    16  func TestEntityNoID(t *testing.T) {
    17  	var err error = Ent("").Desc(ErrValueMalformed)
    18  	assert(t, "entity malformed", err.Error())
    19  
    20  	entErr, ok := err.(EntityError)
    21  	assert(t, true, ok)
    22  	assertNotEqual(t, nil, entErr)
    23  	assert(t, "", entErr.EntityIdentifier())
    24  	assert(t, nil, Unwrap(err))
    25  	assert(t, ErrValueMalformed, UnwrapDescriptor(err))
    26  }
    27  
    28  func TestEntityWithFields(t *testing.T) {
    29  	var err error = Ent("user").Fieldset(
    30  		N("name").Desc(ErrValueEmpty),
    31  		N("age").Desc(ErrValueUnspecified))
    32  	assert(t, "user: name: empty, age: unspecified", err.Error())
    33  }
    34  
    35  func TestEntityWithFieldsNoName(t *testing.T) {
    36  	var err error = Ent("").Fieldset(
    37  		N("name").Desc(ErrValueEmpty),
    38  		N("age").Desc(ErrValueUnspecified))
    39  	assert(t, "entity: name: empty, age: unspecified", err.Error())
    40  }
    41  
    42  func TestEntNotFound(t *testing.T) {
    43  	var fooNotFound error = Ent("foo").Wrap(ErrEntityNotFound)
    44  	assert(t, "foo: not found", fooNotFound.Error())
    45  	assert(t, true, IsEntityNotFoundError(fooNotFound))
    46  }
    47  
    48  func TestErrEntNotFound(t *testing.T) {
    49  	var notFoundBare error = ErrEntityNotFound
    50  	assert(t, false, IsEntityNotFoundError(notFoundBare))
    51  	assert(t, false, IsArgumentError(notFoundBare))
    52  	assert(t, false, IsCallError(notFoundBare))
    53  }
    54  
    55  func TestIsEntNotFoundErrorCustomNegative(t *testing.T) {
    56  	var err error = &customEntError{entID: "foo"}
    57  	if IsEntityNotFoundError(err) {
    58  		t.Error(`IsEntNotFoundError(err)`)
    59  	}
    60  }
    61  
    62  func TestEntDescMsg(t *testing.T) {
    63  	var err error = Ent("foo").DescMsg("custom descriptor")
    64  	assert(t, "foo: custom descriptor", err.Error())
    65  	assert(t, "custom descriptor", UnwrapDescriptor(err).Error())
    66  	assert(t, nil, Unwrap(err))
    67  }
    68  
    69  //----
    70  
    71  type customEntError struct {
    72  	entID string
    73  }
    74  
    75  var (
    76  	_ EntityError = &customEntError{}
    77  )
    78  
    79  func (e *customEntError) EntityIdentifier() string { return e.entID }
    80  func (e *customEntError) Error() string            { return "custom ent error" }
    81  func (e *customEntError) CallError() CallError     { return e }
    82  func (e *customEntError) Unwrap() error            { return nil }
    83  func (e customEntError) FieldErrors() []NamedError { return nil }
    84  
    85  func TestUnwrapEntityErrorSetNil(t *testing.T) {
    86  	var err error
    87  	if callErrors := UnwrapEntityErrorSet(err); len(callErrors) != 0 {
    88  		t.Error("len(callErrors) != 0")
    89  	}
    90  }
    91  
    92  func TestUnwrapEntityErrorSetWrongType(t *testing.T) {
    93  	var err error = ErrValueMalformed
    94  	if callErrors := UnwrapEntityErrorSet(err); len(callErrors) != 0 {
    95  		t.Error("len(callErrors) != 0")
    96  	}
    97  }
    98  
    99  func TestEntRewrapNil(t *testing.T) {
   100  	var err error = Ent("foo").Rewrap(nil)
   101  	assert(t, "foo", err.Error())
   102  	assert(t, true, IsEntityError(err))
   103  	assert(t, nil, UnwrapDescriptor(err))
   104  	assert(t, nil, Unwrap(err))
   105  }
   106  
   107  func TestEntRewrapDesc(t *testing.T) {
   108  	var err error = Ent("foo").Rewrap(ErrValueMalformed)
   109  	assert(t, "foo: malformed", err.Error())
   110  	assert(t, true, IsEntityError(err))
   111  	assertNotEqual(t, nil, UnwrapDescriptor(err))
   112  	assert(t, ErrValueMalformed, UnwrapDescriptor(err))
   113  	assert(t, nil, Unwrap(err))
   114  }
   115  
   116  func TestEntRewrapDescWrapped(t *testing.T) {
   117  	var err error = Ent("foo").Rewrap(Arg1().Desc(ErrValueMalformed).Wrap(Msg("bar")))
   118  	assert(t, "foo: malformed: bar", err.Error())
   119  	assert(t, true, IsEntityError(err))
   120  	assertNotEqual(t, nil, UnwrapDescriptor(err))
   121  	assert(t, ErrValueMalformed, UnwrapDescriptor(err))
   122  	assertNotEqual(t, nil, Unwrap(err))
   123  	assert(t, "bar", Unwrap(err).Error())
   124  }
   125  
   126  func TestEntRewrapRandom(t *testing.T) {
   127  	var err error = Ent("foo").Rewrap(Msg("bar"))
   128  	assert(t, "foo", err.Error())
   129  	assert(t, true, IsEntityError(err))
   130  	assert(t, nil, UnwrapDescriptor(err))
   131  	assert(t, nil, Unwrap(err))
   132  }
   133  
   134  func TestEntRewrapWrappedNoDesc(t *testing.T) {
   135  	var err error = Ent("foo").Rewrap(Arg1().Wrap(Msg("bar")))
   136  	assert(t, "foo: bar", err.Error())
   137  	assert(t, true, IsEntityError(err))
   138  	assert(t, nil, UnwrapDescriptor(err))
   139  	assertNotEqual(t, nil, Unwrap(err))
   140  	assert(t, "bar", Unwrap(err).Error())
   141  }
   142  
   143  func TestEntRewrapFields(t *testing.T) {
   144  	var err error = Ent("simple").Rewrap(Arg1().Fieldset(
   145  		NamedValueUnsupported("foo"),
   146  		N("bar").Desc(ErrValueMalformed),
   147  	))
   148  	assert(t, "simple: foo: unsupported, bar: malformed", err.Error())
   149  	assert(t, true, IsEntityError(err))
   150  	assert(t, nil, UnwrapDescriptor(err))
   151  	assert(t, nil, Unwrap(err))
   152  	assert(t, 2, len(UnwrapFieldErrors(err)))
   153  }
   154  
   155  //----
   156  
   157  func TestEntSetEmpty(t *testing.T) {
   158  	var err error = entErrorSet{}
   159  	if err.Error() != "" {
   160  		t.Errorf(`err.Error() != "" -- %q`, err.Error())
   161  	}
   162  	errSet := asErrorSet(err)
   163  	if errSet == nil {
   164  		t.Error("errSet == nil")
   165  	}
   166  	errs := errSet.Errors()
   167  	if len(errs) != 0 {
   168  		t.Error("len(errors) != 0")
   169  	}
   170  	errs = UnwrapErrorSet(err)
   171  	if len(errs) != 0 {
   172  		t.Error("len(errors) != 0")
   173  	}
   174  	entErrSet := asEntityErrorSet(err)
   175  	if entErrSet == nil {
   176  		t.Error("entErrSet == nil")
   177  	}
   178  	entErrors := entErrSet.EntityErrors()
   179  	if len(entErrors) != 0 {
   180  		t.Error("len(entErrors) != 0")
   181  	}
   182  	entErrors = UnwrapEntityErrorSet(err)
   183  	if len(entErrors) != 0 {
   184  		t.Error("len(entErrors) != 0")
   185  	}
   186  }
   187  
   188  func TestEntSetSinge(t *testing.T) {
   189  	var err error = EntSet(Ent("foo").Desc(ErrValueMalformed))
   190  	if err.Error() != "foo: malformed" {
   191  		t.Errorf(`err.Error() != "foo: malformed" -- %q`, err.Error())
   192  	}
   193  	errSet := asErrorSet(err)
   194  	if errSet == nil {
   195  		t.Error("errSet == nil")
   196  	}
   197  	errs := errSet.Errors()
   198  	if len(errs) != 1 {
   199  		t.Error("len(errors) != 1")
   200  	}
   201  	errs = UnwrapErrorSet(err)
   202  	if len(errs) != 1 {
   203  		t.Error("len(errors) != 1")
   204  	}
   205  	entErrSet := asEntityErrorSet(err)
   206  	if entErrSet == nil {
   207  		t.Error("entErrSet == nil")
   208  	}
   209  	entErrors := entErrSet.EntityErrors()
   210  	if len(entErrors) != 1 {
   211  		t.Error("len(entErrors) != 1")
   212  	}
   213  	entErrors = UnwrapEntityErrorSet(err)
   214  	if len(entErrors) != 1 {
   215  		t.Error("len(entErrors) != 1")
   216  	}
   217  }