-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendpoints_test.go
95 lines (84 loc) · 2.64 KB
/
endpoints_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package innsecure_test
import (
"context"
"errors"
"testing"
"github.com/form3tech/innsecure"
"github.com/go-kit/kit/endpoint"
)
func noopMiddleware(e endpoint.Endpoint) endpoint.Endpoint {
return e
}
type svc struct {
listBookings func(ctx context.Context, u *innsecure.User) (listing *innsecure.Listing, err error)
createBooking func(ctx context.Context, p innsecure.Booking) (*innsecure.Booking, error)
getBookingByID func(ctx context.Context, u *innsecure.User, ID string) (*innsecure.Booking, error)
}
func (s svc) ListBookings(ctx context.Context, u *innsecure.User) (listing *innsecure.Listing, err error) {
return s.listBookings(ctx, u)
}
func (s svc) CreateBooking(ctx context.Context, u *innsecure.User, p innsecure.Booking) (*innsecure.Booking, error) {
return s.createBooking(ctx, p)
}
func (s svc) GetBookingByID(ctx context.Context, u *innsecure.User, ID string) (*innsecure.Booking, error) {
return s.getBookingByID(ctx, u, ID)
}
func TestCanWrapList(t *testing.T) {
want := &innsecure.Listing{}
wantErr := errors.New("testerr")
service := svc{
listBookings: func(_ context.Context, _ *innsecure.User) (*innsecure.Listing, error) {
return want, wantErr
},
}
sut := innsecure.MakeServerEndpoints(service, noopMiddleware)
got, err := sut.ListBookings(context.TODO(), nil)
if got != want {
t.Fatalf("want=%+v, got=%+v", want, got)
}
if err != wantErr {
t.Fatalf("want=%s, got=%s", wantErr, err)
}
}
func TestCanWrapCreate(t *testing.T) {
wantIn := innsecure.Booking{ID: "INPUT"}
wantOut := &innsecure.Booking{ID: "OUTPUT"}
wantErr := errors.New("testerr")
service := svc{
createBooking: func(_ context.Context, in innsecure.Booking) (*innsecure.Booking, error) {
if in.ID != wantIn.ID {
t.Fatalf("unexpected input")
}
return wantOut, wantErr
},
}
sut := innsecure.MakeServerEndpoints(service, noopMiddleware)
got, err := sut.CreateBooking(context.TODO(), wantIn)
if got != wantOut {
t.Fatalf("want=%+v, got=%+v", wantOut, got)
}
if err != wantErr {
t.Fatalf("want=%s, got=%s", wantErr, err)
}
}
func TestCanWrapGetByID(t *testing.T) {
wantIn := "INPUT"
wantOut := &innsecure.Booking{ID: "OUTPUT"}
wantErr := errors.New("testerr")
service := svc{
getBookingByID: func(_ context.Context, _ *innsecure.User, in string) (*innsecure.Booking, error) {
if in != wantIn {
t.Fatalf("want=%s, got=%s", wantIn, in)
}
return wantOut, wantErr
},
}
sut := innsecure.MakeServerEndpoints(service, noopMiddleware)
got, err := sut.GetBookingByID(context.TODO(), wantIn)
if got != wantOut {
t.Fatalf("want=%+v, got=%+v", wantOut, got)
}
if err != wantErr {
t.Fatalf("want=%s, got=%s", wantErr, err)
}
}