Skip to content

Commit

Permalink
add test for GetExpenseSummaryHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
golfz committed May 18, 2024
1 parent 9edea4e commit d0be2f9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
37 changes: 28 additions & 9 deletions api/expense/summary/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ package summary

import (
"database/sql"
"errors"
"github.com/KKGo-Software-engineering/workshop-summer/api/config"
"github.com/kkgo-software-engineering/workshop/mlog"
"github.com/labstack/echo/v4"
"go.uber.org/zap"
"net/http"
"time"
)

var (
ErrInvalidSpender = errors.New("invalid spender")
)

type Err struct {
Message string `json:"message"`
}

type Spender struct {
ID int `param:"id"`
}
Expand Down Expand Up @@ -50,12 +63,18 @@ func summary(data []RawData) Summary {
}
}

//func (h *handler) GetExpenseSummaryHandler(c echo.Context) error {
// var spender Spender
// if err := c.Bind(&spender); err != nil {
// return c.JSON(http.StatusBadRequest, err)
// }
//
// expenses := h.store.GetExpenses()
//
//}
// /api/v1/spenders/{id}/expenses/summary
func (h *handler) GetExpenseSummaryHandler(c echo.Context) error {
logger := mlog.L(c)
//ctx := c.Request().Context()
_ = c.Request().Context()

var spender Spender
err := c.Bind(&spender)
if err != nil {
logger.Error(ErrInvalidSpender.Error(), zap.Error(err))
return c.JSON(http.StatusBadRequest, Err{Message: ErrInvalidSpender.Error()})
}

return c.JSON(http.StatusOK, Summary{})
}
29 changes: 29 additions & 0 deletions api/expense/summary/summary_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package summary

import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/KKGo-Software-engineering/workshop-summer/api/config"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

Expand Down Expand Up @@ -40,3 +45,27 @@ func TestSummary(t *testing.T) {
})
}
}

func TestGetExpenseSummaryHandler(t *testing.T) {

t.Run("invalid spender id expect 400", func(t *testing.T) {
e := echo.New()
defer e.Close()

req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/spenders/:id/expenses/summary")
c.SetParamNames("id")
c.SetParamValues("not_int")

db, _, _ := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
defer db.Close()

h := New(config.FeatureFlag{}, db)
_ = h.GetExpenseSummaryHandler(c)

assert.Equal(t, http.StatusBadRequest, rec.Code)
})

}

0 comments on commit d0be2f9

Please sign in to comment.