Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pkg/cmd/shared/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func GetSynonyms(client *search.APIClient, srcIndex string) ([]search.SynonymHit
srcIndex,
*search.NewEmptySearchSynonymsParams(),
search.WithAggregator(func(res any, _ error) {
response, _ := res.(search.SearchSynonymsResponse)
response, ok := res.(*search.SearchSynonymsResponse)
if !ok || response == nil {
return
}
synonyms = append(synonyms, response.Hits...)
}),
)
Expand All @@ -32,7 +35,10 @@ func GetRules(client *search.APIClient, srcIndex string) ([]search.Rule, error)
srcIndex,
*search.NewEmptySearchRulesParams(),
search.WithAggregator(func(res any, _ error) {
response, _ := res.(search.SearchRulesResponse)
response, ok := res.(*search.SearchRulesResponse)
if !ok || response == nil {
return
}
rules = append(rules, response.Hits...)
}),
)
Expand Down
153 changes: 153 additions & 0 deletions pkg/cmd/shared/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package config

import (
"testing"

"github.com/algolia/algoliasearch-client-go/v4/algolia/search"
"github.com/algolia/algoliasearch-client-go/v4/algolia/transport"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/algolia/cli/pkg/httpmock"
"github.com/algolia/cli/pkg/iostreams"
"github.com/algolia/cli/pkg/utils"
)

func newTestClient(t *testing.T, r *httpmock.Registry) *search.APIClient {
t.Helper()

cfg := search.SearchConfiguration{
Configuration: transport.Configuration{
AppID: "default",
ApiKey: "default",
Requester: r,
},
}
client, err := search.NewClientWithConfig(cfg)
require.NoError(t, err)

return client
}

func Test_GetSynonyms(t *testing.T) {
r := httpmock.Registry{}
r.Register(
httpmock.REST("POST", "1/indexes/foo/synonyms/search"),
httpmock.JSONResponse(search.SearchSynonymsResponse{
Hits: []search.SynonymHit{
{ObjectID: "foo", Type: "synonym"},
{ObjectID: "bar", Type: "synonym"},
},
}),
)
defer r.Verify(t)

client := newTestClient(t, &r)

synonyms, err := GetSynonyms(client, "foo")
require.NoError(t, err)
assert.Equal(t, []search.SynonymHit{
{ObjectID: "foo", Type: "synonym"},
{ObjectID: "bar", Type: "synonym"},
}, synonyms)
}

func Test_GetRules(t *testing.T) {
r := httpmock.Registry{}
r.Register(
httpmock.REST("POST", "1/indexes/foo/rules/search"),
httpmock.JSONResponse(search.SearchRulesResponse{
Hits: []search.Rule{
{ObjectID: "rule-1"},
{ObjectID: "rule-2"},
},
}),
)
defer r.Verify(t)

client := newTestClient(t, &r)

rules, err := GetRules(client, "foo")
require.NoError(t, err)
assert.Equal(t, []search.Rule{
{ObjectID: "rule-1"},
{ObjectID: "rule-2"},
}, rules)
}

func Test_GetIndexConfig(t *testing.T) {
cs := iostreams.NewColorScheme(false, false, false)

tests := []struct {
name string
scope []string
synonymHits []search.SynonymHit
ruleHits []search.Rule
settings search.SettingsResponse
wantErr bool
assertFn func(t *testing.T, cfg *ExportConfigJSON)
}{
{
name: "exports synonyms",
scope: []string{"synonyms"},
synonymHits: []search.SynonymHit{
{ObjectID: "foo", Type: "synonym"},
},
assertFn: func(t *testing.T, cfg *ExportConfigJSON) {
assert.Len(t, cfg.Synonyms, 1)
assert.Equal(t, "foo", cfg.Synonyms[0].ObjectID)
},
},
{
name: "exports rules",
scope: []string{"rules"},
ruleHits: []search.Rule{
{ObjectID: "rule-1"},
},
assertFn: func(t *testing.T, cfg *ExportConfigJSON) {
assert.Len(t, cfg.Rules, 1)
assert.Equal(t, "rule-1", cfg.Rules[0].ObjectID)
},
},
{
name: "no config to export returns error",
scope: []string{"rules", "synonyms"},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httpmock.Registry{}
if utils.Contains(tt.scope, "synonyms") {
r.Register(
httpmock.REST("POST", "1/indexes/foo/synonyms/search"),
httpmock.JSONResponse(search.SearchSynonymsResponse{Hits: tt.synonymHits}),
)
}
if utils.Contains(tt.scope, "rules") {
r.Register(
httpmock.REST("POST", "1/indexes/foo/rules/search"),
httpmock.JSONResponse(search.SearchRulesResponse{Hits: tt.ruleHits}),
)
}
if utils.Contains(tt.scope, "settings") {
r.Register(
httpmock.REST("GET", "1/indexes/foo/settings"),
httpmock.JSONResponse(tt.settings),
)
}
defer r.Verify(t)

client := newTestClient(t, &r)

cfg, err := GetIndexConfig(client, "foo", tt.scope, cs)
if tt.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)
tt.assertFn(t, cfg)
})
}
}