@@ -17,9 +17,12 @@ package pkg
17
17
18
18
import (
19
19
"encoding/json"
20
+ "fmt"
21
+ "time"
20
22
21
23
"github.com/linuxsuren/api-testing/pkg/server"
22
24
"github.com/linuxsuren/api-testing/pkg/testing/remote"
25
+ "google.golang.org/protobuf/types/known/timestamppb"
23
26
)
24
27
25
28
func ConverToDBTestCase (testcase * server.TestCase ) (result * TestCase ) {
@@ -76,6 +79,33 @@ func ConvertToRemoteTestCase(testcase *TestCase) (result *server.TestCase) {
76
79
return
77
80
}
78
81
82
+ func ConvertHistoryToRemoteTestCase (historyTestcase * HistoryTestResult ) (result * server.TestCase ) {
83
+ result = & server.TestCase {
84
+ Name : historyTestcase .CaseName ,
85
+ SuiteName : historyTestcase .SuiteName ,
86
+
87
+ Request : & server.Request {
88
+ Api : historyTestcase .CaseAPI ,
89
+ Method : historyTestcase .Method ,
90
+ Body : historyTestcase .Body ,
91
+ Header : jsonToPair (historyTestcase .Header ),
92
+ Cookie : jsonToPair (historyTestcase .Cookie ),
93
+ Query : jsonToPair (historyTestcase .Query ),
94
+ Form : jsonToPair (historyTestcase .Form ),
95
+ },
96
+
97
+ Response : & server.Response {
98
+ StatusCode : int32 (historyTestcase .ExpectStatusCode ),
99
+ Body : historyTestcase .ExpectBody ,
100
+ Schema : historyTestcase .ExpectSchema ,
101
+ Verify : jsonToSlice (historyTestcase .ExpectVerify ),
102
+ BodyFieldsExpect : jsonToPair (historyTestcase .ExpectBodyFields ),
103
+ Header : jsonToPair (historyTestcase .ExpectHeader ),
104
+ },
105
+ }
106
+ return
107
+ }
108
+
79
109
func ConvertToDBTestSuite (suite * remote.TestSuite ) (result * TestSuite ) {
80
110
result = & TestSuite {
81
111
Name : suite .Name ,
@@ -91,6 +121,76 @@ func ConvertToDBTestSuite(suite *remote.TestSuite) (result *TestSuite) {
91
121
return
92
122
}
93
123
124
+ func ConvertToDBHistoryTestResult (historyTestResult * server.HistoryTestResult ) (result * HistoryTestResult ) {
125
+ result = & HistoryTestResult {
126
+ Message : historyTestResult .Message ,
127
+ Error : historyTestResult .Error ,
128
+ }
129
+ if historyTestResult .CreateTime != nil {
130
+ id := fmt .Sprintf ("%s_%s_%s" , historyTestResult .CreateTime .AsTime ().Local ().Format ("2006-01-02T15:04:05.999999999" ), historyTestResult .Data .SuiteName , historyTestResult .Data .CaseName )
131
+ result .ID = id
132
+ result .CreateTime = historyTestResult .CreateTime .AsTime ().Local ().Format ("2006-01-02T15:04:05.999999999" )
133
+ result .HistorySuiteName = historyTestResult .CreateTime .AsTime ().Local ().Format ("2006-1-2" )
134
+ }
135
+ if historyTestResult .Data != nil {
136
+ result .Param = pairToJSON (historyTestResult .Data .SuiteParam )
137
+ result .CaseName = historyTestResult .Data .CaseName
138
+ result .SuiteName = historyTestResult .Data .SuiteName
139
+ result .SuiteAPI = historyTestResult .Data .SuiteApi
140
+ result .HistoryHeader = pairToJSON (historyTestResult .Data .HistoryHeader )
141
+ if historyTestResult .Data .Request != nil {
142
+ request := historyTestResult .Data .Request
143
+ result .CaseAPI = request .Api
144
+ result .Method = request .Method
145
+ result .Header = pairToJSON (request .Header )
146
+ result .Cookie = pairToJSON (request .Cookie )
147
+ result .Form = pairToJSON (request .Form )
148
+ result .Query = pairToJSON (request .Query )
149
+ }
150
+ if historyTestResult .Data .Response != nil {
151
+ resp := historyTestResult .Data .Response
152
+ result .ExpectBody = resp .Body
153
+ result .ExpectSchema = resp .Schema
154
+ result .ExpectStatusCode = int (resp .StatusCode )
155
+ result .ExpectHeader = pairToJSON (resp .Header )
156
+ result .ExpectBodyFields = pairToJSON (resp .BodyFieldsExpect )
157
+ result .ExpectVerify = SliceToJSON (resp .Verify )
158
+ }
159
+ if historyTestResult .Data .SuiteSpec != nil {
160
+ result .SpecKind = historyTestResult .Data .SuiteSpec .Kind
161
+ result .SpecURL = historyTestResult .Data .SuiteSpec .Url
162
+ }
163
+ }
164
+ for _ , testCase := range historyTestResult .TestCaseResult {
165
+ result .StatusCode = int32 (testCase .StatusCode )
166
+ result .Output = testCase .Output
167
+ result .Body = testCase .Body
168
+ }
169
+ return
170
+ }
171
+
172
+ func ConvertToRemoteHistoryTestResult (historyTestResult * HistoryTestResult ) (result * server.HistoryTestResult ) {
173
+ createTime , err := time .Parse ("2006-01-02T15:04:05.999999999" , historyTestResult .CreateTime )
174
+ if err != nil {
175
+ fmt .Println ("Error parsing time:" , err )
176
+ }
177
+ result = & server.HistoryTestResult {
178
+ Message : historyTestResult .Message ,
179
+ Error : historyTestResult .Error ,
180
+ CreateTime : timestamppb .New (createTime ),
181
+ }
182
+ TestCaseResult := & server.TestCaseResult {
183
+ StatusCode : historyTestResult .StatusCode ,
184
+ Body : historyTestResult .Body ,
185
+ Output : historyTestResult .Output ,
186
+ Error : historyTestResult .Error ,
187
+ Header : jsonToPair (historyTestResult .Header ),
188
+ }
189
+ result .TestCaseResult = append (result .TestCaseResult , TestCaseResult )
190
+ result .Data = ConvertToGRPCHistoryTestCase (historyTestResult )
191
+ return
192
+ }
193
+
94
194
func ConvertToGRPCTestSuite (suite * TestSuite ) (result * remote.TestSuite ) {
95
195
result = & remote.TestSuite {
96
196
Name : suite .Name ,
@@ -104,6 +204,58 @@ func ConvertToGRPCTestSuite(suite *TestSuite) (result *remote.TestSuite) {
104
204
return
105
205
}
106
206
207
+ func ConvertToGRPCHistoryTestSuite (historyTestResult * HistoryTestResult ) (result * remote.HistoryTestSuite ) {
208
+ result = & remote.HistoryTestSuite {
209
+ HistorySuiteName : historyTestResult .HistorySuiteName ,
210
+ }
211
+
212
+ item := ConvertToGRPCHistoryTestCase (historyTestResult )
213
+ result .Items = append (result .Items , item )
214
+ return
215
+ }
216
+
217
+ func ConvertToGRPCHistoryTestCase (historyTestResult * HistoryTestResult ) (result * server.HistoryTestCase ) {
218
+ createTime , err := time .Parse ("2006-01-02T15:04:05.999999999" , historyTestResult .CreateTime )
219
+ if err != nil {
220
+ fmt .Println ("Error parsing time:" , err )
221
+ }
222
+ result = & server.HistoryTestCase {
223
+ ID : historyTestResult .ID ,
224
+ SuiteName : historyTestResult .SuiteName ,
225
+ CaseName : historyTestResult .CaseName ,
226
+ SuiteApi : historyTestResult .SuiteAPI ,
227
+ SuiteParam : jsonToPair (historyTestResult .Param ),
228
+ HistorySuiteName : historyTestResult .HistorySuiteName ,
229
+ CreateTime : timestamppb .New (createTime ),
230
+ HistoryHeader : jsonToPair (historyTestResult .HistoryHeader ),
231
+
232
+ SuiteSpec : & server.APISpec {
233
+ Kind : historyTestResult .SpecKind ,
234
+ Url : historyTestResult .SpecURL ,
235
+ },
236
+
237
+ Request : & server.Request {
238
+ Api : historyTestResult .CaseAPI ,
239
+ Method : historyTestResult .Method ,
240
+ Body : historyTestResult .Body ,
241
+ Header : jsonToPair (historyTestResult .Header ),
242
+ Cookie : jsonToPair (historyTestResult .Cookie ),
243
+ Query : jsonToPair (historyTestResult .Query ),
244
+ Form : jsonToPair (historyTestResult .Form ),
245
+ },
246
+
247
+ Response : & server.Response {
248
+ StatusCode : int32 (historyTestResult .ExpectStatusCode ),
249
+ Body : historyTestResult .ExpectBody ,
250
+ Schema : historyTestResult .ExpectSchema ,
251
+ Verify : jsonToSlice (historyTestResult .ExpectVerify ),
252
+ BodyFieldsExpect : jsonToPair (historyTestResult .ExpectBodyFields ),
253
+ Header : jsonToPair (historyTestResult .ExpectHeader ),
254
+ },
255
+ }
256
+ return
257
+ }
258
+
107
259
func SliceToJSON (slice []string ) (result string ) {
108
260
var data []byte
109
261
var err error
0 commit comments