forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_test.go
136 lines (113 loc) · 3.16 KB
/
action_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package godo
import (
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestAction_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/actions", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"actions": [{"id":1},{"id":2}]}`)
testMethod(t, r, http.MethodGet)
})
actions, _, err := client.Actions.List(ctx, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
expected := []Action{{ID: 1}, {ID: 2}}
if len(actions) != len(expected) || actions[0].ID != expected[0].ID || actions[1].ID != expected[1].ID {
t.Fatalf("unexpected response")
}
}
func TestAction_ListActionMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/actions", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"actions": [{"id":1},{"id":2}], "links":{"pages":{"next":"http://example.com/v2/droplets/?page=2"}}}`)
testMethod(t, r, http.MethodGet)
})
_, resp, err := client.Actions.List(ctx, nil)
if err != nil {
t.Fatal(nil)
}
checkCurrentPage(t, resp, 1)
}
func TestAction_RetrievePageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"actions": [{"id":1},{"id":2}],
"links":{
"pages":{
"next":"http://example.com/v2/actions/?page=3",
"prev":"http://example.com/v2/actions/?page=1",
"last":"http://example.com/v2/actions/?page=3",
"first":"http://example.com/v2/actions/?page=1"
}
}
}`
mux.HandleFunc("/v2/actions", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, resp, err := client.Actions.List(ctx, opt)
if err != nil {
t.Fatal(err)
}
checkCurrentPage(t, resp, 2)
}
func TestAction_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/actions/12345", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"action": {"id":12345,"region":{"name":"name","slug":"slug","available":true,"sizes":["512mb"],"features":["virtio"]},"region_slug":"slug"}}`)
testMethod(t, r, http.MethodGet)
})
action, _, err := client.Actions.Get(ctx, 12345)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if action.ID != 12345 {
t.Fatalf("unexpected response")
}
region := &Region{
Name: "name",
Slug: "slug",
Available: true,
Sizes: []string{"512mb"},
Features: []string{"virtio"},
}
if !reflect.DeepEqual(action.Region, region) {
t.Fatalf("unexpected response, invalid region")
}
if action.RegionSlug != "slug" {
t.Fatalf("unexpected response, invalid region slug")
}
}
func TestAction_String(t *testing.T) {
pt, err := time.Parse(time.RFC3339, "2014-05-08T20:36:47Z")
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
startedAt := &Timestamp{
Time: pt,
}
action := &Action{
ID: 1,
Status: "in-progress",
Type: "transfer",
StartedAt: startedAt,
}
stringified := action.String()
expected := `godo.Action{ID:1, Status:"in-progress", Type:"transfer", ` +
`StartedAt:godo.Timestamp{2014-05-08 20:36:47 +0000 UTC}, ` +
`ResourceID:0, ResourceType:"", RegionSlug:""}`
if expected != stringified {
t.Errorf("Action.Stringify returned %+v, expected %+v", stringified, expected)
}
}