Skip to content

Commit

Permalink
Merge pull request stakwork#1743 from AbdulWahab3181/Refactor-TestSea…
Browse files Browse the repository at this point in the history
…rchBots-UT

Refactor TestSearchBots To Use A Real Postgres DB For The Test
  • Loading branch information
elraphty authored Jun 24, 2024
2 parents d0479a3 + fab6507 commit 47ac598
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
1 change: 1 addition & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
}
Expand Down
33 changes: 22 additions & 11 deletions handlers/bots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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)
})
}

Expand Down

0 comments on commit 47ac598

Please sign in to comment.