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

[usage] added a test for ListUsage #12810

merged 1 commit into from
Sep 9, 2022

Conversation

svenefftinge
Copy link
Member

Description

adds a test for UsageService.ListUsage

Related Issue(s)

Fixes #

How to test

Release Notes

NONE

Documentation

Werft options:

  • /werft with-preview

@svenefftinge svenefftinge requested a review from a team September 9, 2022 10:12
@github-actions github-actions bot added the team: webapp Issue belongs to the WebApp team label Sep 9, 2022
@roboquat roboquat added the size/M label Sep 9, 2022
Copy link
Member

@easyCZ easyCZ left a 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

Comment on lines +309 to +341
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)
})
}
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!

@svenefftinge
Copy link
Member Author

/unhold

@roboquat roboquat merged commit 343c462 into main Sep 9, 2022
@roboquat roboquat deleted the se/listusage-ltest branch September 9, 2022 10:26
@roboquat roboquat added deployed: webapp Meta team change is running in production deployed Change is completely running in production labels Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployed: webapp Meta team change is running in production deployed Change is completely running in production release-note-none size/M team: webapp Issue belongs to the WebApp team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants