From 94ab40580581994f4584b30c8db6390d9579bb4b Mon Sep 17 00:00:00 2001 From: AbdulWahab3181 Date: Sat, 22 Jun 2024 00:48:53 +0500 Subject: [PATCH 1/4] Refactored TestGetBountyIndexById UT --- handlers/bounty_test.go | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 84f69e8d9..681020c3b 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -1070,24 +1070,39 @@ func TestGetBountyById(t *testing.T) { } func TestGetBountyIndexById(t *testing.T) { - mockDb := dbMocks.NewDatabase(t) + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + mockHttpClient := mocks.NewHttpClient(t) - bHandler := NewBountyHandler(mockHttpClient, mockDb) + bHandler := NewBountyHandler(mockHttpClient, db.TestDB) t.Run("successful retrieval of bounty by Index ID", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(bHandler.GetBountyIndexById) + now := time.Now().Unix() bounty := db.NewBounty{ - ID: 1, + ID: 1, + Type: "coding", + Title: "Bounty With ID", + Description: "Bounty ID description", + WorkspaceUuid: "", + Assignee: "", + OwnerID: bountyOwner.OwnerPubKey, + Show: true, + Created: now, } - rctx := chi.NewRouteContext() - rctx.URLParams.Add("bountyId", strconv.Itoa(int(bounty.ID))) - req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/index/1", nil) + db.TestDB.CreateOrEditBounty(bounty) + + bountyInDb, err := db.TestDB.GetBountyByCreated(uint(bounty.Created)) + assert.Equal(t, bounty, bountyInDb) assert.NoError(t, err) - mockDb.On("GetBountyIndexById", "1").Return(int64(12), nil).Once() + rctx := chi.NewRouteContext() + rctx.URLParams.Add("bountyId", strconv.Itoa(int(bountyInDb.ID))) + req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/index/"+strconv.Itoa(int(bountyInDb.ID)), nil) + assert.NoError(t, err) handler.ServeHTTP(rr, req) @@ -1095,11 +1110,9 @@ func TestGetBountyIndexById(t *testing.T) { responseString := strings.TrimSpace(string(responseBody)) returnedIndex, err := strconv.Atoi(responseString) assert.NoError(t, err) - assert.Equal(t, 12, returnedIndex) + assert.Equal(t, 1, returnedIndex) assert.Equal(t, http.StatusOK, rr.Code) - - mockDb.AssertExpectations(t) }) t.Run("bounty index by ID not found", func(t *testing.T) { @@ -1112,12 +1125,8 @@ func TestGetBountyIndexById(t *testing.T) { req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/index/"+bountyID, nil) assert.NoError(t, err) - mockDb.On("GetBountyIndexById", bountyID).Return(int64(0), fmt.Errorf("bounty not found")).Once() - handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusNotFound, rr.Code) - - mockDb.AssertExpectations(t) }) } From f34b7f2331f8625b55d1a71ed9bc8370da5b764a Mon Sep 17 00:00:00 2001 From: AbdulWahab3181 Date: Sat, 22 Jun 2024 00:55:50 +0500 Subject: [PATCH 2/4] typo changes --- handlers/bounty_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 681020c3b..cf7bd98e4 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -1085,7 +1085,7 @@ func TestGetBountyIndexById(t *testing.T) { ID: 1, Type: "coding", Title: "Bounty With ID", - Description: "Bounty ID description", + Description: "Bounty description", WorkspaceUuid: "", Assignee: "", OwnerID: bountyOwner.OwnerPubKey, From ce8c6762b40675b02edc0a87af97fc0e5f06cce0 Mon Sep 17 00:00:00 2001 From: AbdulWahab3181 Date: Sat, 22 Jun 2024 01:36:51 +0500 Subject: [PATCH 3/4] added return in case bounty id not found --- handlers/bounty.go | 1 + 1 file changed, 1 insertion(+) diff --git a/handlers/bounty.go b/handlers/bounty.go index 2d4e774ec..245542d78 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -113,6 +113,7 @@ func (h *bountyHandler) GetBountyIndexById(w http.ResponseWriter, r *http.Reques bountyId := chi.URLParam(r, "bountyId") if bountyId == "" { w.WriteHeader(http.StatusNotFound) + return } bountyIndex := h.db.GetBountyIndexById(bountyId) w.WriteHeader(http.StatusOK) From efdc14a9aa69ae20b1ac01bc17afe1d99d142e35 Mon Sep 17 00:00:00 2001 From: AbdulWahab3181 Date: Sat, 22 Jun 2024 01:56:21 +0500 Subject: [PATCH 4/4] fixed text assertion --- handlers/bounty_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index cf7bd98e4..e07de5ef1 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -1099,6 +1099,8 @@ func TestGetBountyIndexById(t *testing.T) { assert.Equal(t, bounty, bountyInDb) assert.NoError(t, err) + bountyIndex := db.TestDB.GetBountyIndexById(strconv.Itoa(int(bountyInDb.ID))) + rctx := chi.NewRouteContext() rctx.URLParams.Add("bountyId", strconv.Itoa(int(bountyInDb.ID))) req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/index/"+strconv.Itoa(int(bountyInDb.ID)), nil) @@ -1108,9 +1110,9 @@ func TestGetBountyIndexById(t *testing.T) { responseBody := rr.Body.Bytes() responseString := strings.TrimSpace(string(responseBody)) - returnedIndex, err := strconv.Atoi(responseString) + returnedIndex, err := strconv.ParseInt(responseString, 10, 64) assert.NoError(t, err) - assert.Equal(t, 1, returnedIndex) + assert.Equal(t, bountyIndex, returnedIndex) assert.Equal(t, http.StatusOK, rr.Code) })