feat: support finding the parent test cases (#32)
This commit is contained in:
parent
a55b86b51e
commit
19a07a211b
|
@ -6,6 +6,7 @@ import (
|
|||
context "context"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/linuxsuren/api-testing/pkg/render"
|
||||
|
@ -76,7 +77,8 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
|
|||
}
|
||||
|
||||
if targetTestcase != nil {
|
||||
suite.Items = []testing.TestCase{*targetTestcase}
|
||||
parentCases := findParentTestCases(targetTestcase, suite)
|
||||
suite.Items = append(parentCases, *targetTestcase)
|
||||
} else {
|
||||
err = fmt.Errorf("cannot found testcase %s", task.CaseName)
|
||||
return
|
||||
|
@ -124,3 +126,27 @@ func (s *server) GetVersion(ctx context.Context, in *Empty) (reply *HelloReply,
|
|||
reply = &HelloReply{Message: version.GetVersion()}
|
||||
return
|
||||
}
|
||||
|
||||
func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (testcases []testing.TestCase) {
|
||||
reg, matchErr := regexp.Compile(`.*\{\{\.\w*\..*}\}.*`)
|
||||
targetReg, targetErr := regexp.Compile(`\{\{\.\w*\.`)
|
||||
|
||||
if matchErr == nil && targetErr == nil {
|
||||
expectName := ""
|
||||
for _, val := range testcase.Request.Header {
|
||||
if matched := reg.MatchString(val); matched {
|
||||
expectName = targetReg.FindString(val)
|
||||
expectName = strings.TrimPrefix(expectName, "{{.")
|
||||
expectName = strings.TrimSuffix(expectName, ".")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
for _, item := range suite.Items {
|
||||
if item.Name == expectName {
|
||||
testcases = append(testcases, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
_ "embed"
|
||||
|
||||
"github.com/h2non/gock"
|
||||
atesting "github.com/linuxsuren/api-testing/pkg/testing"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
@ -58,6 +59,42 @@ func TestRemoteServer(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestFindParentTestCases(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
testcase *atesting.TestCase
|
||||
suite *atesting.TestSuite
|
||||
expect []atesting.TestCase
|
||||
}{{
|
||||
name: "normal",
|
||||
testcase: &atesting.TestCase{
|
||||
Request: atesting.Request{
|
||||
Header: map[string]string{
|
||||
"Authorization": "Bearer {{.login.data.access_token}}",
|
||||
},
|
||||
},
|
||||
},
|
||||
suite: &atesting.TestSuite{
|
||||
Items: []atesting.TestCase{{
|
||||
Name: "login",
|
||||
}},
|
||||
},
|
||||
expect: []atesting.TestCase{{
|
||||
Name: "login",
|
||||
}},
|
||||
}, {
|
||||
name: "empty cases",
|
||||
testcase: &atesting.TestCase{},
|
||||
suite: &atesting.TestSuite{},
|
||||
}}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := findParentTestCases(tt.testcase, tt.suite)
|
||||
assert.Equal(t, tt.expect, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
//go:embed testdata/simple.yaml
|
||||
var simpleSuite string
|
||||
|
||||
|
|
Loading…
Reference in New Issue