This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
subworkshop_test.go
103 lines (89 loc) · 3.31 KB
/
subworkshop_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
96
97
98
99
100
101
102
103
package controllers_test
import (
"context"
"fmt"
"os"
"testing"
"github.com/go-masonry/mortar/interfaces/http/client"
mock_client "github.com/go-masonry/mortar/interfaces/http/client/mock"
workshop "github.com/go-masonry/tutorial/06-tests/api"
"github.com/go-masonry/tutorial/06-tests/app/controllers"
"github.com/go-masonry/tutorial/06-tests/app/mortar"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"go.uber.org/fx"
"go.uber.org/fx/fxtest"
"google.golang.org/grpc"
)
type subWorkshopSuite struct {
suite.Suite
pwd string
ctrl *gomock.Controller
app *fxtest.App
grpcConnBuilderMock *mock_client.MockGRPCClientConnectionBuilder
gRPCWrapperMock *mock_client.MockGRPCClientConnectionWrapper
subController controllers.SubWorkshopController
}
func TestSubWorkshop(t *testing.T) {
suite.Run(t, new(subWorkshopSuite))
}
func (s *subWorkshopSuite) TestPaintCar() {
// Prepare mocks
fakeConnection := new(fakeGRPCConnection)
s.gRPCWrapperMock.EXPECT().Dial(gomock.Any(), gomock.Any(), gomock.Any()).Return(fakeConnection, nil)
s.grpcConnBuilderMock.EXPECT().Build().Return(s.gRPCWrapperMock)
_, err := s.subController.PaintCar(context.Background(), &workshop.SubPaintCarRequest{
Car: &workshop.Car{Number: "1234"},
DesiredColor: "black",
CallbackServiceAddress: "/dev/null",
})
s.NoError(err)
// It's not really necessary, but for the sake of the example you get see it's called
s.Equal(1, fakeConnection.callCounter)
}
func (s *subWorkshopSuite) TestPaintCarWithFailingDialer() {
// Prepare mocks
s.gRPCWrapperMock.EXPECT().Dial(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("just void"))
s.grpcConnBuilderMock.EXPECT().Build().Return(s.gRPCWrapperMock)
_, err := s.subController.PaintCar(context.Background(), &workshop.SubPaintCarRequest{
Car: &workshop.Car{Number: "1234"},
DesiredColor: "black",
CallbackServiceAddress: "/dev/null",
})
s.EqualError(err, "car painted but we can't callback to /dev/null, just void")
}
func (s *subWorkshopSuite) SetupSuite() {
var err error
s.pwd, err = os.Getwd()
s.Require().NoError(err)
}
func (s *subWorkshopSuite) SetupTest() {
s.ctrl = gomock.NewController(s.T())
s.grpcConnBuilderMock = mock_client.NewMockGRPCClientConnectionBuilder(s.ctrl)
s.gRPCWrapperMock = mock_client.NewMockGRPCClientConnectionWrapper(s.ctrl)
s.app = fxtest.New(s.T(),
fx.NopLogger, // remove fx debug prints
mortar.ViperFxOption(s.pwd+"/../../config/config.yml", s.pwd+"/../../config/config_test.yml"),
mortar.LoggerFxOption(),
fx.Provide(func() client.GRPCClientConnectionBuilder {
return s.grpcConnBuilderMock
}),
fx.Provide(controllers.CreateSubWorkshopController),
fx.Populate(&s.subController),
)
s.app.RequireStart()
}
func (s *subWorkshopSuite) TearDownTest() {
s.app.RequireStop()
s.ctrl.Finish()
}
type fakeGRPCConnection struct {
callCounter int
}
func (f *fakeGRPCConnection) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error {
f.callCounter++
return nil // everything is great
}
func (f *fakeGRPCConnection) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
panic("implement me")
}