Skip to content

Commit

Permalink
Refactor TestGetBot UT
Browse files Browse the repository at this point in the history
  • Loading branch information
AhsanFarooqDev committed Jun 24, 2024
1 parent 764b085 commit ff439ef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 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
38 changes: 26 additions & 12 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 @@ -417,31 +418,44 @@ func TestCreateOrEditBot(t *testing.T) {
}

func TestGetBot(t *testing.T) {
mockDb := dbMocks.NewDatabase(t)
bHandler := NewBotHandler(mockDb)
teardownSuite := SetupSuite(t)
defer teardownSuite(t)
bHandler := NewBotHandler(db.TestDB)

t.Run("should test that a bot can be fetched with its uuid", func(t *testing.T) {
rr := httptest.NewRecorder()
handler := http.HandlerFunc(bHandler.GetBot)

mockUUID := "valid_uuid"
mockBot := db.Bot{UUID: mockUUID, Name: "Test Bot"}
mockDb.On("GetBot", mock.Anything).Return(mockBot).Once()
bot := db.Bot{
UUID: uuid.New().String(),
OwnerPubKey: "owner-pubkey-123",
Name: "bot-name",
UniqueName: "unique-bot-name",
Description: "bot-description",
Tags: pq.StringArray{"tag1", "tag2"},
Img: "bot-img-url",
PricePerUse: 100,
}

db.TestDB.CreateOrEditBot(bot)

rr := httptest.NewRecorder()
rctx := chi.NewRouteContext()
rctx.URLParams.Add("uuid", mockUUID)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/"+mockUUID, nil)
rctx.URLParams.Add("uuid", bot.UUID)
req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/"+bot.UUID, nil)

if err != nil {
t.Fatal(err)
}

handler := http.HandlerFunc(bHandler.GetBot)
fetchedBot := db.TestDB.GetBot(bot.UUID)

handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
var returnedBot db.Bot
json.Unmarshal(rr.Body.Bytes(), &returnedBot)
assert.Equal(t, mockBot, returnedBot)
err = json.Unmarshal(rr.Body.Bytes(), &returnedBot)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, bot, returnedBot)
assert.Equal(t, bot, fetchedBot)
})
}

Expand Down

0 comments on commit ff439ef

Please sign in to comment.