From 92720e143fe5ebe51fec11d09e368413b759db37 Mon Sep 17 00:00:00 2001 From: Reza Mobaraki Date: Mon, 2 Sep 2024 21:55:16 +0330 Subject: [PATCH] add benefactor kind box tests Signed-off-by: Reza Mobaraki --- .../end2end/benefactor_address_test.go | 8 +- .../end2end/benefactor_kindboxes_test.go | 119 ++++++++++++++++++ 2 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 delivery/http_server/end2end/benefactor_kindboxes_test.go diff --git a/delivery/http_server/end2end/benefactor_address_test.go b/delivery/http_server/end2end/benefactor_address_test.go index 2fbeadb..6b6b7e3 100644 --- a/delivery/http_server/end2end/benefactor_address_test.go +++ b/delivery/http_server/end2end/benefactor_address_test.go @@ -28,6 +28,10 @@ type BenefactorAddressTestSuit struct { teardown func() } +func TestBenefactorAddressTestSuit(t *testing.T) { + suite.Run(t, new(BenefactorAddressTestSuit)) +} + // SetupTest runs before each test in the suite func (suite *BenefactorAddressTestSuit) SetupTest() { teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig()) @@ -68,10 +72,6 @@ func (suite *BenefactorAddressTestSuit) SetupTest() { } } -func TestBenefactorAddressTestSuit(t *testing.T) { - suite.Run(t, new(BenefactorAddressTestSuit)) -} - // TestBenefactorAddressGet tests the GET /address/:id endpoint func (suite *BenefactorAddressTestSuit) TestBenefactorAddressGet() { token := LoginBenefactor(suite.benefactorPhone) diff --git a/delivery/http_server/end2end/benefactor_kindboxes_test.go b/delivery/http_server/end2end/benefactor_kindboxes_test.go new file mode 100644 index 0000000..020cb2c --- /dev/null +++ b/delivery/http_server/end2end/benefactor_kindboxes_test.go @@ -0,0 +1,119 @@ +//go:build end2end + +package end2end + +import ( + "encoding/json" + "fmt" + "git.gocasts.ir/ebhomengo/niki/delivery/http_server/end2end/setup" + "git.gocasts.ir/ebhomengo/niki/entity" + benefactorkindboxparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box" + "github.com/stretchr/testify/suite" + "net/http" + "testing" + "time" +) + +type BenefactorKindBoxTestSuite struct { + suite.Suite + benefactorPhone string + benefactorID uint + kindBoxID uint + kindBboxGetAllExpected map[string]interface{} + kindBoxGetExpected benefactorkindboxparam.KindBoxGetResponse +} + +func TestBenefactorKindBoxTestSuite(t *testing.T) { + suite.Run(t, new(BenefactorKindBoxTestSuite)) +} + +func (suite *BenefactorKindBoxTestSuite) SetupTest() { + teardown := setup.SeedMariaDB(testContainer.GetMariaDBConfig()) + suite.T().Cleanup(teardown) + suite.benefactorPhone = "09384664404" + suite.kindBoxID = 1 + suite.kindBboxGetAllExpected = map[string]interface{}{ + "count": 1, + } + /* + "ID" : 1, + "KindBoxReqID" : 1, + "BenefactorID" : 1, + "KindBoxType" : "on-table", + "Amount" : 0, + "SerialNumber" : "serial-1", + "Status" : "delivered", + "DeliverReferTimeID" : 1, + "DeliverReferDate" : "2024-08-26T18:19:26Z", + "DeliverAddressID" : 1, + "SenderAgentID" : 1, + "DeliveredAt" : "2024-09-02T18:19:26Z", + "ReturnReferTimeID" : 0, + "ReturnReferDate" : "0001-01-01T00:00:00Z", + "ReturnAddressID" : 0, + "ReceiverAgentID" : 0, + "ReturnedAt" : "0001-01-01T00:00:00Z" + */ + suite.kindBoxGetExpected = benefactorkindboxparam.KindBoxGetResponse{ + KindBox: entity.KindBox{ + ID: suite.kindBoxID, + KindBoxReqID: 1, + BenefactorID: 1, + KindBoxType: "on-table", + Amount: 0, + SerialNumber: "serial-1", + Status: "delivered", + DeliverReferTimeID: 1, + DeliverReferDate: time.Now().AddDate(0, 0, 7).UTC(), + DeliverAddressID: 1, + SenderAgentID: 1, + DeliveredAt: time.Now().AddDate(0, 0, 7).UTC(), + ReturnReferTimeID: 0, + ReturnReferDate: time.Time{}, + ReturnAddressID: 0, + ReceiverAgentID: 0, + ReturnedAt: time.Time{}, + }, + } + +} + +func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGetAll() { + token := LoginBenefactor(suite.benefactorPhone) + url := fmt.Sprintf("/benefactor/kindboxes") + responseRecord := CreateRequest("GET", url, token, nil) + suite.Require().Equal(http.StatusOK, responseRecord.Code) + + var response benefactorkindboxparam.KindBoxGetAllResponse + err := json.NewDecoder(responseRecord.Body).Decode(&response) + suite.Require().NoError(err) + suite.Require().Equal(suite.kindBboxGetAllExpected["count"], len(response.AllKindBox)) + +} + +func (suite *BenefactorKindBoxTestSuite) TestBenefactorKindBoxGet() { + token := LoginBenefactor(suite.benefactorPhone) + url := fmt.Sprintf("/benefactor/kindboxes/%d", suite.kindBoxID) + responseRecord := CreateRequest("GET", url, token, nil) + suite.Require().Equal(http.StatusOK, responseRecord.Code) + + var response benefactorkindboxparam.KindBoxGetResponse + err := json.NewDecoder(responseRecord.Body).Decode(&response) + suite.Require().NoError(err) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.ID, response.KindBox.ID) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.KindBoxReqID, response.KindBox.KindBoxReqID) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.BenefactorID, response.KindBox.BenefactorID) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.KindBoxType, response.KindBox.KindBoxType) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.Amount, response.KindBox.Amount) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.SerialNumber, response.KindBox.SerialNumber) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.Status, response.KindBox.Status) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.DeliverReferTimeID, response.KindBox.DeliverReferTimeID) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.DeliverReferDate.Format("2006-01-02"), response.KindBox.DeliverReferDate.Format("2006-01-02")) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.DeliverAddressID, response.KindBox.DeliverAddressID) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.SenderAgentID, response.KindBox.SenderAgentID) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.DeliveredAt.Format("2006-01-02"), response.KindBox.DeliveredAt.Format("2006-01-02")) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.ReturnReferTimeID, response.KindBox.ReturnReferTimeID) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.ReturnReferDate.Format("2006-01-02"), response.KindBox.ReturnReferDate.Format("2006-01-02")) + suite.Require().Equal(suite.kindBoxGetExpected.KindBox.ReturnAddressID, response.KindBox.ReturnAddressID) + +}