Skip to content

Commit

Permalink
Merge pull request #25 from ozontech/release/remove-step-by-all-asserts
Browse files Browse the repository at this point in the history
Refactors asserts executions
  • Loading branch information
siller174 authored Sep 15, 2022
2 parents 815b362 + 2d47338 commit 303e905
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 174 deletions.
203 changes: 54 additions & 149 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package cute

import (
"net/http"
"reflect"
"runtime"
"strings"

"github.com/ozontech/allure-go/pkg/allure"
"github.com/ozontech/allure-go/pkg/framework/provider"
"github.com/ozontech/cute/errors"
)

Expand Down Expand Up @@ -35,183 +30,98 @@ type AssertResponseT func(t T, response *http.Response) error

func (it *Test) assertHeaders(t internalT, headers http.Header) []error {
var (
assertHeaders = it.Expect.AssertHeaders
assertHeadersT = it.Expect.AssertHeadersT

errs = make([]error, 0)
asserts = it.Expect.AssertHeaders
assertT = it.Expect.AssertHeadersT
)

if len(assertHeaders) == 0 && len(assertHeadersT) == 0 {
if len(asserts) == 0 && len(assertT) == 0 {
return nil
}

t.WithNewStep("Assert headers", func(stepCtx provider.StepCtx) {
isOption := false
isOptionT := false

// Execute assert only body
for _, f := range assertHeaders {
executeWithStep(stepCtx, getFunctionName(f), func(t T) []error {
err := f(headers)
if err != nil {
errs = append(errs, err)

isOption = isOptionError(err)

return []error{err}
}

return nil
}, true)
}

// Execute assert for body with TB
for _, f := range assertHeadersT {
executeWithStep(stepCtx, getFunctionName(f), func(t T) []error {
err := f(t, headers)
if err != nil {
errs = append(errs, err)

isOptionT = isOptionError(err)

return []error{err}
}

return nil
}, true)
return executeWithStep(t, "Assert headers", func(t T) []error {
errs := make([]error, 0)
// Execute assert only response
for _, f := range asserts {
err := f(headers)
if err != nil {
errs = append(errs, err)
}
}

if len(errs) > 0 {
if isOption && isOptionT {
stepCtx.CurrentStep().Status = allure.Skipped
} else {
stepCtx.CurrentStep().Status = allure.Failed
// Execute assert for response with TB
for _, f := range assertT {
err := f(t, headers)
if err != nil {
errs = append(errs, err)
}
}

return errs
})

return errs
}

func (it *Test) assertResponse(t internalT, response *http.Response) []error {
func (it *Test) assertResponse(t internalT, resp *http.Response) []error {
var (
assertResponse = it.Expect.AssertResponse
assertResponseT = it.Expect.AssertResponseT

errs = make([]error, 0)
asserts = it.Expect.AssertResponse
assertT = it.Expect.AssertResponseT
)

if len(assertResponse) == 0 && len(assertResponseT) == 0 {
if len(asserts) == 0 && len(assertT) == 0 {
return nil
}

t.WithNewStep("Assert response", func(stepCtx provider.StepCtx) {
isOption := false
isOptionT := false

// Execute assert only body
for _, f := range assertResponse {
executeWithStep(stepCtx, getFunctionName(f), func(t T) []error {
err := f(response)
if err != nil {
errs = append(errs, err)

isOption = isOptionError(err)

return []error{err}
}

return nil
}, true)
}

// Execute assert for body with TB
for _, f := range assertResponseT {
executeWithStep(stepCtx, getFunctionName(f), func(t T) []error {
err := f(t, response)
if err != nil {
errs = append(errs, err)

isOptionT = isOptionError(err)

return []error{err}
}

return nil
}, true)
return executeWithStep(t, "Assert response", func(t T) []error {
errs := make([]error, 0)
// Execute assert only response
for _, f := range asserts {
err := f(resp)
if err != nil {
errs = append(errs, err)
}
}

if len(errs) > 0 {
if isOption && isOptionT {
stepCtx.CurrentStep().Status = allure.Skipped
} else {
stepCtx.CurrentStep().Status = allure.Failed
// Execute assert for response with TB
for _, f := range assertT {
err := f(t, resp)
if err != nil {
errs = append(errs, err)
}
}
})

return errs
return errs
})
}

func (it *Test) assertBody(t internalT, body []byte) []error {
var (
assertBody = it.Expect.AssertBody
assertBodyT = it.Expect.AssertBodyT

errs = make([]error, 0)
asserts = it.Expect.AssertBody
assertT = it.Expect.AssertBodyT
)

if len(assertBody) == 0 && len(assertBodyT) == 0 {
if len(asserts) == 0 && len(assertT) == 0 {
return nil
}

t.WithNewStep("Assert body", func(stepCtx provider.StepCtx) {
isOption := false
isOptionT := false

// Execute assert only body
for _, f := range assertBody {
executeWithStep(stepCtx, getFunctionName(f), func(t T) []error {
err := f(body)
if err != nil {
errs = append(errs, err)

isOption = isOptionError(err)

return []error{err}
}

return nil
}, true)
}

// Execute assert for body with TB
for _, f := range assertBodyT {
executeWithStep(stepCtx, getFunctionName(f), func(t T) []error {
err := f(t, body)
if err != nil {
errs = append(errs, err)

isOptionT = isOptionError(err)

return []error{err}
}

return nil
}, true)
return executeWithStep(t, "Assert body", func(t T) []error {
errs := make([]error, 0)
// Execute assert only response
for _, f := range asserts {
err := f(body)
if err != nil {
errs = append(errs, err)
}
}

if len(errs) > 0 {
if isOption && isOptionT {
stepCtx.CurrentStep().Status = allure.Skipped
} else {
stepCtx.CurrentStep().Status = allure.Failed
// Execute assert for response with TB
for _, f := range assertT {
err := f(t, body)
if err != nil {
errs = append(errs, err)
}
}
})

return errs
return errs
})
}

func isOptionError(err error) bool {
Expand All @@ -222,11 +132,6 @@ func isOptionError(err error) bool {
return false
}

func getFunctionName(temp interface{}) string {
strs := strings.Split(runtime.FuncForPC(reflect.ValueOf(temp).Pointer()).Name(), ".")
return strs[len(strs)-2]
}

func optionalAssertHeaders(assert AssertHeaders) AssertHeaders {
return func(headers http.Header) error {
err := assert(headers)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ozontech/cute

go 1.17
go 1.18

require (
github.com/PaesslerAG/jsonpath v0.1.1
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYl
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
11 changes: 3 additions & 8 deletions jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
// Automatically add information about validation to allure.
func (it *Test) validateJSONSchema(t internalT, body []byte) []error {
var (
scope = make([]error, 0)
expect gojsonschema.JSONLoader
)

Expand All @@ -26,13 +25,9 @@ func (it *Test) validateJSONSchema(t internalT, body []byte) []error {
return nil
}

executeWithStep(t, "Validate body by JSON schema", func(t T) []error {
scope = checkJSONSchema(expect, body)

return scope
}, false)

return scope
return executeWithStep(t, "Validate body by JSON schema", func(t T) []error {
return checkJSONSchema(expect, body)
})
}

func checkJSONSchema(expect gojsonschema.JSONLoader, data []byte) []error {
Expand Down
6 changes: 4 additions & 2 deletions roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (it *Test) makeRequest(t internalT, req *http.Request) (*http.Response, []e
}

return nil
}, false)
})

if err == nil {
break
Expand Down Expand Up @@ -105,7 +105,9 @@ func addInformationRequest(t T, req *http.Request) error {
return err
}

t.Log("[Request]" + curl.String())
if c := curl.String(); len(c) <= 2048 {
t.Log("[Request]" + c)
}

headers, err := utils.ToJSON(req.Header)
if err != nil {
Expand Down
17 changes: 6 additions & 11 deletions step.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
"github.com/ozontech/cute/errors"
)

func executeWithStep(t internalT, stepName string, execute func(t T) []error, skipCreateNewStep bool) []error {
func executeWithStep(t internalT, stepName string, execute func(t T) []error) []error {
var (
errs []error
)

t.WithNewStep(stepName, func(stepCtx provider.StepCtx) {
errs = execute(stepCtx)
processStepErrors(stepCtx, errs, skipCreateNewStep)
processStepErrors(stepCtx, errs)
})

return errs
}

func processStepErrors(stepCtx provider.StepCtx, errs []error, skipCreateNewStep bool) {
func processStepErrors(stepCtx provider.StepCtx, errs []error) {
var (
step = stepCtx.CurrentStep()
statuses = make([]allure.Status, 0)
Expand All @@ -40,14 +40,9 @@ func processStepErrors(stepCtx provider.StepCtx, errs []error, skipCreateNewStep
}

if tErr, ok := err.(errors.WithNameError); ok {
if skipCreateNewStep {
currentStep.Name = tErr.GetName()

} else {
currentStep = allure.NewSimpleStep(tErr.GetName())
currentStep.Status = currentStatus
currentStep.WithParent(step)
}
currentStep = allure.NewSimpleStep(tErr.GetName())
currentStep.Status = currentStatus
currentStep.WithParent(step)
}

if tErr, ok := err.(errors.WithFields); ok {
Expand Down
4 changes: 2 additions & 2 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (it *Test) afterTest(t internalT, resp *http.Response, errs []error) []erro
}

return scope
}, false)
})
}

func (it *Test) beforeTest(t internalT, req *http.Request) []error {
Expand All @@ -312,7 +312,7 @@ func (it *Test) beforeTest(t internalT, req *http.Request) []error {
}

return scope
}, false)
})
}

func (it *Test) createRequest(ctx context.Context) (*http.Request, error) {
Expand Down

0 comments on commit 303e905

Please sign in to comment.