Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TestSearchBots To Use A Real Postgres DB For The Test #1743

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
1 change: 1 addition & 0 deletions db/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 22 additions & 11 deletions handlers/bots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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)
})
}

Expand Down
Loading