Skip to content

Commit

Permalink
Merge pull request #1 from bastiendmt/mocks
Browse files Browse the repository at this point in the history
techschool#15 mocks & 100% coverage on getAccount
  • Loading branch information
bastiendmt authored Oct 21, 2024
2 parents b66e665 + 392fcdb commit 59742c0
Show file tree
Hide file tree
Showing 16 changed files with 452 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["oneof"]
"cSpell.words": ["gomock", "mockdb", "oneof"]
}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ test:
server:
go run main.go

.PHONY: postgres createdb dropdb migrateup migratedown sqlc test server
mock:
mockgen -package mockdb -destination db/mock/store.go github.com/bastiendmt/simplebank/db/sqlc Store

.PHONY: postgres createdb dropdb migrateup migratedown sqlc test server mock
2 changes: 1 addition & 1 deletion api/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"database/sql"
"net/http"

db "github.com/bastiendmt/simplebank/db/sqlc"
"github.com/gin-gonic/gin"
db "github.com/techschool/simplebank/db/sqlc"
)

type createAccountRequest struct {
Expand Down
113 changes: 113 additions & 0 deletions api/account_test.go
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)
}
13 changes: 13 additions & 0 deletions api/main_test.go
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())
}
6 changes: 3 additions & 3 deletions api/server.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package api

import (
db "github.com/bastiendmt/simplebank/db/sqlc"
"github.com/gin-gonic/gin"
db "github.com/techschool/simplebank/db/sqlc"
)

// Server serves HTTP requests
type Server struct {
store *db.Store
store db.Store
router *gin.Engine
}

// Creates a new HTTP server and setup routing.
func NewServer(store *db.Store) *Server {
func NewServer(store db.Store) *Server {
server := &Server{store: store}
router := gin.Default()

Expand Down
Loading

0 comments on commit 59742c0

Please sign in to comment.