Skip to content

Commit

Permalink
updated api package tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFeij committed May 13, 2024
1 parent ac54c85 commit 746c20a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
45 changes: 34 additions & 11 deletions api/accounts_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
mockdb "Simple-Bank/db/mock"
"Simple-Bank/db/models"
servicesPackage "Simple-Bank/db/services"
"Simple-Bank/requests"
"Simple-Bank/responses"
"Simple-Bank/token"
Expand Down Expand Up @@ -170,7 +171,7 @@ func TestGetAccount(t *testing.T) {
addAuthorization(t, tokenMaker, authorizationTypeBearer, randomUser.Username, time.Minute, httpReq)
},
buildStubs: func(services *mockdb.MockServices, req requests.GetAccountRequest) {
services.EXPECT().GetAccount(gomock.Eq(account.ID)).Times(1).Return(models.Account{}, sql.ErrNoRows)
services.EXPECT().GetAccount(gomock.Eq(account.ID)).Times(1).Return(models.Account{}, gorm.ErrRecordNotFound)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusNotFound, recorder.Code)
Expand Down Expand Up @@ -255,7 +256,11 @@ func TestGetAccountsList(t *testing.T) {
},
buildStubs: func(services *mockdb.MockServices, req requests.GetAccountsListRequest) {
services.EXPECT().
ListAccounts(gomock.Eq(randomUser.Username), gomock.Eq(req.PageID), gomock.Eq(req.PageSize)).
ListAccounts(gomock.Eq(servicesPackage.ListAccountsRequest{
Owner: randomUser.Username,
PageSize: int(req.PageSize),
PageNumber: int(req.PageID),
})).
Times(1).Return(accounts[startIndex:endIndex], nil)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
Expand All @@ -273,7 +278,7 @@ func TestGetAccountsList(t *testing.T) {
},
buildStubs: func(services *mockdb.MockServices, req requests.GetAccountsListRequest) {
services.EXPECT().
ListAccounts(gomock.Any(), gomock.Any(), gomock.Any()).
ListAccounts(gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
Expand All @@ -291,7 +296,7 @@ func TestGetAccountsList(t *testing.T) {
},
buildStubs: func(services *mockdb.MockServices, req requests.GetAccountsListRequest) {
services.EXPECT().
ListAccounts(gomock.Any(), gomock.Any(), gomock.Any()).
ListAccounts(gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
Expand All @@ -309,8 +314,12 @@ func TestGetAccountsList(t *testing.T) {
},
buildStubs: func(services *mockdb.MockServices, req requests.GetAccountsListRequest) {
services.EXPECT().
ListAccounts(gomock.Eq(randomUser.Username), gomock.Eq(req.PageID), gomock.Eq(req.PageSize)).
Times(1).Return([]models.Account{}, sql.ErrNoRows)
ListAccounts(gomock.Eq(servicesPackage.ListAccountsRequest{
Owner: randomUser.Username,
PageSize: int(req.PageSize),
PageNumber: int(req.PageID),
})).
Times(1).Return([]models.Account{}, gorm.ErrRecordNotFound)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusNotFound, recorder.Code)
Expand All @@ -327,7 +336,11 @@ func TestGetAccountsList(t *testing.T) {
},
buildStubs: func(services *mockdb.MockServices, req requests.GetAccountsListRequest) {
services.EXPECT().
ListAccounts(gomock.Eq(randomUser.Username), gomock.Eq(req.PageID), gomock.Eq(req.PageSize)).
ListAccounts(gomock.Eq(servicesPackage.ListAccountsRequest{
Owner: randomUser.Username,
PageSize: int(req.PageSize),
PageNumber: int(req.PageID),
})).
Times(1).Return([]models.Account{}, sql.ErrConnDone)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
Expand Down Expand Up @@ -400,7 +413,12 @@ func TestTransfer(t *testing.T) {
addAuthorization(t, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute, httpReq)
},
buildStubs: func(services *mockdb.MockServices, req requests.TransferRequest) {
services.EXPECT().Transfer(gomock.Eq(user1.Username), gomock.Eq(req)).Times(1).Return(transfer, nil)
services.EXPECT().Transfer(gomock.Eq(servicesPackage.TransferRequest{
Owner: user1.Username,
FromAccountID: req.FromAccountID,
ToAccountID: req.ToAccountID,
Amount: req.Amount,
})).Times(1).Return(transfer, nil)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recorder.Code)
Expand All @@ -417,7 +435,7 @@ func TestTransfer(t *testing.T) {
setupAuth: func(t *testing.T, httpReq *http.Request, tokenMaker token.Maker) {
},
buildStubs: func(services *mockdb.MockServices, req requests.TransferRequest) {
services.EXPECT().Transfer(gomock.Any(), gomock.Any()).Times(0)
services.EXPECT().Transfer(gomock.Any()).Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusUnauthorized, recorder.Code)
Expand All @@ -434,7 +452,7 @@ func TestTransfer(t *testing.T) {
addAuthorization(t, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute, httpReq)
},
buildStubs: func(services *mockdb.MockServices, req requests.TransferRequest) {
services.EXPECT().Transfer(gomock.Any(), gomock.Any()).Times(0)
services.EXPECT().Transfer(gomock.Any()).Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusBadRequest, recorder.Code)
Expand All @@ -451,7 +469,12 @@ func TestTransfer(t *testing.T) {
addAuthorization(t, tokenMaker, authorizationTypeBearer, user1.Username, time.Minute, httpReq)
},
buildStubs: func(services *mockdb.MockServices, req requests.TransferRequest) {
services.EXPECT().Transfer(gomock.Eq(user1.Username), gomock.Eq(req)).Times(1).
services.EXPECT().Transfer(gomock.Eq(servicesPackage.TransferRequest{
Owner: user1.Username,
FromAccountID: req.FromAccountID,
ToAccountID: req.ToAccountID,
Amount: req.Amount,
})).Times(1).
Return(models.Transfer{}, sql.ErrConnDone)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
Expand Down
4 changes: 2 additions & 2 deletions api/users_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func TestGetUserAPI(t *testing.T) {
services.EXPECT().
GetUser(gomock.Eq(username)).
Times(1).
Return(models.User{}, sql.ErrNoRows)
Return(models.User{}, gorm.ErrRecordNotFound)
},
setupAuth: func(t *testing.T, httpReq *http.Request, tokenMaker token.Maker) {
addAuthorization(t, tokenMaker, authorizationTypeBearer, createdUser.Username, time.Minute, httpReq)
Expand Down Expand Up @@ -487,7 +487,7 @@ func TestLogin(t *testing.T) {
services.EXPECT().
GetUser(gomock.Eq(req.Username)).
Times(1).
Return(models.User{}, sql.ErrNoRows)
Return(models.User{}, gorm.ErrRecordNotFound)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder, tokenMaker token.Maker) {
require.Equal(t, http.StatusNotFound, recorder.Code)
Expand Down

0 comments on commit 746c20a

Please sign in to comment.