Skip to content

Commit 1c3090f

Browse files
easyCZroboquat
authored andcommitted
[usage] Fix test for listing usage with pagination
1 parent 4b13468 commit 1c3090f

File tree

1 file changed

+72
-100
lines changed

1 file changed

+72
-100
lines changed

components/usage/pkg/apiv1/usage_test.go

Lines changed: 72 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,8 @@ func TestUsageService_ListBilledUsage_Pagination(t *testing.T) {
175175
ctx := context.Background()
176176

177177
type Expectation struct {
178-
count int64
179178
total int64
180179
totalPages int64
181-
page int64
182-
perPage int64
183180
}
184181

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

206203
scenarios := []Scenario{
207-
(func() Scenario {
208-
209-
return Scenario{
210-
name: "first page",
211-
Request: &v1.ListBilledUsageRequest{
212-
AttributionId: string(attrID),
213-
From: timestamppb.New(start),
214-
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)),
215-
Pagination: &v1.PaginatedRequest{
216-
PerPage: int64(5),
217-
Page: int64(1),
218-
},
219-
},
220-
Expect: Expectation{
221-
count: int64(5),
222-
total: int64(14),
223-
totalPages: int64(3),
224-
page: int64(1),
225-
perPage: int64(5),
226-
},
227-
}
228-
})(),
229-
(func() Scenario {
230-
231-
return Scenario{
232-
name: "second page",
233-
Request: &v1.ListBilledUsageRequest{
234-
AttributionId: string(attrID),
235-
From: timestamppb.New(start),
236-
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)),
237-
Pagination: &v1.PaginatedRequest{
238-
PerPage: int64(5),
239-
Page: int64(2),
240-
},
241-
},
242-
Expect: Expectation{
243-
count: int64(5),
244-
total: int64(14),
245-
totalPages: int64(3),
246-
page: int64(2),
247-
perPage: int64(5),
248-
},
249-
}
250-
})(),
251-
(func() Scenario {
252-
253-
return Scenario{
254-
name: "third page",
255-
Request: &v1.ListBilledUsageRequest{
256-
AttributionId: string(attrID),
257-
From: timestamppb.New(start),
258-
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)),
259-
Pagination: &v1.PaginatedRequest{
260-
PerPage: int64(5),
261-
Page: int64(3),
262-
},
204+
{
205+
name: "first page",
206+
Request: &v1.ListBilledUsageRequest{
207+
AttributionId: string(attrID),
208+
From: timestamppb.New(start),
209+
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)),
210+
Pagination: &v1.PaginatedRequest{
211+
PerPage: int64(5),
212+
Page: int64(1),
263213
},
264-
Expect: Expectation{
265-
count: int64(4),
266-
total: int64(14),
267-
totalPages: int64(3),
268-
page: int64(3),
269-
perPage: int64(5),
214+
},
215+
Expect: Expectation{
216+
total: int64(14),
217+
totalPages: int64(3),
218+
},
219+
},
220+
{
221+
name: "second page",
222+
Request: &v1.ListBilledUsageRequest{
223+
AttributionId: string(attrID),
224+
From: timestamppb.New(start),
225+
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)),
226+
Pagination: &v1.PaginatedRequest{
227+
PerPage: int64(5),
228+
Page: int64(2),
270229
},
271-
}
272-
})(),
273-
(func() Scenario {
274-
275-
return Scenario{
276-
name: "fourth page",
277-
Request: &v1.ListBilledUsageRequest{
278-
AttributionId: string(attrID),
279-
From: timestamppb.New(start),
280-
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)),
281-
Pagination: &v1.PaginatedRequest{
282-
PerPage: int64(5),
283-
Page: int64(4),
284-
},
230+
},
231+
Expect: Expectation{
232+
total: int64(14),
233+
totalPages: int64(3),
234+
},
235+
},
236+
{
237+
name: "third page",
238+
Request: &v1.ListBilledUsageRequest{
239+
AttributionId: string(attrID),
240+
From: timestamppb.New(start),
241+
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)),
242+
Pagination: &v1.PaginatedRequest{
243+
PerPage: int64(5),
244+
Page: int64(3),
285245
},
286-
Expect: Expectation{
287-
count: int64(0),
288-
total: int64(14),
289-
totalPages: int64(3),
290-
page: int64(4),
291-
perPage: int64(5),
246+
},
247+
Expect: Expectation{
248+
total: int64(14),
249+
totalPages: int64(3),
250+
},
251+
},
252+
{
253+
name: "fourth page",
254+
Request: &v1.ListBilledUsageRequest{
255+
AttributionId: string(attrID),
256+
From: timestamppb.New(start),
257+
To: timestamppb.New(start.Add(20*time.Minute + 2*time.Hour)),
258+
Pagination: &v1.PaginatedRequest{
259+
PerPage: int64(5),
260+
Page: int64(4),
292261
},
293-
}
294-
})(),
262+
},
263+
Expect: Expectation{
264+
total: int64(14),
265+
totalPages: int64(3),
266+
},
267+
},
295268
}
296269

297-
dbconn := dbtest.ConnectForTests(t)
298-
dbtest.CreateWorkspaceInstanceUsageRecords(t, dbconn, instances...)
299-
300-
srv := baseserver.NewForTests(t,
301-
baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)),
302-
)
270+
for _, scenario := range scenarios {
271+
t.Run(scenario.name, func(t *testing.T) {
272+
dbconn := dbtest.ConnectForTests(t)
273+
dbtest.CreateWorkspaceInstanceUsageRecords(t, dbconn, instances...)
303274

304-
generator := NewReportGenerator(dbconn, DefaultWorkspacePricer)
305-
v1.RegisterUsageServiceServer(srv.GRPC(), NewUsageService(dbconn, generator, nil))
306-
baseserver.StartServerForTests(t, srv)
275+
srv := baseserver.NewForTests(t,
276+
baseserver.WithGRPC(baseserver.MustUseRandomLocalAddress(t)),
277+
)
307278

308-
conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
309-
require.NoError(t, err)
279+
generator := NewReportGenerator(dbconn, DefaultWorkspacePricer)
280+
v1.RegisterUsageServiceServer(srv.GRPC(), NewUsageService(dbconn, generator, nil))
281+
baseserver.StartServerForTests(t, srv)
310282

311-
for _, scenario := range scenarios {
312-
t.Run(scenario.name, func(t *testing.T) {
283+
conn, err := grpc.Dial(srv.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
284+
require.NoError(t, err)
313285

314286
client := v1.NewUsageServiceClient(conn)
315287

0 commit comments

Comments
 (0)