Log API/DAL methods

Also renames some variables, removes old comments and harmonizes unit
tests.
This commit is contained in:
Luís Murta 2024-05-18 23:48:54 +01:00
parent 26302b78d4
commit e382609c9b
Signed by: satprog
GPG Key ID: 169EF1BBD7049F94
3 changed files with 19 additions and 23 deletions

View File

@ -14,10 +14,10 @@ type ServerImpl struct {
Dal dal.DAL Dal dal.DAL
} }
func (pf *ServerImpl) GetBanks(ctx echo.Context) error { func (server *ServerImpl) GetBanks(ctx echo.Context) error {
log.Printf("GetBanks") log.Print("GetBanks")
banks, err := pf.Dal.Banks() banks, err := server.Dal.Banks()
if err != nil { if err != nil {
return ctx.NoContent(http.StatusInternalServerError) return ctx.NoContent(http.StatusInternalServerError)
} }
@ -29,10 +29,10 @@ func (pf *ServerImpl) GetBanks(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, convertBanks(banks)) return ctx.JSON(http.StatusOK, convertBanks(banks))
} }
func (pf *ServerImpl) GetBankById(ctx echo.Context, bankId string) error { func (server *ServerImpl) GetBankById(ctx echo.Context, bankId string) error {
log.Printf("GetTransactionById(%v)", bankId) log.Printf("GetBankById(%v)", bankId)
bank, err := pf.Dal.Bank(bankId) bank, err := server.Dal.Bank(bankId)
if err != nil { if err != nil {
log.Printf("%v", err) log.Printf("%v", err)
return ctx.NoContent(http.StatusInternalServerError) return ctx.NoContent(http.StatusInternalServerError)
@ -45,14 +45,10 @@ func (pf *ServerImpl) GetBankById(ctx echo.Context, bankId string) error {
return ctx.JSON(http.StatusOK, convertBank(*bank)) return ctx.JSON(http.StatusOK, convertBank(*bank))
} }
func (pf *ServerImpl) GetTransactions(ctx echo.Context, params GetTransactionsParams) error { func (server *ServerImpl) GetTransactions(ctx echo.Context, params GetTransactionsParams) error {
if pf.Dal == nil { log.Print("GetTransactions")
log.Panic("database not available")
}
// rows, err := pf.Dal.Query("SELECT tc.name, t.date, t.description, t.id, t.amount FROM pfbudget.transactions t LEFT JOIN pfbudget.transactions_categorized tc ON t.id = tc.id") transactions, err := server.Dal.Transactions()
transactions, err := pf.Dal.Transactions()
if err != nil { if err != nil {
return ctx.NoContent(http.StatusInternalServerError) return ctx.NoContent(http.StatusInternalServerError)
} }
@ -64,10 +60,10 @@ func (pf *ServerImpl) GetTransactions(ctx echo.Context, params GetTransactionsPa
return ctx.JSON(http.StatusOK, convertTransactions(transactions)) return ctx.JSON(http.StatusOK, convertTransactions(transactions))
} }
func (pf *ServerImpl) GetTransactionById(ctx echo.Context, transactionId int64) error { func (server *ServerImpl) GetTransactionById(ctx echo.Context, transactionId int64) error {
log.Printf("GetTransactionById(%d)", transactionId) log.Printf("GetTransactionById(%v)", transactionId)
transaction, err := pf.Dal.Transaction(transactionId) transaction, err := server.Dal.Transaction(transactionId)
if err != nil { if err != nil {
log.Printf("%v", err) log.Printf("%v", err)
return ctx.NoContent(http.StatusInternalServerError) return ctx.NoContent(http.StatusInternalServerError)

View File

@ -12,7 +12,7 @@ type DalImpl struct {
} }
func (dal *DalImpl) Transaction(transactionId int64) (*entity.Transaction, error) { func (dal *DalImpl) Transaction(transactionId int64) (*entity.Transaction, error) {
log.Printf("DAL::Transaction(%d)", transactionId) log.Printf("DAL::Transaction(%v)", transactionId)
if dal.Db == nil { if dal.Db == nil {
log.Panic("database not available") log.Panic("database not available")
@ -32,6 +32,8 @@ func (dal *DalImpl) Transaction(transactionId int64) (*entity.Transaction, error
} }
func (dal *DalImpl) Transactions() (entity.Transactions, error) { func (dal *DalImpl) Transactions() (entity.Transactions, error) {
log.Print("DAL::Transactions")
if dal.Db == nil { if dal.Db == nil {
log.Panic("database not available") log.Panic("database not available")
} }
@ -45,7 +47,7 @@ func (dal *DalImpl) Transactions() (entity.Transactions, error) {
} }
func (dal *DalImpl) Bank(bankId string) (*entity.Bank, error) { func (dal *DalImpl) Bank(bankId string) (*entity.Bank, error) {
log.Print("DAL::Bank") log.Printf("DAL::Bank(%v)", bankId)
if dal.Db == nil { if dal.Db == nil {
log.Panic("database not available") log.Panic("database not available")
@ -65,7 +67,7 @@ func (dal *DalImpl) Bank(bankId string) (*entity.Bank, error) {
} }
func (dal *DalImpl) Banks() (entity.Banks, error) { func (dal *DalImpl) Banks() (entity.Banks, error) {
log.Printf("DAL::Banks") log.Print("DAL::Banks")
if dal.Db == nil { if dal.Db == nil {
log.Panic("database not available") log.Panic("database not available")

View File

@ -34,9 +34,8 @@ func TestDalImpl_Transaction(t *testing.T) {
want *entity.Transaction want *entity.Transaction
wantErr bool wantErr bool
}{ }{
{"notfound", fields{db}, args{2, nil}, nil, false},
{ {
"found", "200",
fields{db}, fields{db},
args{ args{
1, 1,
@ -46,6 +45,7 @@ func TestDalImpl_Transaction(t *testing.T) {
&entity.Transaction{Id: 1, Date: date, Description: "income", Value: decimal.NewFromInt(1000)}, &entity.Transaction{Id: 1, Date: date, Description: "income", Value: decimal.NewFromInt(1000)},
false, false,
}, },
{"404", fields{db}, args{2, nil}, nil, false},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -116,12 +116,10 @@ func TestDalImpl_Transactions(t *testing.T) {
} }
mock. mock.
// ExpectQuery("^SELECT .* FROM .*transactions t LEFT JOIN .*transactions_categorized tc ON t.id = tc.id$").
ExpectQuery("^SELECT .* FROM .*transactions t$"). ExpectQuery("^SELECT .* FROM .*transactions t$").
WithoutArgs(). WithoutArgs().
WillReturnRows( WillReturnRows(
mock. mock.
// NewRows([]string{"category", "date", "description", "id", "amount"}).
NewRows([]string{"id", "date", "description", "amount"}). NewRows([]string{"id", "date", "description", "amount"}).
AddRows(tt.args.rows...), AddRows(tt.args.rows...),
) )