Skip to content

[usage] added a test for ListUsage #12810

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

Merged
merged 1 commit into from
Sep 9, 2022
Merged
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
82 changes: 82 additions & 0 deletions components/usage/pkg/apiv1/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package apiv1
import (
"context"
"database/sql"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -259,3 +260,84 @@ func TestGetAndSetCostCenter(t *testing.T) {
}

}

func TestListUsage(t *testing.T) {
conn := dbtest.ConnectForTests(t)

start := time.Date(2022, 7, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2022, 8, 1, 0, 0, 0, 0, time.UTC)

attributionID := db.NewTeamAttributionID(uuid.New().String())

draftBefore := dbtest.NewUsage(t, db.Usage{
AttributionID: attributionID,
EffectiveTime: db.NewVarcharTime(start.Add(-1 * 23 * time.Hour)),
CreditCents: 100,
Draft: true,
})

nondraftBefore := dbtest.NewUsage(t, db.Usage{
AttributionID: attributionID,
EffectiveTime: db.NewVarcharTime(start.Add(-1 * 23 * time.Hour)),
CreditCents: 200,
Draft: false,
})

draftInside := dbtest.NewUsage(t, db.Usage{
AttributionID: attributionID,
EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)),
CreditCents: 300,
Draft: true,
})
nonDraftInside := dbtest.NewUsage(t, db.Usage{
AttributionID: attributionID,
EffectiveTime: db.NewVarcharTime(start.Add(2 * time.Hour)),
CreditCents: 400,
Draft: false,
})

nonDraftAfter := dbtest.NewUsage(t, db.Usage{
AttributionID: attributionID,
EffectiveTime: db.NewVarcharTime(end.Add(2 * time.Hour)),
CreditCents: 1000,
})

dbtest.CreateUsageRecords(t, conn, draftBefore, nondraftBefore, draftInside, nonDraftInside, nonDraftAfter)

usageService := newUsageService(t, conn)

tests := []struct {
start, end time.Time
// expectations
creditsAtStart float64
creditsAtEnd float64
recordsInRange int64
}{
{start, end, 3, 10, 2},
{end, end, 10, 10, 0},
{start, start, 3, 3, 0},
{start.Add(-200 * 24 * time.Hour), end, 0, 10, 4},
{start.Add(-200 * 24 * time.Hour), end.Add(10 * 24 * time.Hour), 0, 20, 5},
}

for i, test := range tests {
t.Run(fmt.Sprintf("Running test no %d", i+1), func(t *testing.T) {
metaData, err := usageService.ListUsage(context.Background(), &v1.ListUsageRequest{
AttributionId: string(attributionID),
From: timestamppb.New(test.start),
To: timestamppb.New(test.end),
Order: v1.ListUsageRequest_ORDERING_DESCENDING,
Pagination: &v1.PaginatedRequest{
PerPage: 1,
Page: 1,
},
})
require.NoError(t, err)

require.Equal(t, test.creditsAtStart, metaData.CreditBalanceAtStart)
require.Equal(t, test.creditsAtEnd, metaData.CreditBalanceAtEnd)
require.Equal(t, test.recordsInRange, metaData.Pagination.Total)
})
}
Comment on lines +309 to +341
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tests := []struct {
start, end time.Time
// expectations
creditsAtStart float64
creditsAtEnd float64
recordsInRange int64
}{
{start, end, 3, 10, 2},
{end, end, 10, 10, 0},
{start, start, 3, 3, 0},
{start.Add(-200 * 24 * time.Hour), end, 0, 10, 4},
{start.Add(-200 * 24 * time.Hour), end.Add(10 * 24 * time.Hour), 0, 20, 5},
}
for i, test := range tests {
t.Run(fmt.Sprintf("Running test no %d", i+1), func(t *testing.T) {
metaData, err := usageService.ListUsage(context.Background(), &v1.ListUsageRequest{
AttributionId: string(attributionID),
From: timestamppb.New(test.start),
To: timestamppb.New(test.end),
Order: v1.ListUsageRequest_ORDERING_DESCENDING,
Pagination: &v1.PaginatedRequest{
PerPage: 1,
Page: 1,
},
})
require.NoError(t, err)
require.Equal(t, test.creditsAtStart, metaData.CreditBalanceAtStart)
require.Equal(t, test.creditsAtEnd, metaData.CreditBalanceAtEnd)
require.Equal(t, test.recordsInRange, metaData.Pagination.Total)
})
}
tests := []struct {
name string // test name
start, end time.Time
// expectations
creditsAtStart float64
creditsAtEnd float64
recordsInRange int64
}{
{start, end, 3, 10, 2},
{end, end, 10, 10, 0},
{start, start, 3, 3, 0},
{start.Add(-200 * 24 * time.Hour), end, 0, 10, 4},
{start.Add(-200 * 24 * time.Hour), end.Add(10 * 24 * time.Hour), 0, 20, 5},
}
for i, test := range tests {
t.Run(test.name, func(t *testing.T) {
metaData, err := usageService.ListUsage(context.Background(), &v1.ListUsageRequest{
AttributionId: string(attributionID),
From: timestamppb.New(test.start),
To: timestamppb.New(test.end),
Order: v1.ListUsageRequest_ORDERING_DESCENDING,
Pagination: &v1.PaginatedRequest{
PerPage: 1,
Page: 1,
},
})
require.NoError(t, err)
require.Equal(t, test.creditsAtStart, metaData.CreditBalanceAtStart)
require.Equal(t, test.creditsAtEnd, metaData.CreditBalanceAtEnd)
require.Equal(t, test.recordsInRange, metaData.Pagination.Total)
})
}

It makes it nicer for humans on what you're testing, but also reflects it into the test when it fails

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense generally. In this case, I believe printing the number of the test is fine as well. But will definitely consider this in future!


}