-
Notifications
You must be signed in to change notification settings - Fork 0
/
not_found_test.go
63 lines (48 loc) · 1.32 KB
/
not_found_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package zerrors
import (
"errors"
"github.com/samber/oops"
"testing"
"github.com/stretchr/testify/assert"
)
func TestToNotFoundOnNil(t *testing.T) {
err := ToNotFound(nil, "id")
ok := IsNotFound(err)
assert.False(t, ok)
assert.Equal(t, err, nil)
}
func TestToNotFound(t *testing.T) {
err := ToNotFound(errors.New("not domain"), "id")
assert.True(t, IsNotFound(err))
var e oops.OopsError
ok := errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrNotFound, e.Code())
}
func TestIsNotFoundOnNonDomain(t *testing.T) {
err := errors.New("I am found!")
ok := IsNotFound(err)
assert.False(t, ok)
}
func TestThrowNotFound(t *testing.T) {
err := ThrowNotFound("create", "user", "sam")
ok := IsNotFound(err)
assert.True(t, ok)
assert.Equal(t, err.Error(), "cannot create 'sam' of kind 'user'")
}
func TestNotFoundf(t *testing.T) {
err := NotFoundf("something happened: %s", "reason")
ok := IsNotFound(err)
assert.True(t, ok)
assert.Equal(t, err.Error(), "something happened: reason")
}
func TestToNotFoundf(t *testing.T) {
err := ToNotFoundf(errors.New("something happened"), "key: %s", "value")
ok := IsNotFound(err)
assert.True(t, ok)
var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrNotFound, e.Code())
assert.Equal(t, err.Error(), "key: value: something happened")
}