forked from vfarcic/go-demo-6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
200 lines (156 loc) · 4.64 KB
/
main_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"gopkg.in/mgo.v2"
)
// Suite
type MainTestSuite struct {
suite.Suite
}
// Suite
func TestMainUnitTestSuite(t *testing.T) {
logFatalOrig := logFatal
logPrintfOrig := logPrintf
httpListenAndServeOrig := httpListenAndServe
defer func() {
logFatal = logFatalOrig
logPrintf = logPrintfOrig
httpListenAndServe = httpListenAndServeOrig
}()
logFatal = func(v ...interface{}) {}
logPrintf = func(format string, v ...interface{}) {}
httpListenAndServe = func(addr string, handler http.Handler) error { return nil }
suite.Run(t, new(MainTestSuite))
}
func (s *MainTestSuite) SetupTest() {}
// init
func (s *MainTestSuite) Test_SetupMetrics_InitializesHistogram() {
s.NotNil(histogram)
}
// RunServer
func (s *MainTestSuite) Test_RunServer_InvokesListenAndServe() {
actual := ""
httpListenAndServe = func(addr string, handler http.Handler) error {
actual = addr
return nil
}
RunServer()
s.Equal(":8080", actual)
}
// RandomErrorServer
func (s *MainTestSuite) Test_HelloServer_WritesOk() {
req, _ := http.NewRequest("GET", "/demo/random-error", nil)
w := getResponseWriterMock()
for i := 0; i <= 3; i++ {
RandomErrorServer(w, req)
}
w.AssertCalled(s.T(), "Write", []byte("Everything is still OK\n"))
}
func (s *MainTestSuite) Test_HelloServer_WritesNokEventually() {
req, _ := http.NewRequest("GET", "/demo/random-error", nil)
w := getResponseWriterMock()
for i := 0; i <= 50; i++ {
RandomErrorServer(w, req)
}
w.AssertCalled(s.T(), "Write", []byte("ERROR: Something, somewhere, went wrong!\n"))
}
// HelloServer
func (s *MainTestSuite) Test_HelloServer_WritesHelloWorld() {
req, _ := http.NewRequest("GET", "/demo/hello", nil)
w := getResponseWriterMock()
HelloServer(w, req)
w.AssertCalled(s.T(), "Write", []byte("hello, world!\n"))
}
func (s *MainTestSuite) Test_HelloServer_Waits_WhenDelayIsPresent() {
sleepOrig := sleep
defer func() { sleep = sleepOrig }()
var actual time.Duration
sleep = func(d time.Duration) {
actual = d
}
req, _ := http.NewRequest("GET", "/demo/hello?delay=10", nil)
w := getResponseWriterMock()
HelloServer(w, req)
s.Equal(10*time.Millisecond, actual)
}
// PersonServer
func (s *MainTestSuite) Test_PersonServer_InvokesUpsertId_WhenPutPerson() {
name := "Viktor"
upsertIdOrig := upsertId
defer func() { upsertId = upsertIdOrig }()
var actualId interface{}
var actualUpdate interface{}
upsertId = func(id interface{}, update interface{}) (info *mgo.ChangeInfo, err error) {
actualId = id
actualUpdate = update
return nil, nil
}
req, _ := http.NewRequest("PUT", fmt.Sprintf("/demo/person?name=%s", name), nil)
w := getResponseWriterMock()
PersonServer(w, req)
s.Equal(name, actualId)
s.Equal(&Person{Name: name}, actualUpdate)
}
func (s *MainTestSuite) Test_PersonServer_Panics_WhenUpsertIdReturnsError() {
upsertIdOrig := upsertId
defer func() { upsertId = upsertIdOrig }()
upsertId = func(id interface{}, update interface{}) (info *mgo.ChangeInfo, err error) {
return nil, fmt.Errorf("This is an error")
}
req, _ := http.NewRequest("PUT", "/demo/person?name=Viktor", nil)
w := getResponseWriterMock()
PersonServer(w, req)
w.AssertCalled(s.T(), "Write", []byte("This is an error"))
}
func (s *MainTestSuite) Test_PersonServer_WritesPeople() {
findPeopleOrig := findPeople
people := []Person{
{Name: "Viktor"},
{Name: "Sara"},
}
defer func() { findPeople = findPeopleOrig }()
findPeople = func(res *[]Person) error {
*res = people
return nil
}
req, _ := http.NewRequest("GET", "/demo/person", nil)
w := getResponseWriterMock()
PersonServer(w, req)
w.AssertCalled(s.T(), "Write", []byte("Viktor\nSara"))
}
func (s *MainTestSuite) Test_PersonServer_Panics_WhenFindReturnsError() {
findPeopleOrig := findPeople
defer func() { findPeople = findPeopleOrig }()
findPeople = func(res *[]Person) error {
return fmt.Errorf("This is an error")
}
req, _ := http.NewRequest("GET", "/demo/person", nil)
w := getResponseWriterMock()
s.Panics(func() { PersonServer(w, req) })
}
type ResponseWriterMock struct {
mock.Mock
}
func (m *ResponseWriterMock) Header() http.Header {
m.Called()
return make(map[string][]string)
}
func (m *ResponseWriterMock) Write(data []byte) (int, error) {
params := m.Called(data)
return params.Int(0), params.Error(1)
}
func (m *ResponseWriterMock) WriteHeader(header int) {
m.Called(header)
}
func getResponseWriterMock() *ResponseWriterMock {
mockObj := new(ResponseWriterMock)
mockObj.On("Header").Return(nil)
mockObj.On("Write", mock.Anything).Return(0, nil)
mockObj.On("WriteHeader", mock.Anything)
return mockObj
}