From ca1ab2f16b39df0f5eadbe3d9e5dab16daf0c295 Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 13 Dec 2024 16:55:46 -0600 Subject: [PATCH] client/instances test files --- api/client_test.go | 36 ++++++++++++++++++++++++++++++++++++ api/instances_test.go | 23 +++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 api/client_test.go create mode 100644 api/instances_test.go diff --git a/api/client_test.go b/api/client_test.go new file mode 100644 index 0000000..f1778e9 --- /dev/null +++ b/api/client_test.go @@ -0,0 +1,36 @@ +package api + +import ( + "net/http" + "testing" +) + +func TestNewClient(t *testing.T) { + apiKey := "test-api-key" + client := NewClient(apiKey) + + if client.apiKey != apiKey { + t.Errorf("expected apiKey to be %s, got %s", apiKey, client.apiKey) + } + + if client.baseURL != "https://console.vast.ai/api/v0" { + t.Errorf("expected baseURL to be %s, got %s", "https://console.vast.ai/api/v0", client.baseURL) + } + + if client.httpClient == nil { + t.Error("expected httpClient to be initialized") + } +} + +func TestApply(t *testing.T) { + apiKey := "abc123" + client := NewClient(apiKey) + req, _ := http.NewRequest("GET", "http://example.com", nil) + + client.Apply(req) + + expected := "Bearer " + apiKey + if got := req.Header.Get("Authorization"); got != expected { + t.Errorf("expected Authorization header to be %s, got %s", expected, got) + } +} diff --git a/api/instances_test.go b/api/instances_test.go new file mode 100644 index 0000000..9938ef8 --- /dev/null +++ b/api/instances_test.go @@ -0,0 +1,23 @@ +//go:build real + +package api + +import ( + "testing" +) + +func GetInstancesTest(t *testing.T) { + // Create a new client with the test server's URL + client := NewClient("test") + + // Call the GetInstances method + resp, err := client.GetInstances() + if err != nil { + t.Fatal(err) + } + + // Ensure the response is valid + if len(resp.Instances) != 0 { + t.Fatalf("expected 0 instances, got %d", len(resp.Instances)) + } +}