Skip to content

Commit

Permalink
Merge pull request #38 from ozontech/release/v0.1.11
Browse files Browse the repository at this point in the history
[v0.1.11] Implement query builder, implement require validations
  • Loading branch information
siller174 authored Feb 13, 2023
2 parents d329f99 + 07e572d commit a91de3b
Show file tree
Hide file tree
Showing 19 changed files with 480 additions and 172 deletions.
72 changes: 0 additions & 72 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package cute

import (
"net/http"

"github.com/ozontech/cute/errors"
)

// This is type of asserts, for create some assert with using custom logic.
Expand Down Expand Up @@ -123,73 +121,3 @@ func (it *Test) assertBody(t internalT, body []byte) []error {
return errs
})
}

func isOptionError(err error) bool {
if tErr, ok := err.(errors.OptionalError); ok {
return tErr.IsOptional()
}

return false
}

func optionalAssertHeaders(assert AssertHeaders) AssertHeaders {
return func(headers http.Header) error {
err := assert(headers)

return wrapOptionalError(err)
}
}

func optionalAssertBody(assert AssertBody) AssertBody {
return func(body []byte) error {
err := assert(body)

return wrapOptionalError(err)
}
}

func optionalAssertResponse(assert AssertResponse) AssertResponse {
return func(resp *http.Response) error {
err := assert(resp)

return wrapOptionalError(err)
}
}

func optionalAssertHeadersT(assert AssertHeadersT) AssertHeadersT {
return func(t T, headers http.Header) error {
err := assert(t, headers)

return wrapOptionalError(err)
}
}

func optionalAssertBodyT(assert AssertBodyT) AssertBodyT {
return func(t T, body []byte) error {
err := assert(t, body)

return wrapOptionalError(err)
}
}

func optionalAssertResponseT(assert AssertResponseT) AssertResponseT {
return func(t T, resp *http.Response) error {
err := assert(t, resp)

return wrapOptionalError(err)
}
}

func wrapOptionalError(err error) error {
if err == nil {
return nil
}

if tErr, ok := err.(errors.OptionalError); ok {
tErr.SetOptional(true)

return tErr.(error)
}

return errors.NewOptionalError(err.Error())
}
69 changes: 69 additions & 0 deletions assert_optional.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cute

import (
"net/http"

"github.com/ozontech/cute/errors"
)

func optionalAssertHeaders(assert AssertHeaders) AssertHeaders {
return func(headers http.Header) error {
err := assert(headers)

return wrapOptionalError(err)
}
}

func optionalAssertBody(assert AssertBody) AssertBody {
return func(body []byte) error {
err := assert(body)

return wrapOptionalError(err)
}
}

func optionalAssertResponse(assert AssertResponse) AssertResponse {
return func(resp *http.Response) error {
err := assert(resp)

return wrapOptionalError(err)
}
}

func optionalAssertHeadersT(assert AssertHeadersT) AssertHeadersT {
return func(t T, headers http.Header) error {
err := assert(t, headers)

return wrapOptionalError(err)
}
}

func optionalAssertBodyT(assert AssertBodyT) AssertBodyT {
return func(t T, body []byte) error {
err := assert(t, body)

return wrapOptionalError(err)
}
}

func optionalAssertResponseT(assert AssertResponseT) AssertResponseT {
return func(t T, resp *http.Response) error {
err := assert(t, resp)

return wrapOptionalError(err)
}
}

func wrapOptionalError(err error) error {
if err == nil {
return nil
}

if tErr, ok := err.(errors.OptionalError); ok {
tErr.SetOptional(true)

return tErr.(error)
}

return errors.NewOptionalError(err.Error())
}
69 changes: 69 additions & 0 deletions assert_require.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cute

import (
"net/http"

"github.com/ozontech/cute/errors"
)

func requireAssertHeaders(assert AssertHeaders) AssertHeaders {
return func(headers http.Header) error {
err := assert(headers)

return wrapRequireError(err)
}
}

func requireAssertBody(assert AssertBody) AssertBody {
return func(body []byte) error {
err := assert(body)

return wrapRequireError(err)
}
}

func requireAssertResponse(assert AssertResponse) AssertResponse {
return func(resp *http.Response) error {
err := assert(resp)

return wrapRequireError(err)
}
}

func requireAssertHeadersT(assert AssertHeadersT) AssertHeadersT {
return func(t T, headers http.Header) error {
err := assert(t, headers)

return wrapRequireError(err)
}
}

func requireAssertBodyT(assert AssertBodyT) AssertBodyT {
return func(t T, body []byte) error {
err := assert(t, body)

return wrapRequireError(err)
}
}

func requireAssertResponseT(assert AssertResponseT) AssertResponseT {
return func(t T, resp *http.Response) error {
err := assert(t, resp)

return wrapRequireError(err)
}
}

