forked from techschool/simplebank
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bastiendmt/mocks
techschool#15 mocks & 100% coverage on getAccount
- Loading branch information
Showing
16 changed files
with
452 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"cSpell.words": ["oneof"] | ||
"cSpell.words": ["gomock", "mockdb", "oneof"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
mockdb "github.com/bastiendmt/simplebank/db/mock" | ||
db "github.com/bastiendmt/simplebank/db/sqlc" | ||
"github.com/bastiendmt/simplebank/util" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetAccountAPI(t *testing.T) { | ||
account := randomAccount() | ||
|
||
testCases := []struct { | ||
name string | ||
accountId int64 | ||
buildStubs func(store *mockdb.MockStore) | ||
checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder) | ||
}{ | ||
{ | ||
name: "OK", | ||
accountId: account.ID, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account.ID)).Times(1).Return(account, nil) | ||
}, | ||
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) { | ||
require.Equal(t, http.StatusOK, recorder.Code) | ||
requireBodyMatchAccount(t, recorder.Body, account) | ||
}, | ||
}, | ||
{ | ||
name: "NotFound", | ||
accountId: account.ID, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account.ID)).Times(1).Return(db.Account{}, sql.ErrNoRows) | ||
}, | ||
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) { | ||
require.Equal(t, http.StatusNotFound, recorder.Code) | ||
}, | ||
}, | ||
{ | ||
name: "InternalError", | ||
accountId: account.ID, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account.ID)).Times(1).Return(db.Account{}, sql.ErrConnDone) | ||
}, | ||
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) { | ||
require.Equal(t, http.StatusInternalServerError, recorder.Code) | ||
}, | ||
}, | ||
{ | ||
name: "InvalidID", | ||
accountId: 0, | ||
buildStubs: func(store *mockdb.MockStore) { | ||
store.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Times(0) | ||
}, | ||
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) { | ||
require.Equal(t, http.StatusBadRequest, recorder.Code) | ||
}, | ||
}, | ||
} | ||
|
||
for i := range testCases { | ||
tc := testCases[i] | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
store := mockdb.NewMockStore(ctrl) | ||
tc.buildStubs(store) | ||
|
||
// start test server and send request | ||
server := NewServer(store) | ||
recorder := httptest.NewRecorder() | ||
|
||
url := fmt.Sprintf("/accounts/%d", tc.accountId) | ||
request, err := http.NewRequest(http.MethodGet, url, nil) | ||
require.NoError(t, err) | ||
|
||
server.router.ServeHTTP(recorder, request) | ||
tc.checkResponse(t, recorder) | ||
}) | ||
} | ||
} | ||
|
||
func randomAccount() db.Account { | ||
return db.Account{ | ||
ID: util.RandomInt(1, 1000), | ||
Owner: util.RandomOwner(), | ||
Balance: util.RandomMoney(), | ||
Currency: util.RandomCurrency(), | ||
} | ||
} | ||
|
||
func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, account db.Account) { | ||
data, err := io.ReadAll(body) | ||
require.NoError(t, err) | ||
|
||
var gotAccount db.Account | ||
err = json.Unmarshal(data, &gotAccount) | ||
require.NoError(t, err) | ||
require.Equal(t, account.ID, gotAccount.ID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package api | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
gin.SetMode(gin.TestMode) | ||
os.Exit(m.Run()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.