jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/testhelper" |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | // LinkedPager sample and test cases. |
| 14 | |
| 15 | type LinkedPageResult struct { |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 16 | pagination.LinkedPageBase |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | func (r LinkedPageResult) IsEmpty() (bool, error) { |
| 20 | is, err := ExtractLinkedInts(r) |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame] | 21 | return len(is) == 0, err |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 22 | } |
| 23 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 24 | func ExtractLinkedInts(r pagination.Page) ([]int, error) { |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame] | 25 | var s struct { |
| 26 | Ints []int `json:"ints"` |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 27 | } |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame] | 28 | err := (r.(LinkedPageResult)).ExtractInto(&s) |
| 29 | return s.Ints, err |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 30 | } |
| 31 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 32 | func createLinked(t *testing.T) pagination.Pager { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 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 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 52 | createPage := func(r pagination.PageResult) pagination.Page { |
| 53 | return LinkedPageResult{pagination.LinkedPageBase{PageResult: r}} |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 54 | } |
| 55 | |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 56 | return pagination.NewPager(client, testhelper.Server.URL+"/page1", createPage) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | func TestEnumerateLinked(t *testing.T) { |
| 60 | pager := createLinked(t) |
| 61 | defer testhelper.TeardownHTTP() |
| 62 | |
| 63 | callCount := 0 |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 64 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 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 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 100 | |
| 101 | func TestAllPagesLinked(t *testing.T) { |
| 102 | pager := createLinked(t) |
| 103 | defer testhelper.TeardownHTTP() |
| 104 | |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 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 | } |