Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TestMetricsBountiesCount To Use A Real Postgres DB For The Test #1796

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 75 additions & 19 deletions handlers/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"net/http"
"net/http/httptest"
"strconv"
"testing"

"fmt"
Expand Down Expand Up @@ -240,9 +241,72 @@ func TestMetricsBounties(t *testing.T) {
}

func TestMetricsBountiesCount(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key")
mockDb := mocks.NewDatabase(t)
mh := NewMetricHandler(mockDb)
mh := NewMetricHandler(db.TestDB)

now := time.Now()
bountyOwner := db.Person{OwnerPubKey: "owner-1"}
db.TestDB.CreateOrEditPerson(bountyOwner)

bounty1 := db.NewBounty{
Type: "coding",
Title: "Bounty With ID 1",
Description: "Bounty ID 1 Description",
WorkspaceUuid: "",
Assignee: "",
OwnerID: bountyOwner.OwnerPubKey,
Show: true,
Created: now.AddDate(0, 0, -30).Unix(),
Paid: true,
}
db.TestDB.CreateOrEditBounty(bounty1)

bounty2 := db.NewBounty{
Type: "coding",
Title: "Bounty With ID 2",
Description: "Bounty ID 2 Description",
WorkspaceUuid: "",
Assignee: "",
OwnerID: bountyOwner.OwnerPubKey,
Show: true,
Created: now.AddDate(0, 0, -20).Unix(),
Paid: true,
}
db.TestDB.CreateOrEditBounty(bounty2)

bounty3 := db.NewBounty{
Type: "coding",
Title: "Bounty With ID 3",
Description: "Bounty ID 3 Description",
WorkspaceUuid: "",
Assignee: "",
OwnerID: bountyOwner.OwnerPubKey,
Show: true,
Created: now.AddDate(0, 0, -10).Unix(),
Paid: false,
}
db.TestDB.CreateOrEditBounty(bounty3)

bounty4 := db.NewBounty{
Type: "coding",
Title: "Bounty With ID 4",
Description: "Bounty ID 4 Description",
WorkspaceUuid: "",
Assignee: "",
OwnerID: bountyOwner.OwnerPubKey,
Show: true,
Created: now.Unix(),
Paid: false,
}
db.TestDB.CreateOrEditBounty(bounty4)

dateRange := db.PaymentDateRange{
StartDate: strconv.FormatInt(bounty1.Created, 10),
EndDate: strconv.FormatInt(bounty4.Created, 10),
}

t.Run("should return error if body is not a valid json", func(t *testing.T) {
rr := httptest.NewRecorder()
Expand Down Expand Up @@ -275,54 +339,46 @@ func TestMetricsBountiesCount(t *testing.T) {
})

t.Run("should fetch bounties count within specified date range", func(t *testing.T) {
db.RedisError = errors.New("redis not initialized")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(mh.MetricsBountiesCount)
dateRange := db.PaymentDateRange{
StartDate: "1111",
EndDate: "2222",
}

body, _ := json.Marshal(dateRange)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/boutnies/count", bytes.NewReader(body))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/bounties/count", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}

mockDb.On("GetBountiesByDateRangeCount", dateRange, req).Return(int64(100)).Once()
handler.ServeHTTP(rr, req)

var res int64
_ = json.Unmarshal(rr.Body.Bytes(), &res)

expectedCount := db.TestDB.GetBountiesByDateRangeCount(dateRange, req)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, res, int64(100))
assert.Equal(t, expectedCount, res)
})

t.Run("should fetch bounties count within specified date range for selected providers", func(t *testing.T) {
db.RedisError = errors.New("redis not initialized")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(mh.MetricsBountiesCount)
dateRange := db.PaymentDateRange{
StartDate: "1111",
EndDate: "2222",
}

body, _ := json.Marshal(dateRange)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/boutnies/count", bytes.NewReader(body))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/bounties/count", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}

// Provide provider IDs in the request query parameters
req.URL.RawQuery = "provider=provider1"
req.URL.RawQuery = "provider=owner-1"

mockDb.On("GetBountiesByDateRangeCount", dateRange, req).Return(int64(50)).Once()
handler.ServeHTTP(rr, req)

var res int64
_ = json.Unmarshal(rr.Body.Bytes(), &res)

expectedCount := db.TestDB.GetBountiesByDateRangeCount(dateRange, req)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, res, int64(50))
assert.Equal(t, expectedCount, res)
})
}

Expand Down
1 change: 0 additions & 1 deletion handlers/tribes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/lib/pq"
"github.com/stakwork/sphinx-tribes/auth"
"github.com/stakwork/sphinx-tribes/db"
mocks "github.com/stakwork/sphinx-tribes/mocks"
"github.com/stretchr/testify/assert"
)

Expand Down
Loading