-
Notifications
You must be signed in to change notification settings - Fork 182
/
api_test_plan.go
138 lines (126 loc) · 3.25 KB
/
api_test_plan.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
package api
import (
"errors"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/rands"
"time"
)
// API测试计划
type APITestPlan struct {
On bool `yaml:"on" json:"on"` // 是否开启
Filename string `yaml:"filename" json:"filename"` // 配置文件名
Hour int `yaml:"hour" json:"hour"` // 小时
Minute int `yaml:"minute" json:"minute"` // 分钟
Second int `yaml:"second" json:"second"` // 秒
Weekdays []int `yaml:"weekdays" json:"weekdays"` // 周
Reports []string `yaml:"reports" json:"reports"` // 报告文件名
APIs []string `yaml:"apis" json:"apis"` // 参与计划的API TODO 需要实现
}
// 获取新对象
func NewAPITestPlan() *APITestPlan {
return &APITestPlan{
On: true,
}
}
// 从文件中加载测试计划
func NewAPITestPlanFromFile(filename string) *APITestPlan {
if len(filename) == 0 {
return nil
}
file := files.NewFile(Tea.ConfigFile(filename))
reader, err := file.Reader()
if err != nil {
logs.Error(err)
return nil
}
defer reader.Close()
plan := NewAPITestPlan()
err = reader.ReadYAML(plan)
if err != nil {
logs.Error(err)
return nil
}
plan.Filename = filename
return plan
}
// 保存当前测试计划
func (this *APITestPlan) Save() error {
if len(this.Filename) == 0 {
this.Filename = "plan." + rands.HexString(16)+ ".conf"
}
file := files.NewFile(Tea.ConfigFile(this.Filename))
writer, err := file.Writer()
if err != nil {
return err
}
defer writer.Close()
_, err = writer.WriteYAML(this)
return nil
}
// 删除当前测试计划
func (this *APITestPlan) Delete() error {
if len(this.Filename) == 0 {
return errors.New("filename should not be empty")
}
file := files.NewFile(Tea.ConfigFile(this.Filename))
err := file.Delete()
if err == nil {
// 删除报告
for _, report := range this.Reports {
err := files.NewFile(Tea.ConfigFile(report)).Delete()
if err != nil {
logs.Error(err)
}
}
}
return err
}
// 添加测试报告
func (this *APITestPlan) AddReport(reportFilename string) {
this.Reports = append(this.Reports, reportFilename)
}
// 读取最后一次报告
func (this *APITestPlan) LastReport() *APITestPlanReport {
if len(this.Reports) == 0 {
return nil
}
reportFile := this.Reports[len(this.Reports)-1]
return NewAPITestPlanReportFromFile(reportFile)
}
// 取得周内日期
func (this *APITestPlan) WeekdayNames() []string {
result := []string{}
for _, weekday := range this.Weekdays {
switch weekday {
case 1:
result = append(result, "周一")
case 2:
result = append(result, "周二")
case 3:
result = append(result, "周三")
case 4:
result = append(result, "周四")
case 5:
result = append(result, "周五")
case 6:
result = append(result, "周六")
case 7:
result = append(result, "周日")
}
}
return result
}
// 检查时间是否匹配
func (this *APITestPlan) MatchTime(currentTime time.Time) bool {
weekday := int(currentTime.Weekday())
if weekday == 0 {
weekday = 7
}
return currentTime.Hour() == this.Hour &&
currentTime.Minute() == this.Minute &&
currentTime.Second() == this.Second &&
lists.Contains(this.Weekdays, weekday)
}