Skip to content

Commit

Permalink
Refactor TestGetWorkspaceBountiesCount To Use A Real Postgres DB For …
Browse files Browse the repository at this point in the history
…The Tes
  • Loading branch information
MahtabBukhari committed Jun 26, 2024
1 parent eea77b7 commit baf8ecb
Showing 1 changed file with 39 additions and 19 deletions.
58 changes: 39 additions & 19 deletions handlers/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import (
"github.com/google/uuid"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
mocks "github.com/stakwork/sphinx-tribes/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestUnitCreateOrEditWorkspace(t *testing.T) {
Expand Down Expand Up @@ -596,34 +594,56 @@ func TestGetWorkspaceBudgetHistory(t *testing.T) {
}

func TestGetWorkspaceBountiesCount(t *testing.T) {
ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
mockDb := mocks.NewDatabase(t)
oHandler := NewWorkspaceHandler(mockDb)
teardownSuite := SetupSuite(t)
defer teardownSuite(t)
oHandler := NewWorkspaceHandler(db.TestDB)

t.Run("should return the count of workspace bounties", func(t *testing.T) {
workspaceUUID := "valid-uuid"
expectedCount := int64(5)

mockDb.On("GetWorkspaceBountiesCount", mock.AnythingOfType("*http.Request"), workspaceUUID).Return(expectedCount).Once()
rr := httptest.NewRecorder()
handler := http.HandlerFunc(oHandler.GetWorkspaceBountiesCount)

expectedCount := int(1)

workspace := db.Workspace{
Uuid: uuid.New().String(),
Name: uuid.New().String(),
OwnerPubKey: uuid.New().String(),
Github: "https://github.com/bounties",
Website: "https://www.bountieswebsite.com",
Description: "Workspace Bounties Description",
}
db.TestDB.CreateOrEditWorkspace(workspace)
bounty := db.NewBounty{
Type: "coding",
Title: "existing bounty",
Description: "existing bounty description",
WorkspaceUuid: workspace.Uuid,
OwnerID: "workspace-user",
Price: 2000,
}

db.TestDB.CreateOrEditBounty(bounty)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", workspaceUUID)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/bounties/"+workspaceUUID+"/count/", nil)
rctx.URLParams.Add("uuid", workspace.Uuid)
ctx := context.WithValue(context.Background(), auth.ContextKey, workspace.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodGet, "/bounties/"+workspace.Uuid+"/count/", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
http.HandlerFunc(oHandler.GetWorkspaceBountiesCount).ServeHTTP(rr, req)
fetchedWorkspace := db.TestDB.GetWorkspaceByUuid(workspace.Uuid)
workspace.ID = fetchedWorkspace.ID

assert.Equal(t, http.StatusOK, rr.Code)
fetchedBounty := db.TestDB.GetWorkspaceBounties(req, bounty.WorkspaceUuid)
bounty.ID = fetchedBounty[0].ID

var count int64
err = json.Unmarshal(rr.Body.Bytes(), &count)
if err != nil {
t.Fatal(err)
}
handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)

assert.Equal(t, expectedCount, count)
assert.Equal(t, expectedCount, len(fetchedBounty))
assert.Equal(t, workspace, fetchedWorkspace)
assert.Equal(t, bounty, fetchedBounty[0])
})
}

0 comments on commit baf8ecb

Please sign in to comment.