From d62b5c85e54298c5d537fa98ffc92d35ee1a539a Mon Sep 17 00:00:00 2001 From: Diego Clair Date: Tue, 20 Feb 2024 22:04:30 -0300 Subject: [PATCH] tests: add unit test for application services --- application/service/account_test.go | 185 +++++++++++++++++++--------- application/service/service_mock.go | 16 ++- 2 files changed, 137 insertions(+), 64 deletions(-) diff --git a/application/service/account_test.go b/application/service/account_test.go index 1431cc2..d5af580 100644 --- a/application/service/account_test.go +++ b/application/service/account_test.go @@ -127,40 +127,60 @@ func Test_accountService_CreateAccount(t *testing.T) { } } -func Test_accountService_GetAccountByUUID(t *testing.T) { +func Test_accountService_AddBalance(t *testing.T) { type args struct { accountUUID string + amount float64 } tests := []struct { - name string - buildMock func(ctx context.Context, mocks allMocks, args args) - args args - wantAccount account.Account - wantErr bool + name string + buildMock func(ctx context.Context, mocks allMocks, args args) + args args + wantErr bool }{ { - name: "Should return an account without error", - args: args{ - accountUUID: "123", + name: "Should add balance without any errors", + args: args{accountUUID: "account123", amount: 7.32}, + buildMock: func(ctx context.Context, mocks allMocks, args args) { + result := account.Account{ID: 12, UUID: args.accountUUID, Balance: 50} + gomock.InOrder( + mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, nil).Times(1), + mocks.mockAccountRepo.EXPECT().UpdateAccountBalance(ctx, result.ID, result.Balance+args.amount).Return(nil).Times(1), + ) }, + }, + { + name: "Should add balance validating floating point", + args: args{accountUUID: "account123", amount: 0.1}, buildMock: func(ctx context.Context, mocks allMocks, args args) { - result := account.Account{ID: 1, UUID: "123", Name: "name"} - mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, nil).Times(1) + result := account.Account{ID: 12, UUID: args.accountUUID, Balance: 0.2} + gomock.InOrder( + mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, nil).Times(1), + mocks.mockAccountRepo.EXPECT().UpdateAccountBalance(ctx, result.ID, 0.3).Return(nil).Times(1), + ) }, - wantAccount: account.Account{ID: 1, UUID: "123", Name: "name"}, - wantErr: false, }, { - name: "Should error if database return some error", - args: args{ - accountUUID: "123", + name: "Should return error with there is some error to get account by uuid", + args: args{accountUUID: "account123", amount: 7.32}, + buildMock: func(ctx context.Context, mocks allMocks, args args) { + result := account.Account{} + mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, assert.AnError).Times(1) }, + wantErr: true, + }, + { + name: "Should return error with there is some error to update account balance", + args: args{accountUUID: "account123", amount: 7.32}, buildMock: func(ctx context.Context, mocks allMocks, args args) { - mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(account.Account{}, errors.New("some error")).Times(1) + result := account.Account{ID: 12, UUID: args.accountUUID, Balance: 50} + gomock.InOrder( + mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, nil).Times(1), + mocks.mockAccountRepo.EXPECT().UpdateAccountBalance(ctx, result.ID, result.Balance+args.amount).Return(assert.AnError).Times(1), + ) }, - wantAccount: account.Account{}, - wantErr: true, + wantErr: true, }, } for _, tt := range tests { @@ -177,73 +197,112 @@ func Test_accountService_GetAccountByUUID(t *testing.T) { if tt.buildMock != nil { tt.buildMock(ctx, allMocks, tt.args) } - - gotAccount, err := s.GetAccountByUUID(ctx, tt.args.accountUUID) - if (err != nil) != tt.wantErr { - t.Errorf("accountService.GetAccountByUUID() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(gotAccount, tt.wantAccount) { - t.Errorf("accountService.GetAccountByUUID() = %v, want %v", gotAccount, tt.wantAccount) + if err := s.AddBalance(ctx, tt.args.accountUUID, tt.args.amount); (err != nil) != tt.wantErr { + t.Errorf("AddBalance() error = %v, wantErr %v", err, tt.wantErr) } }) } } -func Test_accountService_AddBalance(t *testing.T) { - +func Test_accountService_GetAccounts(t *testing.T) { type args struct { - accountUUID string - amount float64 + take int64 + skip int64 } tests := []struct { name string buildMock func(ctx context.Context, mocks allMocks, args args) args args + want []account.Account + want1 int64 wantErr bool }{ { - name: "Should add balance without any errors", - args: args{accountUUID: "account123", amount: 7.32}, + name: "Should return accounts without any errors", + args: args{take: 10, skip: 0}, buildMock: func(ctx context.Context, mocks allMocks, args args) { - result := account.Account{ID: 12, UUID: args.accountUUID, Balance: 50} - gomock.InOrder( - mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, nil).Times(1), - mocks.mockAccountRepo.EXPECT().UpdateAccountBalance(ctx, result.ID, result.Balance+args.amount).Return(nil).Times(1), - ) + result := []account.Account{{ID: 1, UUID: "123", Name: "name"}} + mocks.mockAccountRepo.EXPECT().GetAccounts(ctx, args.take, args.skip).Return(result, int64(1), nil).Times(1) }, + want: []account.Account{{ID: 1, UUID: "123", Name: "name"}}, + want1: 1, + wantErr: false, }, { - name: "Should add balance validating floating point", - args: args{accountUUID: "account123", amount: 0.1}, + name: "Should return error with there is some error to get accounts", + args: args{take: 10, skip: 0}, buildMock: func(ctx context.Context, mocks allMocks, args args) { - result := account.Account{ID: 12, UUID: args.accountUUID, Balance: 0.2} - gomock.InOrder( - mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, nil).Times(1), - mocks.mockAccountRepo.EXPECT().UpdateAccountBalance(ctx, result.ID, 0.3).Return(nil).Times(1), - ) + mocks.mockAccountRepo.EXPECT().GetAccounts(ctx, args.take, args.skip).Return([]account.Account{}, int64(0), errors.New("some error")).Times(1) }, + want: []account.Account{}, + want1: 0, + wantErr: true, }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + + ctx := context.Background() + allMocks, svc, ctrl := newServiceTestMock(t) + defer ctrl.Finish() + + s := &accountService{ + svc: svc, + } + + if tt.buildMock != nil { + tt.buildMock(ctx, allMocks, tt.args) + } + + got, got1, err := s.GetAccounts(ctx, tt.args.take, tt.args.skip) + if (err != nil) != tt.wantErr { + t.Errorf("accountService.GetAccounts() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("accountService.GetAccounts() got = %v, want %v", got, tt.want) + } + if got1 != tt.want1 { + t.Errorf("accountService.GetAccounts() got1 = %v, want %v", got1, tt.want1) + } + }) + } +} + +func Test_accountService_GetAccountByUUID(t *testing.T) { + + type args struct { + accountUUID string + } + tests := []struct { + name string + buildMock func(ctx context.Context, mocks allMocks, args args) + args args + wantAccount account.Account + wantErr bool + }{ { - name: "Should return error with there is some error to get account by uuid", - args: args{accountUUID: "account123", amount: 7.32}, + name: "Should return an account without error", + args: args{ + accountUUID: "123", + }, buildMock: func(ctx context.Context, mocks allMocks, args args) { - result := account.Account{} - mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, assert.AnError).Times(1) + result := account.Account{ID: 1, UUID: "123", Name: "name"} + mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, nil).Times(1) }, - wantErr: true, + wantAccount: account.Account{ID: 1, UUID: "123", Name: "name"}, + wantErr: false, }, { - name: "Should return error with there is some error to update account balance", - args: args{accountUUID: "account123", amount: 7.32}, + name: "Should error if database return some error", + args: args{ + accountUUID: "123", + }, buildMock: func(ctx context.Context, mocks allMocks, args args) { - result := account.Account{ID: 12, UUID: args.accountUUID, Balance: 50} - gomock.InOrder( - mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(result, nil).Times(1), - mocks.mockAccountRepo.EXPECT().UpdateAccountBalance(ctx, result.ID, result.Balance+args.amount).Return(assert.AnError).Times(1), - ) + mocks.mockAccountRepo.EXPECT().GetAccountByUUID(ctx, args.accountUUID).Return(account.Account{}, errors.New("some error")).Times(1) }, - wantErr: true, + wantAccount: account.Account{}, + wantErr: true, }, } for _, tt := range tests { @@ -260,8 +319,14 @@ func Test_accountService_AddBalance(t *testing.T) { if tt.buildMock != nil { tt.buildMock(ctx, allMocks, tt.args) } - if err := s.AddBalance(ctx, tt.args.accountUUID, tt.args.amount); (err != nil) != tt.wantErr { - t.Errorf("AddBalance() error = %v, wantErr %v", err, tt.wantErr) + + gotAccount, err := s.GetAccountByUUID(ctx, tt.args.accountUUID) + if (err != nil) != tt.wantErr { + t.Errorf("accountService.GetAccountByUUID() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotAccount, tt.wantAccount) { + t.Errorf("accountService.GetAccountByUUID() = %v, want %v", gotAccount, tt.wantAccount) } }) } diff --git a/application/service/service_mock.go b/application/service/service_mock.go index 06817f8..1667cfc 100644 --- a/application/service/service_mock.go +++ b/application/service/service_mock.go @@ -27,11 +27,17 @@ func newServiceTestMock(t *testing.T) (m allMocks, svc *service, ctrl *gomock.Co ctrl = gomock.NewController(t) log := logger.NewNoop() + dm := mocks.NewMockDataManager(ctrl) + accountRepo := mocks.NewMockAccountRepo(ctrl) + dm.EXPECT().Account().Return(accountRepo).AnyTimes() + + authRepo := mocks.NewMockAuthRepo(ctrl) + dm.EXPECT().Auth().Return(authRepo).AnyTimes() + cm := mocks.NewMockCacheManager(ctrl) crypto := mocks.NewMockCrypto(ctrl) - authRepo := mocks.NewMockAuthRepo(ctrl) m = allMocks{ mockDataManager: dm, @@ -41,10 +47,12 @@ func newServiceTestMock(t *testing.T) (m allMocks, svc *service, ctrl *gomock.Co mockCrypto: crypto, } - dm.EXPECT().Account().Return(accountRepo).AnyTimes() - dm.EXPECT().Auth().Return(authRepo).AnyTimes() - svc = newService(dm, cfg, cm, crypto, log) + // validate func New + s, err := New(dm, cfg, cm, crypto, log) + require.NoError(t, err) + require.NotNil(t, s) + return }