Skip to content

Commit

Permalink
Merge pull request #1725 from AbdulWahab3181/Refactor-TestGetBountyIn…
Browse files Browse the repository at this point in the history
…dexById-UT

Refactor TestGetBountyIndexById To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty authored Jun 21, 2024
2 parents dbc0c7d + efdc14a commit 1f27f1c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions handlers/bounty.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 26 additions & 15 deletions handlers/bounty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,36 +1070,51 @@ 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 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()
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)
assert.NoError(t, err)

handler.ServeHTTP(rr, req)

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, 12, returnedIndex)
assert.Equal(t, bountyIndex, returnedIndex)

assert.Equal(t, http.StatusOK, rr.Code)

mockDb.AssertExpectations(t)
})

t.Run("bounty index by ID not found", func(t *testing.T) {
Expand All @@ -1112,12 +1127,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)
})
}

Expand Down

0 comments on commit 1f27f1c

Please sign in to comment.