Skip to content

[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

Merged
merged 1 commit into from
Aug 31, 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
172 changes: 72 additions & 100 deletions components/usage/pkg/apiv1/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,8 @@ func TestUsageService_ListBilledUsage_Pagination(t *testing.T) {
ctx := context.Background()

type Expectation struct {
count int64
Copy link
Member Author

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...

total int64
totalPages int64
page int64
perPage int64
}

type Scenario struct {
Expand All @@ -204,112 +201,87 @@ func TestUsageService_ListBilledUsage_Pagination(t *testing.T) {
}

scenarios := []Scenario{
(func() Scenario {
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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)

Expand Down