Skip to content

Commit

Permalink
added tests for createAccount handler
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
1Shubham7 authored Sep 18, 2024
1 parent 3410fad commit e36f27c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
54 changes: 51 additions & 3 deletions api/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,60 @@ func TestGetAccount(t *testing.T){
requireBodyMatchAccount(t, recorder.Body, account)
}

func TestCreateAccount(t *testing.T){
assert := assert.New(t)
account := db.Account{
ID: util.RandomInt(1, 1000),
Owner: util.RandomOwner(),
Balance: util.RandomBalance(),
Currency: util.RandomCurrency(),
}

ctrl := gomock.NewController(t)
defer ctrl.Finish()

store := mockdb.NewMockStore(ctrl)

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

arg := db.CreateAccountParams{
Owner: account.Owner,
Currency: account.Currency,
Balance: account.Balance,
}

store.EXPECT().
CreateAccount(gomock.Any(), gomock.Eq(arg)).
Times(1).
Return(account, nil)

reqBody := createAccountRequest{
Owner: account.Owner,
Balance: account.Balance,
Currency: account.Currency,
}
body, err := json.Marshal(reqBody)
assert.NoError(err)

req, err := http.NewRequest(http.MethodPost, "/accounts", bytes.NewReader((body)))

assert.NoError(err)

req.Header.Set("Content-Type", "application/json")
server.router.ServeHTTP(recorder, req)

assert.Equal(http.StatusOK, recorder.Code)
requireBodyMatchAccount(t, recorder.Body, account)
}

func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, account db.Account) {
assert := assert.New(t)
data, err := io.ReadAll(body)
assert.NoError(t, err)
assert.NoError(err)

var gotAccount db.Account
err = json.Unmarshal(data, &gotAccount)
assert.NoError(t, err)
assert.Equal(t, account, gotAccount)
assert.NoError(err)
assert.Equal(account, gotAccount)
}
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())
}

0 comments on commit e36f27c

Please sign in to comment.