-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreport.go
215 lines (171 loc) · 4.66 KB
/
report.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
)
type testResult int
const (
testResultPassed testResult = iota
testResultFailed
)
var testResultToString = map[testResult]string{
// TODO: Add SKIPPED test result
testResultPassed: "PASSED",
testResultFailed: "FAILED",
}
func (t testResult) String() string {
return testResultToString[t]
}
type actualExpectStatusCodes struct {
Expected int
Actual int
}
func (a actualExpectStatusCodes) Match() bool {
return a.Expected == a.Actual
}
type actualExpectBody struct {
Expected string
Actual string
}
// Need to do deep equal
func (a actualExpectBody) Match() bool {
equalityResult, _ := compareResponse([]byte(a.Expected), []byte(a.Actual))
return equalityResult
}
type resultDetails struct {
HTTPStatusCodes actualExpectStatusCodes
Body actualExpectBody
}
func (rd *resultDetails) SetActualExpectHTTPStatus(expected, actual int) {
rd.HTTPStatusCodes.Expected = expected
rd.HTTPStatusCodes.Actual = actual
}
func (rd *resultDetails) SetActualExpectBody(expected, actual string) {
var p1 bytes.Buffer
err := json.Indent(&p1, []byte(expected), "", " ")
if err != nil {
rd.Body.Expected = expected
} else {
rd.Body.Expected = p1.String()
}
rd.Body.Actual = actual
}
type testSuiteReport struct {
PathName string
Description string
Operation string
Status testResult
ResultDetails resultDetails
ShouldSkipBodyValidation bool
// This error is caused by http client returning error, we cannot assert the statusCodes
// or bodies if there are error in the http call
Err error
}
func (t testSuiteReport) IsPassed() bool {
return t.Status == testResultPassed
}
func (t *testSuiteReport) Fail() {
t.Status = testResultFailed
}
func (t *testSuiteReport) FailWithError(err error) {
t.Err = err
t.Fail()
}
func (t *testSuiteReport) Pass() {
t.Status = testResultPassed
}
type Report struct {
TestSuites []testSuiteReport
}
func (r Report) AreAllSuccess() bool {
for _, testSuiteReport := range r.TestSuites {
if !testSuiteReport.IsPassed() {
return false
}
}
return true
}
func (r Report) generateFailingTestDescriptions() {
fmt.Println()
fmt.Println()
for _, testSuiteReport := range r.TestSuites {
if !testSuiteReport.IsPassed() {
color.New(color.FgRed).Add(color.Bold).Printf("• %s > %s > %s\n\n", testSuiteReport.PathName, testSuiteReport.Operation, testSuiteReport.Description)
// If err is not nil, we should skip assertion and just explain what the errror is to the user
if testSuiteReport.Err != nil {
fmt.Printf("\t Received error: %s\n\n", testSuiteReport.Err.Error())
continue
}
var p1 = fmt.Sprintf("Expected status code: \t%d\nActual status code: \t%d\n\n",
testSuiteReport.ResultDetails.HTTPStatusCodes.Expected,
testSuiteReport.ResultDetails.HTTPStatusCodes.Actual)
if testSuiteReport.ResultDetails.HTTPStatusCodes.Match() {
color.Green(p1)
} else {
color.Red(p1)
}
if testSuiteReport.Description == "[NEG] Invalid otp" {
fmt.Println(testSuiteReport.ShouldSkipBodyValidation)
}
if testSuiteReport.ShouldSkipBodyValidation {
continue
}
var p2 = fmt.Sprintf("Expected body: \n\n%s\n\nActual body: \n\n%s\n",
testSuiteReport.ResultDetails.Body.Expected, testSuiteReport.ResultDetails.Body.Actual)
if testSuiteReport.ResultDetails.Body.Match() {
color.Green(p2)
} else {
color.Red(p2)
}
fmt.Println()
}
}
}
func (r Report) generate() bool {
data := [][]string{}
for _, testSuiteReport := range r.TestSuites {
data = append(data, []string{
testSuiteReport.PathName,
testSuiteReport.Operation,
testSuiteReport.Description,
testSuiteReport.Status.String(),
})
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Path", "Operation", "Desc", "Status"})
table.SetAutoMergeCellsByColumnIndex([]int{0})
table.SetRowLine(true)
for i, row := range data {
// Row 3 is STATUS -- Refer to SetHeader function
if row[3] == "FAILED" {
// The function `Rich` also appends data to tablewriter
// so we do not need to manually append again
table.Rich(data[i], []tablewriter.Colors{
{},
{},
{},
{
// If test is failing, we mark the cell as RED color
tablewriter.Bold, tablewriter.BgRedColor,
},
})
} else {
table.Rich(data[i], []tablewriter.Colors{
{},
{},
{},
{
// If test is failing, we mark the cell as GREEN color
tablewriter.Bold, tablewriter.FgGreenColor,
},
})
}
}
table.Render()
r.generateFailingTestDescriptions()
return r.AreAllSuccess()
}