Skip to content

Commit

Permalink
Merge pull request #38 from nitishm/nitishm/fix/13/testing-initial-co…
Browse files Browse the repository at this point in the history
…mmit

Testing w/ mockery-testify initial commit
  • Loading branch information
nitishm authored Feb 17, 2019
2 parents 27370ca + e758b8e commit 26b929b
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 66 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ require (
github.com/satori/go.uuid v1.2.0
github.com/sirupsen/logrus v1.3.0
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 // indirect
github.com/stretchr/testify v1.2.2
github.com/tsenart/vegeta v12.1.0+incompatible
github.com/ugorji/go/codec v0.0.0-20190128213124-ee1426cffec0 // indirect
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 // indirect
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 // indirect
golang.org/x/text v0.3.0 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 h1:7z3LSn867ex6VSaahyKadf4WtSsJIgne6A1WLOAGM8A=
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down
94 changes: 94 additions & 0 deletions internal/dispatcher/mocks/IDispatcher.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions internal/dispatcher/mocks/ITask.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 5 additions & 40 deletions internal/endpoints/attack.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ import (
func (e *Endpoints) PostAttackEndpoint(c *gin.Context) {
var attackParams models.AttackParams
if err := c.ShouldBindJSON(&attackParams); err != nil {
c.JSON(
http.StatusBadRequest,
gin.H{
"message": "Bad request params",
"code": http.StatusBadRequest,
"error": err.Error(),
},
)
ginErrBadRequest(c, err)
return
}

Expand All @@ -33,14 +26,7 @@ func (e *Endpoints) GetAttackByIDEndpoint(c *gin.Context) {
id := c.Param("attackID")
resp, err := e.dispatcher.Get(id)
if err != nil {
c.JSON(
http.StatusNotFound,
gin.H{
"message": "Not found",
"code": http.StatusNotFound,
"error": err.Error(),
},
)
ginErrNotFound(c, err)
return
}

Expand All @@ -59,40 +45,19 @@ func (e *Endpoints) PostAttackByIDCancelEndpoint(c *gin.Context) {
id := c.Param("attackID")
var attackCancelParams models.AttackCancel
if err := c.ShouldBindJSON(&attackCancelParams); err != nil {
c.JSON(
http.StatusBadRequest,
gin.H{
"message": "Bad request params",
"code": http.StatusBadRequest,
"error": err.Error(),
},
)
ginErrBadRequest(c, err)
return
}

_, err := e.dispatcher.Get(id)
if err != nil {
c.JSON(
http.StatusNotFound,
gin.H{
"message": "Not Found",
"code": http.StatusNotFound,
"error": err.Error(),
},
)
ginErrNotFound(c, err)
return
}

resp, err := e.dispatcher.Cancel(id, attackCancelParams.Cancel)
if err != nil {
c.JSON(
http.StatusInternalServerError,
gin.H{
"message": "Internal server error",
"code": http.StatusInternalServerError,
"error": err.Error(),
},
)
ginErrInternalServerError(c, err)
return
}

Expand Down
77 changes: 77 additions & 0 deletions internal/endpoints/attack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package endpoints

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"vegeta-server/internal/dispatcher"
dmocks "vegeta-server/internal/dispatcher/mocks"
"vegeta-server/models"

"github.com/stretchr/testify/mock"

assert "gopkg.in/go-playground/assert.v1"
)

func setupTestRouter(d dispatcher.IDispatcher, req *http.Request) *httptest.ResponseRecorder {
router := SetupRouter(d, nil)
w := httptest.NewRecorder()

router.ServeHTTP(w, req)
return w
}

func TestEndpoints_PostAttackEndpoint(t *testing.T) {

}

func TestEndpoints_GetAttackByIDEndpoint_NotFound(t *testing.T) {
d := &dmocks.IDispatcher{}

// Prepare mock
wantErr := fmt.Errorf("not found")
d.
On("Get", mock.AnythingOfType("string")).
Return(nil, wantErr)

// Setup router
req, _ := http.NewRequest("GET", "/api/v1/attack/123", nil)
w := setupTestRouter(d, req)

// Assert results
assert.Equal(t, http.StatusNotFound, w.Code)
}

func TestEndpoints_GetAttackByIDEndpoint(t *testing.T) {
d := &dmocks.IDispatcher{}

// Prepare mock
wantBody := &models.AttackResponse{
ID: "123",
Status: models.AttackResponseStatusScheduled,
}

d.
On("Get", "123").
Return(wantBody, nil)

// Setup router
req, _ := http.NewRequest("GET", "/api/v1/attack/123", nil)
w := setupTestRouter(d, req)

// Assert results
assert.Equal(t, http.StatusOK, w.Code)
gotBody := &models.AttackResponse{}
_ = json.Unmarshal(w.Body.Bytes(), gotBody)
assert.Equal(t, *wantBody, *gotBody)
}

func TestEndpoints_GetAttackEndpoint(t *testing.T) {

}

func TestEndpoints_PostAttackByIDCancelEndpoint(t *testing.T) {

}
Loading

0 comments on commit 26b929b

Please sign in to comment.