|
6 | 6 | "fmt" |
7 | 7 | "net/http" |
8 | 8 | "net/http/httptest" |
| 9 | + "reflect" |
9 | 10 | "strconv" |
10 | 11 | "testing" |
11 | 12 | ) |
@@ -265,3 +266,171 @@ func TestRetries(t *testing.T) { |
265 | 266 | t.Fatalf("expected codespace name to be %q but got %q", csName, cs.Name) |
266 | 267 | } |
267 | 268 | } |
| 269 | + |
| 270 | +func TestCodespace_ExportData(t *testing.T) { |
| 271 | + type fields struct { |
| 272 | + Name string |
| 273 | + CreatedAt string |
| 274 | + DisplayName string |
| 275 | + LastUsedAt string |
| 276 | + Owner User |
| 277 | + Repository Repository |
| 278 | + State string |
| 279 | + GitStatus CodespaceGitStatus |
| 280 | + Connection CodespaceConnection |
| 281 | + Machine CodespaceMachine |
| 282 | + } |
| 283 | + type args struct { |
| 284 | + fields []string |
| 285 | + } |
| 286 | + tests := []struct { |
| 287 | + name string |
| 288 | + fields fields |
| 289 | + args args |
| 290 | + want map[string]interface{} |
| 291 | + }{ |
| 292 | + { |
| 293 | + name: "just name", |
| 294 | + fields: fields{ |
| 295 | + Name: "test", |
| 296 | + }, |
| 297 | + args: args{ |
| 298 | + fields: []string{"name"}, |
| 299 | + }, |
| 300 | + want: map[string]interface{}{ |
| 301 | + "name": "test", |
| 302 | + }, |
| 303 | + }, |
| 304 | + { |
| 305 | + name: "just owner", |
| 306 | + fields: fields{ |
| 307 | + Owner: User{ |
| 308 | + Login: "test", |
| 309 | + }, |
| 310 | + }, |
| 311 | + args: args{ |
| 312 | + fields: []string{"owner"}, |
| 313 | + }, |
| 314 | + want: map[string]interface{}{ |
| 315 | + "owner": "test", |
| 316 | + }, |
| 317 | + }, |
| 318 | + { |
| 319 | + name: "just machine", |
| 320 | + fields: fields{ |
| 321 | + Machine: CodespaceMachine{ |
| 322 | + Name: "test", |
| 323 | + }, |
| 324 | + }, |
| 325 | + args: args{ |
| 326 | + fields: []string{"machineName"}, |
| 327 | + }, |
| 328 | + want: map[string]interface{}{ |
| 329 | + "machineName": "test", |
| 330 | + }, |
| 331 | + }, |
| 332 | + } |
| 333 | + for _, tt := range tests { |
| 334 | + t.Run(tt.name, func(t *testing.T) { |
| 335 | + c := &Codespace{ |
| 336 | + Name: tt.fields.Name, |
| 337 | + CreatedAt: tt.fields.CreatedAt, |
| 338 | + DisplayName: tt.fields.DisplayName, |
| 339 | + LastUsedAt: tt.fields.LastUsedAt, |
| 340 | + Owner: tt.fields.Owner, |
| 341 | + Repository: tt.fields.Repository, |
| 342 | + State: tt.fields.State, |
| 343 | + GitStatus: tt.fields.GitStatus, |
| 344 | + Connection: tt.fields.Connection, |
| 345 | + Machine: tt.fields.Machine, |
| 346 | + } |
| 347 | + if got := c.ExportData(tt.args.fields); !reflect.DeepEqual(got, tt.want) { |
| 348 | + t.Errorf("Codespace.ExportData() = %v, want %v", got, tt.want) |
| 349 | + } |
| 350 | + }) |
| 351 | + } |
| 352 | +} |
| 353 | + |
| 354 | +func createFakeEditServer(t *testing.T, codespaceName string) *httptest.Server { |
| 355 | + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 356 | + checkPath := "/user/codespaces/" + codespaceName |
| 357 | + |
| 358 | + if r.URL.Path != checkPath { |
| 359 | + t.Fatal("Incorrect path") |
| 360 | + } |
| 361 | + |
| 362 | + if r.Method != http.MethodPatch { |
| 363 | + t.Fatal("Incorrect method") |
| 364 | + } |
| 365 | + |
| 366 | + body := r.Body |
| 367 | + if body == nil { |
| 368 | + t.Fatal("No body") |
| 369 | + } |
| 370 | + defer body.Close() |
| 371 | + |
| 372 | + var data map[string]interface{} |
| 373 | + err := json.NewDecoder(body).Decode(&data) |
| 374 | + |
| 375 | + if err != nil { |
| 376 | + t.Fatal(err) |
| 377 | + } |
| 378 | + |
| 379 | + if data["display_name"] != "changeTo" { |
| 380 | + t.Fatal("Incorrect display name") |
| 381 | + } |
| 382 | + |
| 383 | + response := Codespace{ |
| 384 | + DisplayName: "changeTo", |
| 385 | + } |
| 386 | + |
| 387 | + responseData, _ := json.Marshal(response) |
| 388 | + fmt.Fprint(w, string(responseData)) |
| 389 | + })) |
| 390 | +} |
| 391 | +func TestAPI_EditCodespace(t *testing.T) { |
| 392 | + type args struct { |
| 393 | + ctx context.Context |
| 394 | + codespaceName string |
| 395 | + params *EditCodespaceParams |
| 396 | + } |
| 397 | + tests := []struct { |
| 398 | + name string |
| 399 | + args args |
| 400 | + want *Codespace |
| 401 | + wantErr bool |
| 402 | + }{ |
| 403 | + { |
| 404 | + name: "success", |
| 405 | + args: args{ |
| 406 | + ctx: context.Background(), |
| 407 | + codespaceName: "test", |
| 408 | + params: &EditCodespaceParams{ |
| 409 | + DisplayName: "changeTo", |
| 410 | + }, |
| 411 | + }, |
| 412 | + want: &Codespace{ |
| 413 | + DisplayName: "changeTo", |
| 414 | + }, |
| 415 | + }, |
| 416 | + } |
| 417 | + for _, tt := range tests { |
| 418 | + t.Run(tt.name, func(t *testing.T) { |
| 419 | + svr := createFakeEditServer(t, tt.args.codespaceName) |
| 420 | + defer svr.Close() |
| 421 | + |
| 422 | + a := &API{ |
| 423 | + client: &http.Client{}, |
| 424 | + githubAPI: svr.URL, |
| 425 | + } |
| 426 | + got, err := a.EditCodespace(tt.args.ctx, tt.args.codespaceName, tt.args.params) |
| 427 | + if (err != nil) != tt.wantErr { |
| 428 | + t.Errorf("API.EditCodespace() error = %v, wantErr %v", err, tt.wantErr) |
| 429 | + return |
| 430 | + } |
| 431 | + if !reflect.DeepEqual(got, tt.want) { |
| 432 | + t.Errorf("API.EditCodespace() = %v, want %v", got.DisplayName, tt.want.DisplayName) |
| 433 | + } |
| 434 | + }) |
| 435 | + } |
| 436 | +} |
0 commit comments