From ee112dd00f890b91446a3ad5a35c0c9b888f5fb4 Mon Sep 17 00:00:00 2001 From: Tom Bayes Date: Wed, 19 Feb 2025 21:31:47 +0900 Subject: [PATCH 1/5] feat: add test codes for search_commands.go --- search_commands_test.go | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 search_commands_test.go diff --git a/search_commands_test.go b/search_commands_test.go new file mode 100644 index 0000000000..ed0aecfe27 --- /dev/null +++ b/search_commands_test.go @@ -0,0 +1,77 @@ +package redis_test + +import ( + "reflect" + + . "github.com/bsm/ginkgo/v2" + . "github.com/bsm/gomega" + "github.com/redis/go-redis/v9" +) + +var _ = Describe("FTAggregateQuery", func() { + It("returns only the base query when options is nil", func() { + args := redis.FTAggregateQuery("testQuery", nil) + Expect(args).To(Equal(redis.AggregateQuery{"testQuery"})) + }) + + It("includes VERBATIM and SCORER when options are set", func() { + options := &redis.FTAggregateOptions{ + Verbatim: true, + Scorer: "BM25", + } + args := redis.FTAggregateQuery("testQuery", options) + Expect(args[0]).To(Equal("testQuery")) + Expect(args).To(ContainElement("VERBATIM")) + Expect(args).To(ContainElement("SCORER")) + Expect(args).To(ContainElement("BM25")) + }) + + It("includes ADDSCORES when AddScores is true", func() { + options := &redis.FTAggregateOptions{ + AddScores: true, + } + args := redis.FTAggregateQuery("q", options) + Expect(args).To(ContainElement("ADDSCORES")) + }) + + It("includes LOADALL when LoadAll is true", func() { + options := &redis.FTAggregateOptions{ + LoadAll: true, + } + args := redis.FTAggregateQuery("q", options) + Expect(args).To(ContainElement("LOAD")) + Expect(args).To(ContainElement("*")) + }) + + It("includes LOAD when Load is provided", func() { + options := &redis.FTAggregateOptions{ + Load: []redis.FTAggregateLoad{ + {Field: "field1", As: "alias1"}, + {Field: "field2"}, + }, + } + args := redis.FTAggregateQuery("q", options) + // Verify LOAD options related arguments + Expect(args).To(ContainElement("LOAD")) + // Check that field names and aliases are present + Expect(args).To(ContainElement("field1")) + Expect(args).To(ContainElement("alias1")) + Expect(args).To(ContainElement("field2")) + }) + + It("includes TIMEOUT when Timeout > 0", func() { + options := &redis.FTAggregateOptions{ + Timeout: 500, + } + args := redis.FTAggregateQuery("q", options) + Expect(args).To(ContainElement("TIMEOUT")) + found := false + for _, a := range args { + if reflect.DeepEqual(a, 500) { + found = true + break + } + } + Expect(found).To(BeTrue()) + }) +}) From cff6483a8c0ec3d8a61b8e053bf90c7dc6b412a4 Mon Sep 17 00:00:00 2001 From: Tom Bayes Date: Tue, 25 Feb 2025 12:38:21 +0900 Subject: [PATCH 2/5] feat: move ftaggregate tests to search_test.go --- .gitignore | 1 + search_commands_test.go | 77 ----------------------------------------- search_test.go | 67 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 77 deletions(-) delete mode 100644 search_commands_test.go diff --git a/.gitignore b/.gitignore index f1883206a7..a02e552172 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ testdata/* *.tar.gz *.dic redis8tests.sh +.vscode diff --git a/search_commands_test.go b/search_commands_test.go deleted file mode 100644 index ed0aecfe27..0000000000 --- a/search_commands_test.go +++ /dev/null @@ -1,77 +0,0 @@ -package redis_test - -import ( - "reflect" - - . "github.com/bsm/ginkgo/v2" - . "github.com/bsm/gomega" - "github.com/redis/go-redis/v9" -) - -var _ = Describe("FTAggregateQuery", func() { - It("returns only the base query when options is nil", func() { - args := redis.FTAggregateQuery("testQuery", nil) - Expect(args).To(Equal(redis.AggregateQuery{"testQuery"})) - }) - - It("includes VERBATIM and SCORER when options are set", func() { - options := &redis.FTAggregateOptions{ - Verbatim: true, - Scorer: "BM25", - } - args := redis.FTAggregateQuery("testQuery", options) - Expect(args[0]).To(Equal("testQuery")) - Expect(args).To(ContainElement("VERBATIM")) - Expect(args).To(ContainElement("SCORER")) - Expect(args).To(ContainElement("BM25")) - }) - - It("includes ADDSCORES when AddScores is true", func() { - options := &redis.FTAggregateOptions{ - AddScores: true, - } - args := redis.FTAggregateQuery("q", options) - Expect(args).To(ContainElement("ADDSCORES")) - }) - - It("includes LOADALL when LoadAll is true", func() { - options := &redis.FTAggregateOptions{ - LoadAll: true, - } - args := redis.FTAggregateQuery("q", options) - Expect(args).To(ContainElement("LOAD")) - Expect(args).To(ContainElement("*")) - }) - - It("includes LOAD when Load is provided", func() { - options := &redis.FTAggregateOptions{ - Load: []redis.FTAggregateLoad{ - {Field: "field1", As: "alias1"}, - {Field: "field2"}, - }, - } - args := redis.FTAggregateQuery("q", options) - // Verify LOAD options related arguments - Expect(args).To(ContainElement("LOAD")) - // Check that field names and aliases are present - Expect(args).To(ContainElement("field1")) - Expect(args).To(ContainElement("alias1")) - Expect(args).To(ContainElement("field2")) - }) - - It("includes TIMEOUT when Timeout > 0", func() { - options := &redis.FTAggregateOptions{ - Timeout: 500, - } - args := redis.FTAggregateQuery("q", options) - Expect(args).To(ContainElement("TIMEOUT")) - found := false - for _, a := range args { - if reflect.DeepEqual(a, 500) { - found = true - break - } - } - Expect(found).To(BeTrue()) - }) -}) diff --git a/search_test.go b/search_test.go index ea3460d3d3..2a29526ea3 100644 --- a/search_test.go +++ b/search_test.go @@ -3,6 +3,7 @@ package redis_test import ( "context" "fmt" + "reflect" "strconv" "time" @@ -806,6 +807,72 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { } }) + It("should return only the base query when options is nil", Label("search", "ftaggregate"), func() { + args := redis.FTAggregateQuery("testQuery", nil) + Expect(args).To(Equal(redis.AggregateQuery{"testQuery"})) + }) + + It("should include VERBATIM and SCORER when options are set", Label("search", "ftaggregate"), func() { + options := &redis.FTAggregateOptions{ + Verbatim: true, + Scorer: "BM25", + } + args := redis.FTAggregateQuery("testQuery", options) + Expect(args[0]).To(Equal("testQuery")) + Expect(args).To(ContainElement("VERBATIM")) + Expect(args).To(ContainElement("SCORER")) + Expect(args).To(ContainElement("BM25")) + }) + + It("should include ADDSCORES when AddScores is true", Label("search", "ftaggregate"), func() { + options := &redis.FTAggregateOptions{ + AddScores: true, + } + args := redis.FTAggregateQuery("q", options) + Expect(args).To(ContainElement("ADDSCORES")) + }) + + It("should include LOADALL when LoadAll is true", Label("search", "ftaggregate"), func() { + options := &redis.FTAggregateOptions{ + LoadAll: true, + } + args := redis.FTAggregateQuery("q", options) + Expect(args).To(ContainElement("LOAD")) + Expect(args).To(ContainElement("*")) + }) + + It("should include LOAD when Load is provided", Label("search", "ftaggregate"), func() { + options := &redis.FTAggregateOptions{ + Load: []redis.FTAggregateLoad{ + {Field: "field1", As: "alias1"}, + {Field: "field2"}, + }, + } + args := redis.FTAggregateQuery("q", options) + // Verify LOAD options related arguments + Expect(args).To(ContainElement("LOAD")) + // Check that field names and aliases are present + Expect(args).To(ContainElement("field1")) + Expect(args).To(ContainElement("alias1")) + Expect(args).To(ContainElement("field2")) + }) + + It("should include TIMEOUT when Timeout > 0", Label("search", "ftaggregate"), func() { + options := &redis.FTAggregateOptions{ + Timeout: 500, + } + args := redis.FTAggregateQuery("q", options) + Expect(args).To(ContainElement("TIMEOUT")) + found := false + for _, a := range args { + if reflect.DeepEqual(a, 500) { + found = true + break + } + } + Expect(found).To(BeTrue()) + }) + It("should FTSearch SkipInitialScan", Label("search", "ftsearch"), func() { client.HSet(ctx, "doc1", "foo", "bar") From 1027710a8032885e7cef35e7affb7416bfe30fd7 Mon Sep 17 00:00:00 2001 From: Tom Bayes Date: Wed, 26 Feb 2025 11:05:49 +0900 Subject: [PATCH 3/5] Update search_test.go Co-authored-by: Nedyalko Dyakov --- search_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/search_test.go b/search_test.go index c93f1667b7..08b393bc62 100644 --- a/search_test.go +++ b/search_test.go @@ -863,8 +863,9 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { args := redis.FTAggregateQuery("q", options) Expect(args).To(ContainElement("TIMEOUT")) found := false - for _, a := range args { - if reflect.DeepEqual(a, 500) { + for i, a := range args { + if fmt.Sprintf("%s", a) == "TIMEOUT" { + Expect(fmt.Sprintf("%s", args[i+1])).To(Equal("500")) found = true break } From 9f4dba8ec8940eea8ec3959d3541b9e579f4e6e2 Mon Sep 17 00:00:00 2001 From: Tom Bayes Date: Wed, 26 Feb 2025 11:07:33 +0900 Subject: [PATCH 4/5] feat: remove reflect from test --- search_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/search_test.go b/search_test.go index 08b393bc62..1c7df1425a 100644 --- a/search_test.go +++ b/search_test.go @@ -3,7 +3,6 @@ package redis_test import ( "context" "fmt" - "reflect" "strconv" "time" From 9394298498962045c22fb06c2869b5e0fdd3edd7 Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Wed, 26 Feb 2025 14:05:03 +0200 Subject: [PATCH 5/5] Update search_test.go fix type in Sprintf --- search_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search_test.go b/search_test.go index 1c7df1425a..1098319e05 100644 --- a/search_test.go +++ b/search_test.go @@ -864,7 +864,7 @@ var _ = Describe("RediSearch commands Resp 2", Label("search"), func() { found := false for i, a := range args { if fmt.Sprintf("%s", a) == "TIMEOUT" { - Expect(fmt.Sprintf("%s", args[i+1])).To(Equal("500")) + Expect(fmt.Sprintf("%d", args[i+1])).To(Equal("500")) found = true break }