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