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
/
workshop_test.go
177 lines (164 loc) · 5.48 KB
/
workshop_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package controllers_test
import (
"context"
"io/ioutil"
"net/http"
"os"
"strings"
"testing"
"github.com/go-masonry/mortar/http/client"
clientInt "github.com/go-masonry/mortar/interfaces/http/client"
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/data"
"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"
)
type workshopSuite struct {
suite.Suite
pwd string
ctrl *gomock.Controller
app *fxtest.App
carDB data.CarDB
controller controllers.WorkshopController
}
func TestWorkshop(t *testing.T) {
suite.Run(t, new(workshopSuite))
}
func (s *workshopSuite) TestAcceptCar() {
_, err := s.controller.AcceptCar(context.Background(), &workshop.Car{
Number: "1234",
Owner: "test owner",
BodyStyle: workshop.Car_PHAETON,
Color: "cyan",
})
s.NoError(err)
car, err := s.carDB.GetCar(context.Background(), "1234")
s.NoError(err)
s.Equal("1234", car.CarNumber)
s.Equal("cyan", car.CurrentColor)
s.Equal("cyan", car.OriginalColor)
s.Equal("PHAETON", car.BodyStyle)
s.Equal("test owner", car.Owner)
s.False(car.Painted)
}
// TestPaintCar is special test, if you look at business logic you can see that the client tries to connect to a remote
// service. We don't have it up and running, but we fake it with the special HTTP client interceptor.
// This will allow us to simply return the response without really going anywhere.
// Check s.specialHTTPClientBuilder function to understand
//
// You are encouraged to replace this special client with a default one, look at the commented line below
// If you do you should see an error similar to this one
// Post "http://localhost:5381/v1/subworkshop/paint": dial tcp [::1]:5381: connect: connection refused
func (s *workshopSuite) TestPaintCar() {
_, err := s.controller.AcceptCar(context.Background(), &workshop.Car{
Number: "1234567",
Owner: "test owner",
BodyStyle: workshop.Car_PHAETON,
Color: "yellow",
})
s.NoError(err)
// test no such car
_, err = s.controller.PaintCar(context.Background(), &workshop.PaintCarRequest{CarNumber: "fake car number"})
s.Error(err)
// now paint
_, err = s.controller.PaintCar(context.Background(), &workshop.PaintCarRequest{
CarNumber: "1234567",
DesiredColor: "orange",
})
s.NoError(err)
}
func (s *workshopSuite) TestRetrieveCar() {
// No car
_, err := s.controller.RetrieveCar(context.Background(), &workshop.RetrieveCarRequest{CarNumber: "not yet there"})
s.EqualError(err, "unknown car ID not yet there")
car := &data.CarEntity{
CarNumber: "12345",
Owner: "test owner",
BodyStyle: "SEDAN",
OriginalColor: "white",
CurrentColor: "white",
Painted: false,
}
// Insert car, but it's not yet painted
err = s.carDB.InsertCar(context.Background(), car)
s.NoError(err)
_, err = s.controller.RetrieveCar(context.Background(), &workshop.RetrieveCarRequest{CarNumber: "12345"})
s.EqualError(err, "car 12345 is not painted")
// Now paint the car and get it
err = s.carDB.PaintCar(context.Background(), "12345", "black")
s.NoError(err)
carProto, err := s.controller.RetrieveCar(context.Background(), &workshop.RetrieveCarRequest{CarNumber: "12345"})
s.NoError(err)
s.Equal("black", carProto.GetColor())
s.Equal("12345", carProto.GetNumber())
s.Equal("test owner", carProto.GetOwner())
s.Equal(workshop.Car_SEDAN, carProto.GetBodyStyle())
}
func (s *workshopSuite) TestCarPainted() {
_, err := s.controller.AcceptCar(context.Background(), &workshop.Car{
Number: "123456",
Owner: "test owner",
BodyStyle: workshop.Car_PHAETON,
Color: "fuchsia",
})
s.NoError(err)
_, err = s.controller.CarPainted(context.Background(), &workshop.PaintFinishedRequest{
CarNumber: "123456",
DesiredColor: "indigo",
})
s.NoError(err)
car, err := s.carDB.GetCar(context.Background(), "123456")
s.NoError(err)
s.Equal("indigo", car.CurrentColor)
s.Equal("fuchsia", car.OriginalColor)
s.True(car.Painted)
}
func (s *workshopSuite) SetupSuite() {
var err error
s.pwd, err = os.Getwd()
s.Require().NoError(err)
}
func (s *workshopSuite) SetupTest() {
s.ctrl = gomock.NewController(s.T())
s.app = fxtest.New(s.T(),
s.fxOptions(),
)
s.app.RequireStart()
}
func (s *workshopSuite) TearDownTest() {
s.app.RequireStop()
s.ctrl.Finish()
}
func (s *workshopSuite) fxOptions() fx.Option {
return fx.Options(
fx.NopLogger, // remove fx debug prints
mortar.ViperFxOption(s.pwd+"/../../config/config.yml", s.pwd+"/../../config/config_test.yml"),
mortar.LoggerFxOption(),
//providers.HTTPClientBuildersFxOption(), // uncomment this line to see that TestPaintCar fails
fx.Provide(s.specialHTTPClientBuilder),
fx.Provide(data.CreateCarDB),
fx.Provide(controllers.CreateWorkshopController),
fx.Populate(&s.carDB),
fx.Populate(&s.controller),
)
}
func (s *workshopSuite) specialHTTPClientBuilder() clientInt.NewHTTPClientBuilder {
return func() clientInt.HTTPClientBuilder {
return client.HTTPClientBuilder().AddInterceptors(func(*http.Request, clientInt.HTTPpHandler) (*http.Response, error) {
// special case, don't go anywhere just return the response
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
ContentLength: 11,
Body: ioutil.NopCloser(strings.NewReader("car painted")),
}, nil
})
}
}