diff --git a/db/db.go b/db/db.go index 4ef08f10f..e528627cf 100644 --- a/db/db.go +++ b/db/db.go @@ -1469,6 +1469,7 @@ func (db database) SearchBots(s string, limit, offset int) []BotRes { // set limit limitStr := strconv.Itoa(limit) offsetStr := strconv.Itoa(offset) + s = strings.ReplaceAll(s, " ", " & ") db.db.Raw( `SELECT uuid, owner_pub_key, name, unique_name, img, description, tags, price_per_use, ts_rank(tsv, q) as rank FROM bots, to_tsquery(?) q diff --git a/db/structs.go b/db/structs.go index b906611be..f722ee23c 100644 --- a/db/structs.go +++ b/db/structs.go @@ -55,7 +55,7 @@ type Bot struct { Name string `json:"name"` UniqueName string `json:"unique_name"` Description string `json:"description"` - Tags pq.StringArray ` ` + Tags pq.StringArray `gorm:"type:text[]" json:"tags"` Img string `json:"img"` PricePerUse int64 `json:"price_per_use"` Created *time.Time `json:"created"` @@ -64,6 +64,7 @@ type Bot struct { Deleted bool `json:"deleted"` MemberCount uint64 `json:"member_count"` OwnerRouteHint string `json:"owner_route_hint"` + Tsv string `gorm:"type:tsvector"` } // Bot struct @@ -73,7 +74,7 @@ type BotRes struct { Name string `json:"name"` UniqueName string `json:"unique_name"` Description string `json:"description"` - Tags pq.StringArray `json:"tags"` + Tags pq.StringArray `gorm:"type:text[]" json:"tags"` Img string `json:"img"` PricePerUse int64 `json:"price_per_use"` } diff --git a/db/test_config.go b/db/test_config.go index 3d43fd6f1..37fe66e17 100644 --- a/db/test_config.go +++ b/db/test_config.go @@ -57,6 +57,7 @@ func InitTestDB() { db.AutoMigrate(&Workspace{}) db.AutoMigrate(&WorkspaceUsers{}) db.AutoMigrate(&WorkspaceUserRoles{}) + db.AutoMigrate(&Bot{}) people := TestDB.GetAllPeople() for _, p := range people { diff --git a/handlers/bots_test.go b/handlers/bots_test.go index cd4b4a48b..40ca200a7 100644 --- a/handlers/bots_test.go +++ b/handlers/bots_test.go @@ -144,14 +144,33 @@ func TestGetBotsByOwner(t *testing.T) { } func TestSearchBots(t *testing.T) { - mockDb := dbMocks.NewDatabase(t) - btHandler := NewBotHandler(mockDb) + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + + btHandler := NewBotHandler(db.TestDB) t.Run("successful search query returns data", func(t *testing.T) { rr := httptest.NewRecorder() handler := http.HandlerFunc(btHandler.SearchBots) - query := "bot" + bot := db.Bot{ + UUID: "uuid-1", + OwnerPubKey: "owner-pubkey-1", + OwnerAlias: "owner-alias-1", + Name: "Bot 1", + UniqueName: "unique-bot-1", + Description: "Description for Bot 1", + Tags: pq.StringArray{"tag1", "tag2"}, + Img: "bot-img-url-1", + PricePerUse: 100, + } + + bot, err := db.TestDB.CreateOrEditBot(bot) + if err != nil { + t.Fatalf("Failed to create bot: %v", err) + } + + query := bot.Name bots := []db.BotRes{ { @@ -166,8 +185,6 @@ func TestSearchBots(t *testing.T) { }, } - mockDb.On("SearchBots", query, mock.AnythingOfType("int"), mock.AnythingOfType("int")).Return(bots) - rctx := chi.NewRouteContext() rctx.URLParams.Add("query", query) req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/search/bots/"+query, nil) @@ -183,8 +200,6 @@ func TestSearchBots(t *testing.T) { assert.NotEmpty(t, returnedBots) assert.EqualValues(t, bots, returnedBots) - - mockDb.AssertExpectations(t) }) t.Run("empty data returned for non-matching search query", func(t *testing.T) { @@ -193,8 +208,6 @@ func TestSearchBots(t *testing.T) { query := "nonexistentbot" - mockDb.On("SearchBots", query, mock.AnythingOfType("int"), mock.AnythingOfType("int")).Return([]db.BotRes{}) - rctx := chi.NewRouteContext() rctx.URLParams.Add("query", query) req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/search/bots/"+query, nil) @@ -208,8 +221,6 @@ func TestSearchBots(t *testing.T) { err = json.Unmarshal(rr.Body.Bytes(), &returnedBots) assert.NoError(t, err) assert.Empty(t, returnedBots) - - mockDb.AssertExpectations(t) }) }