diff --git a/delivery/http_server/end2end/benefactor_address_test.go b/delivery/http_server/end2end/benefactor_address_test.go index 07a750a..adedc90 100644 --- a/delivery/http_server/end2end/benefactor_address_test.go +++ b/delivery/http_server/end2end/benefactor_address_test.go @@ -15,6 +15,7 @@ import ( httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" "github.com/labstack/echo/v4" "github.com/stretchr/testify/suite" + "math" "net/http" "net/http/httptest" "testing" @@ -27,9 +28,9 @@ type BenefactorAddressTestSuit struct { addressID uint getAllExpected map[string]interface{} getExpected addressparam.GetAddressResponse - // TODO: naming of address params request should be fix and follow the same pattern - createData addressparam.BenefactorAddAddressRequest - updateData addressparam.UpdateAddressRequest + createData addressparam.BenefactorAddAddressRequest + updateData addressparam.UpdateAddressRequest + teardown func() } func (suite *BenefactorAddressTestSuit) SetupSuite() { @@ -72,13 +73,15 @@ func (suite *BenefactorAddressTestSuit) SetupSuite() { } } +func (suite *BenefactorAddressTestSuit) TearDownSuite() { + suite.teardown() +} + func TestBenefactorAddressTestSuit(t *testing.T) { suite.Run(t, new(BenefactorAddressTestSuit)) - } // loginBenefactor authenticates the benefactor and returns an access token -// TODO: Move this to a common utility function func (suite *BenefactorAddressTestSuit) loginBenefactor() string { sendOTPRes, err := services.BenefactorSvc.SendOtp(context.Background(), benefactoreparam.SendOtpRequest{ PhoneNumber: suite.benefactorPhone, @@ -95,7 +98,6 @@ func (suite *BenefactorAddressTestSuit) loginBenefactor() string { } // 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 { var buf bytes.Buffer if body != nil { @@ -114,6 +116,7 @@ func (suite *BenefactorAddressTestSuit) createRequest(method, url string, body i return rec } +// TestBenefactorAddressGet tests the GET /address/:id endpoint func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() { responseRecord := suite.createRequest("GET", fmt.Sprintf("/address/%d", suite.addressID), nil) 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.getExpected.Address.PostalCode, response.Address.PostalCode) suite.Require().Equal(suite.getExpected.Address.Address, response.Address.Address) - // TODO: Expected:35.632508 , Actual:35.632507 , Fix this and then revise - //suite.Require().Equal(suite.getExpected.Address.Lat, response.Address.Lat) - //suite.Require().Equal(suite.getExpected.Address.Lon, response.Address.Lon) + // Fixing floating-point comparison with tolerance + suite.Require().True(almostEqual(suite.getExpected.Address.Lat, response.Address.Lat)) + 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.CityID, response.Address.CityID) } +// TestBenefactorAddressGetAll tests the GET /address/ endpoint func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() { responseRecord := suite.createRequest("GET", "/address/", nil) suite.Require().Equal(http.StatusOK, responseRecord.Code) @@ -144,6 +148,7 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGetAll() { suite.Require().Equal(suite.getAllExpected["count"], len(response.AllAddresses)) } +// TestBenefactorAddressCreate tests the POST /address/ endpoint func (suite *BenefactorAddressTestSuit) TestBenefactorAddressCreate() { responseRecord := suite.createRequest("POST", "/address/", suite.createData) 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.Name, response.Address.Name) suite.Require().Equal(suite.createData.CityID, response.Address.CityID) - } +// TestBenefactorAddressUpdate tests the PUT /address/:id endpoint func (suite *BenefactorAddressTestSuit) TestBenefactorAddressUpdate() { // TODO: check Method is patch, however, all fields are required !! responseRecord := suite.createRequest("PATCH", fmt.Sprintf("/address/%d", suite.addressID), suite.updateData) - suite.Require().Equal(http.StatusNoContent, responseRecord.Code) 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.Name, updatedAddress.Address.Name) suite.Require().Equal(suite.updateData.CityID, updatedAddress.Address.CityID) - // TODO: check Expected: 52.497287, Actual:52.497288, Fix this and then revise - //suite.Require().Equal(suite.updateData.Lat, updatedAddress.Address.Lat) - //suite.Require().Equal(suite.updateData.Lon, updatedAddress.Address.Lon) - + // Fixing floating-point comparison with tolerance + suite.Require().True(almostEqual(suite.updateData.Lat, updatedAddress.Address.Lat)) + suite.Require().True(almostEqual(suite.updateData.Lon, updatedAddress.Address.Lon)) } +// TestBenefactorAddressDelete tests the DELETE /address/:id endpoint func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() { 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) _, err := services.BenefactorAddressSvc.Get(context.Background(), @@ -200,3 +203,13 @@ func (suite *BenefactorAddressTestSuit) TestBenefactorAddressDelete() { suite.Equal(http.StatusNotFound, code) 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 +}