This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
hardware_reservations_test.go
106 lines (100 loc) · 2.86 KB
/
hardware_reservations_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
package packngo
import (
"fmt"
"net/http"
"net/url"
"reflect"
"strconv"
"testing"
)
func TestAccListHardwareReservations(t *testing.T) {
skipUnlessAcceptanceTestsAllowed(t)
c, projectID, tearDown := setupWithProject(t)
defer tearDown()
hardwareReservations, _, err := c.HardwareReservations.List(projectID, nil)
if err != nil {
t.Fatal(err)
}
if len(hardwareReservations) != 0 {
t.Fatal("There should not be any hardware reservations.")
}
}
func TestHardwareReservationServiceOp_List(t *testing.T) {
type fields struct {
client requestDoer
}
type args struct {
projectID string
opts *ListOptions
}
tests := []struct {
name string
fields fields
args args
wantReservations []HardwareReservation
wantResp *Response
wantErr bool
}{{
name: "Pagination",
fields: fields{
client: &MockClient{
fnDoRequest: func(method, path string, body, v interface{}) (*Response, error) {
// Return one reservation per page, tests pagination
data := []HardwareReservation{{ID: "1"}, {ID: "2"}, {ID: "3"}}
if v, ok := v.(*hardwareReservationRoot); ok && method == http.MethodGet {
u, _ := url.Parse(path)
q, _ := url.ParseQuery(u.RawQuery)
page, _ := strconv.Atoi(q.Get("page"))
if page == 0 {
page = 1
}
v.HardwareReservations = []HardwareReservation{data[page-1]}
v.Meta.Total = len(data)
v.Meta.CurrentPageNum = page
if page < v.Meta.Total {
nextPage := page + 1
v.Meta.Next = &Href{Href: fmt.Sprintf("%s?page=%d", u.Path, nextPage)}
}
return &Response{}, nil
}
return nil, errBoom
},
},
},
args: args{projectID: testProjectId},
wantReservations: []HardwareReservation{{ID: "1"}, {ID: "2"}, {ID: "3"}},
wantResp: &Response{},
wantErr: false,
}, {
name: "Error",
fields: fields{
client: &MockClient{
fnDoRequest: func(method, path string, body, v interface{}) (*Response, error) {
return nil, errBoom
},
},
},
args: args{projectID: testProjectId},
wantReservations: nil,
wantResp: nil,
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &HardwareReservationServiceOp{
client: tt.fields.client,
}
gotReservations, gotResp, err := s.List(tt.args.projectID, tt.args.opts)
if (err != nil) != tt.wantErr {
t.Errorf("HardwareReservationServiceOp.List() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotReservations, tt.wantReservations) {
t.Errorf("HardwareReservationServiceOp.List() gotReservations = %v, want %v", gotReservations, tt.wantReservations)
}
if !reflect.DeepEqual(gotResp, tt.wantResp) {
t.Errorf("HardwareReservationServiceOp.List() gotResp = %v, want %v", gotResp, tt.wantResp)
}
})
}
}