ft/add first tests to api/account (#20)
This commit is contained in:
parent
f090e5fc9d
commit
fa088f6156
66
api/account_test.go
Normal file
66
api/account_test.go
Normal file
@ -0,0 +1,66 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
mockdb "github.com/itsscb/df/db/mock"
|
||||
db "github.com/itsscb/df/db/sqlc"
|
||||
"github.com/itsscb/df/util"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/mock/gomock"
|
||||
)
|
||||
|
||||
func TestGetAccountAPI(t *testing.T) {
|
||||
account := randomAccount()
|
||||
|
||||
ctrl := gomock.NewController(t)
|
||||
defer ctrl.Finish()
|
||||
|
||||
store := mockdb.NewMockStore(ctrl)
|
||||
|
||||
store.EXPECT().GetAccount(gomock.Any(), gomock.Eq(account.ID)).
|
||||
Times(1).
|
||||
Return(account, nil)
|
||||
|
||||
server := NewServer(store)
|
||||
recorder := httptest.NewRecorder()
|
||||
|
||||
uri := fmt.Sprintf("/accounts/%d", account.ID)
|
||||
req, err := http.NewRequest(http.MethodGet, uri, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
server.router.ServeHTTP(recorder, req)
|
||||
|
||||
require.Equal(t, http.StatusOK, recorder.Code)
|
||||
requireBodyMatchAccount(t, recorder.Body, account)
|
||||
}
|
||||
|
||||
func randomAccount() db.Account {
|
||||
return db.Account{
|
||||
ID: util.RandomInt(1, 1000),
|
||||
Passwordhash: util.RandomString(250),
|
||||
Firstname: util.RandomUser(),
|
||||
Lastname: util.RandomUser(),
|
||||
Email: util.RandomEmail(),
|
||||
Zip: util.RandomUser(),
|
||||
Street: util.RandomUser(),
|
||||
City: util.RandomUser(),
|
||||
Country: util.RandomUser(),
|
||||
}
|
||||
}
|
||||
|
||||
func requireBodyMatchAccount(t *testing.T, body *bytes.Buffer, account db.Account) {
|
||||
data, err := io.ReadAll(body)
|
||||
require.NoError(t, err)
|
||||
|
||||
var getAccount db.Account
|
||||
err = json.Unmarshal(data, &getAccount)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, account, getAccount)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user