forked from ebhomengo/niki
174 lines
6.9 KiB
Go
174 lines
6.9 KiB
Go
//go:build end2end
|
|
|
|
package end2end
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup"
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
benefactorkindboxreqparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// BenefactorKindBoxReqsTestSuite defines the suite for testing Benefactor Kind Box Requests
|
|
type BenefactorKindBoxReqsTestSuite struct {
|
|
suite.Suite
|
|
benefactorPhone string
|
|
benefactorID uint
|
|
kindBoxReqID uint
|
|
getAllExpected map[string]interface{}
|
|
getExpected map[string]interface{}
|
|
createData benefactorkindboxreqparam.KindBoxReqAddRequest
|
|
updateData benefactorkindboxreqparam.KindBoxReqUpdateRequest
|
|
}
|
|
|
|
// TestBenefactorKindBoxReqsTestSuite is the entry point for the test suite
|
|
func TestBenefactorKindBoxReqsTestSuite(t *testing.T) {
|
|
suite.Run(t, new(BenefactorKindBoxReqsTestSuite))
|
|
}
|
|
|
|
// SetupTest runs before each test in the suite
|
|
func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
|
|
teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
|
|
suite.T().Cleanup(teardown)
|
|
|
|
suite.benefactorPhone = "09384664404"
|
|
suite.benefactorID = uint(1)
|
|
suite.kindBoxReqID = uint(1)
|
|
|
|
suite.getAllExpected = map[string]interface{}{
|
|
"count": 5,
|
|
}
|
|
|
|
suite.getExpected = map[string]interface{}{
|
|
"kind_box_type": entity.KindBoxOnTable,
|
|
"deliver_address": uint(1),
|
|
"deliver_refer_date": time.Now().AddDate(0, 0, 7).UTC(),
|
|
"deliver_refer_time": uint(1),
|
|
}
|
|
|
|
suite.createData = benefactorkindboxreqparam.KindBoxReqAddRequest{
|
|
KindBoxType: entity.KindBoxCylindrical,
|
|
DeliverAddressID: uint(1),
|
|
DeliverReferDate: time.Now().AddDate(0, 0, 7).UTC(),
|
|
DeliverReferTimeID: uint(1),
|
|
CountRequested: uint(5),
|
|
}
|
|
|
|
suite.updateData = benefactorkindboxreqparam.KindBoxReqUpdateRequest{
|
|
KindBoxType: entity.KindBoxCylindrical,
|
|
CountRequested: uint(10),
|
|
Description: "updated description",
|
|
DeliverReferTimeID: uint(1),
|
|
DeliverReferDate: time.Now().AddDate(0, 0, 7).UTC(),
|
|
DeliverAddressID: uint(1),
|
|
}
|
|
}
|
|
|
|
// TestBenefactorKindBoxReqs_GetAll_Success tests retrieving all kind box requests
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() {
|
|
token := LoginBenefactor(suite.benefactorPhone)
|
|
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
|
responseRecord := CreateRequest(http.MethodGet, url, token, nil)
|
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
|
|
|
var response benefactorkindboxreqparam.GetAllResponse
|
|
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
|
suite.Require().NoError(err, "failed to decode response body")
|
|
|
|
assert.Equal(suite.T(), suite.getAllExpected["count"], len(response.AllKindBoxReq))
|
|
}
|
|
|
|
// TestBenefactorKindBoxReqs_Get_Success tests retrieving a specific kind box request by ID
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
|
|
token := LoginBenefactor(suite.benefactorPhone)
|
|
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
|
responseRecord := CreateRequest(http.MethodGet, url, token, nil)
|
|
suite.Require().Equal(http.StatusOK, responseRecord.Code)
|
|
|
|
var response benefactorkindboxreqparam.KindBoxReqGetResponse
|
|
err := json.NewDecoder(responseRecord.Body).Decode(&response)
|
|
suite.Require().NoError(err, "failed to decode response body")
|
|
|
|
assert.Equal(suite.T(), suite.kindBoxReqID, response.KindBoxReq.ID)
|
|
assert.Equal(suite.T(), suite.getExpected["kind_box_type"], response.KindBoxReq.KindBoxType)
|
|
assert.Equal(suite.T(), suite.getExpected["deliver_address"], response.KindBoxReq.DeliverAddressID)
|
|
assert.Equal(
|
|
suite.T(),
|
|
suite.getExpected["deliver_refer_date"].(time.Time).Format("2006-01-02"),
|
|
response.KindBoxReq.DeliverReferDate.Format("2006-01-02"),
|
|
)
|
|
assert.Equal(suite.T(), suite.getExpected["deliver_refer_time"], response.KindBoxReq.DeliverReferTimeID)
|
|
}
|
|
|
|
// TestBenefactorKindBoxReqs_Create_Success tests creating a kind box request
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() {
|
|
token := LoginBenefactor(suite.benefactorPhone)
|
|
url := fmt.Sprintf("/benefactor/kindboxreqs/")
|
|
rec := CreateRequest(http.MethodPost, url, token, suite.createData)
|
|
suite.Require().Equal(http.StatusCreated, rec.Code)
|
|
|
|
var response benefactorkindboxreqparam.KindBoxReqAddResponse
|
|
err := json.NewDecoder(rec.Body).Decode(&response)
|
|
suite.Require().NoError(err, "failed to decode response body")
|
|
|
|
assert.Equal(suite.T(), suite.createData.KindBoxType, response.KindBoxReq.KindBoxType)
|
|
assert.Equal(suite.T(), suite.createData.DeliverAddressID, response.KindBoxReq.DeliverAddressID)
|
|
assert.Equal(suite.T(), suite.createData.DeliverReferDate, response.KindBoxReq.DeliverReferDate)
|
|
assert.Equal(suite.T(), suite.createData.CountRequested, response.KindBoxReq.CountRequested)
|
|
}
|
|
|
|
// TestBenefactorKindBoxReqs_Update_Success tests updating a kind box request
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
|
|
token := LoginBenefactor(suite.benefactorPhone)
|
|
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
|
rec := CreateRequest(http.MethodPut, url, token, suite.updateData)
|
|
suite.Require().Equal(http.StatusNoContent, rec.Code)
|
|
|
|
kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
|
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
|
BenefactorID: suite.benefactorID,
|
|
KindBoxReqID: suite.kindBoxReqID,
|
|
},
|
|
)
|
|
suite.Require().NoError(err, "failed to get kind box request")
|
|
|
|
assert.Equal(suite.T(), suite.updateData.KindBoxType, kindBoxReq.KindBoxType)
|
|
assert.Equal(suite.T(), suite.updateData.CountRequested, kindBoxReq.CountRequested)
|
|
assert.Equal(suite.T(), suite.updateData.Description, kindBoxReq.Description)
|
|
assert.Equal(suite.T(), suite.updateData.DeliverReferTimeID, kindBoxReq.DeliverReferTimeID)
|
|
assert.Equal(
|
|
suite.T(),
|
|
suite.updateData.DeliverReferDate.Format("2006-01-02"),
|
|
kindBoxReq.DeliverReferDate.Format("2006-01-02"),
|
|
)
|
|
assert.Equal(suite.T(), suite.updateData.DeliverAddressID, kindBoxReq.DeliverAddressID)
|
|
}
|
|
|
|
// TestBenefactorKindBoxReqs_Delete_Success tests deleting a kind box request
|
|
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
|
|
token := LoginBenefactor(suite.benefactorPhone)
|
|
url := fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID)
|
|
rec := CreateRequest(http.MethodDelete, url, token, nil)
|
|
suite.Require().Equal(http.StatusOK, rec.Code)
|
|
|
|
_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
|
|
benefactorkindboxreqparam.KindBoxReqGetRequest{
|
|
BenefactorID: suite.benefactorID,
|
|
KindBoxReqID: suite.kindBoxReqID,
|
|
},
|
|
)
|
|
message, code := httpmsg.Error(err)
|
|
suite.Require().Error(err)
|
|
assert.Equal(suite.T(), http.StatusNotFound, code)
|
|
assert.Equal(suite.T(), errmsg.ErrorMsgNotFound, message)
|
|
}
|