forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regions_test.go
108 lines (91 loc) · 2.33 KB
/
regions_test.go
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
package godo
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestRegions_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/regions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"regions": [
{
"slug": "1"
},
{
"slug": "2"
}
],
"meta": {
"total": 2
}
}`)
})
regions, resp, err := client.Regions.List(ctx, nil)
if err != nil {
t.Errorf("Regions.List returned error: %v", err)
}
expectedRegions := []Region{{Slug: "1"}, {Slug: "2"}}
if !reflect.DeepEqual(regions, expectedRegions) {
t.Errorf("Regions.List returned regions %+v, expected %+v", regions, expectedRegions)
}
expectedMeta := &Meta{Total: 2}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Regions.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}
func TestRegions_ListRegionsMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/regions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"regions": [{"id":1},{"id":2}], "links":{"pages":{"next":"http://example.com/v2/regions/?page=2"}}}`)
})
_, resp, err := client.Regions.List(ctx, nil)
if err != nil {
t.Fatal(err)
}
checkCurrentPage(t, resp, 1)
}
func TestRegions_RetrievePageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"regions": [{"id":1},{"id":2}],
"links":{
"pages":{
"next":"http://example.com/v2/regions/?page=3",
"prev":"http://example.com/v2/regions/?page=1",
"last":"http://example.com/v2/regions/?page=3",
"first":"http://example.com/v2/regions/?page=1"
}
}
}`
mux.HandleFunc("/v2/regions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, resp, err := client.Regions.List(ctx, opt)
if err != nil {
t.Fatal(err)
}
checkCurrentPage(t, resp, 2)
}
func TestRegion_String(t *testing.T) {
region := &Region{
Slug: "region",
Name: "Region",
Sizes: []string{"1", "2"},
Available: true,
}
stringified := region.String()
expected := `godo.Region{Slug:"region", Name:"Region", Sizes:["1" "2"], Available:true}`
if expected != stringified {
t.Errorf("Region.String returned %+v, expected %+v", stringified, expected)
}
}