github.com/kevinklinger/open_terraform@v1.3.6/noninternal/registry/response/pagination_test.go (about)

     1  package response
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  )
     7  
     8  func prettyJSON(o interface{}) (string, error) {
     9  	bytes, err := json.MarshalIndent(o, "", "\t")
    10  	if err != nil {
    11  		return "", err
    12  	}
    13  	return string(bytes), nil
    14  }
    15  
    16  func TestNewPaginationMeta(t *testing.T) {
    17  	type args struct {
    18  		offset     int
    19  		limit      int
    20  		hasMore    bool
    21  		currentURL string
    22  	}
    23  	tests := []struct {
    24  		name     string
    25  		args     args
    26  		wantJSON string
    27  	}{
    28  		{
    29  			name: "first page",
    30  			args: args{0, 10, true, "http://foo.com/v1/bar"},
    31  			wantJSON: `{
    32  	"limit": 10,
    33  	"current_offset": 0,
    34  	"next_offset": 10,
    35  	"next_url": "http://foo.com/v1/bar?offset=10"
    36  }`,
    37  		},
    38  		{
    39  			name: "second page",
    40  			args: args{10, 10, true, "http://foo.com/v1/bar"},
    41  			wantJSON: `{
    42  	"limit": 10,
    43  	"current_offset": 10,
    44  	"next_offset": 20,
    45  	"prev_offset": 0,
    46  	"next_url": "http://foo.com/v1/bar?offset=20",
    47  	"prev_url": "http://foo.com/v1/bar"
    48  }`,
    49  		},
    50  		{
    51  			name: "last page",
    52  			args: args{40, 10, false, "http://foo.com/v1/bar"},
    53  			wantJSON: `{
    54  	"limit": 10,
    55  	"current_offset": 40,
    56  	"prev_offset": 30,
    57  	"prev_url": "http://foo.com/v1/bar?offset=30"
    58  }`,
    59  		},
    60  		{
    61  			name: "misaligned start ending exactly on boundary",
    62  			args: args{32, 10, false, "http://foo.com/v1/bar"},
    63  			wantJSON: `{
    64  	"limit": 10,
    65  	"current_offset": 32,
    66  	"prev_offset": 22,
    67  	"prev_url": "http://foo.com/v1/bar?offset=22"
    68  }`,
    69  		},
    70  		{
    71  			name: "misaligned start partially through first page",
    72  			args: args{5, 12, true, "http://foo.com/v1/bar"},
    73  			wantJSON: `{
    74  	"limit": 12,
    75  	"current_offset": 5,
    76  	"next_offset": 17,
    77  	"prev_offset": 0,
    78  	"next_url": "http://foo.com/v1/bar?offset=17",
    79  	"prev_url": "http://foo.com/v1/bar"
    80  }`,
    81  		},
    82  		{
    83  			name: "no current URL",
    84  			args: args{10, 10, true, ""},
    85  			wantJSON: `{
    86  	"limit": 10,
    87  	"current_offset": 10,
    88  	"next_offset": 20,
    89  	"prev_offset": 0
    90  }`,
    91  		},
    92  		{
    93  			name: "#58 regression test",
    94  			args: args{1, 3, true, ""},
    95  			wantJSON: `{
    96  	"limit": 3,
    97  	"current_offset": 1,
    98  	"next_offset": 4,
    99  	"prev_offset": 0
   100  }`,
   101  		},
   102  	}
   103  	for _, tt := range tests {
   104  		t.Run(tt.name, func(t *testing.T) {
   105  			got := NewPaginationMeta(tt.args.offset, tt.args.limit, tt.args.hasMore,
   106  				tt.args.currentURL)
   107  			gotJSON, err := prettyJSON(got)
   108  			if err != nil {
   109  				t.Fatalf("failed to marshal PaginationMeta to JSON: %s", err)
   110  			}
   111  			if gotJSON != tt.wantJSON {
   112  				// prettyJSON makes debugging easier due to the annoying pointer-to-ints, but it
   113  				// also implicitly tests JSON marshalling as we can see if it's omitting fields etc.
   114  				t.Fatalf("NewPaginationMeta() =\n%s\n  want:\n%s\n", gotJSON, tt.wantJSON)
   115  			}
   116  		})
   117  	}
   118  }