62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func TestGetTransactions(t *testing.T) {
|
|
db, mock, err := sqlmock.New()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
e := echo.New()
|
|
handlers := PersonalFinanceImpl{db}
|
|
RegisterHandlers(e, &handlers)
|
|
|
|
t.Run("when successful", func(t *testing.T) {
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/transactions", nil)
|
|
|
|
date := time.Now()
|
|
rows := mock.NewRows([]string{"id", "date", "description", "amount", "category"}).
|
|
AddRow(1, date, "#1", 1000, nil).
|
|
AddRow(2, date, "#2", -1000, "expense")
|
|
mock.ExpectQuery("SELECT t.id, t.date, t.description, t.amount, tc.category FROM pfbudget.transactions t LEFT JOIN pfbudget.transactions_categorized tc ON t.id = tc.id").WillReturnRows(rows)
|
|
|
|
ctx := e.NewContext(req, rec)
|
|
err := handlers.GetTransactions(ctx, GetTransactionsParams{})
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if rec.Code != http.StatusOK {
|
|
t.Error(rec.Code)
|
|
}
|
|
|
|
expected := `[
|
|
{
|
|
"id":1,
|
|
"date":"` + date.Format(time.DateOnly) + `",
|
|
description": "#1",
|
|
"amount":1000
|
|
},
|
|
{
|
|
"id":2,
|
|
"date":"` + date.Format(time.DateOnly) + `",
|
|
description": "#2",
|
|
"amount":-1000,
|
|
"category": "expense"
|
|
},
|
|
]`
|
|
if ret := rec.Body.String(); ret != expected {
|
|
t.Error(ret)
|
|
}
|
|
})
|
|
}
|