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) { ctrl := gomock.NewController(t) defer ctrl.Finish() mock_dal := mock.NewMockDAL(ctrl) type fields struct { Dal dal.DAL } type args struct { transactionId int64 } tests := []struct { name string fields fields args args wantErr bool mocks *entity.Transaction }{ { "200", fields{mock_dal}, args{1}, false, &entity.Transaction{Id: 1, Date: time.Now(), Description: "desc#1", Value: decimal.New(0, 0)}, }, { "404", fields{mock_dal}, args{2}, false, nil, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := &ServerImpl{ Dal: tt.fields.Dal, } req := httptest.NewRequest(http.MethodGet, "/transaction", nil) rec := httptest.NewRecorder() ctx := echo.New().NewContext(req, rec) mock_dal.EXPECT().Transaction(tt.args.transactionId).Return(tt.mocks, nil).Times(1) if err := s.GetTransactionById(ctx, tt.args.transactionId); (err != nil) != tt.wantErr { t.Errorf("ServerImpl.GetTransactionById() error = %v, wantErr %v", err, tt.wantErr) } }) } }