Skip to content

Commit

Permalink
Add tests for faceting routes: settings and get/update/reset attribut…
Browse files Browse the repository at this point in the history
…esForFaceting
  • Loading branch information
eskombro committed Jun 11, 2020
1 parent 35af4d5 commit 4358dd1
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 15 deletions.
119 changes: 111 additions & 8 deletions client_settings_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meilisearch

import (
"fmt"
"reflect"
"testing"
)
Expand All @@ -27,13 +28,14 @@ func TestClientSettings_GetAll(t *testing.T) {
}

expected := Settings{
RankingRules: []string{"typo", "words", "proximity", "attribute", "wordsPosition", "exactness"},
DistinctAttribute: nil,
SearchableAttributes: []string{},
DisplayedAttributes: []string{},
StopWords: []string{},
Synonyms: map[string][]string{},
AcceptNewFields: true,
RankingRules: []string{"typo", "words", "proximity", "attribute", "wordsPosition", "exactness"},
DistinctAttribute: nil,
SearchableAttributes: []string{},
DisplayedAttributes: []string{},
StopWords: []string{},
Synonyms: map[string][]string{},
AcceptNewFields: true,
AttributesForFaceting: []string{},
}

if !reflect.DeepEqual(*settingsRes, expected) {
Expand Down Expand Up @@ -64,7 +66,8 @@ func TestClientSettings_UpdateAll(t *testing.T) {
Synonyms: map[string][]string{
"car": []string{"automobile"},
},
AcceptNewFields: false,
AcceptNewFields: false,
AttributesForFaceting: []string{"title"},
}

updateIDRes, err := client.Settings(indexUID).UpdateAll(settings)
Expand Down Expand Up @@ -616,3 +619,103 @@ func TestClientSettings_UpdateAcceptNewFields(t *testing.T) {
client.DefaultWaitForPendingUpdate(indexUID, updateIDRes)

}

func TestClientSettings_GetAttributesForFaceting(t *testing.T) {
var indexUID = "TestClientSettings_GetAttributesForFaceting"

var client = NewClient(Config{
Host: "http://localhost:7700",
})

_, err := client.Indexes().Create(CreateIndexRequest{
UID: indexUID,
})

if err != nil {
t.Fatal(err)
}

AttributesForFacetingRes, err := client.Settings(indexUID).GetAttributesForFaceting()

if err != nil {
t.Fatal(err)
}

if reflect.DeepEqual(*AttributesForFacetingRes, nil) {
t.Fatal("getAttributesForFaceting: Error getting attributesForFaceting on empty index")
}
}

func TestClientSettings_UpdateAttributesForFaceting(t *testing.T) {
var indexUID = "TestClientSettings_UpdateAttributesForFaceting"

var client = NewClient(Config{
Host: "http://localhost:7700",
})

attributesForFaceting := []string{"tag", "title"}

_, err := client.Indexes().Create(CreateIndexRequest{
UID: indexUID,
})

if err != nil {
t.Fatal(err)
}

updateIDRes, err := client.Settings(indexUID).UpdateAttributesForFaceting(attributesForFaceting)

if err != nil {
t.Fatal(err)
}

client.DefaultWaitForPendingUpdate(indexUID, updateIDRes)
r, _ := client.Settings(indexUID).GetAttributesForFaceting()
if !reflect.DeepEqual(*r, attributesForFaceting) {
fmt.Println(*r)
t.Fatal("updateAttributesForFaceting: Error getting attributesForFaceting after update")
}
}

func TestClientSettings_ResetAttributesForFaceting(t *testing.T) {
var indexUID = "TestClientSettings_ResetAttributesForFaceting"

var client = NewClient(Config{
Host: "http://localhost:7700",
})

attributesForFaceting := []string{"tag", "title"}

_, err := client.Indexes().Create(CreateIndexRequest{
UID: indexUID,
})

if err != nil {
t.Fatal(err)
}

updateIDRes, err := client.Settings(indexUID).UpdateAttributesForFaceting(attributesForFaceting)

if err != nil {
t.Fatal(err)
}

client.DefaultWaitForPendingUpdate(indexUID, updateIDRes)
r, _ := client.Settings(indexUID).GetAttributesForFaceting()
if !reflect.DeepEqual(*r, attributesForFaceting) {
fmt.Println(*r)
t.Fatal("resetAttributesForFaceting: Error getting attributesForFaceting after update")
}

updateIDRes, err = client.Settings(indexUID).ResetAttributesForFaceting()

if err != nil {
t.Fatal(err)
}

client.DefaultWaitForPendingUpdate(indexUID, updateIDRes)
r, _ = client.Settings(indexUID).GetAttributesForFaceting()
if reflect.DeepEqual(*r, nil) {
t.Fatal("resetAttributesForFaceting: Error getting attributesForFaceting after reset")
}
}
15 changes: 8 additions & 7 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ type Index struct {

// Settings is the type that represent the settings in MeiliSearch
type Settings struct {
RankingRules []string `json:"rankingRules,omitempty"`
DistinctAttribute *string `json:"distinctAttribute,omitempty"`
SearchableAttributes []string `json:"searchableAttributes,omitempty"`
DisplayedAttributes []string `json:"displayedAttributes,omitempty"`
StopWords []string `json:"stopWords,omitempty"`
Synonyms map[string][]string `json:"synonyms,omitempty"`
AcceptNewFields bool `json:"acceptNewFields,omitempty"`
RankingRules []string `json:"rankingRules,omitempty"`
DistinctAttribute *string `json:"distinctAttribute,omitempty"`
SearchableAttributes []string `json:"searchableAttributes,omitempty"`
DisplayedAttributes []string `json:"displayedAttributes,omitempty"`
StopWords []string `json:"stopWords,omitempty"`
Synonyms map[string][]string `json:"synonyms,omitempty"`
AcceptNewFields bool `json:"acceptNewFields,omitempty"`
AttributesForFaceting []string `json:"attributesForFaceting,omitempty"`
}

// Version is the type that represent the versions in MeiliSearch
Expand Down

0 comments on commit 4358dd1

Please sign in to comment.