Skip to content

Commit

Permalink
updated entity_get_all_test.go to use testify/suite
Browse files Browse the repository at this point in the history
  • Loading branch information
jlgerber committed Jan 24, 2016
1 parent b80953c commit 30fe364
Showing 1 changed file with 75 additions and 49 deletions.
124 changes: 75 additions & 49 deletions entity_get_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,37 @@ import (
"net/http/httptest"
"testing"

log "github.com/Sirupsen/logrus"

"github.com/gorilla/context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func TestFindAllSimple(t *testing.T) {
// EntityGetAllTestSuite defines the suite, and absorbs the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type EntityGetAllTestSuite struct {
suite.Suite
}

// Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *EntityGetAllTestSuite) SetupSuite() {
manager := GetQPManager()
log.Info(" -- EntityGetAll Test Suite --\n")
log.Debug("EntityGetAllTestSuite.SetupSuite() - setting active parsers to format1, format2, format3")
manager.SetActiveParsers("format1", "format2", "format3")
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestEntityGetAllTestSuite(t *testing.T) {
log.Debug("EntityGetALlTestSuite - Running test suite")
suite.Run(t, new(EntityGetAllTestSuite))
log.Debug("EntityGetAllTestSuite - Finished test suite")
}

func (suite *EntityGetAllTestSuite) TestFindAllSimple() {
req := getRequest("/Project")
w := httptest.NewRecorder()

Expand All @@ -22,7 +48,7 @@ func TestFindAllSimple(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
suite.Equal(http.StatusOK, w.Code)

type Entity struct {
Type string `json:"type"`
Expand All @@ -32,16 +58,16 @@ func TestFindAllSimple(t *testing.T) {
expected := `[{"type":"Project","id":63},{"type":"Project","id":65}]`
var jsonExpected []Entity
err := json.Unmarshal([]byte(expected), &jsonExpected)
assert.Nil(t, err)
suite.Nil(err)

var jsonResp []Entity
err = json.Unmarshal(w.Body.Bytes(), &jsonResp)
assert.Nil(t, err)
suite.Nil(err)

assert.Equal(t, jsonExpected, jsonResp)
suite.Equal(jsonExpected, jsonResp)
}

func TestFindAllWithFields(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllWithFields() {
req := getRequest("/Project?fields=name,sg_status")
w := httptest.NewRecorder()

Expand All @@ -53,7 +79,7 @@ func TestFindAllWithFields(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
suite.Equal(http.StatusOK, w.Code)

type Entity struct {
Type string `json:"type"`
Expand All @@ -65,16 +91,16 @@ func TestFindAllWithFields(t *testing.T) {
expected := `[{"id":63,"name":"Template Project","sg_status":null,"type":"Project"},{"id":65,"name":"Big Buck Bunny","sg_status":"Active","type":"Project"}]`
var jsonExpected []Entity
err := json.Unmarshal([]byte(expected), &jsonExpected)
assert.Nil(t, err)
suite.Nil(err)

var jsonResp []Entity
err = json.Unmarshal(w.Body.Bytes(), &jsonResp)
assert.Nil(t, err)
suite.Nil(err)

assert.Equal(t, jsonExpected, jsonResp)
suite.Equal(jsonExpected, jsonResp)
}

func TestFindAllPagingLimit1(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllPagingLimit1() {
req := getRequest("/Project?limit=1&page=2")
w := httptest.NewRecorder()

Expand All @@ -86,7 +112,7 @@ func TestFindAllPagingLimit1(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
suite.Equal(http.StatusOK, w.Code)

type Entity struct {
Type string `json:"type"`
Expand All @@ -96,15 +122,15 @@ func TestFindAllPagingLimit1(t *testing.T) {
expected := `[{"type":"Project","id":63}]`
var jsonExpected []Entity
err := json.Unmarshal([]byte(expected), &jsonExpected)
assert.Nil(t, err)
suite.Nil(err)

var jsonResp []Entity
err = json.Unmarshal(w.Body.Bytes(), &jsonResp)
assert.Nil(t, err)
suite.Nil(err)

}

func TestFindAllBadPageValue(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllBadPageValue() {
req := getRequest("/Project?limit=1&page=foo")
w := httptest.NewRecorder()

Expand All @@ -115,11 +141,11 @@ func TestFindAllBadPageValue(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
suite.Equal(http.StatusBadRequest, w.Code)

}

func TestFindAllBadLimitValue(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllBadLimitValue() {
req := getRequest("/Project?limit=foo&page=1")
w := httptest.NewRecorder()

Expand All @@ -130,11 +156,11 @@ func TestFindAllBadLimitValue(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
suite.Equal(http.StatusBadRequest, w.Code)
}

// Coming soon
func TestFindAllNoResults(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllNoResults() {
req := getRequest(`/Project?q=[["name", "is", "foo"]]`)
w := httptest.NewRecorder()

Expand All @@ -145,10 +171,10 @@ func TestFindAllNoResults(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusNoContent, w.Code)
suite.Equal(http.StatusNoContent, w.Code)
}

func TestFindAllMapQuery(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllMapQuery() {
req := getRequest(`/Project?q={"logical_operator": "and", "conditions": [["name", "starts_with", "Big"]]}`)
w := httptest.NewRecorder()

Expand All @@ -159,7 +185,7 @@ func TestFindAllMapQuery(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
suite.Equal(http.StatusOK, w.Code)

type Entity struct {
Type string `json:"type"`
Expand All @@ -169,17 +195,17 @@ func TestFindAllMapQuery(t *testing.T) {
expected := `[{"type":"Project","id":65}]`
var jsonExpected []Entity
err := json.Unmarshal([]byte(expected), &jsonExpected)
assert.Nil(t, err)
suite.Nil(err)

var jsonResp []Entity
err = json.Unmarshal(w.Body.Bytes(), &jsonResp)
assert.Nil(t, err)
suite.Nil(err)

assert.Equal(t, jsonExpected, jsonResp)
suite.Equal(jsonExpected, jsonResp)

}

func TestFindAllArrayQuery(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllArrayQuery() {
req := getRequest(`/Project?q=[["name", "starts_with", "Big"]]`)
w := httptest.NewRecorder()

Expand All @@ -190,7 +216,7 @@ func TestFindAllArrayQuery(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
suite.Equal(http.StatusOK, w.Code)

type Entity struct {
Type string `json:"type"`
Expand All @@ -200,17 +226,17 @@ func TestFindAllArrayQuery(t *testing.T) {
expected := `[{"type":"Project","id":65}]`
var jsonExpected []Entity
err := json.Unmarshal([]byte(expected), &jsonExpected)
assert.Nil(t, err)
suite.Nil(err)

var jsonResp []Entity
err = json.Unmarshal(w.Body.Bytes(), &jsonResp)
assert.Nil(t, err)
suite.Nil(err)

assert.Equal(t, jsonExpected, jsonResp)
suite.Equal(jsonExpected, jsonResp)

}

func TestFindAllAndStringQuery(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllAndStringQuery() {
req := getRequest(`/Project?q=and(["name", "starts_with", "Big"])`)
w := httptest.NewRecorder()

Expand All @@ -221,7 +247,7 @@ func TestFindAllAndStringQuery(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
suite.Equal(http.StatusOK, w.Code)

type Entity struct {
Type string `json:"type"`
Expand All @@ -231,17 +257,17 @@ func TestFindAllAndStringQuery(t *testing.T) {
expected := `[{"type":"Project","id":65}]`
var jsonExpected []Entity
err := json.Unmarshal([]byte(expected), &jsonExpected)
assert.Nil(t, err)
suite.Nil(err)

var jsonResp []Entity
err = json.Unmarshal(w.Body.Bytes(), &jsonResp)
assert.Nil(t, err)
suite.Nil(err)

assert.Equal(t, jsonExpected, jsonResp)
suite.Equal(jsonExpected, jsonResp)

}

func TestFindAllOrStringQuery(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllOrStringQuery() {
req := getRequest(`/Project?q=or(["name", "starts_with", "Big"],["name", "starts_with", "Test"])`)
w := httptest.NewRecorder()

Expand All @@ -252,7 +278,7 @@ func TestFindAllOrStringQuery(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
suite.Equal(http.StatusOK, w.Code)

type Entity struct {
Type string `json:"type"`
Expand All @@ -262,17 +288,17 @@ func TestFindAllOrStringQuery(t *testing.T) {
expected := `[{"type":"Project","id":65},{"type":"Project","id":66},{"type":"Project","id":71}]`
var jsonExpected []Entity
err := json.Unmarshal([]byte(expected), &jsonExpected)
assert.Nil(t, err)
suite.Nil(err)

var jsonResp []Entity
err = json.Unmarshal(w.Body.Bytes(), &jsonResp)
assert.Nil(t, err)
suite.Nil(err)

assert.Equal(t, jsonExpected, jsonResp)
suite.Equal(jsonExpected, jsonResp)

}

func TestFindAllBadQuery(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllBadQuery() {
req := getRequest(`/Project?q=and(["name", "starts_with", "Test"],)`)
w := httptest.NewRecorder()

Expand All @@ -283,11 +309,11 @@ func TestFindAllBadQuery(t *testing.T) {
context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
suite.Equal(http.StatusBadRequest, w.Code)

}

func TestFindAllBadAuth(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllBadAuth() {
req, _ := http.NewRequest("GET", `/Project`, nil)
w := httptest.NewRecorder()

Expand All @@ -297,10 +323,10 @@ func TestFindAllBadAuth(t *testing.T) {

router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusUnauthorized, w.Code)
suite.Equal(http.StatusUnauthorized, w.Code)
}

func TestFindAllBadResponseJson(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllBadResponseJson() {
req := getRequest("/Project")
w := httptest.NewRecorder()

Expand All @@ -309,10 +335,10 @@ func TestFindAllBadResponseJson(t *testing.T) {

context.Set(req, "sgConn", *client)
router(config).ServeHTTP(w, req)
assert.Equal(t, http.StatusBadGateway, w.Code)
suite.Equal(http.StatusBadGateway, w.Code)
}

func TestFindAllAnException(t *testing.T) {
func (suite *EntityGetAllTestSuite) TestFindAllAnException() {
req := getRequest("/Project")
w := httptest.NewRecorder()

Expand All @@ -322,5 +348,5 @@ func TestFindAllAnException(t *testing.T) {

router(config).ServeHTTP(w, req)

assert.Equal(t, http.StatusInternalServerError, w.Code)
suite.Equal(http.StatusInternalServerError, w.Code)
}

0 comments on commit 30fe364

Please sign in to comment.