hiling_go/api/test/location_test.go

167 lines
4.5 KiB
Go
Raw Permalink Normal View History

package api_test
import (
2023-09-14 13:49:03 +07:00
"bytes"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
2023-09-14 13:49:03 +07:00
"time"
2023-09-14 13:49:03 +07:00
mockdb "git.nochill.in/nochill/hiling_go/db/mock"
db "git.nochill.in/nochill/hiling_go/db/sqlc"
"git.nochill.in/nochill/hiling_go/util"
2024-02-06 11:55:25 +07:00
"github.com/jackc/pgx/v5/pgtype"
2023-09-14 13:49:03 +07:00
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
2023-09-14 13:49:03 +07:00
func TestGetLocationsAPI(t *testing.T) {
location := randomLocation()
testCases := []struct {
name string
id int32
buildStubs func(store *mockdb.MockStore)
checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder)
}{
{
name: "OK",
id: location.ID,
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetLocation(gomock.Any(), gomock.Eq(location.ID)).
Times(1).
Return(location, nil)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, 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 := newTestServer(t, store)
recorder := httptest.NewRecorder()
url := fmt.Sprintf("/location/%d", tc.id)
request, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)
server.Router.ServeHTTP(recorder, request)
tc.checkResponse(t, recorder)
})
}
}
func TestCreateLocationAPI(t *testing.T) {
2023-09-14 13:49:03 +07:00
location := db.CreateLocationParams{
Address: util.RandomString(10),
Name: util.RandomString(10),
2023-09-14 13:49:03 +07:00
SubmittedBy: 1,
RegencyID: 1305,
2024-02-06 11:55:25 +07:00
GoogleMapsLink: pgtype.Text{Valid: true, String: util.RandomString(10)},
}
2023-09-14 13:49:03 +07:00
testCases := []struct {
name string
body db.CreateLocationParams
buildStubs func(store *mockdb.MockStore)
checkResponse func(t *testing.T, recorder *httptest.ResponseRecorder)
}{
{
name: "OK",
body: location,
buildStubs: func(store *mockdb.MockStore) {
arg := db.CreateLocationParams{
Address: location.Address,
Name: location.Name,
SubmittedBy: location.SubmittedBy,
RegencyID: location.RegencyID,
GoogleMapsLink: location.GoogleMapsLink,
}
store.EXPECT().
CreateLocation(gomock.Any(), gomock.Eq(arg)).
Times(1).
Return(nil)
},
checkResponse: func(t *testing.T, recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recorder.Code)
},
},
{
name: "Bad Request",
body: db.CreateLocationParams{},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
CreateLocation(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) {
bodyBuffer := new(bytes.Buffer)
mw := multipart.NewWriter(bodyBuffer)
mw.WriteField("address", tc.body.Address)
mw.WriteField("name", tc.body.Name)
mw.WriteField("submitted_by", fmt.Sprintf("%d", tc.body.SubmittedBy))
mw.WriteField("regency_id", fmt.Sprintf("%d", tc.body.RegencyID))
mw.WriteField("google_maps_link", tc.body.GoogleMapsLink.String)
mw.Close()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
2023-09-14 13:49:03 +07:00
store := mockdb.NewMockStore(ctrl)
tc.buildStubs(store)
server := newTestServer(t, store)
recorder := httptest.NewRecorder()
url := "/locations"
request, err := http.NewRequest(http.MethodPost, url, bodyBuffer)
request.Header.Set("Content-Type", mw.FormDataContentType())
require.NoError(t, err)
server.Router.ServeHTTP(recorder, request)
tc.checkResponse(t, recorder)
})
}
}
func randomLocation() db.Location {
return db.Location{
ID: int32(util.RandomInt(1, 20)),
Address: util.RandomString(10),
Name: util.RandomString(10),
2024-02-06 11:55:25 +07:00
GoogleMapsLink: pgtype.Text{Valid: true, String: util.RandomString(20)},
2023-09-14 13:49:03 +07:00
SubmittedBy: 1,
2024-02-06 11:55:25 +07:00
TotalVisited: pgtype.Int4{Valid: true, Int32: int32(util.RandomInt(0, 32))},
Thumbnail: pgtype.Text{Valid: false, String: ""},
2023-09-14 13:49:03 +07:00
RegencyID: 1305,
2023-10-03 16:14:58 +07:00
IsDeleted: false,
2024-02-06 11:55:25 +07:00
CreatedAt: pgtype.Timestamp{Valid: true, Time: time.Now()},
UpdatedAt: pgtype.Timestamp{Valid: true, Time: time.Now()},
ApprovedBy: pgtype.Int4{Valid: true, Int32: 1},
ApprovedAt: pgtype.Timestamp{Valid: true, Time: time.Now()},
2023-09-14 13:49:03 +07:00
}
}