Skip to content

Commit

Permalink
[add] added TestClient_SynUpdate. TestClient_SynDump running on >= v2…
Browse files Browse the repository at this point in the history
….0 also
  • Loading branch information
filipecosta90 committed Aug 17, 2020
1 parent 4958ddc commit eb0a92d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
2 changes: 1 addition & 1 deletion redisearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func (i *Client) SynAdd(indexName string, terms []string) (int64, error) {
return redis.Int64(conn.Do("FT.SYNADD", args...))
}

// Updates a synonym group.
// Updates a synonym group, with additional terms.
func (i *Client) SynUpdate(indexName string, synonymGroupId int64, terms []string) (string, error) {
conn := i.pool.Get()
defer conn.Close()
Expand Down
89 changes: 68 additions & 21 deletions redisearch/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,30 +516,25 @@ func TestClient_SynAdd(t *testing.T) {

func TestClient_SynDump(t *testing.T) {
c := createClient("testsyndump")
version, err := c.GetRediSearchVersion()
assert.Nil(t, err)
if version <= 10699 {

sc := NewSchema(DefaultOptions).
AddField(NewTextField("name")).
AddField(NewTextField("addr"))
c.Drop()
err := c.CreateIndex(sc)
assert.Nil(t, err)

gid, err := c.SynAdd("testsyndump", []string{"girl", "baby"})
assert.Nil(t, err)
assert.True(t, gid >= 0)
sc := NewSchema(DefaultOptions).
AddField(NewTextField("name")).
AddField(NewTextField("addr"))
c.Drop()
err := c.CreateIndex(sc)
assert.Nil(t, err)
ret, err := c.SynUpdate("testsyndump", 1, []string{"girl", "baby"})
assert.Nil(t, err)
assert.Equal(t, "OK", ret)

gid2, err := c.SynAdd("testsyndump", []string{"child"})
_, err = c.SynUpdate("testsyndump", 2, []string{"child"})
assert.Nil(t, err)
assert.Equal(t, "OK", ret)

m, err := c.SynDump("testsyndump")
assert.Contains(t, m, "baby")
assert.Contains(t, m, "girl")
assert.Contains(t, m, "child")
assert.Equal(t, gid, m["baby"][0])
assert.Equal(t, gid2, m["child"][0])
}
m, err := c.SynDump("testsyndump")
assert.Contains(t, m, "baby")
assert.Contains(t, m, "girl")
assert.Contains(t, m, "child")
teardown(c)
}

Expand Down Expand Up @@ -618,6 +613,18 @@ func TestClient_CreateIndexWithIndexDefinition(t *testing.T) {
AddField(NewTextField("name")).
AddField(NewTextField("lang")).
AddField(NewTextField("addr")), NewIndexDefinition().SetLanguageField("lang")}, false},
{"score_field-indexDefinition", args{NewSchema(DefaultOptions).
AddField(NewTextField("name")).
AddField(NewTextField("addr")).AddField(NewNumericField("score")), NewIndexDefinition().SetScoreField("score")}, false},
{"payload_field-indexDefinition", args{NewSchema(DefaultOptions).
AddField(NewTextField("name")).
AddField(NewTextField("addr")).AddField(NewNumericField("score")).AddField(NewTextField("payload")), NewIndexDefinition().SetPayloadField("payload")}, false},
{"prefix-indexDefinition", args{NewSchema(DefaultOptions).
AddField(NewTextField("name")).
AddField(NewTextField("addr")).AddField(NewNumericField("score")).AddField(NewTextField("payload")), NewIndexDefinition().AddPrefix("doc:*")}, false},
{"filter-indexDefinition", args{NewSchema(DefaultOptions).
AddField(NewTextField("name")).
AddField(NewTextField("addr")).AddField(NewNumericField("score")).AddField(NewTextField("payload")), NewIndexDefinition().SetFilterExpression("@score > 0")}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -629,3 +636,43 @@ func TestClient_CreateIndexWithIndexDefinition(t *testing.T) {
}
}
}

func TestClient_SynUpdate(t *testing.T) {
c := createClient("syn-update-test")
sc := NewSchema(DefaultOptions).
AddField(NewTextField("name")).
AddField(NewTextField("addr"))

type args struct {
indexName string
synonymGroupId int64
terms []string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{"1-syn", args{"syn-update-test", 1, []string{"abc"}}, "OK", false},
{"3-syn", args{"syn-update-test", 1, []string{"abc", "def", "ghi"}}, "OK", false},
{"err-empty-syn", args{"syn-update-test", 1, []string{}}, "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c.Drop()
err := c.CreateIndex(sc)
assert.Nil(t, err)

got, err := c.SynUpdate(tt.args.indexName, tt.args.synonymGroupId, tt.args.terms)
if (err != nil) != tt.wantErr {
t.Errorf("SynUpdate() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("SynUpdate() got = %v, want %v", got, tt.want)
}
teardown(c)
})
}
}

0 comments on commit eb0a92d

Please sign in to comment.