forked from ebhomengo/niki
				
			Refactor(benefactor-kindBoxReqs-test): move data to setupTest
Addressed #140 Signed-off-by: Reza Mobaraki <rezam578@gmail.com>
This commit is contained in:
		
							parent
							
								
									ae4854070c
								
							
						
					
					
						commit
						79b054e09f
					
				| 
						 | 
					@ -23,27 +23,59 @@ import (
 | 
				
			||||||
// Define a suite struct that embeds suite.Suite
 | 
					// Define a suite struct that embeds suite.Suite
 | 
				
			||||||
type BenefactorKindBoxReqsTestSuite struct {
 | 
					type BenefactorKindBoxReqsTestSuite struct {
 | 
				
			||||||
	suite.Suite
 | 
						suite.Suite
 | 
				
			||||||
 | 
						benefactorPhone string
 | 
				
			||||||
 | 
						benefactorID    uint
 | 
				
			||||||
 | 
						kindBoxReqID    uint
 | 
				
			||||||
 | 
						getAllExpected  map[string]interface{}
 | 
				
			||||||
 | 
						getExpected     map[string]interface{}
 | 
				
			||||||
 | 
						createData      benefactorkindboxreqparam.KindBoxReqAddRequest
 | 
				
			||||||
 | 
						updateData      benefactorkindboxreqparam.KindBoxReqUpdateRequest
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// SetupTest will run before each test in the suite
 | 
					// SetupTest will run before each test in the suite
 | 
				
			||||||
func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
 | 
					func (suite *BenefactorKindBoxReqsTestSuite) SetupTest() {
 | 
				
			||||||
	teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
 | 
						teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig())
 | 
				
			||||||
	suite.T().Cleanup(teardown)
 | 
						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),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TODO: find better place
 | 
					// TODO: find better place
 | 
				
			||||||
// loginBenefactor utility function
 | 
					// loginBenefactor utility function
 | 
				
			||||||
func (suite *BenefactorKindBoxReqsTestSuite) loginBenefactor() string {
 | 
					func (suite *BenefactorKindBoxReqsTestSuite) loginBenefactor() string {
 | 
				
			||||||
 | 
					 | 
				
			||||||
	phone := "09384664404"
 | 
					 | 
				
			||||||
	sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
 | 
						sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
 | 
				
			||||||
		PhoneNumber: phone,
 | 
							PhoneNumber: suite.benefactorPhone,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		suite.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:      suite.benefactorPhone,
 | 
				
			||||||
		VerificationCode: sendOTPRes.Code,
 | 
							VerificationCode: sendOTPRes.Code,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
| 
						 | 
					@ -72,21 +104,22 @@ func (suite *BenefactorKindBoxReqsTestSuite) createRequest(method, url string, b
 | 
				
			||||||
// Test to get all kind box requests
 | 
					// Test to get all kind box requests
 | 
				
			||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() {
 | 
					func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_GetAll_Success() {
 | 
				
			||||||
	rec := suite.createRequest(http.MethodGet, "/benefactor/kindboxreqs/", nil)
 | 
						rec := suite.createRequest(http.MethodGet, "/benefactor/kindboxreqs/", nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert.Equal(suite.T(), http.StatusOK, rec.Code)
 | 
						assert.Equal(suite.T(), http.StatusOK, rec.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var response benefactorkindboxreqparam.GetAllResponse
 | 
						var response benefactorkindboxreqparam.GetAllResponse
 | 
				
			||||||
	err := json.NewDecoder(rec.Body).Decode(&response)
 | 
						err := json.NewDecoder(rec.Body).Decode(&response)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		suite.T().Fatalf("failed to decode response body: %s", err)
 | 
							suite.T().Fatalf("failed to decode response body: %s", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	assert.Equal(suite.T(), 5, len(response.AllKindBoxReq))
 | 
						assert.Equal(suite.T(), suite.getAllExpected["count"], len(response.AllKindBoxReq))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Test to get a specific kind box request by ID
 | 
					// Test to get a specific kind box request by ID
 | 
				
			||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
 | 
					func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Success() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var kindBoxReqID uint = 1
 | 
						rec := suite.createRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.benefactorID), nil)
 | 
				
			||||||
	rec := suite.createRequest(http.MethodGet, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), nil)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert.Equal(suite.T(), http.StatusOK, rec.Code)
 | 
						assert.Equal(suite.T(), http.StatusOK, rec.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -96,31 +129,21 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Get_Succe
 | 
				
			||||||
		suite.T().Fatalf("failed to decode response body: %s", err)
 | 
							suite.T().Fatalf("failed to decode response body: %s", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert.Equal(suite.T(), kindBoxReqID, response.KindBoxReq.ID)
 | 
						assert.Equal(suite.T(), suite.benefactorID, response.KindBoxReq.ID)
 | 
				
			||||||
	assert.Equal(suite.T(), entity.KindBoxOnTable, response.KindBoxReq.KindBoxType)
 | 
						assert.Equal(suite.T(), suite.getExpected["kind_box_type"], response.KindBoxReq.KindBoxType)
 | 
				
			||||||
	assert.Equal(suite.T(), uint(1), response.KindBoxReq.DeliverAddressID)
 | 
						assert.Equal(suite.T(), suite.getExpected["deliver_address"], response.KindBoxReq.DeliverAddressID)
 | 
				
			||||||
	expectedDate := time.Now().AddDate(0, 0, 7)
 | 
						assert.Equal(
 | 
				
			||||||
	assert.Equal(suite.T(), expectedDate.Format("2006-01-02"), response.KindBoxReq.DeliverReferDate.Format("2006-01-02"))
 | 
							suite.T(),
 | 
				
			||||||
	assert.Equal(suite.T(), uint(1), response.KindBoxReq.DeliverReferTimeID)
 | 
							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)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Test to create a kind box request
 | 
					// Test to create a kind box request
 | 
				
			||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Success() {
 | 
					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{
 | 
						rec := suite.createRequest(http.MethodPost, "/benefactor/kindboxreqs/", suite.createData)
 | 
				
			||||||
		KindBoxType:        entity.KindBoxOnTable,
 | 
					 | 
				
			||||||
		DeliverAddressID:   1,
 | 
					 | 
				
			||||||
		DeliverReferDate:   time.Date(2025, time.January, 2, 15, 4, 5, 0, time.UTC),
 | 
					 | 
				
			||||||
		DeliverReferTimeID: 1,
 | 
					 | 
				
			||||||
		CountRequested:     2,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	rec := suite.createRequest(http.MethodPost, "/benefactor/kindboxreqs/", newKindBox)
 | 
					 | 
				
			||||||
	assert.Equal(suite.T(), http.StatusCreated, rec.Code)
 | 
						assert.Equal(suite.T(), http.StatusCreated, rec.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var response benefactorkindboxreqparam.KindBoxReqAddResponse
 | 
						var response benefactorkindboxreqparam.KindBoxReqAddResponse
 | 
				
			||||||
| 
						 | 
					@ -128,52 +151,56 @@ func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Create_Su
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		suite.T().Fatalf("failed to decode response body: %s", err)
 | 
							suite.T().Fatalf("failed to decode response body: %s", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	assert.Equal(suite.T(), newKindBox.KindBoxType, response.KindBoxReq.KindBoxType)
 | 
						assert.Equal(suite.T(), suite.createData.KindBoxType, response.KindBoxReq.KindBoxType)
 | 
				
			||||||
	assert.Equal(suite.T(), newKindBox.DeliverAddressID, response.KindBoxReq.DeliverAddressID)
 | 
						assert.Equal(suite.T(), suite.createData.DeliverAddressID, response.KindBoxReq.DeliverAddressID)
 | 
				
			||||||
	assert.Equal(suite.T(), newKindBox.DeliverReferDate, response.KindBoxReq.DeliverReferDate)
 | 
						assert.Equal(suite.T(), suite.createData.DeliverReferDate, response.KindBoxReq.DeliverReferDate)
 | 
				
			||||||
	assert.Equal(suite.T(), newKindBox.DeliverReferTimeID, response.KindBoxReq.DeliverReferTimeID)
 | 
						assert.Equal(
 | 
				
			||||||
	assert.Equal(suite.T(), newKindBox.CountRequested, response.KindBoxReq.CountRequested)
 | 
							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.createData.CountRequested, response.KindBoxReq.CountRequested)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Test to update a kind box request
 | 
					// Test to update a kind box request
 | 
				
			||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
 | 
					func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Update_Success() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var kindBoxReqID uint = 1
 | 
						rec := suite.createRequest(
 | 
				
			||||||
	now := time.Now().AddDate(0, 0, 7).UTC()
 | 
							http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), suite.updateData,
 | 
				
			||||||
	updatedKindBox := benefactorkindboxreqparam.KindBoxReqUpdateRequest{
 | 
						)
 | 
				
			||||||
		KindBoxType:        entity.KindBoxCylindrical,
 | 
					 | 
				
			||||||
		CountRequested:     5,
 | 
					 | 
				
			||||||
		Description:        "updated description",
 | 
					 | 
				
			||||||
		DeliverReferTimeID: 1,
 | 
					 | 
				
			||||||
		DeliverReferDate:   now,
 | 
					 | 
				
			||||||
		DeliverAddressID:   1,
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	rec := suite.createRequest(http.MethodPut, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), updatedKindBox)
 | 
					 | 
				
			||||||
	assert.Equal(suite.T(), http.StatusNoContent, rec.Code)
 | 
						assert.Equal(suite.T(), http.StatusNoContent, rec.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(), benefactorkindboxreqparam.KindBoxReqGetRequest{
 | 
						kindBoxReq, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
 | 
				
			||||||
		BenefactorID: 1,
 | 
							benefactorkindboxreqparam.KindBoxReqGetRequest{
 | 
				
			||||||
		KindBoxReqID: kindBoxReqID,
 | 
								BenefactorID: suite.benefactorID,
 | 
				
			||||||
	})
 | 
								KindBoxReqID: suite.kindBoxReqID,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		suite.T().Fatalf("failed to get kind box request: %s", err)
 | 
							suite.T().Fatalf("failed to get kind box request: %s", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	assert.Equal(suite.T(), updatedKindBox.KindBoxType, kindBoxReq.KindBoxType)
 | 
						assert.Equal(suite.T(), suite.updateData.KindBoxType, kindBoxReq.KindBoxType)
 | 
				
			||||||
	assert.Equal(suite.T(), updatedKindBox.CountRequested, kindBoxReq.CountRequested)
 | 
						assert.Equal(suite.T(), suite.updateData.CountRequested, kindBoxReq.CountRequested)
 | 
				
			||||||
	assert.Equal(suite.T(), updatedKindBox.Description, kindBoxReq.Description)
 | 
						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, kindBoxReq.DeliverReferDate)
 | 
				
			||||||
 | 
						assert.Equal(suite.T(), suite.updateData.DeliverAddressID, kindBoxReq.DeliverAddressID)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
 | 
					func (suite *BenefactorKindBoxReqsTestSuite) TestBenefactorKindBoxReqs_Delete_Success() {
 | 
				
			||||||
	var kindBoxReqID uint = 1
 | 
						rec := suite.createRequest(
 | 
				
			||||||
	rec := suite.createRequest(http.MethodDelete, fmt.Sprintf("/benefactor/kindboxreqs/%d", kindBoxReqID), nil)
 | 
							http.MethodDelete, fmt.Sprintf("/benefactor/kindboxreqs/%d", suite.kindBoxReqID), nil,
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
	assert.Equal(suite.T(), http.StatusOK, rec.Code)
 | 
						assert.Equal(suite.T(), http.StatusOK, rec.Code)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(), benefactorkindboxreqparam.KindBoxReqGetRequest{
 | 
						_, err := services.BenefactorKindBoxReqSvc.Get(context.Background(),
 | 
				
			||||||
		BenefactorID: 1,
 | 
							benefactorkindboxreqparam.KindBoxReqGetRequest{
 | 
				
			||||||
		KindBoxReqID: kindBoxReqID,
 | 
								BenefactorID: suite.benefactorID,
 | 
				
			||||||
	})
 | 
								KindBoxReqID: suite.kindBoxReqID,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
	// TODO: Fix to assert equal to errmsg.ErrorMsgNotFound
 | 
						// TODO: Fix to assert equal to errmsg.ErrorMsgNotFound
 | 
				
			||||||
	assert.Error(suite.T(), err)
 | 
						assert.Error(suite.T(), err)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue