This repository was archived by the owner on Oct 20, 2020. It is now read-only.
forked from educlos/testrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase.go
160 lines (145 loc) · 6.11 KB
/
case.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
package testrail
import "fmt"
// Case represents a Test Case
type Case struct {
CreatedBy int `json:"created_by"`
CreatedOn int `json:"created_on"`
CustomExpected string `json:"custom_expected"`
CustomPreconds string `json:"custom_preconds"`
CustomSteps string `json:"custom_steps"`
CustomStepsSeparated []CustomStep `json:"custom_steps_separated"`
Estimate string `json:"estimate"`
EstimateForecast string `json:"estimate_forecast"`
ID int `json:"id"`
MilestoneID int `json:"milestone_id"`
PriorityID int `json:"priority_id"`
Refs string `json:"refs"`
SectionID int `json:"section_id"`
SuiteID int `json:"suite_id"`
Title string `json:"title"`
TypeID int `json:"type_id"`
UpdatedBy int `json:"updated_by"`
UdpatedOn int `json:"updated_on"`
}
// CustomStep represents the custom steps
// a Test Case can have
type CustomStep struct {
Content string `json:"content"`
Expected string `json:"expected"`
}
// RequestFilterForCases represents the filters
// usable to get the test cases
type RequestFilterForCases struct {
CreatedAfter string `json:"created_after"`
CreatedBefore string `json:"created_before"`
CreatedBy []int `json:"created_by"`
MilestoneID []int `json:"milestone_id"`
PriorityID []int `json:"priority_id"`
TypeID []int `json:"type_id"`
UpdatedAfter string `json:"updated_after"`
UpdatedBefore string `json:"updated_before"`
UpdatedBy []int `json:"updated_by"`
}
// SendableCase represents a Test Case
// that can be created or updated via the api
type SendableCase struct {
Title string `json:"title"`
TypeID int `json:"type_id,omitempty"`
TemplateID int `json:"template_id,omitempty"`
PriorityID int `json:"priority_id,omitempty"`
Estimate string `json:"estimate,omitempty"`
MilestoneID int `json:"milestone_id,omitempty"`
Refs string `json:"refs,omitempty"`
Checkbox bool `json:"custom_checkbox,omitempty"`
Date string `json:"custom_date,omitempty"`
Dropdown int `json:"custom_dropdown,omitempty"`
Integer int `json:"custom_integer,omitempty"`
Milestone int `json:"custom_milestone,omitempty"`
MultiSelect []int `json:"custom_multi-select,omitempty"`
Steps []CustomStep `json:"custom_steps_separated,omitempty"`
String string `json:"custom_string,omitempty"`
Text string `json:"custom_text,omitempty"`
URL string `json:"custom_url,omitempty"`
User int `json:"custom_user,omitempty"`
Preconditions string `json:"custom_preconds,omitempty"`
Description string `json:"custom_detailed_description,omitempty"`
DeliveryTeam int `json:"custom_delivery_team,omitempty"`
}
// GetCase returns the existing Test Case caseID
func (c *Client) GetCase(caseID int) (Case, error) {
returnCase := Case{}
err := c.sendRequest("GET", fmt.Sprintf("get_case/%d", caseID), nil, &returnCase)
return returnCase, err
}
// GetCases returns a list of Test Cases on project projectID
// for a Test Suite suiteID
// or for specific section sectionID in a Test Suite
func (c *Client) GetCases(projectID, suiteID int, sectionID ...int) ([]Case, error) {
uri := fmt.Sprintf("get_cases/%d&suite_id=%d", projectID, suiteID)
if len(sectionID) > 0 {
uri = fmt.Sprintf("%s§ion_id=%d", uri, sectionID[0])
}
returnCases := []Case{}
err := c.sendRequest("GET", uri, nil, &returnCases)
return returnCases, err
}
// GetCasesWithFilters returns a list of Test Cases on project projectID
// for a Test Suite suiteID validating the filters
func (c *Client) GetCasesWithFilters(projectID, suiteID int, filters ...RequestFilterForCases) ([]Case, error) {
uri := fmt.Sprintf("get_cases/%d&suite_id=%d", projectID, suiteID)
if len(filters) > 0 {
uri = applyFiltersForCase(uri, filters[0])
}
returnCases := []Case{}
fmt.Println(uri)
err := c.sendRequest("GET", uri, nil, &returnCases)
return returnCases, err
}
// AddCase creates a new Test Case newCase and returns it
func (c *Client) AddCase(sectionID int, newCase SendableCase) (Case, error) {
createdCase := Case{}
err := c.sendRequest("POST", fmt.Sprintf("add_case/%d", sectionID), newCase, &createdCase)
return createdCase, err
}
// UpdateCase updates an existing Test Case caseID and returns it
func (c *Client) UpdateCase(caseID int, updates SendableCase) (Case, error) {
updatedCase := Case{}
err := c.sendRequest("POST", fmt.Sprintf("update_case/%d", caseID), updates, &updatedCase)
return updatedCase, err
}
// DeleteCase deletes the existing Test Case caseID
func (c *Client) DeleteCase(caseID int) error {
return c.sendRequest("POST", fmt.Sprintf("delete_case/%d", caseID), nil, nil)
}
// applyFiltersForCase go through each possible filters and create the
// uri for the wanted ones
func applyFiltersForCase(uri string, filters RequestFilterForCases) string {
if filters.CreatedAfter != "" {
uri = fmt.Sprintf("%s&created_after=%s", uri, filters.CreatedAfter)
}
if filters.CreatedBefore != "" {
uri = fmt.Sprintf("%s&created_before=%s", uri, filters.CreatedBefore)
}
if len(filters.CreatedBy) != 0 {
uri = applySpecificFilter(uri, "created_by", filters.CreatedBy)
}
if len(filters.MilestoneID) != 0 {
uri = applySpecificFilter(uri, "milestone_id", filters.MilestoneID)
}
if len(filters.PriorityID) != 0 {
uri = applySpecificFilter(uri, "priority_id", filters.PriorityID)
}
if len(filters.TypeID) != 0 {
uri = applySpecificFilter(uri, "type_id", filters.TypeID)
}
if filters.UpdatedAfter != "" {
uri = fmt.Sprintf("%s&updated_after=%s", uri, filters.UpdatedAfter)
}
if filters.UpdatedBefore != "" {
uri = fmt.Sprintf("%s&updated_before=%s", uri, filters.UpdatedBefore)
}
if len(filters.UpdatedBy) != 0 {
uri = applySpecificFilter(uri, "updated_by", filters.UpdatedBy)
}
return uri
}