Skip to content

Commit 4e54fa9

Browse files
committed
fix: add tests
1 parent 22d5878 commit 4e54fa9

File tree

3 files changed

+360
-0
lines changed

3 files changed

+360
-0
lines changed

pkg/server/convert_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ limitations under the License.
1616
package server
1717

1818
import (
19+
"google.golang.org/protobuf/types/known/timestamppb"
1920
"testing"
21+
"time"
2022

2123
atest "github.com/linuxsuren/api-testing/pkg/testing"
2224
"github.com/stretchr/testify/assert"
@@ -149,3 +151,132 @@ func TestToNormalSuite(t *testing.T) {
149151
},
150152
}))
151153
}
154+
155+
func TestConvertToGRPCHistoryTestCase(t *testing.T) {
156+
now := time.Now().UTC()
157+
result := ConvertToGRPCHistoryTestCase(atest.HistoryTestCase{
158+
CreateTime: now,
159+
SuiteParam: defaultMap,
160+
SuiteSpec: atest.APISpec{
161+
Kind: "http",
162+
URL: "/v1",
163+
RPC: &atest.RPCDesc{
164+
Raw: "fake",
165+
},
166+
Secure: &atest.Secure{
167+
KeyFile: "fake",
168+
},
169+
},
170+
Data: atest.TestCase{
171+
Request: atest.Request{
172+
Header: defaultMap,
173+
},
174+
Expect: atest.Response{
175+
BodyFieldsExpect: defaultInterMap,
176+
},
177+
},
178+
})
179+
assert.Equal(t, result.Request.Header, defaultPairs)
180+
assert.Equal(t, result.SuiteParam, defaultPairs)
181+
assert.Equal(t, result.Response.BodyFieldsExpect, defaultPairs)
182+
assert.Equal(t, "fake", result.SuiteSpec.Secure.Key)
183+
assert.Equal(t, timestamppb.New(now), result.CreateTime)
184+
}
185+
186+
func TestToNormalTestCaseResult(t *testing.T) {
187+
assert.Equal(t, atest.TestCaseResult{
188+
Body: "body",
189+
Error: "error",
190+
Header: defaultMap,
191+
Id: "id",
192+
Output: "output",
193+
StatusCode: 200,
194+
}, ToNormalTestCaseResult(&TestCaseResult{
195+
Body: "body",
196+
Error: "error",
197+
Header: defaultPairs,
198+
Id: "id",
199+
Output: "output",
200+
StatusCode: 200,
201+
}))
202+
}
203+
204+
func TestToGRPCHistoryTestCaseResult(t *testing.T) {
205+
t.Run("TestCaseResult is empty", func(t *testing.T) {
206+
historyTestResult := atest.HistoryTestResult{
207+
Message: "test message",
208+
Error: "test error",
209+
CreateTime: time.Now(),
210+
Data: atest.HistoryTestCase{
211+
ID: "test-id",
212+
},
213+
TestCaseResult: nil,
214+
}
215+
216+
result := ToGRPCHistoryTestCaseResult(historyTestResult)
217+
218+
assert.Equal(t, 0, len(result.TestCaseResult))
219+
assert.Equal(t, historyTestResult.Message, result.Message)
220+
assert.Equal(t, historyTestResult.Error, result.Error)
221+
})
222+
223+
t.Run("TestCaseResult is not empty", func(t *testing.T) {
224+
now := time.Now().UTC()
225+
226+
result := ToGRPCHistoryTestCaseResult(atest.HistoryTestResult{
227+
Message: "fake message",
228+
CreateTime: now,
229+
Data: atest.HistoryTestCase{
230+
ID: "fake id",
231+
},
232+
TestCaseResult: []atest.TestCaseResult{
233+
{
234+
StatusCode: 200,
235+
Output: "fake output",
236+
},
237+
{
238+
Output: "fake output 2",
239+
},
240+
},
241+
})
242+
243+
assert.Equal(t, 2, len(result.TestCaseResult))
244+
assert.Equal(t, "fake message", result.Message)
245+
assert.Equal(t, now, result.CreateTime.AsTime())
246+
assert.Equal(t, "fake output", result.TestCaseResult[0].Output)
247+
assert.Equal(t, "fake output 2", result.TestCaseResult[1].Output)
248+
})
249+
}
250+
251+
func TestToGRPCTestSuiteSpec(t *testing.T) {
252+
253+
t.Run("empty", func(t *testing.T) {
254+
assert.Equal(t, &APISpec{}, ToGRPCTestSuiteSpec(atest.APISpec{}))
255+
})
256+
257+
t.Run("fields", func(t *testing.T) {
258+
assert.Equal(t, &APISpec{
259+
Url: "/v1",
260+
Kind: "http",
261+
Rpc: &RPC{
262+
Raw: "fake",
263+
},
264+
Secure: &Secure{
265+
Key: "fake",
266+
},
267+
}, ToGRPCTestSuiteSpec(atest.APISpec{
268+
Kind: "http",
269+
URL: "/v1",
270+
RPC: &atest.RPCDesc{
271+
Raw: "fake",
272+
},
273+
Secure: &atest.Secure{
274+
KeyFile: "fake",
275+
},
276+
}))
277+
})
278+
}
279+
280+
var defaultInterMap = map[string]interface{}{"foo": "bar"}
281+
var defaultMap map[string]string = map[string]string{"foo": "bar"}
282+
var defaultPairs []*Pair = []*Pair{{Key: "foo", Value: "bar"}}

