forked from ebhomengo/niki
Chore(benefactor-kindBoxReqs-test): add testify/suit in order to handle
- test Update benefactorKindBoxReqs Signed-off-by: Reza Mobaraki <rezam578@gmail.com>
This commit is contained in:
parent
15f37001db
commit
b132249ffc
|
@ -13,40 +13,52 @@ import (
|
||||||
benefactorkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
benefactorkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func loginBenefactor(t *testing.T) string {
|
// Define a suite struct that embeds suite.Suite
|
||||||
// TODO: Consider mocking the OTP service & fetching the verification code from Redis.
|
type BenefactorKindBoxReqsTestSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetupTest will run before each test in the suite
|
||||||
|
func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
|
||||||
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
||||||
|
suite.T().Cleanup(teardown)
|
||||||
|
}
|
||||||
|
|
||||||
|
// loginBenefactor utility function
|
||||||
|
func (suite *BenefactorKindBoxReqsTestSuite) loginBenefactor() string {
|
||||||
phone := "09384664404"
|
phone := "09384664404"
|
||||||
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
|
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
|
||||||
PhoneNumber: phone,
|
PhoneNumber: phone,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to send OTP: %s", err)
|
suite.T().Fatalf("failed to send OTP: %s", err)
|
||||||
}
|
}
|
||||||
registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), benefactoreparam.LoginOrRegisterRequest{
|
registerRes, err := services.BenefactorSvc.LoginOrRegister(context.Background(), benefactoreparam.LoginOrRegisterRequest{
|
||||||
PhoneNumber: phone,
|
PhoneNumber: phone,
|
||||||
VerificationCode: sendOTPRes.Code,
|
VerificationCode: sendOTPRes.Code,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to login or register: %s", err)
|
suite.T().Fatalf("failed to login or register: %s", err)
|
||||||
}
|
}
|
||||||
return registerRes.Tokens.AccessToken
|
return registerRes.Tokens.AccessToken
|
||||||
}
|
}
|
||||||
|
|
||||||
// Utility function to create and send HTTP requests
|
// Utility function to create and send HTTP requests
|
||||||
func createRequest(t *testing.T, method, url string, body interface{}) *httptest.ResponseRecorder {
|
func (suite *BenefactorKindBoxReqsTestSuite) createRequest(method, url string, body interface{}) *httptest.ResponseRecorder {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if body != nil {
|
if body != nil {
|
||||||
if err := json.NewEncoder(&buf).Encode(body); err != nil {
|
if err := json.NewEncoder(&buf).Encode(body); err != nil {
|
||||||
t.Fatalf("could not encode body: %s", err)
|
suite.T().Fatalf("could not encode body: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
token := loginBenefactor(t)
|
token := suite.loginBenefactor()
|
||||||
req := httptest.NewRequest(method, url, &buf)
|
req := httptest.NewRequest(method, url, &buf)
|
||||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||||
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token))
|
req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", token))
|
||||||
|
@ -55,18 +67,48 @@ func createRequest(t *testing.T, method, url string, body interface{}) *httptest
|
||||||
return rec
|
return rec
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBenefactorKindBoxReqs_GetAll_Success(t *testing.T) {
|
// Test to get all kind box requests
|
||||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() {
|
||||||
t.Cleanup(teardown)
|
rec := suite.createRequest(http.MethodGet, "/benefactor/kindboxreqs/", nil)
|
||||||
|
assert.Equal(suite.T(), http.StatusOK, rec.Code)
|
||||||
|
var response benefactorkindboxreqparam.GetAllResponse
|
||||||
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
||||||
|
if err != nil {
|
||||||
|
suite.T().Fatalf("failed to decode response body: %s", err)
|
||||||
|
}
|
||||||
|
assert.Equal(suite.T(), 5, len(response.AllKindBoxReq))
|
||||||
|
|
||||||
rec := createRequest(t, http.MethodGet, "/benefactor/kindboxreqs/", nil)
|
|
||||||
|
|
||||||
assert.Equal(t, http.StatusOK, rec.Code)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBenefactorKindBoxReqs_Create_Success(t *testing.T) {
|
// Test to get a specific kind box request by ID
|
||||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
|
||||||
t.Cleanup(teardown)
|
|
||||||
|
var kindBoxReqID uint = 1
|
||||||
|
rec := suite.createRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), nil)
|
||||||
|
|
||||||
|
assert.Equal(suite.T(), http.StatusOK, rec.Code)
|
||||||
|
|
||||||
|
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
||||||
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
||||||
|
if err != nil {
|
||||||
|
suite.T().Fatalf("failed to decode response body: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(suite.T(), kindBoxReqID, response.KindBoxReq.ID)
|
||||||
|
assert.Equal(suite.T(), entity.KindBoxOnTable, response.KindBoxReq.KindBoxType)
|
||||||
|
assert.Equal(suite.T(), uint(1), response.KindBoxReq.DeliverAddressID)
|
||||||
|
expectedDate := time.Now().AddDate(0, 0, 7)
|
||||||
|
assert.Equal(suite.T(), expectedDate.Format("2006-01-02"), response.KindBoxReq.DeliverReferDate.Format("2006-01-02"))
|
||||||
|
assert.Equal(suite.T(), uint(1), response.KindBoxReq.DeliverReferTimeID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test to create a kind box request
|
||||||
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() {
|
||||||
|
// TODO : fix constraint with address_id
|
||||||
|
// panic: can't rollback migrations: Error 1451 (23000):
|
||||||
|
// Cannot delete or update a parent row: a foreign key constraint fails
|
||||||
|
// (mysql.kind_box_reqs, CONSTRAINT kind_box_reqs_ibfk_3 FOREIGN KEY (deliver_address_id) REFERENCES addresses (id))
|
||||||
|
// handling 6_create-address-for-benefactor1.sql
|
||||||
|
|
||||||
newKindBox := benefactorkindboxreqparam.KindBoxReqAddRequest{
|
newKindBox := benefactorkindboxreqparam.KindBoxReqAddRequest{
|
||||||
KindBoxType: entity.KindBoxOnTable,
|
KindBoxType: entity.KindBoxOnTable,
|
||||||
|
@ -76,83 +118,65 @@ func TestBenefactorKindBoxReqs_Create_Success(t *testing.T) {
|
||||||
CountRequested: 2,
|
CountRequested: 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
rec := createRequest(t, http.MethodPost, "/benefactor/kindboxreqs/", newKindBox)
|
rec := suite.createRequest(http.MethodPost, "/benefactor/kindboxreqs/", newKindBox)
|
||||||
|
assert.Equal(suite.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)
|
suite.T().Fatalf("failed to decode response body: %s", err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, newKindBox.KindBoxType, response.KindBoxReq.KindBoxType)
|
assert.Equal(suite.T(), newKindBox.KindBoxType, response.KindBoxReq.KindBoxType)
|
||||||
assert.Equal(t, newKindBox.DeliverAddressID, response.KindBoxReq.DeliverAddressID)
|
assert.Equal(suite.T(), newKindBox.DeliverAddressID, response.KindBoxReq.DeliverAddressID)
|
||||||
assert.Equal(t, newKindBox.DeliverReferDate, response.KindBoxReq.DeliverReferDate)
|
assert.Equal(suite.T(), newKindBox.DeliverReferDate, response.KindBoxReq.DeliverReferDate)
|
||||||
assert.Equal(t, newKindBox.DeliverReferTimeID, response.KindBoxReq.DeliverReferTimeID)
|
assert.Equal(suite.T(), newKindBox.DeliverReferTimeID, response.KindBoxReq.DeliverReferTimeID)
|
||||||
assert.Equal(t, newKindBox.CountRequested, response.KindBoxReq.CountRequested)
|
assert.Equal(suite.T(), newKindBox.CountRequested, response.KindBoxReq.CountRequested)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBenefactorKindBoxReqs_Get_Success(t *testing.T) {
|
// Test to update a kind box request
|
||||||
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
|
||||||
t.Cleanup(teardown)
|
|
||||||
|
|
||||||
token := fmt.Sprintf("Bearer %s", loginBenefactor(t))
|
|
||||||
|
|
||||||
var kindBoxReqID uint = 1
|
|
||||||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), 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)
|
|
||||||
|
|
||||||
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
|
||||||
err := json.NewDecoder(rec.Body).Decode(&response)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to decode response body: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, kindBoxReqID, response.KindBoxReq.ID)
|
|
||||||
assert.Equal(t, entity.KindBoxOnTable, response.KindBoxReq.KindBoxType)
|
|
||||||
assert.Equal(t, uint(1), response.KindBoxReq.DeliverAddressID)
|
|
||||||
expectedDate := time.Now().AddDate(0, 0, 7)
|
|
||||||
assert.Equal(t, expectedDate.Format("2006-01-02"), response.KindBoxReq.DeliverReferDate.Format("2006-01-02"))
|
|
||||||
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
|
var kindBoxReqID uint = 1
|
||||||
|
now := time.Now().AddDate(0, 0, 7).UTC()
|
||||||
updatedKindBox := benefactorkindboxreqparam.KindBoxReqUpdateRequest{
|
updatedKindBox := benefactorkindboxreqparam.KindBoxReqUpdateRequest{
|
||||||
KindBoxType: entity.KindBoxOnTable,
|
KindBoxType: entity.KindBoxCylindrical,
|
||||||
DeliverAddressID: 2,
|
CountRequested: 5,
|
||||||
DeliverReferDate: time.Date(2025, time.January, 2, 15, 4, 5, 0, time.UTC),
|
Description: "updated description",
|
||||||
DeliverReferTimeID: 1,
|
DeliverReferTimeID: 1,
|
||||||
CountRequested: 2,
|
DeliverReferDate: now,
|
||||||
|
DeliverAddressID: 1,
|
||||||
}
|
}
|
||||||
|
rec := suite.createRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), updatedKindBox)
|
||||||
|
assert.Equal(suite.T(), http.StatusNoContent, rec.Code)
|
||||||
|
|
||||||
requestBody, err := json.Marshal(updatedKindBox)
|
kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(), benefactorkindboxreqparam.KindBoxReqGetRequest{
|
||||||
|
BenefactorID: 1,
|
||||||
|
KindBoxReqID: kindBoxReqID,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to marshal request body: %s", err)
|
suite.T().Fatalf("failed to get kind box request: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), bytes.NewBuffer(requestBody))
|
assert.Equal(suite.T(), updatedKindBox.KindBoxType, kindBoxReq.KindBoxType)
|
||||||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
assert.Equal(suite.T(), updatedKindBox.CountRequested, kindBoxReq.CountRequested)
|
||||||
req.Header.Set(echo.HeaderAuthorization, token)
|
assert.Equal(suite.T(), updatedKindBox.Description, kindBoxReq.Description)
|
||||||
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)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
|
||||||
|
var kindBoxReqID uint = 1
|
||||||
|
rec := suite.createRequest(http.MethodDelete, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), nil)
|
||||||
|
assert.Equal(suite.T(), http.StatusOK, rec.Code)
|
||||||
|
|
||||||
|
_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(), benefactorkindboxreqparam.KindBoxReqGetRequest{
|
||||||
|
BenefactorID: 1,
|
||||||
|
KindBoxReqID: kindBoxReqID,
|
||||||
|
})
|
||||||
|
// TODO: Fix to assert equal to errmsg.ErrorMsgNotFound
|
||||||
|
assert.Error(suite.T(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entry point for the test suite
|
||||||
|
func TestBenefactorKindBoxReqsTestSuite(t *testing.T) {
|
||||||
|
suite.Run(t, new(BenefactorKindBoxReqsTestSuite))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue