-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
589: Refactor interface pollution r=curquiza a=Ja7ad # Pull Request ## What does this PR do? - This PR fix interface pollution in service and index manager and don't have break changes. ## PR checklist Please check if your PR fulfills the following requirements: - [ ] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Javad <ja7ad@live.com>
- Loading branch information
Showing
8 changed files
with
1,218 additions
and
1,072 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package meilisearch | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func (i *index) GetTask(taskUID int64) (*Task, error) { | ||
return i.GetTaskWithContext(context.Background(), taskUID) | ||
} | ||
|
||
func (i *index) GetTaskWithContext(ctx context.Context, taskUID int64) (*Task, error) { | ||
return getTask(ctx, i.client, taskUID) | ||
} | ||
|
||
func (i *index) GetTasks(param *TasksQuery) (*TaskResult, error) { | ||
return i.GetTasksWithContext(context.Background(), param) | ||
} | ||
|
||
func (i *index) GetTasksWithContext(ctx context.Context, param *TasksQuery) (*TaskResult, error) { | ||
resp := new(TaskResult) | ||
req := &internalRequest{ | ||
endpoint: "/tasks", | ||
method: http.MethodGet, | ||
withRequest: nil, | ||
withResponse: resp, | ||
withQueryParams: map[string]string{}, | ||
acceptedStatusCodes: []int{http.StatusOK}, | ||
functionName: "GetTasks", | ||
} | ||
if param != nil { | ||
if param.Limit != 0 { | ||
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10) | ||
} | ||
if param.From != 0 { | ||
req.withQueryParams["from"] = strconv.FormatInt(param.From, 10) | ||
} | ||
if len(param.Statuses) != 0 { | ||
statuses := make([]string, len(param.Statuses)) | ||
for i, status := range param.Statuses { | ||
statuses[i] = string(status) | ||
} | ||
req.withQueryParams["statuses"] = strings.Join(statuses, ",") | ||
} | ||
|
||
if len(param.Types) != 0 { | ||
types := make([]string, len(param.Types)) | ||
for i, t := range param.Types { | ||
types[i] = string(t) | ||
} | ||
req.withQueryParams["types"] = strings.Join(types, ",") | ||
} | ||
if len(param.IndexUIDS) != 0 { | ||
param.IndexUIDS = append(param.IndexUIDS, i.uid) | ||
req.withQueryParams["indexUids"] = strings.Join(param.IndexUIDS, ",") | ||
} else { | ||
req.withQueryParams["indexUids"] = i.uid | ||
} | ||
} | ||
if err := i.client.executeRequest(ctx, req); err != nil { | ||
return nil, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (i *index) WaitForTask(taskUID int64, interval time.Duration) (*Task, error) { | ||
return waitForTask(context.Background(), i.client, taskUID, interval) | ||
} | ||
|
||
func (i *index) WaitForTaskWithContext(ctx context.Context, taskUID int64, interval time.Duration) (*Task, error) { | ||
return waitForTask(ctx, i.client, taskUID, interval) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,280 @@ | ||
package meilisearch | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestIndex_GetTask(t *testing.T) { | ||
sv := setup(t, "") | ||
customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ | ||
InsecureSkipVerify: true, | ||
})) | ||
|
||
type args struct { | ||
UID string | ||
client ServiceManager | ||
taskUID int64 | ||
document []docTest | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "TestIndexBasicGetTask", | ||
args: args{ | ||
UID: "TestIndexBasicGetTask", | ||
client: sv, | ||
taskUID: 0, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TestIndexGetTaskWithCustomClient", | ||
args: args{ | ||
UID: "TestIndexGetTaskWithCustomClient", | ||
client: customSv, | ||
taskUID: 0, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TestIndexGetTask", | ||
args: args{ | ||
UID: "TestIndexGetTask", | ||
client: sv, | ||
taskUID: 0, | ||
document: []docTest{ | ||
{ID: "456", Name: "Le Petit Prince"}, | ||
{ID: "1", Name: "Alice In Wonderland"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
t.Cleanup(cleanup(sv, customSv)) | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := tt.args.client | ||
i := c.Index(tt.args.UID) | ||
t.Cleanup(cleanup(c)) | ||
|
||
task, err := i.AddDocuments(tt.args.document) | ||
require.NoError(t, err) | ||
|
||
_, err = c.WaitForTask(task.TaskUID, 0) | ||
require.NoError(t, err) | ||
|
||
gotResp, err := i.GetTask(task.TaskUID) | ||
require.NoError(t, err) | ||
require.NotNil(t, gotResp) | ||
require.GreaterOrEqual(t, gotResp.UID, tt.args.taskUID) | ||
require.Equal(t, gotResp.IndexUID, tt.args.UID) | ||
require.Equal(t, gotResp.Status, TaskStatusSucceeded) | ||
|
||
// Make sure that timestamps are also retrieved | ||
require.NotZero(t, gotResp.EnqueuedAt) | ||
require.NotZero(t, gotResp.StartedAt) | ||
require.NotZero(t, gotResp.FinishedAt) | ||
}) | ||
} | ||
} | ||
|
||
func TestIndex_GetTasks(t *testing.T) { | ||
sv := setup(t, "") | ||
customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ | ||
InsecureSkipVerify: true, | ||
})) | ||
|
||
type args struct { | ||
UID string | ||
client ServiceManager | ||
document []docTest | ||
query *TasksQuery | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{ | ||
name: "TestIndexBasicGetTasks", | ||
args: args{ | ||
UID: "indexUID", | ||
client: sv, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TestIndexGetTasksWithCustomClient", | ||
args: args{ | ||
UID: "indexUID", | ||
client: customSv, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TestIndexBasicGetTasksWithFilters", | ||
args: args{ | ||
UID: "indexUID", | ||
client: sv, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
}, | ||
query: &TasksQuery{ | ||
Statuses: []TaskStatus{TaskStatusSucceeded}, | ||
Types: []TaskType{TaskTypeDocumentAdditionOrUpdate}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TestTasksWithParams", | ||
args: args{ | ||
UID: "indexUID", | ||
client: sv, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
}, | ||
query: &TasksQuery{ | ||
IndexUIDS: []string{"indexUID"}, | ||
Limit: 10, | ||
From: 1, | ||
Statuses: []TaskStatus{TaskStatusSucceeded}, | ||
Types: []TaskType{TaskTypeDocumentAdditionOrUpdate}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := tt.args.client | ||
i := c.Index(tt.args.UID) | ||
t.Cleanup(cleanup(c)) | ||
|
||
task, err := i.AddDocuments(tt.args.document) | ||
require.NoError(t, err) | ||
|
||
_, err = c.WaitForTask(task.TaskUID, 0) | ||
require.NoError(t, err) | ||
|
||
gotResp, err := i.GetTasks(tt.args.query) | ||
require.NoError(t, err) | ||
require.NotNil(t, (*gotResp).Results[0].Status) | ||
require.NotNil(t, (*gotResp).Results[0].Type) | ||
}) | ||
} | ||
} | ||
|
||
func TestIndex_WaitForTask(t *testing.T) { | ||
sv := setup(t, "") | ||
customSv := setup(t, "", WithCustomClientWithTLS(&tls.Config{ | ||
InsecureSkipVerify: true, | ||
})) | ||
|
||
type args struct { | ||
UID string | ||
client ServiceManager | ||
interval time.Duration | ||
timeout time.Duration | ||
document []docTest | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want TaskStatus | ||
}{ | ||
{ | ||
name: "TestWaitForTask50", | ||
args: args{ | ||
UID: "TestWaitForTask50", | ||
client: sv, | ||
interval: time.Millisecond * 50, | ||
timeout: time.Second * 5, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
{ID: "456", Name: "Le Petit Prince"}, | ||
{ID: "1", Name: "Alice In Wonderland"}, | ||
}, | ||
}, | ||
want: "succeeded", | ||
}, | ||
{ | ||
name: "TestWaitForTask50WithCustomClient", | ||
args: args{ | ||
UID: "TestWaitForTask50WithCustomClient", | ||
client: customSv, | ||
interval: time.Millisecond * 50, | ||
timeout: time.Second * 5, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
{ID: "456", Name: "Le Petit Prince"}, | ||
{ID: "1", Name: "Alice In Wonderland"}, | ||
}, | ||
}, | ||
want: "succeeded", | ||
}, | ||
{ | ||
name: "TestWaitForTask10", | ||
args: args{ | ||
UID: "TestWaitForTask10", | ||
client: sv, | ||
interval: time.Millisecond * 10, | ||
timeout: time.Second * 5, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
{ID: "456", Name: "Le Petit Prince"}, | ||
{ID: "1", Name: "Alice In Wonderland"}, | ||
}, | ||
}, | ||
want: "succeeded", | ||
}, | ||
{ | ||
name: "TestWaitForTaskWithTimeout", | ||
args: args{ | ||
UID: "TestWaitForTaskWithTimeout", | ||
client: sv, | ||
interval: time.Millisecond * 50, | ||
timeout: time.Millisecond * 10, | ||
document: []docTest{ | ||
{ID: "123", Name: "Pride and Prejudice"}, | ||
{ID: "456", Name: "Le Petit Prince"}, | ||
{ID: "1", Name: "Alice In Wonderland"}, | ||
}, | ||
}, | ||
want: "succeeded", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := tt.args.client | ||
i := c.Index(tt.args.UID) | ||
t.Cleanup(cleanup(c)) | ||
|
||
task, err := i.AddDocuments(tt.args.document) | ||
require.NoError(t, err) | ||
|
||
ctx, cancelFunc := context.WithTimeout(context.Background(), tt.args.timeout) | ||
defer cancelFunc() | ||
|
||
gotTask, err := i.WaitForTaskWithContext(ctx, task.TaskUID, 0) | ||
if tt.args.timeout < tt.args.interval { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tt.want, gotTask.Status) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.