Welcome to mirror list, hosted at ThFree Co, Russian Federation.

errors_test.go « helper « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3ed5642700763364d1d7d06ea843a33a06522167 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
package helper

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/require"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

func TestError(t *testing.T) {
	input := errors.New("sentinel error")
	for _, tc := range []struct {
		desc     string
		decorate func(err error) error
		code     codes.Code
	}{
		{
			desc:     "Internal",
			decorate: ErrInternal,
			code:     codes.Internal,
		},
		{
			desc:     "InvalidArgument",
			decorate: ErrInvalidArgument,
			code:     codes.InvalidArgument,
		},
		{
			desc:     "PreconditionFailed",
			decorate: ErrPreconditionFailed,
			code:     codes.FailedPrecondition,
		},
		{
			desc:     "NotFound",
			decorate: ErrNotFound,
			code:     codes.NotFound,
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			err := tc.decorate(input)
			require.True(t, errors.Is(err, input))
			require.Equal(t, tc.code, status.Code(err))
		})
	}
}

func TestErrorf(t *testing.T) {
	for _, tc := range []struct {
		desc   string
		errorf func(format string, a ...interface{}) error
		code   codes.Code
	}{
		{
			desc:   "Internalf",
			errorf: ErrInternalf,
			code:   codes.Internal,
		},
		{
			desc:   "InvalidArgumentf",
			errorf: ErrInvalidArgumentf,
			code:   codes.InvalidArgument,
		},
		{
			desc:   "PreconditionFailedf",
			errorf: ErrPreconditionFailedf,
			code:   codes.FailedPrecondition,
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			err := tc.errorf("expected %s", "message")
			require.EqualError(t, err, "expected message")
			require.Equal(t, tc.code, status.Code(err))
		})
	}
}