[usage] added a test for ListUsage#12810
Merged
Merged
Conversation
easyCZ
approved these changes
Sep 9, 2022
easyCZ
left a comment
Member
There was a problem hiding this comment.
/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) | ||
| }) | ||
| } |
Member
There was a problem hiding this comment.
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
Contributor
Author
There was a problem hiding this comment.
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!
Contributor
Author
|
/unhold |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
adds a test for UsageService.ListUsage
Related Issue(s)
Fixes #
How to test
Release Notes
Documentation
Werft options: