File tree Expand file tree Collapse file tree 2 files changed +67
-1
lines changed Expand file tree Collapse file tree 2 files changed +67
-1
lines changed Original file line number Diff line number Diff line change 1
1
package compute
2
2
3
- import "fmt"
3
+ import (
4
+ "fmt"
5
+ )
4
6
5
7
// PagedResult represents the common fields for all paged results from the compute API.
6
8
type PagedResult struct {
@@ -30,6 +32,17 @@ func (page *PagedResult) NextPage() *Paging {
30
32
}
31
33
}
32
34
35
+ // IsLastPage determines whether the page represents the last page of results.
36
+ func (page * PagedResult ) IsLastPage () bool {
37
+ if page .IsEmpty () {
38
+ return true
39
+ }
40
+
41
+ itemCountUpToCurrentPage := ((page .PageNumber - 1 ) * page .PageSize ) + page .PageCount
42
+
43
+ return itemCountUpToCurrentPage >= page .TotalCount // i.e. Are there any more records?
44
+ }
45
+
33
46
// Paging contains the paging configuration for a compute API operation.
34
47
type Paging struct {
35
48
PageNumber int
Original file line number Diff line number Diff line change
1
+ package compute
2
+
3
+ import (
4
+ "testing"
5
+ )
6
+
7
+ // Verify that a PagedResult correctly determines that a page is not the last page.
8
+ func TestPagedResult_IsLastPage_PageSize_2_FirstPage_2of6Records (test * testing.T ) {
9
+ expect := expect (test )
10
+
11
+ result := & PagedResult {
12
+ PageNumber : 1 ,
13
+ PageSize : 2 ,
14
+ PageCount : 2 ,
15
+ TotalCount : 6 ,
16
+ }
17
+
18
+ expect .IsFalse ("PagedResult.IsLastPage" ,
19
+ result .IsLastPage (),
20
+ )
21
+ }
22
+
23
+ // Verify that a PagedResult correctly determines that a page is not the last page.
24
+ func TestPagedResult_IsLastPage_PageSize_2_SecondLastPage_3of6Records (test * testing.T ) {
25
+ expect := expect (test )
26
+
27
+ result := & PagedResult {
28
+ PageNumber : 2 ,
29
+ PageSize : 2 ,
30
+ PageCount : 1 ,
31
+ TotalCount : 6 ,
32
+ }
33
+
34
+ expect .IsFalse ("PagedResult.IsLastPage" ,
35
+ result .IsLastPage (),
36
+ )
37
+ }
38
+
39
+ // Verify that a PagedResult correctly determines that a page is the last page.
40
+ func TestPagedResult_IsLastPage_PageSize_2_LastPage_6of6Records (test * testing.T ) {
41
+ expect := expect (test )
42
+
43
+ result := & PagedResult {
44
+ PageNumber : 3 ,
45
+ PageSize : 2 ,
46
+ PageCount : 2 ,
47
+ TotalCount : 6 ,
48
+ }
49
+
50
+ expect .IsTrue ("PagedResult.IsLastPage" ,
51
+ result .IsLastPage (),
52
+ )
53
+ }
You can’t perform that action at this time.
0 commit comments