-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[usage] Fix test for listing usage with pagination #12562
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,11 +175,8 @@ func TestUsageService_ListBilledUsage_Pagination(t *testing.T) { | |
ctx := context.Background() | ||
|
||
type Expectation struct { | ||
count int64 | ||
total int64 | ||
totalPages int64 | ||
page int64 | ||
perPage int64 | ||
} | ||
|
||
type Scenario struct { | ||
|
@@ -204,112 +201,87 @@ func TestUsageService_ListBilledUsage_Pagination(t *testing.T) { | |
} | ||
|
||
scenarios := []Scenario{ | ||
(func() Scenario { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wrapping in a self-invoking anonymous function isn't needed. It would be useful we wanted to do some imperative work, but we're doing only declarative statements. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 thanks 💯 |
||
|
||
return Scenario{ | ||
name: "first page", | ||
Request: &v1.ListBilledUsageRequest{ | ||
AttributionId: string(attrID), | ||
From: timestamppb.New(start), | ||
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)), | ||
Pagination: &v1.PaginatedRequest{ | ||
PerPage: int64(5), | ||
Page: int64(1), | ||
}, | ||
}, | ||
Expect: Expectation{ | ||
count: int64(5), | ||
total: int64(14), | ||
totalPages: int64(3), | ||
page: int64(1), | ||
perPage: int64(5), | ||
}, | ||
} | ||
})(), | ||
(func() Scenario { | ||
|
||
return Scenario{ | ||
name: "second page", | ||
Request: &v1.ListBilledUsageRequest{ | ||
AttributionId: string(attrID), | ||
From: timestamppb.New(start), | ||
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)), | ||
Pagination: &v1.PaginatedRequest{ | ||
PerPage: int64(5), | ||
Page: int64(2), | ||
}, | ||
}, | ||
Expect: Expectation{ | ||
count: int64(5), | ||
total: int64(14), | ||
totalPages: int64(3), | ||
page: int64(2), | ||
perPage: int64(5), | ||
}, | ||
} | ||
})(), | ||
(func() Scenario { | ||
|
||
return Scenario{ | ||
name: "third page", | ||
Request: &v1.ListBilledUsageRequest{ | ||
AttributionId: string(attrID), | ||
From: timestamppb.New(start), | ||
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)), | ||
Pagination: &v1.PaginatedRequest{ | ||
PerPage: int64(5), | ||
Page: int64(3), | ||
}, | ||
{ | ||
name: "first page", | ||
Request: &v1.ListBilledUsageRequest{ | ||
AttributionId: string(attrID), | ||
From: timestamppb.New(start), | ||
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)), | ||
Pagination: &v1.PaginatedRequest{ | ||
PerPage: int64(5), | ||
Page: int64(1), | ||
}, | ||
Expect: Expectation{ | ||
count: int64(4), | ||
total: int64(14), | ||
totalPages: int64(3), | ||
page: int64(3), | ||
perPage: int64(5), | ||
}, | ||
Expect: Expectation{ | ||
total: int64(14), | ||
totalPages: int64(3), | ||
}, | ||
}, | ||
{ | ||
name: "second page", | ||
Request: &v1.ListBilledUsageRequest{ | ||
AttributionId: string(attrID), | ||
From: timestamppb.New(start), | ||
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)), | ||
Pagination: &v1.PaginatedRequest{ | ||
PerPage: int64(5), | ||
Page: int64(2), | ||
}, | ||
} | ||
})(), | ||
(func() Scenario { | ||
|
||
return Scenario{ | ||
name: "fourth page", | ||
Request: &v1.ListBilledUsageRequest{ | ||
AttributionId: string(attrID), | ||
From: timestamppb.New(start), | ||
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)), | ||
Pagination: &v1.PaginatedRequest{ | ||
PerPage: int64(5), | ||
Page: int64(4), | ||
}, | ||
}, | ||
Expect: Expectation{ | ||
total: int64(14), | ||
totalPages: int64(3), | ||
}, | ||
}, | ||
{ | ||
name: "third page", | ||
Request: &v1.ListBilledUsageRequest{ | ||
AttributionId: string(attrID), | ||
From: timestamppb.New(start), | ||
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)), | ||
Pagination: &v1.PaginatedRequest{ | ||
PerPage: int64(5), | ||
Page: int64(3), | ||
}, | ||
Expect: Expectation{ | ||
count: int64(0), | ||
total: int64(14), | ||
totalPages: int64(3), | ||
page: int64(4), | ||
perPage: int64(5), | ||
}, | ||
Expect: Expectation{ | ||
total: int64(14), | ||
totalPages: int64(3), | ||
}, | ||
}, | ||
{ | ||
name: "fourth page", | ||
Request: &v1.ListBilledUsageRequest{ | ||
AttributionId: string(attrID), | ||
From: timestamppb.New(start), | ||
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)), | ||
Pagination: &v1.PaginatedRequest{ | ||
PerPage: int64(5), | ||
Page: int64(4), | ||
}, | ||
} | ||
})(), | ||
}, | ||
Expect: Expectation{ | ||
total: int64(14), | ||
totalPages: int64(3), | ||
}, | ||
}, | ||
} | ||
|
||
dbconn := dbtest.ConnectForTests(t) | ||
dbtest.CreateWorkspaceInstanceUsageRecords(t, dbconn, instances...) | ||
|
||
srv := baseserver.NewForTests(t, | ||
baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)), | ||
) | ||
for _, scenario := range scenarios { | ||
t.Run(scenario.name, func(t *testing.T) { | ||
dbconn := dbtest.ConnectForTests(t) | ||
dbtest.CreateWorkspaceInstanceUsageRecords(t, dbconn, instances...) | ||
|
||
generator := NewReportGenerator(dbconn, DefaultWorkspacePricer) | ||
v1.RegisterUsageServiceServer(srv.GRPC(), NewUsageService(dbconn, generator, nil)) | ||
baseserver.StartServerForTests(t, srv) | ||
srv := baseserver.NewForTests(t, | ||
baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)), | ||
) | ||
|
||
conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
require.NoError(t, err) | ||
generator := NewReportGenerator(dbconn, DefaultWorkspacePricer) | ||
v1.RegisterUsageServiceServer(srv.GRPC(), NewUsageService(dbconn, generator, nil)) | ||
baseserver.StartServerForTests(t, srv) | ||
|
||
for _, scenario := range scenarios { | ||
t.Run(scenario.name, func(t *testing.T) { | ||
conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
require.NoError(t, err) | ||
|
||
client := v1.NewUsageServiceClient(conn) | ||
|
||
|
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.
these properties were not used in the test anywhere...