Skip to content

Commit

Permalink
[ISSUE-44] add broken validation (#61)
Browse files Browse the repository at this point in the history
* [ISSUE-44] add broken validation

* [ISSUE-44] add wrappers for errors, fix docs and linter

* [ISSUE-44] fix docs, test

* [ISSUE-44] fix linter
  • Loading branch information
siller174 authored Feb 12, 2024
1 parent 97a43ec commit 9c6558e
Show file tree
Hide file tree
Showing 29 changed files with 925 additions and 346 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
with:
only-new-issues: true
# golangci-lint command line arguments
args: --timeout=5m0s
args: --timeout=5m0s --new-from-rev=origin/master

examples:
name: examples
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ install:
# run full lint like in pipeline
.PHONY: lint
lint: install-lint
$(GOLANGCI_BIN) run --config=.golangci.yaml ./... --build-tags=examples,allure_go,provider
$(GOLANGCI_BIN) run --config=.golangci.yaml ./... --new-from-rev=origin/master --build-tags=examples,allure_go,provider


.PHONY: install-lint
Expand Down
20 changes: 10 additions & 10 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Three steps for testing your HTTP service:
## Installation

```bash
go get -u github.com/ozontech/cute
go get -u github.com/ozontech/cute
```

## Requirements
Expand All @@ -58,16 +58,16 @@ go get -u github.com/ozontech/cute

1) Install allure
```bash
$ brew install allure
brew install allure
```
2) Run example
```bash
$ make example
make example
```

3) Run allure
```bash
$ allure serve ./examples/allure-results
allure serve ./examples/allure-results
```

## Examples
Expand Down Expand Up @@ -447,9 +447,9 @@ You can implement [3 type of asserts](assert.go):
Types for creating custom assertions.

```go
type AssertBody func(body []byte) error
type AssertHeaders func(headers http.Header) error
type AssertResponse func(response *http.Response) error
type AssertBody func(body []byte) error
type AssertHeaders func(headers http.Header) error
type AssertResponse func(response *http.Response) error
```

**Example:**
Expand All @@ -473,9 +473,9 @@ You can log some information to Allure. \
Also you can log error on Allure yourself or just return error.

```go
type AssertBodyT func(t cute.T, body []byte) error
type AssertHeadersT func(t cute.T, headers http.Header) error
type AssertResponseT func(t cute.T, response *http.Response) error
type AssertBodyT func(t cute.T, body []byte) error
type AssertHeadersT func(t cute.T, headers http.Header) error
type AssertResponseT func(t cute.T, response *http.Response) error
```

**Example with T:**
Expand Down
69 changes: 69 additions & 0 deletions assert_broken.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 brokenAssertHeaders(assert AssertHeaders) AssertHeaders {
return func(headers http.Header) error {
err := assert(headers)

return wrapBrokenError(err)
}
}

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

return wrapBrokenError(err)
}
}

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

return wrapBrokenError(err)
}
}

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

return wrapBrokenError(err)
}
}

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

return wrapBrokenError(err)
}
}

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

return wrapBrokenError(err)
}
}

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

if tErr, ok := err.(errors.BrokenError); ok {
tErr.SetBroken(true)

return tErr.(error)
}

return errors.WrapBrokenError(err)
}
98 changes: 98 additions & 0 deletions assert_broken_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cute

import (
"errors"
"net/http"
"testing"

cuteErrors "github.com/ozontech/cute/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBrokenAssertResponse(t *testing.T) {
v := &http.Response{}
f := func(_ *http.Response) error {
return errors.New("test error")
}

err := brokenAssertResponse(f)(v)

if BrokenError, ok := err.(cuteErrors.BrokenError); assert.True(t, ok) {
require.True(t, BrokenError.IsBroken())
}
}

func TestBrokenAssertResponseT(t *testing.T) {
v := &http.Response{}
f := func(T, *http.Response) error {
return errors.New("test error")
}

err := brokenAssertResponseT(f)(nil, v)

if BrokenError, ok := err.(cuteErrors.BrokenError); assert.True(t, ok) {
require.True(t, BrokenError.IsBroken())
}
}

