Refactor entities endpoint test to use mockstore
@@ -5,16 +5,23 @@ import (
"net/http"
"testing"
- "github.com/sensu/sensu-go/testing/fixtures"
+ "github.com/sensu/sensu-go/testing/mockstore"
"github.com/sensu/sensu-go/types"
"github.com/stretchr/testify/assert"
)
func TestHttpApiEntitiesGet(t *testing.T) {
+ store := &mockstore.MockStore{}
+
c := &EntitiesController{
- Store: fixtures.NewFixtureStore(),
+ Store: store,
}
+ entities := []*types.Entity{
+ types.FixtureEntity("entity1"),
+ types.FixtureEntity("entity2"),
+ }
+ store.On("GetEntities").Return(entities, nil)
req, _ := http.NewRequest("GET", "/entities", nil)
res := processRequest(c, req)
@@ -22,23 +29,31 @@ func TestHttpApiEntitiesGet(t *testing.T) {
body := res.Body.Bytes()
- entities := []*types.Entity{}
- err := json.Unmarshal(body, &entities)
+ returnedEntities := []*types.Entity{}
+ err := json.Unmarshal(body, &returnedEntities)
assert.NoError(t, err)
- assert.Condition(t, func() bool { return len(entities) >= 1 })
+ for i, entity := range returnedEntities {
+ assert.EqualValues(t, entities[i], entity)
+ }
}
func TestHttpApiEntityGet(t *testing.T) {
+ store := &mockstore.MockStore{}
+
c := &EntitiesController{
- Store: fixtures.NewFixtureStore(),
+ Store: store,
}
+ var nilEntity *types.Entity
+ store.On("GetEntityByID", "someentity").Return(nilEntity, nil)
notFoundReq, _ := http.NewRequest("GET", "/entities/someentity", nil)
notFoundRes := processRequest(c, notFoundReq)
assert.Equal(t, http.StatusNotFound, notFoundRes.Code)
+ entity1 := types.FixtureEntity("entity1")
+ store.On("GetEntityByID", "entity1").Return(entity1, nil)
foundReq, _ := http.NewRequest("GET", "/entities/entity1", nil)
foundRes := processRequest(c, foundReq)
@@ -46,12 +61,9 @@ func TestHttpApiEntityGet(t *testing.T) {
body := foundRes.Body.Bytes()
- entity := &types.Entity{}
- err := json.Unmarshal(body, &entity)
+ returnedEntity := &types.Entity{}
+ err := json.Unmarshal(body, &returnedEntity)
assert.NoError(t, err)
- assert.NotNil(t, entity.ID)
- assert.NotNil(t, entity.Class)
- assert.NotEqual(t, entity.ID, "")
- assert.Equal(t, entity.Class, "")
+ assert.EqualValues(t, entity1, returnedEntity)
}
