File: location_test.go

package info (click to toggle)
golang-github-hetznercloud-hcloud-go 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,072 kB
  • sloc: sh: 5; makefile: 2
file content (273 lines) | stat: -rw-r--r-- 6,513 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package hcloud

import (
	"context"
	"encoding/json"
	"net/http"
	"testing"

	"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)

func TestLocationClient(t *testing.T) {
	t.Run("GetByID", func(t *testing.T) {
		env := newTestEnv()
		defer env.Teardown()

		env.Mux.HandleFunc("/locations/1", func(w http.ResponseWriter, r *http.Request) {
			json.NewEncoder(w).Encode(schema.LocationGetResponse{
				Location: schema.Location{
					ID: 1,
				},
			})
		})

		ctx := context.Background()
		location, _, err := env.Client.Location.GetByID(ctx, 1)
		if err != nil {
			t.Fatal(err)
		}
		if location == nil {
			t.Fatal("no location")
		}
		if location.ID != 1 {
			t.Errorf("unexpected location ID: %v", location.ID)
		}

		t.Run("via Get", func(t *testing.T) {
			location, _, err := env.Client.Location.Get(ctx, "1")
			if err != nil {
				t.Fatal(err)
			}
			if location == nil {
				t.Fatal("no location")
			}
			if location.ID != 1 {
				t.Errorf("unexpected location ID: %v", location.ID)
			}
		})
	})

	t.Run("GetByID (not found)", func(t *testing.T) {
		env := newTestEnv()
		defer env.Teardown()

		env.Mux.HandleFunc("/locations/1", func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Content-Type", "application/json")
			w.WriteHeader(http.StatusNotFound)
			json.NewEncoder(w).Encode(schema.ErrorResponse{
				Error: schema.Error{
					Code: string(ErrorCodeNotFound),
				},
			})
		})

		ctx := context.Background()
		location, _, err := env.Client.Location.GetByID(ctx, 1)
		if err != nil {
			t.Fatal(err)
		}
		if location != nil {
			t.Fatal("expected no location")
		}
	})

	t.Run("GetByName", func(t *testing.T) {
		env := newTestEnv()
		defer env.Teardown()

		env.Mux.HandleFunc("/locations", func(w http.ResponseWriter, r *http.Request) {
			if r.URL.RawQuery != "name=fsn1-dc8" {
				t.Fatal("missing name query")
			}
			json.NewEncoder(w).Encode(schema.LocationListResponse{
				Locations: []schema.Location{
					{
						ID: 1,
					},
				},
			})
		})

		ctx := context.Background()
		location, _, err := env.Client.Location.GetByName(ctx, "fsn1-dc8")
		if err != nil {
			t.Fatal(err)
		}
		if location == nil {
			t.Fatal("no location")
		}
		if location.ID != 1 {
			t.Errorf("unexpected location ID: %v", location.ID)
		}

		t.Run("via Get", func(t *testing.T) {
			location, _, err := env.Client.Location.Get(ctx, "fsn1-dc8")
			if err != nil {
				t.Fatal(err)
			}
			if location == nil {
				t.Fatal("no location")
			}
			if location.ID != 1 {
				t.Errorf("unexpected location ID: %v", location.ID)
			}
		})
	})

	t.Run("GetByName (not found)", func(t *testing.T) {
		env := newTestEnv()
		defer env.Teardown()

		env.Mux.HandleFunc("/locations", func(w http.ResponseWriter, r *http.Request) {
			if r.URL.RawQuery != "name=fsn1-dc8" {
				t.Fatal("missing name query")
			}
			json.NewEncoder(w).Encode(schema.LocationListResponse{
				Locations: []schema.Location{},
			})
		})

		ctx := context.Background()
		location, _, err := env.Client.Location.GetByName(ctx, "fsn1-dc8")
		if err != nil {
			t.Fatal(err)
		}
		if location != nil {
			t.Fatal("unexpected location")
		}
	})

	t.Run("GetByName (empty)", func(t *testing.T) {
		env := newTestEnv()
		defer env.Teardown()

		ctx := context.Background()
		location, _, err := env.Client.Location.GetByName(ctx, "")
		if err != nil {
			t.Fatal(err)
		}
		if location != nil {
			t.Fatal("unexpected location")
		}
	})

	t.Run("List", func(t *testing.T) {
		env := newTestEnv()
		defer env.Teardown()

		env.Mux.HandleFunc("/locations", func(w http.ResponseWriter, r *http.Request) {
			if page := r.URL.Query().Get("page"); page != "2" {
				t.Errorf("expected page 2; got %q", page)
			}
			if perPage := r.URL.Query().Get("per_page"); perPage != "50" {
				t.Errorf("expected per_page 50; got %q", perPage)
			}
			if name := r.URL.Query().Get("name"); name != "fsn1" {
				t.Errorf("expected name fsn1; got %q", name)
			}
			json.NewEncoder(w).Encode(schema.LocationListResponse{
				Locations: []schema.Location{
					{ID: 1},
					{ID: 2},
				},
			})
		})

		opts := LocationListOpts{}
		opts.Page = 2
		opts.PerPage = 50
		opts.Name = "fsn1"

		ctx := context.Background()
		locations, _, err := env.Client.Location.List(ctx, opts)
		if err != nil {
			t.Fatal(err)
		}
		if len(locations) != 2 {
			t.Fatal("expected 2 locations")
		}
	})

	t.Run("All", func(t *testing.T) {
		env := newTestEnv()
		defer env.Teardown()

		env.Mux.HandleFunc("/locations", func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Content-Type", "application/json")
			json.NewEncoder(w).Encode(struct {
				Locations []schema.Location `json:"locations"`
				Meta      schema.Meta       `json:"meta"`
			}{
				Locations: []schema.Location{
					{ID: 1},
					{ID: 2},
					{ID: 3},
				},
				Meta: schema.Meta{
					Pagination: &schema.MetaPagination{
						Page:         1,
						LastPage:     1,
						PerPage:      3,
						TotalEntries: 3,
					},
				},
			})
		})

		ctx := context.Background()
		locations, err := env.Client.Location.All(ctx)
		if err != nil {
			t.Fatalf("Location.List failed: %s", err)
		}
		if len(locations) != 3 {
			t.Fatalf("expected 3 locations; got %d", len(locations))
		}
		if locations[0].ID != 1 || locations[1].ID != 2 || locations[2].ID != 3 {
			t.Errorf("unexpected locations")
		}
	})

	t.Run("AllWithOpts", func(t *testing.T) {
		env := newTestEnv()
		defer env.Teardown()

		env.Mux.HandleFunc("/locations", func(w http.ResponseWriter, r *http.Request) {
			if name := r.URL.Query().Get("name"); name != "my-location" {
				t.Errorf("unexpected name: %s", name)
			}
			w.Header().Set("Content-Type", "application/json")
			json.NewEncoder(w).Encode(struct {
				Locations []schema.Location `json:"locations"`
				Meta      schema.Meta       `json:"meta"`
			}{
				Locations: []schema.Location{
					{ID: 1},
					{ID: 2},
					{ID: 3},
				},
				Meta: schema.Meta{
					Pagination: &schema.MetaPagination{
						Page:         1,
						LastPage:     1,
						PerPage:      3,
						TotalEntries: 3,
					},
				},
			})
		})

		ctx := context.Background()
		opts := LocationListOpts{Name: "my-location"}
		locations, err := env.Client.Location.AllWithOpts(ctx, opts)
		if err != nil {
			t.Fatalf("Location.List failed: %s", err)
		}
		if len(locations) != 3 {
			t.Fatalf("expected 3 locations; got %d", len(locations))
		}
		if locations[0].ID != 1 || locations[1].ID != 2 || locations[2].ID != 3 {
			t.Errorf("unexpected locations")
		}
	})
}