Skip to content

Commit

Permalink
feat: connect sumIncome with db
Browse files Browse the repository at this point in the history
  • Loading branch information
varissara-techup committed May 19, 2024
1 parent e01f332 commit 79ae8cf
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 16 deletions.
34 changes: 33 additions & 1 deletion api/transaction/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,39 @@ func (h handler) GetSummaryExpensesHandler(c echo.Context) error {
}

func (h handler) GetSummaryIncomeHandler(c echo.Context) error {
s := GetSummary(getTransection())
logger := mlog.L(c)
ctx := c.Request().Context()

rows, err := h.db.QueryContext(ctx, `SELECT * FROM transaction WHERE transaction_type = $1`, "income")
if err != nil {
logger.Error("query error", zap.Error(err))
return c.JSON(http.StatusInternalServerError, err.Error())
}

defer rows.Close()

var ts []Transaction
for rows.Next() {
var t Transaction

err := rows.Scan(&t.Id, &t.Date, &t.Amount, &t.Catergory, &t.TransactionType, &t.Note, &t.ImageUrl, &t.SpenderId)
if err != nil {
logger.Error("query error", zap.Error(err))
return c.JSON(http.StatusInternalServerError, ResponseMsg{
Message: QueryErrorMsg,
})
}

ts = append(ts, t)
}

if len(ts) == 0 {
return c.JSON(http.StatusOK, ResponseMsg{
Message: NoTransactionMsg,
})
}

s := GetSummary(ts)

return c.JSON(http.StatusOK, SummaryIncome{
TotalAmountEarned: s.Total,
Expand Down
87 changes: 72 additions & 15 deletions api/transaction/summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ var expectSummaryExpesnse = SummaryExpenses{
}

var expectSummaryIncome = SummaryIncome{
TotalAmountEarned: 5000,
AvgAmountEarnedPerDay: 1666.67,
TotalNumberEarned: 3,
TotalAmountEarned: 2800,
AvgAmountEarnedPerDay: 1400,
TotalNumberEarned: 2,
}

var expectBalance = Balance{
Expand Down Expand Up @@ -107,21 +107,78 @@ func TestGetSummaryExpenses(t *testing.T) {
}

func TestGetSummaryIncome(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/api/v1/incomes/summary", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

cfg := config.FeatureFlag{EnableCreateSpender: true}
t.Run("have error with query should return error", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/api/v1/incomes/summary", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

p := New(cfg, nil)
err := p.GetSummaryIncomeHandler(c)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
m, _ := json.Marshal(expectSummaryIncome)
db, mock, _ := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
defer db.Close()

assert.JSONEq(t, string(m), rec.Body.String())
sqlmock.NewRows([]string{"id", "date", "amount", "category", "transaction_type", "note", "image_url", "spender_id"})
mock.ExpectQuery(`SELECT FROM transaction WHERE transaction_type = $1`).WithArgs("income").WillReturnError(assert.AnError)
cfg := config.FeatureFlag{EnableCreateSpender: true}

p := New(cfg, db)
err := p.GetSummaryIncomeHandler(c)

assert.NoError(t, err)
assert.Equal(t, http.StatusInternalServerError, rec.Code)
})

t.Run("get all summary income should return list", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/api/v1/incomes/summary", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

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

rows := sqlmock.NewRows([]string{"id", "date", "amount", "category", "transaction_type", "note", "image_url", "spender_id"}).
AddRow(1, "2024-04-30 09:00:00+00", 1000, "Food", "income", "Lunch", "https://example.com/image1.jpg", 1).
AddRow(2, "2024-04-27 19:00:00+00", 1800, "Bills", "income", "Electricity bill", "https://example.com/image9.jpg", 1)

mock.ExpectQuery(`SELECT * FROM transaction WHERE transaction_type = $1`).WithArgs("income").WillReturnRows(rows)
cfg := config.FeatureFlag{EnableCreateSpender: true}

p := New(cfg, db)
err := p.GetSummaryIncomeHandler(c)

assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)

m, _ := json.Marshal(expectSummaryIncome)
assert.JSONEq(t, string(m), rec.Body.String())
})

t.Run("no transaction should return message transaction not found", func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/api/v1/incomes/summary", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)

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

rows := sqlmock.NewRows([]string{"id", "date", "amount", "category", "transaction_type", "note", "image_url", "spender_id"})
mock.ExpectQuery(`SELECT * FROM transaction WHERE transaction_type = $1`).WithArgs("income").WillReturnRows(rows)
cfg := config.FeatureFlag{EnableCreateSpender: true}

p := New(cfg, db)
err := p.GetSummaryIncomeHandler(c)

assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)

m, _ := json.Marshal(ResponseMsg{Message: NoTransactionMsg})
assert.JSONEq(t, string(m), rec.Body.String())
})

}

Expand Down

0 comments on commit 79ae8cf

Please sign in to comment.