-
Notifications
You must be signed in to change notification settings - Fork 0
/
failed_precondition_test.go
63 lines (48 loc) · 1.5 KB
/
failed_precondition_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 TestToFailedPreconditionOnNil(t *testing.T) {
err := ToFailedPrecondition(nil, "id")
ok := IsFailedPrecondition(err)
assert.False(t, ok)
assert.Equal(t, err, nil)
}
func TestToFailedPrecondition(t *testing.T) {
err := ToFailedPrecondition(errors.New("not domain"), "id")
assert.True(t, IsFailedPrecondition(err))
var e oops.OopsError
ok := errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrFailedPrecondition, e.Code())
}
func TestIsFailedPreconditionOnNonDomain(t *testing.T) {
err := errors.New("I am found!")
ok := IsFailedPrecondition(err)
assert.False(t, ok)
}
func TestThrowFailedPrecondition(t *testing.T) {
err := ThrowFailedPrecondition("create", "user", "sam")
ok := IsFailedPrecondition(err)
assert.True(t, ok)
assert.Equal(t, err.Error(), "cannot create 'sam' of kind 'user'")
}
func TestFailedPreconditionf(t *testing.T) {
err := FailedPreconditionf("something happened: %s", "reason")
ok := IsFailedPrecondition(err)
assert.True(t, ok)
assert.Equal(t, err.Error(), "something happened: reason")
}
func TestToFailedPreconditionf(t *testing.T) {
err := ToFailedPreconditionf(errors.New("something happened"), "key: %s", "value")
ok := IsFailedPrecondition(err)
assert.True(t, ok)
var e oops.OopsError
ok = errors.As(err, &e)
assert.True(t, ok)
assert.Equal(t, ErrFailedPrecondition, e.Code())
assert.Equal(t, err.Error(), "key: value: something happened")
}