Skip to content

Commit

Permalink
feat(command): add new client method for get_all_raw_indexes
Browse files Browse the repository at this point in the history
Issue: #201
Extra: run gofmt the whole project
  • Loading branch information
Rizal Widyarta Gowandy committed Oct 9, 2021
1 parent 4dc2cc3 commit 8fd9975
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
meilisearch-go.iml
vendor
16 changes: 16 additions & 0 deletions client_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
98 changes: 98 additions & 0 deletions client_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions index_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ func TestIndex_SearchWithFilters(t *testing.T) {
},
request: SearchRequest{
Filter: [][]string{
[]string{"year < 1850"},
[]string{"tag = romance"},
{"year < 1850"},
{"tag = romance"},
},
},
},
Expand Down
42 changes: 21 additions & 21 deletions index_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{"*"},
Expand All @@ -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{"*"},
Expand Down Expand Up @@ -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{"*"},
Expand All @@ -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{"*"},
Expand Down Expand Up @@ -1217,7 +1217,7 @@ func TestIndex_UpdateSettings(t *testing.T) {
FilterableAttributes: []string{
"title",
},
SortableAttributes: []string{
SortableAttributes: []string{
"title",
},
},
Expand All @@ -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{"*"},
Expand Down Expand Up @@ -1261,7 +1261,7 @@ func TestIndex_UpdateSettings(t *testing.T) {
FilterableAttributes: []string{
"title",
},
SortableAttributes: []string{
SortableAttributes: []string{
"title",
},
},
Expand All @@ -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{"*"},
Expand Down Expand Up @@ -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{"*"},
Expand Down Expand Up @@ -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{"*"},
Expand Down Expand Up @@ -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{"*"},
Expand Down Expand Up @@ -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{"*"},
Expand Down Expand Up @@ -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{"*"},
Expand Down Expand Up @@ -1649,7 +1649,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) {
FilterableAttributes: []string{
"title",
},
SortableAttributes: []string{},
SortableAttributes: []string{},
},
secondRequest: Settings{
FilterableAttributes: []string{
Expand All @@ -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{"*"},
Expand Down Expand Up @@ -1708,7 +1708,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) {
StopWords: []string{},
Synonyms: map[string][]string(nil),
FilterableAttributes: []string{},
SortableAttributes: []string{
SortableAttributes: []string{
"title",
},
},
Expand All @@ -1727,7 +1727,7 @@ func TestIndex_UpdateSettingsOneByOne(t *testing.T) {
StopWords: []string{},
Synonyms: map[string][]string(nil),
FilterableAttributes: []string{},
SortableAttributes: []string{
SortableAttributes: []string{
"title",
},
},
Expand All @@ -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{"*"},
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8fd9975

Please sign in to comment.