github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/pagination/testing/linked_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"reflect"
     7  	"testing"
     8  
     9  	"github.com/huaweicloud/golangsdk/pagination"
    10  	"github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  // LinkedPager sample and test cases.
    14  
    15  type LinkedPageResult struct {
    16  	pagination.LinkedPageBase
    17  }
    18  
    19  func (r LinkedPageResult) IsEmpty() (bool, error) {
    20  	is, err := ExtractLinkedInts(r)
    21  	return len(is) == 0, err
    22  }
    23  
    24  func ExtractLinkedInts(r pagination.Page) ([]int, error) {
    25  	var s struct {
    26  		Ints []int `json:"ints"`
    27  	}
    28  	err := (r.(LinkedPageResult)).ExtractInto(&s)
    29  	return s.Ints, err
    30  }
    31  
    32  func createLinked(t *testing.T) pagination.Pager {
    33  	testhelper.SetupHTTP()
    34  
    35  	testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) {
    36  		w.Header().Add("Content-Type", "application/json")
    37  		fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, testhelper.Server.URL)
    38  	})
    39  
    40  	testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
    41  		w.Header().Add("Content-Type", "application/json")
    42  		fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL)
    43  	})
    44  
    45  	testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) {
    46  		w.Header().Add("Content-Type", "application/json")
    47  		fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`)
    48  	})
    49  
    50  	client := createClient()
    51  
    52  	createPage := func(r pagination.PageResult) pagination.Page {
    53  		return LinkedPageResult{pagination.LinkedPageBase{PageResult: r}}
    54  	}
    55  
    56  	return pagination.NewPager(client, testhelper.Server.URL+"/page1", createPage)
    57  }
    58  
    59  func TestEnumerateLinked(t *testing.T) {
    60  	pager := createLinked(t)
    61  	defer testhelper.TeardownHTTP()
    62  
    63  	callCount := 0
    64  	err := pager.EachPage(func(page pagination.Page) (bool, error) {
    65  		actual, err := ExtractLinkedInts(page)
    66  		if err != nil {
    67  			return false, err
    68  		}
    69  
    70  		t.Logf("Handler invoked with %v", actual)
    71  
    72  		var expected []int
    73  		switch callCount {
    74  		case 0:
    75  			expected = []int{1, 2, 3}
    76  		case 1:
    77  			expected = []int{4, 5, 6}
    78  		case 2:
    79  			expected = []int{7, 8, 9}
    80  		default:
    81  			t.Fatalf("Unexpected call count: %d", callCount)
    82  			return false, nil
    83  		}
    84  
    85  		if !reflect.DeepEqual(expected, actual) {
    86  			t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
    87  		}
    88  
    89  		callCount++
    90  		return true, nil
    91  	})
    92  	if err != nil {
    93  		t.Errorf("Unexpected error for page iteration: %v", err)
    94  	}
    95  
    96  	if callCount != 3 {
    97  		t.Errorf("Expected 3 calls, but was %d", callCount)
    98  	}
    99  }
   100  
   101  func TestAllPagesLinked(t *testing.T) {
   102  	pager := createLinked(t)
   103  	defer testhelper.TeardownHTTP()
   104  
   105  	page, err := pager.AllPages()
   106  	testhelper.AssertNoErr(t, err)
   107  
   108  	expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
   109  	actual, err := ExtractLinkedInts(page)
   110  	testhelper.AssertNoErr(t, err)
   111  	testhelper.CheckDeepEquals(t, expected, actual)
   112  }