Skip to content

Commit

Permalink
remove separate unit test step add more client unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RaynorChavez committed Nov 7, 2024
1 parent 2b43426 commit 75a885e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 15 deletions.
16 changes: 1 addition & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
99 changes: 99 additions & 0 deletions go_marqo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 75a885e

Please sign in to comment.