datastore/internal/api/impl_test.go
Luís Murta a52bca5882
Adapt API implementation with DAL interface
Swap direct access to the DB on the API server with an data abstraction
layer.
Implement each API type converter separately and revert changes to the
auto-generated server implementation types.

Add error return to DAL methods. Implement `Transactions`.

Add tools.go with oapi-codegen and mockgen.
https://www.jvt.me/posts/2022/06/15/go-tools-dependency-management/

Update go packages.

Issues #5, #12
2024-05-12 22:16:24 +01:00

156 lines
3.2 KiB
Go

package api
import (
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"git.rosemyrtle.work/personal-finance/server/internal/dal"
"git.rosemyrtle.work/personal-finance/server/internal/entity"
"git.rosemyrtle.work/personal-finance/server/internal/mock"
"github.com/labstack/echo/v4"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)
func TestServerImpl_GetBanks(t *testing.T) {
type fields struct {
Dal dal.DAL
}
type args struct {
ctx echo.Context
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &ServerImpl{
Dal: tt.fields.Dal,
}
if err := s.GetBanks(tt.args.ctx); (err != nil) != tt.wantErr {
t.Errorf("ServerImpl.GetBanks() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestServerImpl_GetBankById(t *testing.T) {
type fields struct {
Dal dal.DAL
}
type args struct {
ctx echo.Context
bankId int64
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &ServerImpl{
Dal: tt.fields.Dal,
}
if err := s.GetBankById(tt.args.ctx, tt.args.bankId); (err != nil) != tt.wantErr {
t.Errorf("ServerImpl.GetBankById() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestServerImpl_GetTransactions(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mock_dal := mock.NewMockDAL(ctrl)
type fields struct {
Dal dal.DAL
}
type args struct {
params GetTransactionsParams
}
tests := []struct {
name string
fields fields
args args
wantErr bool
mocks entity.Transactions
}{
{
"200",
fields{mock_dal},
args{GetTransactionsParams{}},
false,
entity.Transactions{{Id: 1, Date: time.Now(), Description: "desc#1", Value: decimal.New(0, 0)}},
},
{
"204",
fields{mock_dal},
args{GetTransactionsParams{}},
false,
entity.Transactions{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pf := &ServerImpl{
Dal: tt.fields.Dal,
}
req := httptest.NewRequest(http.MethodGet, "/transactions", nil)
rec := httptest.NewRecorder()
ctx := echo.New().NewContext(req, rec)
mock_dal.EXPECT().Transactions().Return(tt.mocks, nil).Times(1)
if err := pf.GetTransactions(ctx, tt.args.params); (err != nil) != tt.wantErr {
t.Errorf("ServerImpl.GetTransactions() error = %v,", err)
}
code, _ := strconv.Atoi(tt.name)
assert.Equal(t, code, rec.Code)
})
}
}
func TestServerImpl_GetTransactionById(t *testing.T) {
type fields struct {
Dal dal.DAL
}
type args struct {
ctx echo.Context
transactionId int64
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &ServerImpl{
Dal: tt.fields.Dal,
}
if err := s.GetTransactionById(tt.args.ctx, tt.args.transactionId); (err != nil) != tt.wantErr {
t.Errorf("ServerImpl.GetTransactionById() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}