Skip to content

Commit 5a3b114

Browse files
authored
chore: order the source code about runner (#93)
1 parent 2520e80 commit 5a3b114

File tree

8 files changed

+160
-81
lines changed

8 files changed

+160
-81
lines changed

pkg/apispec/fake.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package apispec
2+
3+
type fakeAPISpec struct {
4+
apis [][]string
5+
}
6+
7+
// NewFakeAPISpec creates a new instance of fakeAPISpec
8+
func NewFakeAPISpec(apis [][]string) APIConverage {
9+
return &fakeAPISpec{apis: apis}
10+
}
11+
12+
// HaveAPI is fake method
13+
func (f *fakeAPISpec) HaveAPI(path, method string) (exist bool) {
14+
for _, item := range f.apis {
15+
if len(item) >= 2 && item[0] == path && item[1] == method {
16+
exist = true
17+
break
18+
}
19+
}
20+
return
21+
}
22+
23+
// APICount is fake method
24+
func (f *fakeAPISpec) APICount() (count int) {
25+
count = len(f.apis)
26+
return
27+
}

pkg/apispec/fake_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package apispec_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/linuxsuren/api-testing/pkg/apispec"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestFakeAPISpec(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
data [][]string
14+
path, method string
15+
expectExist bool
16+
expectCount int
17+
}{{
18+
name: "normal",
19+
data: [][]string{{
20+
"/api", "get",
21+
}},
22+
path: "/api",
23+
method: "get",
24+
expectExist: true,
25+
expectCount: 1,
26+
}, {
27+
name: "empty",
28+
data: [][]string{},
29+
path: "/api",
30+
method: "post",
31+
expectExist: false,
32+
expectCount: 0,
33+
}}
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
coverage := apispec.NewFakeAPISpec(tt.data)
37+
exist := coverage.HaveAPI(tt.path, tt.method)
38+
count := coverage.APICount()
39+
assert.Equal(t, tt.expectExist, exist)
40+
assert.Equal(t, tt.expectCount, count)
41+
})
42+
}
43+
}

pkg/runner/simple.go renamed to pkg/runner/http.go

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/andreyvit/diff"
1515
"github.com/antonmedv/expr"
1616
"github.com/antonmedv/expr/vm"
17-
"github.com/linuxsuren/api-testing/pkg/apispec"
1817
"github.com/linuxsuren/api-testing/pkg/runner/kubernetes"
1918
"github.com/linuxsuren/api-testing/pkg/testing"
2019
fakeruntime "github.com/linuxsuren/go-fake-runtime"
@@ -71,54 +70,6 @@ func (w *defaultLevelWriter) Debug(format string, a ...any) {
7170
w.Fprintf(w.Writer, 7, format, a...)
7271
}
7372

74-
// TestCaseRunner represents a test case runner
75-
type TestCaseRunner interface {
76-
RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx context.Context) (output interface{}, err error)
77-
WithOutputWriter(io.Writer) TestCaseRunner
78-
WithWriteLevel(level string) TestCaseRunner
79-
WithTestReporter(TestReporter) TestCaseRunner
80-
WithExecer(fakeruntime.Execer) TestCaseRunner
81-
}
82-
83-
// ReportRecord represents the raw data of a HTTP request
84-
type ReportRecord struct {
85-
Method string
86-
API string
87-
Body string
88-
BeginTime time.Time
89-
EndTime time.Time
90-
Error error
91-
}
92-
93-
// Duration returns the duration between begin and end time
94-
func (r *ReportRecord) Duration() time.Duration {
95-
return r.EndTime.Sub(r.BeginTime)
96-
}
97-
98-
// ErrorCount returns the count number of errors
99-
func (r *ReportRecord) ErrorCount() int {
100-
if r.Error == nil {
101-
return 0
102-
}
103-
return 1
104-
}
105-
106-
// GetErrorMessage returns the error message
107-
func (r *ReportRecord) GetErrorMessage() string {
108-
if r.ErrorCount() > 0 {
109-
return r.Body
110-
} else {
111-
return ""
112-
}
113-
}
114-
115-
// NewReportRecord creates a record, and set the begin time to be now
116-
func NewReportRecord() *ReportRecord {
117-
return &ReportRecord{
118-
BeginTime: time.Now(),
119-
}
120-
}
121-
12273
// ReportResult represents the report result of a set of the same API requests
12374
type ReportResult struct {
12475
API string
@@ -151,19 +102,6 @@ func (r ReportResultSlice) Swap(i, j int) {
151102
r[j] = tmp
152103
}
153104

154-
// ReportResultWriter is the interface of the report writer
155-
type ReportResultWriter interface {
156-
Output([]ReportResult) error
157-
WithAPIConverage(apiConverage apispec.APIConverage) ReportResultWriter
158-
}
159-
160-
// TestReporter is the interface of the report
161-
type TestReporter interface {
162-
PutRecord(*ReportRecord)
163-
GetAllRecords() []*ReportRecord
164-
ExportAllReportResults() (ReportResultSlice, error)
165-
}
166-
167105
type simpleTestCaseRunner struct {
168106
testReporter TestReporter
169107
writer io.Writer
@@ -318,18 +256,6 @@ func (r *simpleTestCaseRunner) WithExecer(execer fakeruntime.Execer) TestCaseRun
318256
return r
319257
}
320258

321-
func (r *simpleTestCaseRunner) doCleanPrepare(testcase *testing.TestCase) (err error) {
322-
count := len(testcase.Before.Items)
323-
for i := count - 1; i >= 0; i-- {
324-
item := testcase.Before.Items[i]
325-
326-
if err = r.execer.RunCommand("kubectl", "delete", "-f", item); err != nil {
327-
return
328-
}
329-
}
330-
return
331-
}
332-
333259
func expectInt(name string, expect, actual int) (err error) {
334260
if expect != actual {
335261
err = fmt.Errorf("case: %s, expect %d, actual %d", name, expect, actual)
File renamed without changes.

pkg/runner/reporter.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package runner
2+
3+
import "time"
4+
5+
// TestReporter is the interface of the report
6+
type TestReporter interface {
7+
PutRecord(*ReportRecord)
8+
GetAllRecords() []*ReportRecord
9+
ExportAllReportResults() (ReportResultSlice, error)
10+
}
11+
12+
// ReportRecord represents the raw data of a HTTP request
13+
type ReportRecord struct {
14+
Method string
15+
API string
16+
Body string
17+
BeginTime time.Time
18+
EndTime time.Time
19+
Error error
20+
}
21+
22+
// Duration returns the duration between begin and end time
23+
func (r *ReportRecord) Duration() time.Duration {
24+
return r.EndTime.Sub(r.BeginTime)
25+
}
26+
27+
// ErrorCount returns the count number of errors
28+
func (r *ReportRecord) ErrorCount() int {
29+
if r.Error == nil {
30+
return 0
31+
}
32+
return 1
33+
}
34+
35+
// GetErrorMessage returns the error message
36+
func (r *ReportRecord) GetErrorMessage() string {
37+
if r.ErrorCount() > 0 {
38+
return r.Body
39+
} else {
40+
return ""
41+
}
42+
}
43+
44+
// NewReportRecord creates a record, and set the begin time to be now
45+
func NewReportRecord() *ReportRecord {
46+
return &ReportRecord{
47+
BeginTime: time.Now(),
48+
}
49+
}

pkg/runner/runner.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package runner
2+
3+
import (
4+
"context"
5+
"io"
6+
7+
"github.com/linuxsuren/api-testing/pkg/testing"
8+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
9+
)
10+
11+
// TestCaseRunner represents a test case runner
12+
type TestCaseRunner interface {
13+
RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx context.Context) (output interface{}, err error)
14+
WithOutputWriter(io.Writer) TestCaseRunner
15+
WithWriteLevel(level string) TestCaseRunner
16+
WithTestReporter(TestReporter) TestCaseRunner
17+
WithExecer(fakeruntime.Execer) TestCaseRunner
18+
}

pkg/runner/writer.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package runner
2+
3+
import "github.com/linuxsuren/api-testing/pkg/apispec"
4+
5+
// ReportResultWriter is the interface of the report writer
6+
type ReportResultWriter interface {
7+
Output([]ReportResult) error
8+
WithAPIConverage(apiConverage apispec.APIConverage) ReportResultWriter
9+
}

pkg/runner/writer_std_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ import (
44
"bytes"
55
"testing"
66

7+
"github.com/linuxsuren/api-testing/pkg/apispec"
78
"github.com/linuxsuren/api-testing/pkg/runner"
89
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestNewStdResultWriter(t *testing.T) {
1213
tests := []struct {
13-
name string
14-
buf *bytes.Buffer
15-
results []runner.ReportResult
16-
expect string
14+
name string
15+
buf *bytes.Buffer
16+
apiConverage apispec.APIConverage
17+
results []runner.ReportResult
18+
expect string
1719
}{{
1820
name: "result is nil",
1921
buf: new(bytes.Buffer),
@@ -23,8 +25,11 @@ func TestNewStdResultWriter(t *testing.T) {
2325
}, {
2426
name: "have one item",
2527
buf: new(bytes.Buffer),
28+
apiConverage: apispec.NewFakeAPISpec([][]string{{
29+
"/api", "GET",
30+
}}),
2631
results: []runner.ReportResult{{
27-
API: "api",
32+
API: "/api",
2833
Average: 1,
2934
Max: 1,
3035
Min: 1,
@@ -33,7 +38,9 @@ func TestNewStdResultWriter(t *testing.T) {
3338
Error: 0,
3439
}},
3540
expect: `API Average Max Min QPS Count Error
36-
api 1ns 1ns 1ns 10 1 0
41+
/api 1ns 1ns 1ns 10 1 0
42+
43+
API Coverage: 1/1
3744
`,
3845
}, {
3946
name: "have errors",
@@ -76,7 +83,7 @@ api 1ns 1ns 1ns 10 1 0
7683
return
7784
}
7885

79-
writer.WithAPIConverage(nil)
86+
writer.WithAPIConverage(tt.apiConverage)
8087
err := writer.Output(tt.results)
8188
assert.Nil(t, err)
8289
assert.Equal(t, tt.expect, tt.buf.String())

0 commit comments

Comments
 (0)