func wrapRequireError(err error) error {
if err == nil {
return nil
}

if tErr, ok := err.(errors.RequireError); ok {
tErr.SetRequire(true)

return tErr.(error)
}

return errors.NewRequireError(err.Error())
}
74 changes: 73 additions & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
const defaultHTTPTimeout = 30

var (
errorAssertIsNil = "Assert must be not nil"
errorAssertIsNil = "assert must be not nil"
)

// HTTPTestMaker is a creator tests
Expand Down Expand Up @@ -468,6 +468,18 @@ func (it *cute) OptionalAssertBody(asserts ...AssertBody) ExpectHTTPBuilder {
return it
}

func (it *cute) RequireBody(asserts ...AssertBody) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
panic(errorAssertIsNil)
}

it.tests[it.countTests].Expect.AssertBody = append(it.tests[it.countTests].Expect.AssertBody, requireAssertBody(assert))
}

return it
}

func (it *cute) AssertHeaders(asserts ...AssertHeaders) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
Expand All @@ -492,6 +504,18 @@ func (it *cute) OptionalAssertHeaders(asserts ...AssertHeaders) ExpectHTTPBuilde
return it
}

func (it *cute) RequireHeaders(asserts ...AssertHeaders) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
panic(errorAssertIsNil)
}

it.tests[it.countTests].Expect.AssertHeaders = append(it.tests[it.countTests].Expect.AssertHeaders, requireAssertHeaders(assert))
}

return it
}

func (it *cute) AssertResponse(asserts ...AssertResponse) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
Expand All @@ -516,6 +540,18 @@ func (it *cute) OptionalAssertResponse(asserts ...AssertResponse) ExpectHTTPBuil
return it
}

func (it *cute) RequireResponse(asserts ...AssertResponse) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
panic(errorAssertIsNil)
}

it.tests[it.countTests].Expect.AssertResponse = append(it.tests[it.countTests].Expect.AssertResponse, requireAssertResponse(assert))
}

return it
}

func (it *cute) AssertBodyT(asserts ...AssertBodyT) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
Expand All @@ -540,6 +576,18 @@ func (it *cute) OptionalAssertBodyT(asserts ...AssertBodyT) ExpectHTTPBuilder {
return it
}

func (it *cute) RequireBodyT(asserts ...AssertBodyT) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
panic(errorAssertIsNil)
}

it.tests[it.countTests].Expect.AssertBodyT = append(it.tests[it.countTests].Expect.AssertBodyT, requireAssertBodyT(assert))
}

return it
}

func (it *cute) AssertHeadersT(asserts ...AssertHeadersT) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
Expand All @@ -564,6 +612,18 @@ func (it *cute) OptionalAssertHeadersT(asserts ...AssertHeadersT) ExpectHTTPBuil
return it
}

func (it *cute) RequireHeadersT(asserts ...AssertHeadersT) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
panic(errorAssertIsNil)
}

it.tests[it.countTests].Expect.AssertHeadersT = append(it.tests[it.countTests].Expect.AssertHeadersT, requireAssertHeadersT(assert))
}

return it
}

func (it *cute) AssertResponseT(asserts ...AssertResponseT) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
Expand All @@ -588,6 +648,18 @@ func (it *cute) OptionalAssertResponseT(asserts ...AssertResponseT) ExpectHTTPBu
return it
}

func (it *cute) RequireResponseT(asserts ...AssertResponseT) ExpectHTTPBuilder {
for _, assert := range asserts {
if assert == nil {
panic(errorAssertIsNil)
}

it.tests[it.countTests].Expect.AssertResponseT = append(it.tests[it.countTests].Expect.AssertResponseT, requireAssertResponseT(assert))
}

return it
}

func (it *cute) EnableHardValidation() ExpectHTTPBuilder {
it.tests[it.countTests].HardValidation = true

Expand Down
18 changes: 18 additions & 0 deletions cute.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ func (it *cute) executeTest(ctx context.Context, allureProvider allureProvider)
inT.Logf("Test start %v", tableTestName)
resT := currentTest.execute(ctx, inT)
res = append(res, resT)

if resT.IsFailed() {
inT.Fail()

allureProvider.Logf("Test was failed %v", currentTest.Name)

return
}

inT.Logf("Test finished %v", tableTestName)
})
} else {
Expand All @@ -129,8 +138,17 @@ func (it *cute) executeTest(ctx context.Context, allureProvider allureProvider)
currentTest.Name = allureProvider.Name()

allureProvider.Logf("Test start %v", currentTest.Name)

resT := currentTest.execute(ctx, allureProvider)
res = append(res, resT)

if resT.IsFailed() {
allureProvider.Fail()

allureProvider.Logf("Test was failed %v", currentTest.Name)
break
}

allureProvider.Logf("Test finished %v", currentTest.Name)
}
}
Expand Down
Loading

0 comments on commit a91de3b

Please sign in to comment.