pkg/testing/remote/converter_test.go

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ limitations under the License.
1717
package remote
1818

1919
import (
20+
"google.golang.org/protobuf/types/known/timestamppb"
2021
"testing"
22+
"time"
2123

2224
server "github.com/linuxsuren/api-testing/pkg/server"
2325
atest "github.com/linuxsuren/api-testing/pkg/testing"
2426
"github.com/stretchr/testify/assert"
2527
)
2628

2729
func TestConvert(t *testing.T) {
30+
now := time.Now().UTC()
2831
t.Run("convertToNormalTestSuite, empty object", func(t *testing.T) {
2932
assert.Equal(t, &atest.TestSuite{
3033
Param: map[string]string{},
@@ -123,6 +126,211 @@ func TestConvert(t *testing.T) {
123126
assert.Equal(t, defaultPairs, result.Response.BodyFieldsExpect)
124127
assert.Equal(t, defaultPairs, result.Response.Header)
125128
})
129+
130+
t.Run("convertHistoryToGRPCTestCase", func(t *testing.T) {
131+
result := ConvertHistoryToGRPCTestCase(&server.HistoryTestCase{
132+
CaseName: "fake",
133+
Request: &server.Request{
134+
Header: defaultPairs,
135+
},
136+
Response: &server.Response{
137+
BodyFieldsExpect: defaultPairs,
138+
},
139+
})
140+
if !assert.NotNil(t, result) {
141+
return
142+
}
143+
assert.Equal(t, defaultMap, result.Request.Header)
144+
assert.Equal(t, defaultInterMap, result.Expect.BodyFieldsExpect)
145+
assert.Equal(t, "fake", result.Name)
146+
})
147+
148+
t.Run("convertToNormalHistoryTestCase", func(t *testing.T) {
149+
assert.Equal(t, atest.HistoryTestCase{
150+
CreateTime: now,
151+
SuiteParam: defaultMap,
152+
SuiteSpec: atest.APISpec{
153+
Kind: "http",
154+
URL: "/v1",
155+
RPC: &atest.RPCDesc{
156+
Raw: "fake",
157+
},
158+
Secure: &atest.Secure{
159+
KeyFile: "fake",
160+
},
161+
},
162+
Data: atest.TestCase{
163+
Request: atest.Request{
164+
API: "/v1",
165+
Header: defaultMap,
166+
Query: map[string]interface{}{},
167+
Form: map[string]string{},
168+
},
169+
Expect: atest.Response{
170+
BodyFieldsExpect: defaultInterMap,
171+
Header: map[string]string{},
172+
},
173+
},
174+
}, ConvertToNormalHistoryTestCase(&server.HistoryTestCase{
175+
CreateTime: timestamppb.New(now),
176+
SuiteParam: defaultPairs,
177+
SuiteSpec: &server.APISpec{
178+
Url: "/v1",
179+
Kind: "http",
180+
Rpc: &server.RPC{
181+
Raw: "fake",
182+
},
183+
Secure: &server.Secure{
184+
Key: "fake",
185+
},
186+
},
187+
Request: &server.Request{
188+
Header: defaultPairs,
189+
Query: nil,
190+
Api: "/v1",
191+
},
192+
Response: &server.Response{
193+
BodyFieldsExpect: defaultPairs,
194+
},
195+
}))
196+
})
197+
198+
t.Run("convertToNormalHistoryTestSuite, empty object", func(t *testing.T) {
199+
assert.Equal(t, &atest.HistoryTestSuite{}, ConvertToNormalHistoryTestSuite(&HistoryTestSuite{}))
200+
})
201+
202+
t.Run("convertToNormalHistoryTestSuite, normal object", func(t *testing.T) {
203+
assert.Equal(t, &atest.HistoryTestSuite{
204+
HistorySuiteName: "fake",
205+
Items: []atest.HistoryTestCase{
206+
{
207+
CreateTime: now,
208+
SuiteParam: defaultMap,
209+
SuiteSpec: atest.APISpec{
210+
Kind: "http",
211+
URL: "/v1",
212+
RPC: &atest.RPCDesc{
213+
Raw: "fake",
214+
},
215+
Secure: &atest.Secure{
216+
KeyFile: "fake",
217+
},
218+
},
219+
},
220+
},
221+
}, ConvertToNormalHistoryTestSuite(&HistoryTestSuite{
222+
HistorySuiteName: "fake",
223+
Items: []*server.HistoryTestCase{
224+
{
225+
CreateTime: timestamppb.New(now),
226+
SuiteParam: defaultPairs,
227+
SuiteSpec: &server.APISpec{
228+
Url: "/v1",
229+
Kind: "http",
230+
Rpc: &server.RPC{
231+
Raw: "fake",
232+
},
233+
Secure: &server.Secure{
234+
Key: "fake",
235+
},
236+
},
237+
},
238+
},
239+
}))
240+
})
241+
242+
t.Run("convertToGRPCHistoryTestCase", func(t *testing.T) {
243+
result := ConvertToGRPCHistoryTestCase(atest.HistoryTestCase{
244+
SuiteParam: defaultMap,
245+
SuiteSpec: atest.APISpec{
246+
Secure: &atest.Secure{
247+
KeyFile: "fake",
248+
},
249+
},
250+
Data: atest.TestCase{
251+
Request: atest.Request{
252+
Header: defaultMap,
253+
},
254+
Expect: atest.Response{
255+
BodyFieldsExpect: defaultInterMap,
256+
},
257+
},
258+
})
259+
assert.Equal(t, defaultPairs, result.SuiteParam)
260+
assert.Equal(t, defaultPairs, result.Request.Header)
261+
assert.Equal(t, defaultPairs, result.Response.BodyFieldsExpect)
262+
assert.Equal(t, "fake", result.SuiteSpec.Secure.Key)
263+
})
264+
265+
t.Run("convertToGRPCHistoryTestCaseResult", func(t *testing.T) {
266+
result := ConvertToGRPCHistoryTestCaseResult(atest.TestCaseResult{
267+
Body: "fake body",
268+
Output: "fake output",
269+
}, &atest.TestSuite{
270+
Param: defaultMap,
271+
Spec: atest.APISpec{
272+
Secure: &atest.Secure{
273+
KeyFile: "fake",
274+
},
275+
},
276+
Items: []atest.TestCase{
277+
{
278+
Request: atest.Request{
279+
Header: defaultMap,
280+
},
281+
Expect: atest.Response{
282+
BodyFieldsExpect: defaultInterMap,
283+
},
284+
},
285+
},
286+
})
287+
assert.Equal(t, defaultPairs, result.Data.SuiteParam)
288+
assert.Equal(t, defaultPairs, result.Data.Request.Header)
289+
assert.Equal(t, defaultPairs, result.Data.Response.BodyFieldsExpect)
290+
assert.Equal(t, "fake", result.Data.SuiteSpec.Secure.Key)
291+
assert.Equal(t, "fake output", result.TestCaseResult[0].Output)
292+
assert.Equal(t, "fake body", result.TestCaseResult[0].Body)
293+
})
294+
295+
t.Run("convertToNormalTestCaseResult", func(t *testing.T) {
296+
assert.Equal(t, atest.HistoryTestResult{
297+
CreateTime: now,
298+
Data: atest.HistoryTestCase{
299+
SuiteParam: defaultMap,
300+
CreateTime: now,
301+
},
302+
TestCaseResult: []atest.TestCaseResult{
303+
{
304+
Body: "fake body",
305+
Output: "fake output",
306+
Header: defaultMap,
307+
},
308+
{
309+
Body: "fake body 2",
310+
Output: "fake output 2",
311+
Header: defaultMap,
312+
},
313+
},
314+
}, ConvertToNormalTestCaseResult(&server.HistoryTestResult{
315+
CreateTime: timestamppb.New(now),
316+
Data: &server.HistoryTestCase{
317+
SuiteParam: defaultPairs,
318+
CreateTime: timestamppb.New(now),
319+
},
320+
TestCaseResult: []*server.TestCaseResult{
321+
{
322+
Body: "fake body",
323+
Output: "fake output",
324+
Header: defaultPairs,
325+
},
326+
{
327+
Body: "fake body 2",
328+
Output: "fake output 2",
329+
Header: defaultPairs,
330+
},
331+
},
332+
}))
333+
})
126334
}
127335

128336
var defaultInterMap = map[string]interface{}{"foo": "bar"}

0 commit comments

Comments
 (0)