forked from ebhomengo/niki
Chore(benefactor-kindBoxReqs-test): make it cleaner
Signed-off-by: Reza Mobaraki <rezam578@gmail.com>
This commit is contained in:
parent
6c78c8098a
commit
15f37001db
|
@ -38,28 +38,36 @@ func loginBenefactor(t *testing.T) string {
|
||||||
return registerRes.Tokens.AccessToken
|
return registerRes.Tokens.AccessToken
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Utility function to create and send HTTP requests
|
||||||
|
func createRequest(t *testing.T, method, url string, body interface{}) *httptest.ResponseRecorder {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
if body != nil {
|
||||||
|
if err := json.NewEncoder(&buf).Encode(body); err != nil {
|
||||||
|
t.Fatalf("could not encode body: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
token := loginBenefactor(t)
|
||||||
|
req := httptest.NewRequest(method, url, &buf)
|
||||||
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||||
|
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token))
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
testServer.Serve(rec, req)
|
||||||
|
return rec
|
||||||
|
}
|
||||||
|
|
||||||
func TestBenefactorKindBoxReqs_GetAll_Success(t *testing.T) {
|
func TestBenefactorKindBoxReqs_GetAll_Success(t *testing.T) {
|
||||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
||||||
t.Cleanup(teardown)
|
t.Cleanup(teardown)
|
||||||
|
|
||||||
token := fmt.Sprintf("Bearer %s", loginBenefactor(t))
|
rec := createRequest(t, http.MethodGet, "/benefactor/kindboxreqs/", nil)
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/benefactor/kindboxreqs/", nil)
|
|
||||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
||||||
req.Header.Set(echo.HeaderAuthorization, token)
|
|
||||||
rec := httptest.NewRecorder()
|
|
||||||
testServer.Serve(rec, req)
|
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
assert.Equal(t, http.StatusOK, rec.Code)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBenefactorKindBoxReqs_Create_Success(t *testing.T) {
|
func TestBenefactorKindBoxReqs_Create_Success(t *testing.T) {
|
||||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
||||||
t.Cleanup(teardown)
|
t.Cleanup(teardown)
|
||||||
|
|
||||||
token := fmt.Sprintf("Bearer %s", loginBenefactor(t))
|
|
||||||
|
|
||||||
newKindBox := benefactorkindboxreqparam.KindBoxReqAddRequest{
|
newKindBox := benefactorkindboxreqparam.KindBoxReqAddRequest{
|
||||||
KindBoxType: entity.KindBoxOnTable,
|
KindBoxType: entity.KindBoxOnTable,
|
||||||
DeliverAddressID: 1,
|
DeliverAddressID: 1,
|
||||||
|
@ -68,22 +76,12 @@ func TestBenefactorKindBoxReqs_Create_Success(t *testing.T) {
|
||||||
CountRequested: 2,
|
CountRequested: 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
requestBody, err := json.Marshal(newKindBox)
|
rec := createRequest(t, http.MethodPost, "/benefactor/kindboxreqs/", newKindBox)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to marshal request body: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPost, "/benefactor/kindboxreqs/", bytes.NewBuffer(requestBody))
|
|
||||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
|
||||||
req.Header.Set(echo.HeaderAuthorization, token)
|
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
|
||||||
testServer.Serve(rec, req)
|
|
||||||
|
|
||||||
assert.Equal(t, http.StatusCreated, rec.Code)
|
assert.Equal(t, http.StatusCreated, rec.Code)
|
||||||
|
|
||||||
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
||||||
err = json.NewDecoder(rec.Body).Decode(&response)
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to decode response body: %s", err)
|
t.Fatalf("failed to decode response body: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -122,3 +120,39 @@ func TestBenefactorKindBoxReqs_Get_Success(t *testing.T) {
|
||||||
assert.Equal(t, expectedDate.Format("2006-01-02"), response.KindBoxReq.DeliverReferDate.Format("2006-01-02"))
|
assert.Equal(t, expectedDate.Format("2006-01-02"), response.KindBoxReq.DeliverReferDate.Format("2006-01-02"))
|
||||||
assert.Equal(t, uint(1), response.KindBoxReq.DeliverReferTimeID)
|
assert.Equal(t, uint(1), response.KindBoxReq.DeliverReferTimeID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBenefactorKindBoxReqs_Update_Success(t *testing.T) {
|
||||||
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
||||||
|
t.Cleanup(teardown)
|
||||||
|
|
||||||
|
token := fmt.Sprintf("Bearer %s", loginBenefactor(t))
|
||||||
|
|
||||||
|
var kindBoxReqID uint = 1
|
||||||
|
updatedKindBox := benefactorkindboxreqparam.KindBoxReqUpdateRequest{
|
||||||
|
KindBoxType: entity.KindBoxOnTable,
|
||||||
|
DeliverAddressID: 2,
|
||||||
|
DeliverReferDate: time.Date(2025, time.January, 2, 15, 4, 5, 0, time.UTC),
|
||||||
|
DeliverReferTimeID: 1,
|
||||||
|
CountRequested: 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
requestBody, err := json.Marshal(updatedKindBox)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal request body: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), bytes.NewBuffer(requestBody))
|
||||||
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||||
|
req.Header.Set(echo.HeaderAuthorization, token)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
testServer.Serve(rec, req)
|
||||||
|
|
||||||
|
var response benefactorkindboxreqparam.KindBoxReqUpdateResponse
|
||||||
|
err = json.NewDecoder(rec.Body).Decode(&response)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to decode response body: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusNoContent, rec.Code)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue