Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 1 | package pagination |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud/testhelper" |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // SinglePage sample and test cases. |
| 12 | |
| 13 | type SinglePageResult struct { |
| 14 | SinglePageBase |
| 15 | } |
| 16 | |
| 17 | func (r SinglePageResult) IsEmpty() (bool, error) { |
| 18 | is, err := ExtractSingleInts(r) |
| 19 | if err != nil { |
| 20 | return true, err |
| 21 | } |
| 22 | return len(is) == 0, nil |
| 23 | } |
| 24 | |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame] | 25 | func ExtractSingleInts(r Page) ([]int, error) { |
| 26 | var s struct { |
| 27 | Ints []int `json:"ints"` |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 28 | } |
Jon Perritt | c7d828e | 2016-02-25 03:06:33 -0600 | [diff] [blame] | 29 | err := (r.(SinglePageResult)).ExtractInto(&s) |
| 30 | return s.Ints, err |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | func setupSinglePaged() Pager { |
| 34 | testhelper.SetupHTTP() |
| 35 | client := createClient() |
| 36 | |
| 37 | testhelper.Mux.HandleFunc("/only", func(w http.ResponseWriter, r *http.Request) { |
| 38 | w.Header().Add("Content-Type", "application/json") |
| 39 | fmt.Fprintf(w, `{ "ints": [1, 2, 3] }`) |
| 40 | }) |
| 41 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 42 | createPage := func(r PageResult) Page { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 43 | return SinglePageResult{SinglePageBase(r)} |
| 44 | } |
| 45 | |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 46 | return NewPager(client, testhelper.Server.URL+"/only", createPage) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func TestEnumerateSinglePaged(t *testing.T) { |
| 50 | callCount := 0 |
| 51 | pager := setupSinglePaged() |
| 52 | defer testhelper.TeardownHTTP() |
| 53 | |
| 54 | err := pager.EachPage(func(page Page) (bool, error) { |
| 55 | callCount++ |
| 56 | |
| 57 | expected := []int{1, 2, 3} |
| 58 | actual, err := ExtractSingleInts(page) |
| 59 | testhelper.AssertNoErr(t, err) |
| 60 | testhelper.CheckDeepEquals(t, expected, actual) |
| 61 | return true, nil |
| 62 | }) |
| 63 | testhelper.CheckNoErr(t, err) |
| 64 | testhelper.CheckEquals(t, 1, callCount) |
| 65 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 66 | |
| 67 | func TestAllPagesSingle(t *testing.T) { |
| 68 | pager := setupSinglePaged() |
| 69 | defer testhelper.TeardownHTTP() |
| 70 | |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 71 | page, err := pager.AllPages() |
| 72 | testhelper.AssertNoErr(t, err) |
| 73 | |
| 74 | expected := []int{1, 2, 3} |
| 75 | actual, err := ExtractSingleInts(page) |
| 76 | testhelper.AssertNoErr(t, err) |
| 77 | testhelper.CheckDeepEquals(t, expected, actual) |
| 78 | } |