Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 219 additions & 0 deletions api/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/lib/pq"
mockdb "github.com/sinachaichi/gault/db/mock"
db "github.com/sinachaichi/gault/db/sqlc"
"github.com/sinachaichi/gault/util"
Expand Down Expand Up @@ -38,6 +40,223 @@ func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, account db.Accoun
require.Equal(t, account, gotAccount)
}

func TestCreateAccountAPI(t *testing.T) {
account := randomAccount()

testCases := []struct {
name string
body gin.H
buildStubs func(store *mockdb.MockStore)
checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder)
}{
{
name: "OK",
body: gin.H{
"owner": account.Owner,
"currency": account.Currency,
},
buildStubs: func(store *mockdb.MockStore) {
arg := db.CreateAccountParams{
Owner: account.Owner,
Currency: account.Currency,
Balance: 0,
}
store.EXPECT().
CreateAccount(gomock.Any(), gomock.Eq(arg)).
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: "InternalError",
body: gin.H{
"owner": account.Owner,
"currency": account.Currency,
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
CreateAccount(gomock.Any(), gomock.Any()).
Times(1).
Return(db.Account{}, sql.ErrConnDone)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusInternalServerError, recorder.Code)
},
},
{
name: "DuplicateOwnerCurrency",
body: gin.H{
"owner": account.Owner,
"currency": account.Currency,
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
CreateAccount(gomock.Any(), gomock.Any()).
Times(1).
Return(db.Account{}, &pq.Error{Code: "23505"})
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusForbidden, recorder.Code)
},
},
{
name: "InvalidCurrency",
body: gin.H{
"owner": account.Owner,
"currency": "XYZ",
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
CreateAccount(gomock.Any(), gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusBadRequest, recorder.Code)
},
},
{
name: "MissingOwner",
body: gin.H{
"currency": account.Currency,
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
CreateAccount(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)

server := NewServer(store)
recorder := httptest.NewRecorder()

data, err := json.Marshal(tc.body)
require.NoError(t, err)

request, err := http.NewRequest(http.MethodPost, "/accounts", bytes.NewReader(data))
require.NoError(t, err)
request.Header.Set("Content-Type", "application/json")

server.router.ServeHTTP(recorder, request)
tc.checkResponse(t, recorder)
})
}
}

func TestListAccountAPI(t *testing.T) {
n := 5
accounts := make([]db.Account, n)
for i := 0; i < n; i++ {
accounts[i] = randomAccount()
}

testCases := []struct {
name string
pageID int
pageSize int
buildStubs func(store *mockdb.MockStore)
checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder)
}{
{
name: "OK",
pageID: 1,
pageSize: 5,
buildStubs: func(store *mockdb.MockStore) {
arg := db.ListAccountsParams{
Limit: 5,
Offset: 0,
}
store.EXPECT().
ListAccounts(gomock.Any(), gomock.Eq(arg)).
Times(1).
Return(accounts, nil)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recorder.Code)
},
},
{
name: "InternalError",
pageID: 1,
pageSize: 5,
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
ListAccounts(gomock.Any(), gomock.Any()).
Times(1).
Return(nil, sql.ErrConnDone)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusInternalServerError, recorder.Code)
},
},
{
name: "InvalidPageID",
pageID: 0,
pageSize: 5,
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
ListAccounts(gomock.Any(), gomock.Any()).
Times(0)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusBadRequest, recorder.Code)
},
},
{
name: "InvalidPageSize",
pageID: 1,
pageSize: 2,
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
ListAccounts(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)

server := NewServer(store)
recorder := httptest.NewRecorder()

url := fmt.Sprintf("/accounts?page_id=%d&page_size=%d", tc.pageID, tc.pageSize)
request, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)

server.router.ServeHTTP(recorder, request)
tc.checkResponse(t, recorder)
})
}
}

func TestGetAccountAPI(t *testing.T) {
account := randomAccount()

Expand Down
Loading
Loading