linked_test.go 2.8 KB

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