github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/pkg/cmd/api/pagination_test.go (about)

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"net/http"
     7  	"testing"
     8  )
     9  
    10  func Test_findNextPage(t *testing.T) {
    11  	tests := []struct {
    12  		name  string
    13  		resp  *http.Response
    14  		want  string
    15  		want1 bool
    16  	}{
    17  		{
    18  			name:  "no Link header",
    19  			resp:  &http.Response{},
    20  			want:  "",
    21  			want1: false,
    22  		},
    23  		{
    24  			name: "no next page in Link",
    25  			resp: &http.Response{
    26  				Header: http.Header{
    27  					"Link": []string{`<https://api.github.com/issues?page=3>; rel="last"`},
    28  				},
    29  			},
    30  			want:  "",
    31  			want1: false,
    32  		},
    33  		{
    34  			name: "has next page",
    35  			resp: &http.Response{
    36  				Header: http.Header{
    37  					"Link": []string{`<https://api.github.com/issues?page=2>; rel="next", <https://api.github.com/issues?page=3>; rel="last"`},
    38  				},
    39  			},
    40  			want:  "https://api.github.com/issues?page=2",
    41  			want1: true,
    42  		},
    43  	}
    44  	for _, tt := range tests {
    45  		t.Run(tt.name, func(t *testing.T) {
    46  			got, got1 := findNextPage(tt.resp)
    47  			if got != tt.want {
    48  				t.Errorf("findNextPage() got = %v, want %v", got, tt.want)
    49  			}
    50  			if got1 != tt.want1 {
    51  				t.Errorf("findNextPage() got1 = %v, want %v", got1, tt.want1)
    52  			}
    53  		})
    54  	}
    55  }
    56  
    57  func Test_findEndCursor(t *testing.T) {
    58  	tests := []struct {
    59  		name string
    60  		json io.Reader
    61  		want string
    62  	}{
    63  		{
    64  			name: "blank",
    65  			json: bytes.NewBufferString(`{}`),
    66  			want: "",
    67  		},
    68  		{
    69  			name: "unrelated fields",
    70  			json: bytes.NewBufferString(`{
    71  				"hasNextPage": true,
    72  				"endCursor": "THE_END"
    73  			}`),
    74  			want: "",
    75  		},
    76  		{
    77  			name: "has next page",
    78  			json: bytes.NewBufferString(`{
    79  				"pageInfo": {
    80  					"hasNextPage": true,
    81  					"endCursor": "THE_END"
    82  				}
    83  			}`),
    84  			want: "THE_END",
    85  		},
    86  		{
    87  			name: "more pageInfo blocks",
    88  			json: bytes.NewBufferString(`{
    89  				"pageInfo": {
    90  					"hasNextPage": true,
    91  					"endCursor": "THE_END"
    92  				},
    93  				"pageInfo": {
    94  					"hasNextPage": true,
    95  					"endCursor": "NOT_THIS"
    96  				}
    97  			}`),
    98  			want: "THE_END",
    99  		},
   100  		{
   101  			name: "no next page",
   102  			json: bytes.NewBufferString(`{
   103  				"pageInfo": {
   104  					"hasNextPage": false,
   105  					"endCursor": "THE_END"
   106  				}
   107  			}`),
   108  			want: "",
   109  		},
   110  	}
   111  	for _, tt := range tests {
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			if got := findEndCursor(tt.json); got != tt.want {
   114  				t.Errorf("findEndCursor() = %v, want %v", got, tt.want)
   115  			}
   116  		})
   117  	}
   118  }
   119  
   120  func Test_addPerPage(t *testing.T) {
   121  	type args struct {
   122  		p       string
   123  		perPage int
   124  		params  map[string]interface{}
   125  	}
   126  	tests := []struct {
   127  		name string
   128  		args args
   129  		want string
   130  	}{
   131  		{
   132  			name: "adds per_page",
   133  			args: args{
   134  				p:       "items",
   135  				perPage: 13,
   136  				params:  nil,
   137  			},
   138  			want: "items?per_page=13",
   139  		},
   140  		{
   141  			name: "avoids adding per_page if already in params",
   142  			args: args{
   143  				p:       "items",
   144  				perPage: 13,
   145  				params: map[string]interface{}{
   146  					"state":    "open",
   147  					"per_page": 99,
   148  				},
   149  			},
   150  			want: "items",
   151  		},
   152  		{
   153  			name: "avoids adding per_page if already in query",
   154  			args: args{
   155  				p:       "items?per_page=6&state=open",
   156  				perPage: 13,
   157  				params:  nil,
   158  			},
   159  			want: "items?per_page=6&state=open",
   160  		},
   161  	}
   162  	for _, tt := range tests {
   163  		t.Run(tt.name, func(t *testing.T) {
   164  			if got := addPerPage(tt.args.p, tt.args.perPage, tt.args.params); got != tt.want {
   165  				t.Errorf("addPerPage() = %v, want %v", got, tt.want)
   166  			}
   167  		})
   168  	}
   169  }