From f7fc28bc53963a60df8838f39355c6a11ed4e515 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 8 Dec 2021 11:12:02 +0800 Subject: [PATCH] feat: log transaction when running tests --- convert.go | 4 ++++ internal/version/init.go | 2 +- models.go | 6 ++++++ runner.go | 10 ++++++++++ step.go | 20 ++++++++++++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) diff --git a/convert.go b/convert.go index 761a22fd..94e2bc40 100644 --- a/convert.go +++ b/convert.go @@ -116,6 +116,10 @@ func (tc *TCase) ToTestCase() (*TestCase, error) { testCase.TestSteps = append(testCase.TestSteps, &stepTestCaseWithOptionalArgs{ step: step, }) + } else if step.Transaction != nil { + testCase.TestSteps = append(testCase.TestSteps, &stepTransaction{ + step: step, + }) } else { log.Warn().Interface("step", step).Msg("[convertTestCase] unexpected step") } diff --git a/internal/version/init.go b/internal/version/init.go index b62e2970..a29e664d 100644 --- a/internal/version/init.go +++ b/internal/version/init.go @@ -1,3 +1,3 @@ package version -const VERSION = "v0.2.2" +const VERSION = "v0.3.0" diff --git a/models.go b/models.go index 20f727e7..85b284e9 100644 --- a/models.go +++ b/models.go @@ -50,6 +50,7 @@ type TStep struct { Name string `json:"name" yaml:"name"` // required Request *Request `json:"request,omitempty" yaml:"request,omitempty"` TestCase *TestCase `json:"testcase,omitempty" yaml:"testcase,omitempty"` + Transaction *Transaction `json:"transaction,omitempty" yaml:"transaction,omitempty"` Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"` SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"` TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"` @@ -58,6 +59,11 @@ type TStep struct { Export []string `json:"export,omitempty" yaml:"export,omitempty"` } +type Transaction struct { + Name string `json:"name" yaml:"name"` + Type string `json:"type" yaml:"type"` // start/end +} + // TCase represents testcase data structure. // Each testcase includes one public config and several sequential teststeps. type TCase struct { diff --git a/runner.go b/runner.go index 84acf75f..9f524bfb 100644 --- a/runner.go +++ b/runner.go @@ -118,6 +118,16 @@ func (r *hrpRunner) runCase(testcase *TestCase) error { } func (r *hrpRunner) runStep(step IStep, config *TConfig) (stepResult *stepData, err error) { + // step type priority order: transaction > testcase > request + if stepTransaction, ok := step.(*stepTransaction); ok { + // transaction + log.Info(). + Str("name", stepTransaction.step.Transaction.Name). + Str("type", stepTransaction.step.Transaction.Type). + Msg("transaction") + return nil, nil + } + log.Info().Str("step", step.Name()).Msg("run step start") // copy step to avoid data racing diff --git a/step.go b/step.go index f83845ea..1f7a22c1 100644 --- a/step.go +++ b/step.go @@ -277,3 +277,23 @@ func (s *stepTestCaseWithOptionalArgs) Type() string { func (s *stepTestCaseWithOptionalArgs) ToStruct() *TStep { return s.step } + +// implements IStep interface +type stepTransaction struct { + step *TStep +} + +func (s *stepTransaction) Name() string { + if s.step.Name != "" { + return s.step.Name + } + return fmt.Sprintf("transaction %s %s", s.step.Transaction.Name, s.step.Transaction.Type) +} + +func (s *stepTransaction) Type() string { + return "transaction" +} + +func (s *stepTransaction) ToStruct() *TStep { + return s.step +}