From 8fd9975287ada2ed2f4e9e801339d0a525523da1 Mon Sep 17 00:00:00 2001 From: Rizal Widyarta Gowandy Date: Sat, 9 Oct 2021 21:14:43 +0700 Subject: [PATCH] feat(command): add new client method for get_all_raw_indexes Issue: https://github.com/meilisearch/meilisearch-go/issues/201 Extra: run gofmt the whole project --- .gitignore | 1 + client_index.go | 16 +++++++ client_index_test.go | 98 ++++++++++++++++++++++++++++++++++++++++++ index_search_test.go | 4 +- index_settings_test.go | 42 +++++++++--------- types.go | 2 +- 6 files changed, 139 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 05953f6f..1c12b665 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea meilisearch-go.iml +vendor diff --git a/client_index.go b/client_index.go index 285a005b..b2b950d8 100644 --- a/client_index.go +++ b/client_index.go @@ -46,6 +46,22 @@ func (c *Client) GetAllIndexes() (resp []*Index, err error) { return resp, nil } +func (c *Client) GetAllRawIndexes() (resp []map[string]interface{}, err error) { + resp = []map[string]interface{}{} + req := internalRequest{ + endpoint: "/indexes", + method: http.MethodGet, + withRequest: nil, + withResponse: &resp, + acceptedStatusCodes: []int{http.StatusOK}, + functionName: "GetAllRawIndexes", + } + if err := c.executeRequest(req); err != nil { + return nil, err + } + return resp, nil +} + func (c *Client) GetOrCreateIndex(config *IndexConfig) (resp *Index, err error) { resp, err = c.GetIndex(config.Uid) if err == nil { diff --git a/client_index_test.go b/client_index_test.go index 6277fd24..b9d1358a 100644 --- a/client_index_test.go +++ b/client_index_test.go @@ -463,6 +463,104 @@ func TestClient_GetAllIndexes(t *testing.T) { } } +func TestClient_GetAllRawIndexes(t *testing.T) { + type args struct { + uid []string + } + tests := []struct { + name string + client *Client + args args + wantResp []map[string]interface{} + }{ + { + name: "TestGelAllIndexesOnNoIndexes", + client: defaultClient, + args: args{ + uid: []string{}, + }, + wantResp: []map[string]interface{}{}, + }, + { + name: "TestBasicGelAllIndexes", + client: defaultClient, + args: args{ + uid: []string{"1"}, + }, + wantResp: []map[string]interface{}{ + { + "uid": "1", + }, + }, + }, + { + name: "TestGelAllIndexesWithCustomClient", + client: customClient, + args: args{ + uid: []string{"1"}, + }, + wantResp: []map[string]interface{}{ + { + "uid": "1", + }, + }, + }, + { + name: "TestGelAllIndexesOnMultipleIndex", + client: defaultClient, + args: args{ + uid: []string{"1", "2", "3"}, + }, + wantResp: []map[string]interface{}{ + { + "uid": "1", + }, + { + "uid": "2", + }, + { + "uid": "3", + }, + }, + }, + { + name: "TestGelAllIndexesOnMultipleIndexWithPrimaryKey", + client: defaultClient, + args: args{ + uid: []string{"1", "2", "3"}, + }, + wantResp: []map[string]interface{}{ + { + "uid": "1", + "primaryKey": "PrimaryKey1", + }, + { + "uid": "2", + "primaryKey": "PrimaryKey2", + }, + { + "uid": "3", + "primaryKey": "PrimaryKey3", + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := tt.client + t.Cleanup(cleanup(c)) + + for _, uid := range tt.args.uid { + _, err := c.CreateIndex(&IndexConfig{Uid: uid}) + require.NoError(t, err, "CreateIndex() in TestGetAllIndexes error should be nil") + } + gotResp, err := c.GetAllRawIndexes() + require.NoError(t, err) + require.Equal(t, len(tt.wantResp), len(gotResp)) + }) + } +} + func TestClient_GetIndex(t *testing.T) { type args struct { config IndexConfig diff --git a/index_search_test.go b/index_search_test.go index 65ef4e7b..45d2db6e 100644 --- a/index_search_test.go +++ b/index_search_test.go @@ -470,8 +470,8 @@ func TestIndex_SearchWithFilters(t *testing.T) { }, request: SearchRequest{ Filter: [][]string{ - []string{"year < 1850"}, - []string{"tag = romance"}, + {"year < 1850"}, + {"tag = romance"}, }, }, }, diff --git a/index_settings_test.go b/index_settings_test.go index fa403144..fa7988c0 100644 --- a/index_settings_test.go +++ b/index_settings_test.go @@ -221,7 +221,7 @@ func TestIndex_GetSettings(t *testing.T) { client: defaultClient, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -238,7 +238,7 @@ func TestIndex_GetSettings(t *testing.T) { client: customClient, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -656,7 +656,7 @@ func TestIndex_ResetSettings(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -676,7 +676,7 @@ func TestIndex_ResetSettings(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1217,7 +1217,7 @@ func TestIndex_UpdateSettings(t *testing.T) { FilterableAttributes: []string{ "title", }, - SortableAttributes: []string{ + SortableAttributes: []string{ "title", }, }, @@ -1226,7 +1226,7 @@ func TestIndex_UpdateSettings(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1261,7 +1261,7 @@ func TestIndex_UpdateSettings(t *testing.T) { FilterableAttributes: []string{ "title", }, - SortableAttributes: []string{ + SortableAttributes: []string{ "title", }, }, @@ -1270,7 +1270,7 @@ func TestIndex_UpdateSettings(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1370,7 +1370,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1431,7 +1431,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1492,7 +1492,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1553,7 +1553,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1614,7 +1614,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1649,7 +1649,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { FilterableAttributes: []string{ "title", }, - SortableAttributes: []string{}, + SortableAttributes: []string{}, }, secondRequest: Settings{ FilterableAttributes: []string{ @@ -1668,14 +1668,14 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { FilterableAttributes: []string{ "title", }, - SortableAttributes: []string{}, + SortableAttributes: []string{}, }, }, wantUpdate: &AsyncUpdateID{ UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1708,7 +1708,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { StopWords: []string{}, Synonyms: map[string][]string(nil), FilterableAttributes: []string{}, - SortableAttributes: []string{ + SortableAttributes: []string{ "title", }, }, @@ -1727,7 +1727,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { StopWords: []string{}, Synonyms: map[string][]string(nil), FilterableAttributes: []string{}, - SortableAttributes: []string{ + SortableAttributes: []string{ "title", }, }, @@ -1736,7 +1736,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) { UpdateID: 1, }, wantResp: &Settings{ - RankingRules: defaultRankingRules, + RankingRules: defaultRankingRules, DistinctAttribute: (*string)(nil), SearchableAttributes: []string{"*"}, DisplayedAttributes: []string{"*"}, @@ -1858,7 +1858,7 @@ func TestIndex_UpdateSynonyms(t *testing.T) { UID: "indexUID", client: defaultClient, request: map[string][]string{ - "wolverine": []string{"logan", "xmen"}, + "wolverine": {"logan", "xmen"}, }, }, wantUpdate: &AsyncUpdateID{ @@ -1871,7 +1871,7 @@ func TestIndex_UpdateSynonyms(t *testing.T) { UID: "indexUID", client: customClient, request: map[string][]string{ - "wolverine": []string{"logan", "xmen"}, + "wolverine": {"logan", "xmen"}, }, }, wantUpdate: &AsyncUpdateID{ diff --git a/types.go b/types.go index e3ad6f99..5a49d083 100644 --- a/types.go +++ b/types.go @@ -34,7 +34,7 @@ type Settings struct { StopWords []string `json:"stopWords,omitempty"` Synonyms map[string][]string `json:"synonyms,omitempty"` FilterableAttributes []string `json:"filterableAttributes,omitempty"` - SortableAttributes []string `json:"sortableAttributes,omitempty"` + SortableAttributes []string `json:"sortableAttributes,omitempty"` } // Version is the type that represents the versions in MeiliSearch