github.com/leg100/ots@v0.0.7-0.20210919080622-034055ced4bd/pagination_test.go (about)

     1  package ots
     2  
     3  import (
     4  	"testing"
     5  
     6  	tfe "github.com/leg100/go-tfe"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestPagination(t *testing.T) {
    11  	tests := []struct {
    12  		name  string
    13  		opts  tfe.ListOptions
    14  		count int
    15  		want  tfe.Pagination
    16  	}{
    17  		{
    18  			name:  "one page",
    19  			opts:  tfe.ListOptions{PageNumber: 1, PageSize: 20},
    20  			count: 5,
    21  			want: tfe.Pagination{
    22  				CurrentPage:  1,
    23  				PreviousPage: 1,
    24  				NextPage:     1,
    25  				TotalCount:   5,
    26  				TotalPages:   1,
    27  			},
    28  		},
    29  		{
    30  			name:  "multiple pages",
    31  			opts:  tfe.ListOptions{PageNumber: 3, PageSize: 20},
    32  			count: 101,
    33  			want: tfe.Pagination{
    34  				CurrentPage:  3,
    35  				PreviousPage: 2,
    36  				NextPage:     4,
    37  				TotalCount:   101,
    38  				TotalPages:   6,
    39  			},
    40  		},
    41  		{
    42  			name:  "no results",
    43  			opts:  tfe.ListOptions{PageNumber: 1, PageSize: 20},
    44  			count: 0,
    45  			want: tfe.Pagination{
    46  				CurrentPage:  1,
    47  				PreviousPage: 1,
    48  				NextPage:     1,
    49  				TotalCount:   0,
    50  				TotalPages:   1,
    51  			},
    52  		},
    53  	}
    54  
    55  	for _, tt := range tests {
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			pagination := NewPagination(tt.opts, tt.count)
    58  			assert.Equal(t, tt.want, *pagination)
    59  		})
    60  	}
    61  }