-
Notifications
You must be signed in to change notification settings - Fork 1
/
testexecs.go
292 lines (235 loc) · 9.54 KB
/
testexecs.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package controllers
import (
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
"github.com/wisedog/ladybug/errors"
"github.com/wisedog/ladybug/interfacer"
"github.com/wisedog/ladybug/models"
log "gopkg.in/inconshreveable/log15.v2"
)
//ExecIndex renders A page to show all test executions
func ExecIndex(c *interfacer.AppContext, w http.ResponseWriter, r *http.Request) error {
var prj models.Project
if err := c.Db.Where("name = ?", c.ProjectName).First(&prj); err.Error != nil {
log.Error("TestPlan", "type", "database", "msg ", err.Error)
return errors.HttpError{Status: http.StatusInternalServerError,
Desc: "An error while find project in ExecIndex.Index"}
}
var testexecs []models.Execution
c.Db.Preload("Plan").Preload("Executor").Preload("TargetBuild").Where("project_id = ?", prj.ID).Find(&testexecs)
//Update progress
for idx := 0; idx < len(testexecs); idx++ {
prg := float32(testexecs[idx].PassCaseNum+testexecs[idx].FailCaseNum) / float32(testexecs[idx].TotalCaseNum) * 100
testexecs[idx].Progress = int(prg)
}
items := map[string]interface{}{
"TestExecs": testexecs,
"Active_idx": 5,
}
return Render2(c, w, items, "views/base.tmpl", "views/testexecs/execindex.tmpl")
}
// ExecDone is a POST Handler for Done button of test execution.
// This function ensures that all test cases are executed.
func ExecDone(c *interfacer.AppContext, w http.ResponseWriter, r *http.Request) error {
if err := r.ParseForm(); err != nil {
log.Error("TestExec", "type", "http", "msg ", err)
return errors.HttpError{Status: http.StatusInternalServerError, Desc: "ParseForm failed"}
}
execID := r.FormValue("exec_id")
//comment := r.FormValue("comment")
var prj models.Project
if err := c.Db.Where("name = ?", c.ProjectName).First(&prj); err.Error != nil {
log.Error("TestExec", "type", "database", "msg ", err.Error)
return RenderJSONWithStatus(w, Resp{Msg: "Find Project failed in TestExecs.Done"}, http.StatusInternalServerError)
}
var testexec models.Execution
if err := c.Db.Preload("Plan").Where("id = ?", execID).First(&testexec); err.Error != nil {
log.Error("TestExec", "type", "database", "msg ", err.Error)
return RenderJSONWithStatus(w, Resp{Msg: "Find Test Execution entity failed in TestExecs.Done"},
http.StatusInternalServerError)
}
// validation this execution is finished.
// first, just verify that (PassCaseNum + FailCaseNum == TotalCaseNum)
if testexec.PassCaseNum+testexec.FailCaseNum < testexec.TotalCaseNum {
return RenderJSONWithStatus(w, Resp{Msg: "Not complete test execution."}, http.StatusInternalServerError)
}
// TODO below second, find all test results of this test execution and count
count := 0
c.Db.Model(&models.TestCaseResult{}).Count(&count)
if count != testexec.TotalCaseNum {
return RenderJSONWithStatus(w, Resp{Msg: "Not complete test execution."}, http.StatusInternalServerError)
}
if testexec.FailCaseNum > 0 {
testexec.Status = models.ExecStatusFail
} else {
testexec.Status = models.ExecStatusPass
}
if err := c.Db.Model(&testexec).Update("status", testexec.Status); err.Error != nil {
log.Error("TestExec", "type", "database", "msg ", err.Error)
return RenderJSONWithStatus(w, Resp{Msg: "Update Test Execution entity failed in TestExecs.Done"},
http.StatusInternalServerError)
}
return RenderJSONWithStatus(w, Resp{Msg: "OK"}, http.StatusOK)
}
// ExecUpdateResult updates the result of each test case
func ExecUpdateResult(c *interfacer.AppContext, w http.ResponseWriter, r *http.Request) error {
var rv models.TestCaseResult
if err := r.ParseForm(); err != nil {
log.Error("TestExec", "type", "http", "msg ", err)
return errors.HttpError{Status: http.StatusInternalServerError, Desc: "ParseForm failed"}
}
execIDStr := r.FormValue("exec_id")
execID, _ := strconv.Atoi(execIDStr)
caseIDStr := r.FormValue("case_id")
caseID, _ := strconv.Atoi(caseIDStr)
resultStr := r.FormValue("result")
result, _ := strconv.ParseBool(resultStr)
actual := r.FormValue("actual")
caseVerStr := r.FormValue("case_ver")
caseVer, _ := strconv.Atoi(caseVerStr)
var exec models.Execution
if err := c.Db.Where("id = ?", execID).First(&exec); err.Error != nil {
log.Error("An error while on finding test result in TestExecs.calculateProgress")
return RenderJSONWithStatus(w, Resp{Msg: "An error while on finding test result in TestExecs.calculateProgress"},
http.StatusInternalServerError)
}
var rvTmp []models.TestCaseResult
c.Db.Where("exec_id = ? and test_case_id = ?", execID, caseID).Find(&rvTmp)
// if there is no exist test result
if len(rvTmp) == 0 {
rv.TestCaseID = caseID
rv.ExecID = execID
rv.Status = result
rv.Actual = actual
rv.TestCaseVer = caseVer
//TODO validation
c.Db.NewRecord(rv)
if err := c.Db.Create(&rv); err.Error != nil {
log.Error("Insert operation failed in TestExecs.UpdateResult")
return RenderJSONWithStatus(w, Resp{Msg: "Insert operation failed in TestExecs.UpdateResult"},
http.StatusInternalServerError)
}
//set exec's status ready to in progress
exec.Status = models.ExecStatusInProgress
if rv.Status == true {
exec.PassCaseNum = exec.PassCaseNum + 1
} else {
exec.FailCaseNum = exec.FailCaseNum + 1
}
c.Db.Save(&exec)
} else {
if err := c.Db.Where("exec_id = ? and test_case_id = ?", execID, caseID).First(&rv); err.Error != nil {
log.Error("Select operation failed in TestExecs.UpdateResult")
return RenderJSONWithStatus(w, Resp{Msg: "Select operation failed in TestExecs.UpdateResult"},
http.StatusInternalServerError)
}
if rv.Status == true {
if result == true {
// same value. not updated
} else {
// update. decrease execution.passcounter and increase failcounter
exec.PassCaseNum = exec.PassCaseNum - 1
exec.FailCaseNum = exec.FailCaseNum + 1
}
} else {
if result == true {
// update increase execution.passcounter and decrease failcounter
exec.PassCaseNum = exec.PassCaseNum + 1
exec.FailCaseNum = exec.FailCaseNum - 1
} else {
// same value. not updated
}
}
rv.Status = result
rv.Actual = actual
rv.TestPlanID = exec.PlanID
// update testcase result
if err := c.Db.Save(&rv); err.Error != nil {
log.Error("Update operation failed in TestExecs.UpdateResult")
return RenderJSONWithStatus(w, Resp{Msg: "Update operation failed in TestExecs.UpdateResult"},
http.StatusInternalServerError)
}
// update exec
//db.Model(&user).Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})
if err := c.Db.Model(&exec).Updates(
map[string]interface{}{
"pass_case_num": exec.PassCaseNum,
"fail_case_num": exec.FailCaseNum,
"status": exec.Status}); err.Error != nil {
log.Error("Update operation failed in TestExecs.UpdateResult")
return RenderJSONWithStatus(w, Resp{Msg: "Update operation failed in TestExecs.UpdateResult"},
http.StatusInternalServerError)
}
}
return RenderJSONWithStatus(w, Resp{Msg: "OK"}, http.StatusOK)
}
// ExecRun renders a test running page.
func ExecRun(c *interfacer.AppContext, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
id := vars["id"] // test execution id
var testexec models.Execution
var prj models.Project
if err := c.Db.Where("name = ?", c.ProjectName).First(&prj); err.Error != nil {
log.Error("TestExec", "type", "database", "msg ", err)
return errors.HttpError{Status: http.StatusInternalServerError, Desc: "ParseForm failed"}
}
c.Db.Preload("Plan").Preload("TargetBuild").Where("id = ?", id).First(&testexec)
arr := strings.Split(testexec.Plan.ExecuteCases, ",")
// Question. Could the test execution have no testcase?
var cases []models.TestCase
c.Db.Where("id in (?)", arr).Find(&cases)
var results []models.TestCaseResult
c.Db.Order("test_case_id").Where("exec_id = ?", id).Find(&results)
passCounter := 0
failCounter := 0
for _, k := range results {
if k.Status == true {
passCounter++
} else {
failCounter++
}
}
items := map[string]interface{}{
"TestExec": testexec,
"Cases": cases,
"Results": results,
"PassCounter": passCounter,
"FailCounter": failCounter,
"Active_idx": 5,
}
return Render2(c, w, items, "views/base.tmpl", "views/testexecs/exec_run.tmpl")
}
// ExecRemove is a POST handler and deletes test execution entity.
func ExecRemove(c *interfacer.AppContext, w http.ResponseWriter, r *http.Request) error {
if err := r.ParseForm(); err != nil {
log.Error("TestExec", "type", "http", "msg ", err)
return errors.HttpError{Status: http.StatusInternalServerError, Desc: "ParseForm failed"}
}
id := r.FormValue("id")
if err := c.Db.Where("id = ?", id).Delete(models.Execution{}); err.Error != nil {
return RenderJSONWithStatus(w, Resp{Msg: "Problem while deleting execution"},
http.StatusInternalServerError)
}
return RenderJSON(w, Resp{Msg: "ok"})
}
//ExecDeny makes the test execution denied
func ExecDeny(c *interfacer.AppContext, w http.ResponseWriter, r *http.Request) error {
if err := r.ParseForm(); err != nil {
log.Error("TestExec", "type", "http", "msg ", err)
return errors.HttpError{Status: http.StatusInternalServerError, Desc: "ParseForm failed"}
}
id := r.FormValue("id")
msg := r.FormValue("msg")
var testexec models.Execution
if err := c.Db.Where("id = ?", id).First(&testexec); err.Error != nil {
return RenderJSONWithStatus(w, Resp{Msg: "Not found Test Execution"}, http.StatusInternalServerError)
}
testexec.Status = models.ExecStatusDeny
testexec.Message = msg
if err := c.Db.Save(testexec); err.Error != nil {
return RenderJSONWithStatus(w, Resp{Msg: "Error while saving"}, http.StatusInternalServerError)
}
return RenderJSON(w, Resp{Msg: "ok"})
}