Skip to content

Commit

Permalink
Refactor TestGetBotsByOwner UT
Browse files Browse the repository at this point in the history
  • Loading branch information
MirzaHanan committed Jun 24, 2024
1 parent 764b085 commit 0ed7dcc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 38 deletions.
2 changes: 1 addition & 1 deletion 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 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
102 changes: 65 additions & 37 deletions handlers/bots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/google/uuid"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -62,60 +63,67 @@ func TestGetBotByUniqueName(t *testing.T) {
}

func TestGetBotsByOwner(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

mockDb := dbMocks.NewDatabase(t)
btHandler := NewBotHandler(mockDb)
btHandler := NewBotHandler(db.TestDB)

t.Run("empty list is returned when a user has no bots", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(btHandler.GetBotsByOwner)

bot := db.Bot{
UUID: "uuid-123",
OwnerPubKey: "owner-pubkey-123",
OwnerAlias: "owner-alias",
Name: "bot-name",
UniqueName: "unique-bot-name",
Description: "bot-description",
Tags: pq.StringArray{"tag1", "tag2"},
Img: "bot-img-url",
PricePerUse: 100,
Created: nil,
Updated: nil,
Unlisted: false,
Deleted: false,
MemberCount: 10,
OwnerRouteHint: "route-hint",
person := db.Person{
Uuid: uuid.New().String(),
OwnerAlias: "person",
UniqueName: "person",
OwnerPubKey: uuid.New().String(),
PriceToMeet: 0,
Description: "this is test user 1",
Tags: pq.StringArray{},
Extras: db.PropertyMap{},
GithubIssues: db.PropertyMap{},
}
db.TestDB.CreateOrEditPerson(person)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("pubkey", bot.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bots/owner/"+bot.OwnerPubKey, nil)
rctx.URLParams.Add("pubkey", person.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bots/owner/"+person.OwnerPubKey, nil)
assert.NoError(t, err)

mockDb.On("GetBotsByOwner", bot.OwnerPubKey).Return([]db.Bot{}, nil).Once()

handler.ServeHTTP(rr, req)

var returnedBot []db.BotRes
err = json.Unmarshal(rr.Body.Bytes(), &returnedBot)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
mockDb.AssertExpectations(t)
assert.Empty(t, returnedBot)
})

t.Run("retrieval all bots by an owner", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(btHandler.GetBotsByOwner)

person := db.Person{
Uuid: uuid.New().String(),
OwnerAlias: "person",
UniqueName: "person",
OwnerPubKey: uuid.New().String(),
PriceToMeet: 0,
Description: "this is test user 1",
Tags: pq.StringArray{},
Extras: db.PropertyMap{},
GithubIssues: db.PropertyMap{},
}
db.TestDB.CreateOrEditPerson(person)

bot := db.Bot{
UUID: "uuid-123",
OwnerPubKey: "owner-pubkey-123",
OwnerAlias: "owner-alias",
Name: "bot-name",
UniqueName: "unique-bot-name",
Description: "bot-description",
Tags: pq.StringArray{"tag1", "tag2"},
UUID: "bot1_uuid",
OwnerPubKey: person.OwnerPubKey,
OwnerAlias: person.OwnerAlias,
Name: "test_bot_owner",
UniqueName: "test_bot_owner",
Description: "bot description",
Tags: pq.StringArray{},
Img: "bot-img-url",
PricePerUse: 100,
Created: nil,
Expand All @@ -126,20 +134,40 @@ func TestGetBotsByOwner(t *testing.T) {
OwnerRouteHint: "route-hint",
}

bot2 := db.Bot{
UUID: "bot2_uuid",
OwnerPubKey: person.OwnerPubKey,
OwnerAlias: person.OwnerAlias,
Name: "test_bot_owner2",
UniqueName: "test_bot_owner2",
Description: "bot description",
Tags: pq.StringArray{},
Img: "bot-img-url",
PricePerUse: 100,
Created: nil,
Updated: nil,
Unlisted: false,
Deleted: false,
MemberCount: 10,
OwnerRouteHint: "route-hint",
}

db.TestDB.CreateOrEditBot(bot)
db.TestDB.CreateOrEditBot(bot2)

rctx := chi.NewRouteContext()
rctx.URLParams.Add("pubkey", bot.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bots/owner/"+bot.OwnerPubKey, nil)
rctx.URLParams.Add("pubkey", person.OwnerPubKey)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bots/owner/"+person.OwnerPubKey, nil)
assert.NoError(t, err)

mockDb.On("GetBotsByOwner", bot.OwnerPubKey).Return([]db.Bot{bot}, nil)

handler.ServeHTTP(rr, req)

var returnedBot []db.BotRes
err = json.Unmarshal(rr.Body.Bytes(), &returnedBot)
var returnedBots []db.BotRes
err = json.Unmarshal(rr.Body.Bytes(), &returnedBots)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rr.Code)
mockDb.AssertExpectations(t)
assert.Len(t, returnedBots, 2)
assert.ElementsMatch(t, []string{bot.UUID, bot2.UUID}, []string{returnedBots[0].UUID, returnedBots[1].UUID})
})
}

Expand Down

0 comments on commit 0ed7dcc

Please sign in to comment.