File: marker_test.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 0.0~git20180917.45f1c769-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,768 kB
  • sloc: sh: 98; makefile: 14
file content (127 lines) | stat: -rw-r--r-- 2,997 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package testing

import (
	"fmt"
	"net/http"
	"strings"
	"testing"

	"github.com/gophercloud/gophercloud/pagination"
	"github.com/gophercloud/gophercloud/testhelper"
)

// MarkerPager sample and test cases.

type MarkerPageResult struct {
	pagination.MarkerPageBase
}

func (r MarkerPageResult) IsEmpty() (bool, error) {
	results, err := ExtractMarkerStrings(r)
	if err != nil {
		return true, err
	}
	return len(results) == 0, err
}

func (r MarkerPageResult) LastMarker() (string, error) {
	results, err := ExtractMarkerStrings(r)
	if err != nil {
		return "", err
	}
	if len(results) == 0 {
		return "", nil
	}
	return results[len(results)-1], nil
}

func createMarkerPaged(t *testing.T) pagination.Pager {
	testhelper.SetupHTTP()

	testhelper.Mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
		r.ParseForm()
		ms := r.Form["marker"]
		switch {
		case len(ms) == 0:
			fmt.Fprintf(w, "aaa\nbbb\nccc")
		case len(ms) == 1 && ms[0] == "ccc":
			fmt.Fprintf(w, "ddd\neee\nfff")
		case len(ms) == 1 && ms[0] == "fff":
			fmt.Fprintf(w, "ggg\nhhh\niii")
		case len(ms) == 1 && ms[0] == "iii":
			w.WriteHeader(http.StatusNoContent)
		default:
			t.Errorf("Request with unexpected marker: [%v]", ms)
		}
	})

	client := createClient()

	createPage := func(r pagination.PageResult) pagination.Page {
		p := MarkerPageResult{pagination.MarkerPageBase{PageResult: r}}
		p.MarkerPageBase.Owner = p
		return p
	}

	return pagination.NewPager(client, testhelper.Server.URL+"/page", createPage)
}

func ExtractMarkerStrings(page pagination.Page) ([]string, error) {
	content := page.(MarkerPageResult).Body.([]uint8)
	parts := strings.Split(string(content), "\n")
	results := make([]string, 0, len(parts))
	for _, part := range parts {
		if len(part) > 0 {
			results = append(results, part)
		}
	}
	return results, nil
}

func TestEnumerateMarker(t *testing.T) {
	pager := createMarkerPaged(t)
	defer testhelper.TeardownHTTP()

	callCount := 0
	err := pager.EachPage(func(page pagination.Page) (bool, error) {
		actual, err := ExtractMarkerStrings(page)
		if err != nil {
			return false, err
		}

		t.Logf("Handler invoked with %v", actual)

		var expected []string
		switch callCount {
		case 0:
			expected = []string{"aaa", "bbb", "ccc"}
		case 1:
			expected = []string{"ddd", "eee", "fff"}
		case 2:
			expected = []string{"ggg", "hhh", "iii"}
		default:
			t.Fatalf("Unexpected call count: %d", callCount)
			return false, nil
		}

		testhelper.CheckDeepEquals(t, expected, actual)

		callCount++
		return true, nil
	})
	testhelper.AssertNoErr(t, err)
	testhelper.AssertEquals(t, callCount, 3)
}

func TestAllPagesMarker(t *testing.T) {
	pager := createMarkerPaged(t)
	defer testhelper.TeardownHTTP()

	page, err := pager.AllPages()
	testhelper.AssertNoErr(t, err)

	expected := []string{"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii"}
	actual, err := ExtractMarkerStrings(page)
	testhelper.AssertNoErr(t, err)
	testhelper.CheckDeepEquals(t, expected, actual)
}