forked from ebhomengo/niki
1
0
Fork 0

Chore(e2e.benefactor-address-test): some changes

Signed-off-by: Reza Mobaraki <rezam578@gmail.com>
This commit is contained in:
Reza Mobaraki 2024-08-26 16:42:54 +03:30
parent 17d3502854
commit d8bf950a89
No known key found for this signature in database
GPG Key ID: 922CBCF25B541A6F
1 changed files with 29 additions and 16 deletions

View File

@ -15,6 +15,7 @@ import (
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"math"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -27,9 +28,9 @@ type BenefactorAddressTestSuit struct {
addressID uint addressID uint
getAllExpected map[string]interface{} getAllExpected map[string]interface{}
getExpected addressparam.GetAddressResponse getExpected addressparam.GetAddressResponse
// TODO: naming of address params request should be fix and follow the same pattern
createData addressparam.BenefactorAddAddressRequest createData addressparam.BenefactorAddAddressRequest
updateData addressparam.UpdateAddressRequest updateData addressparam.UpdateAddressRequest
teardown func()
} }
func (suite *BenefactorAddressTestSuit) SetupSuite() { func (suite *BenefactorAddressTestSuit) SetupSuite() {
@ -72,13 +73,15 @@ func (suite *BenefactorAddressTestSuit) SetupSuite() {
} }
} }
func (suite *BenefactorAddressTestSuit) TearDownSuite() {
suite.teardown()
}
func TestBenefactorAddressTestSuit(t *testing.T) { func TestBenefactorAddressTestSuit(t *testing.T) {
suite.Run(t, new(BenefactorAddressTestSuit)) suite.Run(t, new(BenefactorAddressTestSuit))
} }
// loginBenefactor authenticates the benefactor and returns an access token // loginBenefactor authenticates the benefactor and returns an access token
// TODO: Move this to a common utility function
func (suite *BenefactorAddressTestSuit) loginBenefactor() string { func (suite *BenefactorAddressTestSuit) loginBenefactor() string {
sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{ sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{
PhoneNumber: suite.benefactorPhone, PhoneNumber: suite.benefactorPhone,
@ -95,7 +98,6 @@ func (suite *BenefactorAddressTestSuit) loginBenefactor() string {
} }
// createRequest is a utility function to create and send HTTP requests // createRequest is a utility function to create and send HTTP requests
// TODO: Move this to a common utility function
func (suite *BenefactorAddressTestSuit) createRequest(method, url string, body interface{}) *httptest.ResponseRecorder { func (suite *BenefactorAddressTestSuit) createRequest(method, url string, body interface{}) *httptest.ResponseRecorder {
var buf bytes.Buffer var buf bytes.Buffer
if body != nil { if body != nil {
@ -114,6 +116,7 @@ func (suite *BenefactorAddressTestSuit) createRequest(method, url string, body i
return rec return rec
} }
// TestBenefactorAddressGet tests the GET /address/:id endpoint
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() {
responseRecord := suite.createRequest("GET", fmt.Sprintf("/address/%d", suite.addressID), nil) responseRecord := suite.createRequest("GET", fmt.Sprintf("/address/%d", suite.addressID), nil)
suite.Require().Equal(http.StatusOK, responseRecord.Code) suite.Require().Equal(http.StatusOK, responseRecord.Code)
@ -126,13 +129,14 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() {
suite.Require().Equal(suite.benefactorID, response.Address.BenefactorID) suite.Require().Equal(suite.benefactorID, response.Address.BenefactorID)
suite.Require().Equal(suite.getExpected.Address.PostalCode, response.Address.PostalCode) suite.Require().Equal(suite.getExpected.Address.PostalCode, response.Address.PostalCode)
suite.Require().Equal(suite.getExpected.Address.Address, response.Address.Address) suite.Require().Equal(suite.getExpected.Address.Address, response.Address.Address)
// TODO: Expected:35.632508 , Actual:35.632507 , Fix this and then revise // Fixing floating-point comparison with tolerance
//suite.Require().Equal(suite.getExpected.Address.Lat, response.Address.Lat) suite.Require().True(almostEqual(suite.getExpected.Address.Lat, response.Address.Lat))
//suite.Require().Equal(suite.getExpected.Address.Lon, response.Address.Lon) suite.Require().True(almostEqual(suite.getExpected.Address.Lon, response.Address.Lon))
suite.Require().Equal(suite.getExpected.Address.Name, response.Address.Name) suite.Require().Equal(suite.getExpected.Address.Name, response.Address.Name)
suite.Require().Equal(suite.getExpected.Address.CityID, response.Address.CityID) suite.Require().Equal(suite.getExpected.Address.CityID, response.Address.CityID)
} }
// TestBenefactorAddressGetAll tests the GET /address/ endpoint
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() {
responseRecord := suite.createRequest("GET", "/address/", nil) responseRecord := suite.createRequest("GET", "/address/", nil)
suite.Require().Equal(http.StatusOK, responseRecord.Code) suite.Require().Equal(http.StatusOK, responseRecord.Code)
@ -144,6 +148,7 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() {
suite.Require().Equal(suite.getAllExpected["count"], len(response.AllAddresses)) suite.Require().Equal(suite.getAllExpected["count"], len(response.AllAddresses))
} }
// TestBenefactorAddressCreate tests the POST /address/ endpoint
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() {
responseRecord := suite.createRequest("POST", "/address/", suite.createData) responseRecord := suite.createRequest("POST", "/address/", suite.createData)
suite.Require().Equal(http.StatusCreated, responseRecord.Code) suite.Require().Equal(http.StatusCreated, responseRecord.Code)
@ -157,13 +162,12 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() {
suite.Require().Equal(suite.createData.PostalCode, response.Address.PostalCode) suite.Require().Equal(suite.createData.PostalCode, response.Address.PostalCode)
suite.Require().Equal(suite.createData.Name, response.Address.Name) suite.Require().Equal(suite.createData.Name, response.Address.Name)
suite.Require().Equal(suite.createData.CityID, response.Address.CityID) suite.Require().Equal(suite.createData.CityID, response.Address.CityID)
} }
// TestBenefactorAddressUpdate tests the PUT /address/:id endpoint
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() {
// TODO: check Method is patch, however, all fields are required !! // TODO: check Method is patch, however, all fields are required !!
responseRecord := suite.createRequest("PATCH", fmt.Sprintf("/address/%d", suite.addressID), suite.updateData) responseRecord := suite.createRequest("PATCH", fmt.Sprintf("/address/%d", suite.addressID), suite.updateData)
suite.Require().Equal(http.StatusNoContent, responseRecord.Code) suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
updatedAddress, sErr := services.BenefactorAddressSvc.Get(context.Background(), updatedAddress, sErr := services.BenefactorAddressSvc.Get(context.Background(),
@ -177,15 +181,14 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() {
suite.Require().Equal(suite.updateData.Address, updatedAddress.Address.Address) suite.Require().Equal(suite.updateData.Address, updatedAddress.Address.Address)
suite.Require().Equal(suite.updateData.Name, updatedAddress.Address.Name) suite.Require().Equal(suite.updateData.Name, updatedAddress.Address.Name)
suite.Require().Equal(suite.updateData.CityID, updatedAddress.Address.CityID) suite.Require().Equal(suite.updateData.CityID, updatedAddress.Address.CityID)
// TODO: check Expected: 52.497287, Actual:52.497288, Fix this and then revise // Fixing floating-point comparison with tolerance
//suite.Require().Equal(suite.updateData.Lat, updatedAddress.Address.Lat) suite.Require().True(almostEqual(suite.updateData.Lat, updatedAddress.Address.Lat))
//suite.Require().Equal(suite.updateData.Lon, updatedAddress.Address.Lon) suite.Require().True(almostEqual(suite.updateData.Lon, updatedAddress.Address.Lon))
} }
// TestBenefactorAddressDelete tests the DELETE /address/:id endpoint
func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() { func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() {
responseRecord := suite.createRequest("DELETE", fmt.Sprintf("/address/%d", suite.addressID), nil) responseRecord := suite.createRequest("DELETE", fmt.Sprintf("/address/%d", suite.addressID), nil)
// TODO: all delete operations should return same code StatusNoContent Or StatusOK
suite.Require().Equal(http.StatusNoContent, responseRecord.Code) suite.Require().Equal(http.StatusNoContent, responseRecord.Code)
_, err := services.BenefactorAddressSvc.Get(context.Background(), _, err := services.BenefactorAddressSvc.Get(context.Background(),
@ -200,3 +203,13 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() {
suite.Equal(http.StatusNotFound, code) suite.Equal(http.StatusNotFound, code)
suite.Equal(errmsg.ErrorMsgNotFound, message) suite.Equal(errmsg.ErrorMsgNotFound, message)
} }
// Helper function for floating-point comparison with tolerance
func almostEqual(a, b float64) bool {
const epsilon = 1e-5 // Adjusted tolerance
diff := math.Abs(a - b)
if diff >= epsilon {
fmt.Printf("Debug: Values %f and %f differ by %f, which is greater than epsilon %f\n", a, b, diff, epsilon)
}
return diff < epsilon
}