-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: order the source code about runner (#93)
- Loading branch information
1 parent
2520e80
commit 5a3b114
Showing
8 changed files
with
160 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package apispec | ||
|
||
type fakeAPISpec struct { | ||
apis [][]string | ||
} | ||
|
||
// NewFakeAPISpec creates a new instance of fakeAPISpec | ||
func NewFakeAPISpec(apis [][]string) APIConverage { | ||
return &fakeAPISpec{apis: apis} | ||
} | ||
|
||
// HaveAPI is fake method | ||
func (f *fakeAPISpec) HaveAPI(path, method string) (exist bool) { | ||
for _, item := range f.apis { | ||
if len(item) >= 2 && item[0] == path && item[1] == method { | ||
exist = true | ||
break | ||
} | ||
} | ||
return | ||
} | ||
|
||
// APICount is fake method | ||
func (f *fakeAPISpec) APICount() (count int) { | ||
count = len(f.apis) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package apispec_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/linuxsuren/api-testing/pkg/apispec" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFakeAPISpec(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data [][]string | ||
path, method string | ||
expectExist bool | ||
expectCount int | ||
}{{ | ||
name: "normal", | ||
data: [][]string{{ | ||
"/api", "get", | ||
}}, | ||
path: "/api", | ||
method: "get", | ||
expectExist: true, | ||
expectCount: 1, | ||
}, { | ||
name: "empty", | ||
data: [][]string{}, | ||
path: "/api", | ||
method: "post", | ||
expectExist: false, | ||
expectCount: 0, | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
coverage := apispec.NewFakeAPISpec(tt.data) | ||
exist := coverage.HaveAPI(tt.path, tt.method) | ||
count := coverage.APICount() | ||
assert.Equal(t, tt.expectExist, exist) | ||
assert.Equal(t, tt.expectCount, count) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package runner | ||
|
||
import "time" | ||
|
||
// TestReporter is the interface of the report | ||
type TestReporter interface { | ||
PutRecord(*ReportRecord) | ||
GetAllRecords() []*ReportRecord | ||
ExportAllReportResults() (ReportResultSlice, error) | ||
} | ||
|
||
// ReportRecord represents the raw data of a HTTP request | ||
type ReportRecord struct { | ||
Method string | ||
API string | ||
Body string | ||
BeginTime time.Time | ||
EndTime time.Time | ||
Error error | ||
} | ||
|
||
// Duration returns the duration between begin and end time | ||
func (r *ReportRecord) Duration() time.Duration { | ||
return r.EndTime.Sub(r.BeginTime) | ||
} | ||
|
||
// ErrorCount returns the count number of errors | ||
func (r *ReportRecord) ErrorCount() int { | ||
if r.Error == nil { | ||
return 0 | ||
} | ||
return 1 | ||
} | ||
|
||
// GetErrorMessage returns the error message | ||
func (r *ReportRecord) GetErrorMessage() string { | ||
if r.ErrorCount() > 0 { | ||
return r.Body | ||
} else { | ||
return "" | ||
} | ||
} | ||
|
||
// NewReportRecord creates a record, and set the begin time to be now | ||
func NewReportRecord() *ReportRecord { | ||
return &ReportRecord{ | ||
BeginTime: time.Now(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/linuxsuren/api-testing/pkg/testing" | ||
fakeruntime "github.com/linuxsuren/go-fake-runtime" | ||
) | ||
|
||
// TestCaseRunner represents a test case runner | ||
type TestCaseRunner interface { | ||
RunTestCase(testcase *testing.TestCase, dataContext interface{}, ctx context.Context) (output interface{}, err error) | ||
WithOutputWriter(io.Writer) TestCaseRunner | ||
WithWriteLevel(level string) TestCaseRunner | ||
WithTestReporter(TestReporter) TestCaseRunner | ||
WithExecer(fakeruntime.Execer) TestCaseRunner | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package runner | ||
|
||
import "github.com/linuxsuren/api-testing/pkg/apispec" | ||
|
||
// ReportResultWriter is the interface of the report writer | ||
type ReportResultWriter interface { | ||
Output([]ReportResult) error | ||
WithAPIConverage(apiConverage apispec.APIConverage) ReportResultWriter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters