Skip to content

Commit

Permalink
Merge #604
Browse files Browse the repository at this point in the history
604: Changes related to the next Meilisearch release (v1.13) r=curquiza a=meili-bot

Related to this issue: meilisearch/integration-guides#314

This PR:
- gathers the changes related to the next Meilisearch release (v1.13) so that this package is ready when the official release is out.
- should pass the tests against the [latest pre-release of Meilisearch](https://github.com/meilisearch/meilisearch/releases).
- might eventually contain test failures until the Meilisearch v1.13 is out.

⚠️ This PR should NOT be merged until the next release of Meilisearch (v1.13) is out.

_This PR is auto-generated for the [pre-release week](https://github.com/meilisearch/integration-guides/blob/main/resources/pre-release-week.md) purpose._


Co-authored-by: meili-bot <74670311+meili-bot@users.noreply.github.com>
Co-authored-by: Strift <lau.cazanove@gmail.com>
Co-authored-by: Javad Rajabzadeh <ja7ad@live.com>
Co-authored-by: Laurent Cazanove <lau.cazanove@gmail.com>
Co-authored-by: Bruno Casali <bruno@meilisearch.com>
  • Loading branch information
5 people authored Feb 19, 2025
2 parents 40ea18f + 2c5bf59 commit a6d464c
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 66 deletions.
21 changes: 17 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,23 @@ func (c *client) sendRequest(
}

if !c.contentEncoding.IsZero() {
body, err = c.encoder.Encode(body)
if err != nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to marshal with json.Marshal: %w", err))
// Get the data from the buffer before encoding
var bufData []byte
if buf, ok := body.(*bytes.Buffer); ok {
bufData = buf.Bytes()
encodedBuf, err := c.encoder.Encode(bytes.NewReader(bufData))
if err != nil {
if buf, ok := body.(*bytes.Buffer); ok {
c.bufferPool.Put(buf)
}
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to encode request body: %w", err))
}
// Return the original buffer to the pool since we have a new one
if buf, ok := body.(*bytes.Buffer); ok {
c.bufferPool.Put(buf)
}
body = encodedBuf
}
}
}
Expand Down
49 changes: 34 additions & 15 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"context"
"encoding/json"
"errors"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)

// Mock structures for testing
Expand All @@ -26,6 +26,18 @@ type mockJsonMarshaller struct {
Bar string `json:"bar"`
}

// failingEncoder is used to simulate encoder failure
type failingEncoder struct{}

func (fe failingEncoder) Encode(r io.Reader) (*bytes.Buffer, error) {
return nil, errors.New("dummy encoding failure")
}

// Implement Decode method to satisfy the encoder interface, though it won't be used here
func (fe failingEncoder) Decode(b []byte, v interface{}) error {
return errors.New("dummy decode failure")
}

func TestExecuteRequest(t *testing.T) {
retryCount := 0

Expand Down Expand Up @@ -115,6 +127,8 @@ func TestExecuteRequest(t *testing.T) {
}
w.WriteHeader(http.StatusBadGateway)
retryCount++
} else if r.URL.Path == "/dummy" {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
Expand Down Expand Up @@ -241,19 +255,6 @@ func TestExecuteRequest(t *testing.T) {
expectedResp: nil,
wantErr: true,
},
{
name: "Successful request with io.reader",
internalReq: &internalRequest{
endpoint: "/io-reader",
method: http.MethodPost,
withResponse: nil,
contentType: "text/plain",
withRequest: strings.NewReader("foobar"),
acceptedStatusCodes: []int{http.StatusOK},
},
expectedResp: nil,
wantErr: false,
},
{
name: "Test null body response",
internalReq: &internalRequest{
Expand Down Expand Up @@ -422,6 +423,19 @@ func TestExecuteRequest(t *testing.T) {
withTimeout: true,
wantErr: true,
},
{
name: "Test request encoding with []byte encode failure",
internalReq: &internalRequest{
endpoint: "/dummy",
method: http.MethodPost,
withRequest: []byte("test data"),
contentType: "text/plain",
acceptedStatusCodes: []int{http.StatusOK},
},
expectedResp: nil,
contentEncoding: GzipEncoding,
wantErr: true,
},
}

for _, tt := range tests {
Expand All @@ -438,6 +452,11 @@ func TestExecuteRequest(t *testing.T) {
},
})

// For the specific test case, override the encoder to force an error
if tt.name == "Test request encoding with []byte encode failure" {
c.encoder = failingEncoder{}
}

ctx := context.Background()

if tt.withTimeout {
Expand Down
4 changes: 4 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ coverage:
range: 60..80
round: down
precision: 2
status:
project:
default:
threshold: 1%
6 changes: 0 additions & 6 deletions features.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ func (m *meilisearch) ExperimentalFeatures() *ExperimentalFeatures {
return &ExperimentalFeatures{client: m.client}
}

func (ef *ExperimentalFeatures) SetVectorStore(vectorStore bool) *ExperimentalFeatures {
ef.VectorStore = &vectorStore
return ef
}

func (ef *ExperimentalFeatures) SetLogsRoute(logsRoute bool) *ExperimentalFeatures {
ef.LogsRoute = &logsRoute
return ef
Expand Down Expand Up @@ -69,7 +64,6 @@ func (ef *ExperimentalFeatures) Update() (*ExperimentalFeaturesResult, error) {

func (ef *ExperimentalFeatures) UpdateWithContext(ctx context.Context) (*ExperimentalFeaturesResult, error) {
request := ExperimentalFeaturesBase{
VectorStore: ef.VectorStore,
LogsRoute: ef.LogsRoute,
Metrics: ef.Metrics,
EditDocumentsByFunction: ef.EditDocumentsByFunction,
Expand Down
2 changes: 0 additions & 2 deletions features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ func TestUpdate_ExperimentalFeatures(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ef := tt.client.ExperimentalFeatures()
ef.SetVectorStore(true)
ef.SetLogsRoute(true)
ef.SetMetrics(true)
ef.SetEditDocumentsByFunction(true)
ef.SetContainsFilter(true)
gotResp, err := ef.Update()
require.NoError(t, err)
require.Equal(t, true, gotResp.VectorStore, "ExperimentalFeatures.Update() should return vectorStore as true")
require.Equal(t, true, gotResp.LogsRoute, "ExperimentalFeatures.Update() should return logsRoute as true")
require.Equal(t, true, gotResp.Metrics, "ExperimentalFeatures.Update() should return metrics as true")
require.Equal(t, true, gotResp.EditDocumentsByFunction, "ExperimentalFeatures.Update() should return editDocumentsByFunction as true")
Expand Down
18 changes: 13 additions & 5 deletions index_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package meilisearch
import (
"crypto/tls"
"encoding/json"
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

func TestIndex_SearchWithContentEncoding(t *testing.T) {
Expand All @@ -23,6 +24,8 @@ func TestIndex_SearchWithContentEncoding(t *testing.T) {
Query: "prince",
Request: &SearchRequest{
IndexUID: "indexUID",
Limit: 20,
Offset: 0,
},
FacetRequest: &FacetSearchRequest{
FacetName: "tag",
Expand Down Expand Up @@ -56,6 +59,8 @@ func TestIndex_SearchWithContentEncoding(t *testing.T) {
Query: "prince",
Request: &SearchRequest{
IndexUID: "indexUID",
Limit: 20,
Offset: 0,
},
Response: &SearchResponse{
Hits: []interface{}{
Expand Down Expand Up @@ -89,6 +94,8 @@ func TestIndex_SearchWithContentEncoding(t *testing.T) {
Query: "prince",
Request: &SearchRequest{
IndexUID: "indexUID",
Limit: 20,
Offset: 0,
},
Response: &SearchResponse{
Hits: []interface{}{
Expand Down Expand Up @@ -137,7 +144,8 @@ func TestIndex_SearchWithContentEncoding(t *testing.T) {
require.NoError(t, err, "error unmarshalling raw got SearchResponse")
require.Equal(t, len(tt.Response.Hits), len(resp.Hits))

task, err := i.UpdateFilterableAttributes(&[]string{"tag"})
filterableAttrs := []string{"tag"}
task, err := i.UpdateFilterableAttributes(&filterableAttrs)
require.NoError(t, err)
testWaitForTask(t, i, task)

Expand Down Expand Up @@ -2015,15 +2023,15 @@ func TestIndex_SearchSimilarDocuments(t *testing.T) {
UID: "indexUID",
client: sv,
request: &SimilarDocumentQuery{
Id: "123",
Id: "123",
Embedder: "default",
},
resp: new(SimilarDocumentResult),
wantErr: false,
},
{
UID: "indexUID",
client: sv,
UID: "indexUID",
client: sv,
request: &SimilarDocumentQuery{
Embedder: "default",
},
Expand Down
2 changes: 1 addition & 1 deletion index_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestIndex_GetTasks(t *testing.T) {
query: &TasksQuery{
IndexUIDS: []string{"indexUID"},
Limit: 10,
From: 1,
From: 0,
Statuses: []TaskStatus{TaskStatusSucceeded},
Types: []TaskType{TaskTypeDocumentAdditionOrUpdate},
Reverse: true,
Expand Down
3 changes: 2 additions & 1 deletion meilisearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2241,7 +2241,8 @@ func Test_ListIndex(t *testing.T) {

for _, idx := range tt.Indexes {
info, err := c.CreateIndex(&IndexConfig{
Uid: idx,
Uid: idx,
PrimaryKey: "id", // Adding a default primary key
})
if tt.WantErr {
require.Error(t, err)
Expand Down
2 changes: 0 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,13 @@ type UpdateDocumentByFunctionRequest struct {

// ExperimentalFeaturesResult represents the experimental features result from the API.
type ExperimentalFeaturesBase struct {
VectorStore *bool `json:"vectorStore,omitempty"`
LogsRoute *bool `json:"logsRoute,omitempty"`
Metrics *bool `json:"metrics,omitempty"`
EditDocumentsByFunction *bool `json:"editDocumentsByFunction,omitempty"`
ContainsFilter *bool `json:"containsFilter,omitempty"`
}

type ExperimentalFeaturesResult struct {
VectorStore bool `json:"vectorStore"`
LogsRoute bool `json:"logsRoute"`
Metrics bool `json:"metrics"`
EditDocumentsByFunction bool `json:"editDocumentsByFunction"`
Expand Down
33 changes: 3 additions & 30 deletions types_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a6d464c

Please sign in to comment.