-
Notifications
You must be signed in to change notification settings - Fork 42
/
result.go
213 lines (187 loc) · 6.83 KB
/
result.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
package testrail
import "strconv"
// Result represents a Test Case result
type Result struct {
AssignedtoID int `json:"assignedto_id"`
Comment string `json:"comment"`
CreatedBy int `json:"created_by"`
CreatedOn timestamp `json:"created_on"`
Defects string `json:"defects"`
Elapsed timespan `json:"elapsed"`
ID int `json:"id"`
StatusID int `json:"status_id"`
TestID int `json:"test_id"`
Version string `json:"version"`
customResult
}
// CustomStepResult represents the custom steps
// results a Result can have
type CustomStepResult struct {
Content string `json:"content"`
Expected string `json:"expected"`
Actual string `json:"actual"`
StatusID int `json:"status_id"`
}
// RequestFilterForCaseResults represents the filters
// usable to get the test case results
type RequestFilterForCaseResults struct {
Limit *int `json:"limit,omitempty"`
Offest *int `json:"offset, omitempty"`
StatusID []int `json:"status_id,omitempty"`
}
// RequestFilterForRunResults represents the filters
// usable to get the run results
type RequestFilterForRunResults struct {
CreatedAfter string `json:"created_after,omitempty"`
CreatedBefore string `json:"created_before,omitempty"`
CreatedBy []int `json:"created_by,omitempty"`
Limit *int `json:"limit,omitempty"`
Offest *int `json:"offset, omitempty"`
StatusID []int `json:"status_id,omitempty"`
}
// SendableResult represents a Test Case result
// that can be created or updated via the api
type SendableResult struct {
StatusID int `json:"status_id,omitempty"`
Comment string `json:"comment,omitempty"`
Version string `json:"version,omitempty"`
Elapsed timespan `json:"elapsed,omitempty"`
Defects string `json:"defects,omitempty"`
AssignedToID int `json:"assignedto_id,omitempty"`
customResult
}
// SendableResults represents a list of run results
// that can be created or updated via the api
type SendableResults struct {
Results []Results `json:"results"`
}
// Results represents a run result
// that can be created or updated via the api
type Results struct {
TestID int `json:"test_id"`
SendableResult
}
// Results represents a run result
// that can be created or updated via the api
type ResultsForCase struct {
CaseID int `json:"case_id"`
SendableResult
}
// SendableResultsForCase represents a Test Case result
// that can be created or updated via the api
type SendableResultsForCase struct {
Results []ResultsForCase `json:"results"`
}
// GetResults returns a list of results for the test testID
// validating the filters
func (c *Client) GetResults(testID int, filters ...RequestFilterForCaseResults) ([]Result, error) {
returnResults := []Result{}
uri := "get_results/" + strconv.Itoa(testID)
if len(filters) > 0 {
uri = applyFiltersForCaseResults(uri, filters[0])
}
var err error
if c.useBetaApi {
err = c.sendRequestBeta("GET", uri, nil, &returnResults, "results")
} else {
err = c.sendRequest("GET", uri, nil, &returnResults)
}
return returnResults, err
}
// GetResultsForCase returns a list of results for the case caseID
// on run runID validating the filters
func (c *Client) GetResultsForCase(runID, caseID int, filters ...RequestFilterForCaseResults) ([]Result, error) {
returnResults := []Result{}
uri := "get_results_for_case/" + strconv.Itoa(runID) + "/" + strconv.Itoa(caseID)
if len(filters) > 0 {
uri = applyFiltersForCaseResults(uri, filters[0])
}
var err error
if c.useBetaApi {
err = c.sendRequestBeta("GET", uri, nil, &returnResults, "results")
} else {
err = c.sendRequest("GET", uri, nil, &returnResults)
}
return returnResults, err
}
// GetResultsForRun returns a list of results for the run runID
// validating the filters
func (c *Client) GetResultsForRun(runID int, filters ...RequestFilterForRunResults) ([]Result, error) {
returnResults := struct {
Results []Result `json:"results"`
}{}
uri := "get_results_for_run/" + strconv.Itoa(runID)
if len(filters) > 0 {
uri = applyFiltersForRunResults(uri, filters[0])
}
var err error
if c.useBetaApi {
err = c.sendRequestBeta("GET", uri, nil, &returnResults, "results")
} else {
err = c.sendRequest("GET", uri, nil, &returnResults)
}
return returnResults.Results, err
}
// AddResult adds a new result, comment or assigns a test to testID
func (c *Client) AddResult(testID int, newResult SendableResult) (Result, error) {
createdResult := Result{}
err := c.sendRequest("POST", "add_result/"+strconv.Itoa(testID), newResult, &createdResult)
return createdResult, err
}
// AddResultForCase adds a new result, comment or assigns a test to the case caseID on run runID
func (c *Client) AddResultForCase(runID, caseID int, newResult SendableResult) (Result, error) {
createdResult := Result{}
uri := "add_result_for_case/" + strconv.Itoa(runID) + "/" + strconv.Itoa(caseID)
err := c.sendRequest("POST", uri, newResult, &createdResult)
return createdResult, err
}
// AddResults adds new results, comment or assigns tests to runID
func (c *Client) AddResults(runID int, newResult SendableResults) ([]Result, error) {
createdResult := []Result{}
err := c.sendRequest("POST", "add_results/"+strconv.Itoa(runID), newResult, &createdResult)
return createdResult, err
}
// AddResultsForCases adds new results, comments or assigns tests to run runID
// each result being assigned to a test case
func (c *Client) AddResultsForCases(runID int, newResult SendableResultsForCase) ([]Result, error) {
createdResult := []Result{}
err := c.sendRequest("POST", "add_results_for_cases/"+strconv.Itoa(runID), newResult, &createdResult)
return createdResult, err
}
// applyFiltersForCaseResults go through each possible filters and create the
// uri for the wanted ones
func applyFiltersForCaseResults(uri string, filters RequestFilterForCaseResults) string {
if filters.Limit != nil {
uri = uri + "&limit=" + strconv.Itoa(*filters.Limit)
}
if filters.Offest != nil {
uri = uri + "&offset=" + strconv.Itoa(*filters.Offest)
}
if len(filters.StatusID) != 0 {
uri = applySpecificFilter(uri, "status_id", filters.StatusID)
}
return uri
}
// applyFiltersForCaseResults go through each possible filters and create the
// uri for the wanted ones
func applyFiltersForRunResults(uri string, filters RequestFilterForRunResults) string {
if filters.CreatedAfter != "" {
uri = uri + "&created_after=" + filters.CreatedAfter
}
if filters.CreatedBefore != "" {
uri = uri + "&created_before=" + filters.CreatedBefore
}
if len(filters.CreatedBy) != 0 {
uri = applySpecificFilter(uri, "created_by", filters.CreatedBy)
}
if filters.Limit != nil {
uri = uri + "&limit=" + strconv.Itoa(*filters.Limit)
}
if filters.Offest != nil {
uri = uri + "&offset=" + strconv.Itoa(*filters.Offest)
}
if len(filters.StatusID) != 0 {
uri = applySpecificFilter(uri, "status_id", filters.StatusID)
}
return uri
}