Skip to content

Commit

Permalink
Enable Go Modules, Fix tests for 2.0+ version (#78)
Browse files Browse the repository at this point in the history
* [add] Enable Go Modules. [fix] Fix tests for 2.0+ version

* [add] consistent flushall on dict tests
  • Loading branch information
filipecosta90 authored Jul 28, 2020
1 parent 236fe93 commit a48d16f
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
build: # test with redisearch:edge
docker:
- image: circleci/golang:1.9
- image: circleci/golang:1.12
- image: redislabs/redisearch:edge

working_directory: /go/src/github.com/RediSearch/redisearch-go
Expand All @@ -57,7 +57,7 @@ jobs:

build_nightly: # test nightly with redisearch:edge
docker:
- image: circleci/golang:1.9
- image: circleci/golang:1.12
- image: redislabs/redisearch:edge

working_directory: /go/src/github.com/RediSearch/redisearch-go
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
*
!/**/
!**/*.go
!**/go.mod
!**/go.sum
!.gitignore
!.circleci/config.yml
!/tests/*.bz2
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Go parameters
GOCMD=go
GOCMD=GO111MODULE=on go

GOBUILD=$(GOCMD) build
GOINSTALL=$(GOCMD) install
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOTEST=$(GOCMD) test -count=1
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod

Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/RediSearch/redisearch-go

go 1.12

require (
github.com/gomodule/redigo v2.0.0+incompatible
github.com/stretchr/testify v1.6.1
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
21 changes: 10 additions & 11 deletions redisearch/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"compress/bzip2"
"encoding/json"
"fmt"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
"log"
"math/rand"
"os"
"reflect"
"strconv"
"strings"
"testing"
)

"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
)

// Game struct which contains a Asin, a Description, a Title, a Price, and a list of categories
// a type and a list of social links
Expand All @@ -30,7 +30,6 @@ type Game struct {
Categories []string `json:"categories"`
}


func init() {
/* load test data */
value, exists := os.LookupEnv("REDISEARCH_RDB_LOADED")
Expand All @@ -50,7 +49,7 @@ func init() {
ndocs := 10000
docs := make([]Document, ndocs)
for i := 0; i < ndocs; i++ {
docs[i] = NewDocument(fmt.Sprintf("doc%d", i), 1).Set("foo", "hello world")
docs[i] = NewDocument(fmt.Sprintf("bench.ft.aggregate.doc%d", i), 1).Set("foo", "hello world")
}

if err := c.IndexOptions(DefaultIndexingOptions, docs...); err != nil {
Expand Down Expand Up @@ -317,9 +316,9 @@ func TestCursor_Serialize(t *testing.T) {
fields fields
want redis.Args
}{
{"TestCursor_Serialize_1", fields{1, 0, 0,}, redis.Args{"WITHCURSOR"}},
{"TestCursor_Serialize_2_MAXIDLE", fields{1, 0, 30000,}, redis.Args{"WITHCURSOR", "MAXIDLE", 30000}},
{"TestCursor_Serialize_3_COUNT_MAXIDLE", fields{1, 10, 30000,}, redis.Args{"WITHCURSOR", "COUNT", 10, "MAXIDLE", 30000}},
{"TestCursor_Serialize_1", fields{1, 0, 0}, redis.Args{"WITHCURSOR"}},
{"TestCursor_Serialize_2_MAXIDLE", fields{1, 0, 30000}, redis.Args{"WITHCURSOR", "MAXIDLE", 30000}},
{"TestCursor_Serialize_3_COUNT_MAXIDLE", fields{1, 10, 30000}, redis.Args{"WITHCURSOR", "COUNT", 10, "MAXIDLE", 30000}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -352,7 +351,7 @@ func TestGroupBy_AddFields(t *testing.T) {
}{
{"TestGroupBy_AddFields_1",
fields{[]string{}, nil, nil},
args{"a",},
args{"a"},
&GroupBy{[]string{"a"}, nil, nil},
},
}
Expand Down Expand Up @@ -651,8 +650,8 @@ func TestProcessAggResponse(t *testing.T) {
args args
want [][]string
}{
{"empty-reply", args{[]interface{}{}}, [][]string{},},
{"1-element-reply", args{[]interface{}{[]interface{}{"userFullName", "berge, julius", "count", "2783"}}}, [][]string{{"userFullName", "berge, julius", "count", "2783"}},},
{"empty-reply", args{[]interface{}{}}, [][]string{}},
{"1-element-reply", args{[]interface{}{[]interface{}{"userFullName", "berge, julius", "count", "2783"}}}, [][]string{{"userFullName", "berge, julius", "count", "2783"}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion redisearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,4 @@ func (i *Client) AddHash(docId string, score float32, language string, replace b
args = args.Add("REPLACE")
}
return redis.String(conn.Do("FT.ADDHASH", args...))
}
}
46 changes: 28 additions & 18 deletions redisearch/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/stretchr/testify/assert"
)

func flush(c *Client) (err error) {
conn := c.pool.Get()
defer conn.Close()
return conn.Send("FLUSHALL")
}

func TestClient_Get(t *testing.T) {

c := createClient("test-get")
Expand Down Expand Up @@ -72,7 +78,6 @@ func TestClient_Get(t *testing.T) {
}

func TestClient_MultiGet(t *testing.T) {

c := createClient("test-get")
c.Drop()

Expand Down Expand Up @@ -134,9 +139,9 @@ func TestClient_MultiGet(t *testing.T) {
}

func TestClient_DictAdd(t *testing.T) {
c := createClient("test-get")
_, err := c.pool.Get().Do("FLUSHALL")
assert.Nil(t, err)
c := createClient("TestClient_DictAdd_Index")
// dict tests require flushall
flush(c)

type fields struct {
pool ConnPool
Expand All @@ -155,8 +160,8 @@ func TestClient_DictAdd(t *testing.T) {
}{
{"empty-error", fields{pool: c.pool, name: c.name}, args{"dict1", []string{}}, 0, true},
{"1-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"}}, 1, false},
{"2nd-time-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1"}}, 0, false},
{"multi-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"t1", "t2", "t3", "t4", "t5"}}, 5, false},
{"2nd-time-term", fields{pool: c.pool, name: c.name}, args{"dict1", []string{"term1","term1"}}, 1, false},
{"multi-term", fields{pool: c.pool, name: c.name}, args{"dict-multi-term", []string{"t1", "t2", "t3", "t4", "t5"}}, 5, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -172,15 +177,16 @@ func TestClient_DictAdd(t *testing.T) {
if gotNewTerms != tt.wantNewTerms {
t.Errorf("DictAdd() gotNewTerms = %v, want %v", gotNewTerms, tt.wantNewTerms)
}
i.DictDel(tt.args.dictionaryName, tt.args.terms)
})
}
}

func TestClient_DictDel(t *testing.T) {

c := createClient("test-get")
_, err := c.pool.Get().Do("FLUSHALL")
assert.Nil(t, err)
c := createClient("TestClient_DictDel_Index")
// dict tests require flushall
flush(c)

terms := make([]string, 10)
for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -227,15 +233,15 @@ func TestClient_DictDel(t *testing.T) {
}

func TestClient_DictDump(t *testing.T) {
c := createClient("test-get")
_, err := c.pool.Get().Do("FLUSHALL")
assert.Nil(t, err)
c := createClient("TestClient_DictDump_Index")
// dict tests require flushall
flush(c)

terms1 := make([]string, 10)
for i := 0; i < 10; i++ {
terms1[i] = fmt.Sprintf("term%d", i)
}
c.DictAdd("dict1", terms1)
c.DictAdd("dictdump-dict1", terms1)

type fields struct {
pool ConnPool
Expand All @@ -252,7 +258,7 @@ func TestClient_DictDump(t *testing.T) {
wantErr bool
}{
{"empty-error", fields{pool: c.pool, name: c.name}, args{"dontexist"}, []string{}, true},
{"dict1", fields{pool: c.pool, name: c.name}, args{"dict1"}, terms1, false},
{"dictdump-dict1", fields{pool: c.pool, name: c.name}, args{"dictdump-dict1"}, terms1, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -381,7 +387,7 @@ func TestClient_AliasUpdate(t *testing.T) {

docs := make([]Document, 100)
for i := 0; i < 100; i++ {
docs[i] = NewDocument(fmt.Sprintf("doc-alias-del-%d", i), 1).Set("foo", "hello world").Set("bar", "hello world foo bar baz")
docs[i] = NewDocument(fmt.Sprintf("doc-alias-update-%d", i), 1).Set("foo", "hello world").Set("bar", "hello world foo bar baz")
}
err = c.Index(docs...)

Expand Down Expand Up @@ -418,7 +424,7 @@ func TestClient_AliasUpdate(t *testing.T) {

func TestClient_Config(t *testing.T) {
c := createClient("testconfigindex")

c.Drop()
ret, err := c.SetConfig("TIMEOUT", "100")
assert.Nil(t, err)
assert.Equal(t, "OK", ret)
Expand Down Expand Up @@ -527,8 +533,12 @@ func TestClient_AddHash(t *testing.T) {
c.pool.Get().Do("HMSET", "myhash", "field1", "Hello")

ret, err := c.AddHash("myhash", 1, "english", false)
assert.Nil(t, err)
assert.Equal(t, "OK", ret)
// Given that FT.ADDHASH is no longer valid for search2+ we assert it's error
if err != nil {
assert.Equal(t, "ERR unknown command `FT.ADDHASH`, with args beginning with: `testAddHash`, `myhash`, `1`, `LANGUAGE`, `english`, ", err.Error())
} else {
assert.Equal(t, "OK", ret)
}
}

func TestClient_AddField(t *testing.T) {
Expand Down
15 changes: 8 additions & 7 deletions redisearch/example_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/RediSearch/redisearch-go/redisearch"
"github.com/gomodule/redigo/redis"
"io/ioutil"
"log"
"os"
"time"

"github.com/RediSearch/redisearch-go/redisearch"
"github.com/gomodule/redigo/redis"
)

// exemplifies the NewClient function
Expand All @@ -33,7 +34,7 @@ func ExampleNewClient() {
}

// Create a document with an id and given score
doc := redisearch.NewDocument("doc1", 1.0)
doc := redisearch.NewDocument("ExampleNewClient:doc1", 1.0)
doc.Set("title", "Hello world").
Set("body", "foo bar").
Set("date", time.Now().Unix())
Expand All @@ -49,7 +50,7 @@ func ExampleNewClient() {
SetReturnFields("title"))

fmt.Println(docs[0].Id, docs[0].Properties["title"], total, err)
// Output: doc1 Hello world 1 <nil>
// Output: ExampleNewClient:doc1 Hello world 1 <nil>
}

// exemplifies the NewClientFromPool function
Expand All @@ -76,7 +77,7 @@ func ExampleNewClientFromPool() {
}

// Create a document with an id and given score
doc := redisearch.NewDocument("doc1", 1.0)
doc := redisearch.NewDocument("ExampleNewClientFromPool:doc2", 1.0)
doc.Set("title", "Hello world").
Set("body", "foo bar").
Set("date", time.Now().Unix())
Expand All @@ -92,7 +93,7 @@ func ExampleNewClientFromPool() {
SetReturnFields("title"))

fmt.Println(docs[0].Id, docs[0].Properties["title"], total, err)
// Output: doc1 Hello world 1 <nil>
// Output: ExampleNewClientFromPool:doc2 Hello world 1 <nil>
}

//Example of how to establish an SSL connection from your app to the RedisAI Server
Expand Down Expand Up @@ -163,7 +164,7 @@ func ExampleNewClientFromPool_ssl() {
}

// Create a document with an id and given score
doc := redisearch.NewDocument("doc1", 1.0)
doc := redisearch.NewDocument("ExampleNewClientFromPool_ssl:doc3", 1.0)
doc.Set("title", "Hello world").
Set("body", "foo bar").
Set("date", time.Now().Unix())
Expand Down
7 changes: 4 additions & 3 deletions redisearch/example_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package redisearch_test

import (
"fmt"
"github.com/RediSearch/redisearch-go/redisearch"
"log"
"time"

"github.com/RediSearch/redisearch-go/redisearch"
)

// exemplifies the CreateIndex function with a temporary index specification
Expand All @@ -25,7 +26,7 @@ func ExampleCreateIndex_temporary() {
}

// Create a document with an id and given score
doc := redisearch.NewDocument("doc1", 1.0)
doc := redisearch.NewDocument("ExampleCreateIndex_temporary:doc1", 1.0)
doc.Set("title", "Hello world").
Set("body", "foo bar").
Set("date", time.Now().Unix())
Expand All @@ -46,6 +47,6 @@ func ExampleCreateIndex_temporary() {
// Searching with limit and sorting
_, err = c.Info()
fmt.Println(err)
// Output: doc1 Hello world 1 <nil>
// Output: ExampleCreateIndex_temporary:doc1 Hello world 1 <nil>
// Unknown Index name
}
Loading

0 comments on commit a48d16f

Please sign in to comment.