From 75a885e1429ffe6d2d5a4d383bf1107c75748137 Mon Sep 17 00:00:00 2001 From: Raynor Chavez Date: Thu, 7 Nov 2024 17:34:23 +1100 Subject: [PATCH] remove separate unit test step add more client unit tests --- .github/workflows/test.yml | 16 +----- go_marqo/client_test.go | 99 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 21f2239..6c23684 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,23 +34,9 @@ jobs: run: | git diff --compact-summary --exit-code || \ (echo; echo "Unexpected difference in directories after code generation. Run 'go generate ./...' command and commit."; exit 1) - unit-test: - name: Unit Tests - needs: build - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-go@v5 - with: - go-version-file: 'go.mod' - cache: true - - run: go mod download - - name: Run unit tests - run: go test -v -cover ./... -short test: name: Terraform Provider Acceptance Tests - needs: [build, unit-test] + needs: build runs-on: ubuntu-latest timeout-minutes: 60 strategy: diff --git a/go_marqo/client_test.go b/go_marqo/client_test.go index 84ef47c..7a00a3d 100644 --- a/go_marqo/client_test.go +++ b/go_marqo/client_test.go @@ -111,3 +111,102 @@ func TestCreateIndex(t *testing.T) { err := client.CreateIndex("test-index", settings) assert.NoError(t, err) } + +func TestGetIndexStats(t *testing.T) { + // Create a test server to mock the API response + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/indexes/test-index/stats", r.URL.Path) + w.WriteHeader(http.StatusOK) + _, err := w.Write([]byte(`{ + "numberOfDocuments": 100, + "numberOfVectors": 200, + "backend": { + "memory_used_percentage": 75.5, + "storage_used_percentage": 60.2 + } + }`)) + if err != nil { + t.Fatal(err) + } + })) + defer server.Close() + + client := &go_marqo.Client{ + BaseURL: server.URL, + APIKey: "test-api-key", + } + + stats, err := client.GetIndexStats("test-index") + assert.NoError(t, err) + assert.Equal(t, int64(100), stats.NumberOfDocuments) + assert.Equal(t, int64(200), stats.NumberOfVectors) + assert.Equal(t, 75.5, stats.Backend.MemoryUsedPercentage) + assert.Equal(t, 60.2, stats.Backend.StorageUsedPercentage) +} + +func TestDeleteIndex(t *testing.T) { + // Create a test server to mock the API response + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/indexes/test-index", r.URL.Path) + assert.Equal(t, "DELETE", r.Method) + w.WriteHeader(http.StatusOK) + _, err := w.Write([]byte(`{"message": "Index deleted successfully"}`)) + if err != nil { + t.Fatal(err) + } + })) + defer server.Close() + + client := &go_marqo.Client{ + BaseURL: server.URL, + APIKey: "test-api-key", + } + + err := client.DeleteIndex("test-index") + assert.NoError(t, err) +} + +func TestUpdateIndex(t *testing.T) { + // Create a test server to mock the API response + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Verify request method and path + assert.Equal(t, "PUT", r.Method) + assert.Equal(t, "/indexes/test-index", r.URL.Path) + + // Verify headers + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) + assert.Equal(t, "test-api-key", r.Header.Get("X-API-KEY")) + + // Read and verify request body + body, err := io.ReadAll(r.Body) + assert.NoError(t, err) + + // Verify JSON payload + var settings map[string]interface{} + err = json.Unmarshal(body, &settings) + assert.NoError(t, err) + assert.Equal(t, "gpu", settings["type"]) + + // Return success response + w.WriteHeader(http.StatusOK) + _, err = w.Write([]byte(`{"message": "Index updated successfully"}`)) + if err != nil { + t.Fatal(err) + } + })) + defer server.Close() + + client := &go_marqo.Client{ + BaseURL: server.URL, + APIKey: "test-api-key", + } + + // Test settings + settings := map[string]interface{}{ + "type": "gpu", + } + + // Test the UpdateIndex function + err := client.UpdateIndex("test-index", settings) + assert.NoError(t, err) +}