From ff439ef30c7923138fcce37a4b3559e8b84eb2b1 Mon Sep 17 00:00:00 2001 From: AhsanFarooqDev Date: Mon, 24 Jun 2024 22:56:04 +0500 Subject: [PATCH] Refactor TestGetBot UT --- db/structs.go | 2 +- db/test_config.go | 1 + handlers/bots_test.go | 38 ++++++++++++++++++++++++++------------ 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/db/structs.go b/db/structs.go index b906611be..5c91db0f4 100644 --- a/db/structs.go +++ b/db/structs.go @@ -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"` diff --git a/db/test_config.go b/db/test_config.go index 3d43fd6f1..37fe66e17 100644 --- a/db/test_config.go +++ b/db/test_config.go @@ -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 { diff --git a/handlers/bots_test.go b/handlers/bots_test.go index cd4b4a48b..7715cfe21 100644 --- a/handlers/bots_test.go +++ b/handlers/bots_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/google/uuid" "net/http" "net/http/httptest" "testing" @@ -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) }) }