func TestBrokenAssertHeaders(t *testing.T) {
h := http.Header{}
f := func(_ http.Header) error {
return errors.New("test error")
}

err := brokenAssertHeaders(f)(h)

if BrokenError, ok := err.(cuteErrors.BrokenError); assert.True(t, ok) {
require.True(t, BrokenError.IsBroken())
}
}

func TestBrokenAssertHeadersT(t *testing.T) {
h := http.Header{}
f := func(T, http.Header) error {
return errors.New("test error")
}

err := brokenAssertHeadersT(f)(nil, h)

if BrokenError, ok := err.(cuteErrors.BrokenError); assert.True(t, ok) {
require.True(t, BrokenError.IsBroken())
}
}

func TestBrokenAssertBody(t *testing.T) {
v := []byte{}
f := func(_ []byte) error {
return errors.New("test error")
}

err := brokenAssertBody(f)(v)

if BrokenError, ok := err.(cuteErrors.BrokenError); assert.True(t, ok) {
require.True(t, BrokenError.IsBroken())
}
}

func TestBrokenAssertBodyT(t *testing.T) {
v := []byte{}
f := func(T, []byte) error {
return errors.New("test error")
}

err := brokenAssertBodyT(f)(nil, v)

if BrokenError, ok := err.(cuteErrors.BrokenError); assert.True(t, ok) {
require.True(t, BrokenError.IsBroken())
}
}

func TestWrapBrokenError(t *testing.T) {
err := errors.New("test error")

optError := wrapBrokenError(err)
if BrokenError, ok := optError.(cuteErrors.BrokenError); assert.True(t, ok) {
require.True(t, BrokenError.IsBroken())
}
}
2 changes: 1 addition & 1 deletion assert_optional.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ func wrapOptionalError(err error) error {
return tErr.(error)
}

return errors.NewOptionalError(err.Error())
return errors.WrapOptionalError(err)
}
12 changes: 6 additions & 6 deletions assert_test.go → assert_optional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestOptionalAssertResponse(t *testing.T) {
v := &http.Response{}
f := func(resp *http.Response) error {
f := func(*http.Response) error {
return errors.New("test error")
}

Expand All @@ -25,7 +25,7 @@ func TestOptionalAssertResponse(t *testing.T) {

func TestOptionalAssertResponseT(t *testing.T) {
v := &http.Response{}
f := func(t T, resp *http.Response) error {
f := func(T, *http.Response) error {
return errors.New("test error")
}

Expand All @@ -38,7 +38,7 @@ func TestOptionalAssertResponseT(t *testing.T) {

func TestOptionalAssertHeaders(t *testing.T) {
h := http.Header{}
f := func(headers http.Header) error {
f := func(http.Header) error {
return errors.New("test error")
}

Expand All @@ -51,7 +51,7 @@ func TestOptionalAssertHeaders(t *testing.T) {

func TestOptionalAssertHeadersT(t *testing.T) {
h := http.Header{}
f := func(t T, headers http.Header) error {
f := func(T, http.Header) error {
return errors.New("test error")
}

Expand All @@ -64,7 +64,7 @@ func TestOptionalAssertHeadersT(t *testing.T) {

func TestOptionalAssertBody(t *testing.T) {
v := []byte{}
f := func(body []byte) error {
f := func([]byte) error {
return errors.New("test error")
}

Expand All @@ -77,7 +77,7 @@ func TestOptionalAssertBody(t *testing.T) {

func TestOptionalAssertBodyT(t *testing.T) {
v := []byte{}
f := func(t T, body []byte) error {
f := func(T, []byte) error {
return errors.New("test error")
}

Expand Down
2 changes: 1 addition & 1 deletion assert_require.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ func wrapRequireError(err error) error {
return tErr.(error)
}

return errors.NewRequireError(err.Error())
return errors.WrapRequireError(err)
}
Loading

0 comments on commit 9c6558e

Please sign in to comment.