refactor: change TStep API/TestCase type

This commit is contained in:
debugtalk
2022-03-28 16:47:15 +08:00
parent 98bdb765a1
commit e31f23cbe0
4 changed files with 60 additions and 38 deletions
+29 -13
View File
@@ -103,35 +103,51 @@ func (tc *TCase) ToTestCase() (*TestCase, error) {
log.Info().Str("dir", projectRootDir).Msg("located project root dir") log.Info().Str("dir", projectRootDir).Msg("located project root dir")
for _, step := range tc.TestSteps { for _, step := range tc.TestSteps {
if step.APIPath != "" { if step.API != nil {
path := filepath.Join(projectRootDir, step.APIPath) if apiContent, ok := step.API.(*API); ok {
if !builtin.IsFilePathExists(path) { step.API = apiContent
return nil, errors.New("referenced api file not found: " + path) testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{
step: step,
})
return testCase, nil
} }
refAPI := APIPath(path) // reference api path
step.APIContent = &refAPI var apiFullPath string
apiContent, err := step.APIContent.ToAPI() if apiPath, ok := step.API.(string); ok {
apiFullPath = filepath.Join(projectRootDir, apiPath)
} else if apiPath, ok := step.API.(APIPath); ok {
apiFullPath = filepath.Join(projectRootDir, apiPath.GetPath())
} else {
return nil, errors.New("invalid api format")
}
if !builtin.IsFilePathExists(apiFullPath) {
return nil, errors.New("referenced api file not found: " + apiFullPath)
}
refAPI := APIPath(apiFullPath)
apiContent, err := refAPI.ToAPI()
if err != nil { if err != nil {
return nil, err return nil, err
} }
step.APIContent = apiContent step.API = apiContent
testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{ testCase.TestSteps = append(testCase.TestSteps, &StepAPIWithOptionalArgs{
step: step, step: step,
}) })
} else if step.TestCasePath != "" { } else if step.TestCase != nil {
path := filepath.Join(projectRootDir, step.TestCasePath) path := filepath.Join(projectRootDir, step.TestCase.(string))
if !builtin.IsFilePathExists(path) { if !builtin.IsFilePathExists(path) {
return nil, errors.New("referenced testcase file not found: " + path) return nil, errors.New("referenced testcase file not found: " + path)
} }
refTestCase := TestCasePath(path) refTestCase := TestCasePath(path)
step.TestCaseContent = &refTestCase tc, err := refTestCase.ToTestCase()
tc, err := step.TestCaseContent.ToTestCase()
if err != nil { if err != nil {
return nil, err return nil, err
} }
step.TestCaseContent = tc step.TestCase = tc
testCase.TestSteps = append(testCase.TestSteps, &StepTestCaseWithOptionalArgs{ testCase.TestSteps = append(testCase.TestSteps, &StepTestCaseWithOptionalArgs{
step: step, step: step,
}) })
+13 -15
View File
@@ -222,21 +222,19 @@ type IAPI interface {
// TStep represents teststep data structure. // TStep represents teststep data structure.
// Each step maybe two different type: make one HTTP request or reference another testcase. // Each step maybe two different type: make one HTTP request or reference another testcase.
type TStep struct { type TStep struct {
Name string `json:"name" yaml:"name"` // required Name string `json:"name" yaml:"name"` // required
Request *Request `json:"request,omitempty" yaml:"request,omitempty"` Request *Request `json:"request,omitempty" yaml:"request,omitempty"`
APIPath string `json:"api,omitempty" yaml:"api,omitempty"` API interface{} `json:"api,omitempty" yaml:"api,omitempty"` // *APIPath or *API
APIContent IAPI `json:"api_content,omitempty" yaml:"api_content,omitempty"` TestCase interface{} `json:"testcase,omitempty" yaml:"testcase,omitempty"` // *TestCasePath or *TestCase
TestCasePath string `json:"testcase,omitempty" yaml:"testcase,omitempty"` Transaction *Transaction `json:"transaction,omitempty" yaml:"transaction,omitempty"`
TestCaseContent ITestCase `json:"testcase_content,omitempty" yaml:"testcase_content,omitempty"` Rendezvous *Rendezvous `json:"rendezvous,omitempty" yaml:"rendezvous,omitempty"`
Transaction *Transaction `json:"transaction,omitempty" yaml:"transaction,omitempty"` ThinkTime *ThinkTime `json:"think_time,omitempty" yaml:"think_time,omitempty"`
Rendezvous *Rendezvous `json:"rendezvous,omitempty" yaml:"rendezvous,omitempty"` Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
ThinkTime *ThinkTime `json:"think_time,omitempty" yaml:"think_time,omitempty"` SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"` TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"`
SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"` Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"`
TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"` Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"`
Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"` Export []string `json:"export,omitempty" yaml:"export,omitempty"`
Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"`
Export []string `json:"export,omitempty" yaml:"export,omitempty"`
} }
type stepType string type stepType string
+2 -2
View File
@@ -345,7 +345,7 @@ func (r *caseRunner) runStep(index int, caseConfig *TConfig) (stepResult *stepDa
if _, ok := step.(*StepAPIWithOptionalArgs); ok { if _, ok := step.(*StepAPIWithOptionalArgs); ok {
// run referenced API // run referenced API
log.Info().Str("api", copiedStep.Name).Msg("run referenced api") log.Info().Str("api", copiedStep.Name).Msg("run referenced api")
api, _ := copiedStep.APIContent.ToAPI() api, _ := copiedStep.API.(*API)
extendWithAPI(copiedStep, api) extendWithAPI(copiedStep, api)
} }
// override headers // override headers
@@ -967,7 +967,7 @@ func (r *caseRunner) runStepTestCase(step *TStep) (stepResult *stepData, err err
StepType: stepTypeTestCase, StepType: stepTypeTestCase,
Success: false, Success: false,
} }
testcase := step.TestCaseContent testcase := step.TestCase.(*TestCase)
// copy testcase to avoid data racing // copy testcase to avoid data racing
copiedTestCase := &TestCase{} copiedTestCase := &TestCase{}
+16 -8
View File
@@ -169,7 +169,7 @@ func (s *StepRequest) PATCH(url string) *StepRequestWithOptionalArgs {
// CallRefCase calls a referenced testcase. // CallRefCase calls a referenced testcase.
func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs { func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs {
var err error var err error
s.step.TestCaseContent, err = tc.ToTestCase() s.step.TestCase, err = tc.ToTestCase()
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to load testcase") log.Error().Err(err).Msg("failed to load testcase")
os.Exit(1) os.Exit(1)
@@ -182,7 +182,7 @@ func (s *StepRequest) CallRefCase(tc ITestCase) *StepTestCaseWithOptionalArgs {
// CallRefAPI calls a referenced api. // CallRefAPI calls a referenced api.
func (s *StepRequest) CallRefAPI(api IAPI) *StepAPIWithOptionalArgs { func (s *StepRequest) CallRefAPI(api IAPI) *StepAPIWithOptionalArgs {
var err error var err error
s.step.APIContent, err = api.ToAPI() s.step.API, err = api.ToAPI()
if err != nil { if err != nil {
log.Error().Err(err).Msg("failed to load api") log.Error().Err(err).Msg("failed to load api")
os.Exit(1) os.Exit(1)
@@ -332,8 +332,10 @@ func (s *StepAPIWithOptionalArgs) TeardownHook(hook string) *StepAPIWithOptional
// Export specifies variable names to export from referenced api for current step. // Export specifies variable names to export from referenced api for current step.
func (s *StepAPIWithOptionalArgs) Export(names ...string) *StepAPIWithOptionalArgs { func (s *StepAPIWithOptionalArgs) Export(names ...string) *StepAPIWithOptionalArgs {
api, _ := s.step.APIContent.ToAPI() api, ok := s.step.API.(*API)
s.step.Export = append(api.Export, names...) if ok {
s.step.Export = append(api.Export, names...)
}
return s return s
} }
@@ -341,8 +343,11 @@ func (s *StepAPIWithOptionalArgs) Name() string {
if s.step.Name != "" { if s.step.Name != "" {
return s.step.Name return s.step.Name
} }
api, _ := s.step.APIContent.ToAPI() api, ok := s.step.API.(*API)
return api.Name if ok {
return api.Name
}
return ""
} }
func (s *StepAPIWithOptionalArgs) Type() string { func (s *StepAPIWithOptionalArgs) Type() string {
@@ -374,8 +379,11 @@ func (s *StepTestCaseWithOptionalArgs) Name() string {
if s.step.Name != "" { if s.step.Name != "" {
return s.step.Name return s.step.Name
} }
ts, _ := s.step.TestCaseContent.ToTestCase() ts, ok := s.step.TestCase.(*TestCase)
return ts.Config.Name if ok {
return ts.Config.Name
}
return ""
} }
func (s *StepTestCaseWithOptionalArgs) Type() string { func (s *StepTestCaseWithOptionalArgs) Type() string {