-
Notifications
You must be signed in to change notification settings - Fork 3
/
validation_test.go
70 lines (58 loc) · 1.63 KB
/
validation_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
64
65
66
67
68
69
70
package grpctools
import (
"reflect"
"sort"
"testing"
"google.golang.org/grpc/codes"
)
func TestVErrors(t *testing.T) {
var verr VErrors
verr = verr.Reset()
verr = verr.Append("name", "is required")
verr = verr.Append("name", "must be 5 chars")
verr = verr.Append("external_id", "is taken")
t.Run("Append", func(t *testing.T) {
got := verr
exp := VErrors{
{Field: "name", Description: "is required"},
{Field: "name", Description: "must be 5 chars"},
{Field: "external_id", Description: "is taken"},
}
if !reflect.DeepEqual(exp, got) {
t.Fatalf("expected %v, got %v", exp, got)
}
})
t.Run("Messages", func(t *testing.T) {
got := verr.Messages()
sort.Strings(got)
exp := []string{
"external_id: is taken",
"name: is required",
"name: must be 5 chars",
}
if !reflect.DeepEqual(exp, got) {
t.Fatalf("expected %v, got %v", exp, got)
}
})
t.Run("Status", func(t *testing.T) {
sts := verr.Status(codes.InvalidArgument, "custom")
got := sts.Err().Error()
if exp := `rpc error: code = InvalidArgument desc = custom`; exp != got {
t.Fatalf("expected %v, got %v", exp, got)
}
})
t.Run("VErrorsFromStatus", func(t *testing.T) {
sts := verr.Status(codes.InvalidArgument, "custom")
got := VErrorsFromStatus(sts).Messages()
if exp := verr.Messages(); !reflect.DeepEqual(exp, got) {
t.Fatalf("expected %v, got %v", exp, got)
}
})
t.Run("VErrorsConvert", func(t *testing.T) {
sts := verr.Status(codes.InvalidArgument, "custom")
got := VErrorsConvert(sts.Err()).Messages()
if exp := verr.Messages(); !reflect.DeepEqual(exp, got) {
t.Fatalf("expected %v, got %v", exp, got)
}
})
}