Skip to content

Commit

Permalink
tests: add unit test for application services
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoclair committed Feb 21, 2024
1 parent 9085f4a commit d62b5c8
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 64 deletions.
185 changes: 125 additions & 60 deletions application/service/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
})
}
Expand Down
16 changes: 12 additions & 4 deletions application/service/service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

0 comments on commit d62b5c8

Please sign in to comment.