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 5c91db0f4..f722ee23c 100644 --- a/db/structs.go +++ b/db/structs.go @@ -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/handlers/bots_test.go b/handlers/bots_test.go index 14d05fd52..5d8147663 100644 --- a/handlers/bots_test.go +++ b/handlers/bots_test.go @@ -148,14 +148,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{ { @@ -170,8 +189,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) @@ -187,8 +204,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) { @@ -197,8 +212,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) @@ -212,8 +225,6 @@ func TestSearchBots(t *testing.T) { err = json.Unmarshal(rr.Body.Bytes(), &returnedBots) assert.NoError(t, err) assert.Empty(t, returnedBots) - - mockDb.AssertExpectations(t) }) }