-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/hold in case you want to add name to the tests
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) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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!
/unhold |
Description
adds a test for UsageService.ListUsage
Related Issue(s)
Fixes #
How to test
Release Notes
Documentation
Werft options: