From a395f50be01368968225dfab8682fc585fb053ac Mon Sep 17 00:00:00 2001 From: MuhammadUmer44 Date: Mon, 24 Jun 2024 22:21:27 +0500 Subject: [PATCH 1/5] Refactor TestGetBotByUniqueName UT --- db/structs.go | 2 +- db/test_config.go | 1 + handlers/bots_test.go | 13 ++++++++----- 3 files changed, 10 insertions(+), 6 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..62e3c42f5 100644 --- a/handlers/bots_test.go +++ b/handlers/bots_test.go @@ -18,9 +18,10 @@ import ( ) func TestGetBotByUniqueName(t *testing.T) { + teardownSuite := SetupSuite(t) + defer teardownSuite(t) - mockDb := dbMocks.NewDatabase(t) - btHandler := NewBotHandler(mockDb) + btHandler := NewBotHandler(db.TestDB) t.Run("successful retrieval of bots by uniqueName", func(t *testing.T) { rr := httptest.NewRecorder() @@ -44,20 +45,22 @@ func TestGetBotByUniqueName(t *testing.T) { OwnerRouteHint: "route-hint", } + db.TestDB.CreateOrEditBot(bot) + rctx := chi.NewRouteContext() rctx.URLParams.Add("name", bot.UniqueName) req, err := http.NewRequestWithContext(context.WithValue(context.Background(), chi.RouteCtxKey, rctx), http.MethodGet, "/bot/"+bot.UniqueName, nil) assert.NoError(t, err) - mockDb.On("GetBotByUniqueName", bot.UniqueName).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.Equal(t, bot.UUID, returnedBot.UUID) + assert.Equal(t, bot.Name, returnedBot.Name) + assert.Equal(t, bot.UniqueName, returnedBot.UniqueName) }) } From ff439ef30c7923138fcce37a4b3559e8b84eb2b1 Mon Sep 17 00:00:00 2001 From: AhsanFarooqDev Date: Mon, 24 Jun 2024 22:56:04 +0500 Subject: [PATCH 2/5] 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) }) } From fab6507ca85f547b2faaff5de41b687950fb0f7f Mon Sep 17 00:00:00 2001 From: AbdulWahab3181 Date: Tue, 25 Jun 2024 00:34:13 +0500 Subject: [PATCH 3/5] Refactored TestSearchBots UT --- db/db.go | 1 + db/structs.go | 5 +++-- db/test_config.go | 1 + handlers/bots_test.go | 33 ++++++++++++++++++++++----------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/db/db.go b/db/db.go index 4ef08f10f..e528627cf 100644 --- a/db/db.go +++ b/db/db.go @@ -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 diff --git a/db/structs.go b/db/structs.go index b906611be..f722ee23c 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"` @@ -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 @@ -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"` } 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..40ca200a7 100644 --- a/handlers/bots_test.go +++ b/handlers/bots_test.go @@ -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{ { @@ -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) @@ -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) { @@ -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) @@ -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) }) } From 2c4fe468cf9b4dfa67241ef1dd576ccc43f390f5 Mon Sep 17 00:00:00 2001 From: saithsab877 Date: Tue, 25 Jun 2024 01:20:00 +0500 Subject: [PATCH 4/5] Refactor TestDeleteBot UT --- db/structs.go | 2 +- db/test_config.go | 1 + handlers/bots_test.go | 50 +++++++++++++++++++++++++++---------------- 3 files changed, 33 insertions(+), 20 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..8e747388d 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" @@ -214,38 +215,49 @@ func TestSearchBots(t *testing.T) { } func TestDeleteBot(t *testing.T) { + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + + btHandler := NewBotHandler(db.TestDB) + + btHandler.verifyTribeUUID = func(uuid string, checkTimestamp bool) (string, error) { + return "owner-pubkey-123", nil + } + t.Run("bot can be deleted by the creator of the bot", func(t *testing.T) { - mockDb := dbMocks.NewDatabase(t) - mockVerifyTribeUUID := func(uuid string, checkTimestamp bool) (string, error) { - return "creator-public-key", nil - } - mockUUID := "123-456-789" + rr := httptest.NewRecorder() + handler := http.HandlerFunc(btHandler.DeleteBot) - btHandler := &botHandler{ - db: mockDb, - verifyTribeUUID: mockVerifyTribeUUID, + 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, } - ctx := context.WithValue(context.Background(), auth.ContextKey, "creator-public-key") + db.TestDB.CreateOrEditBot(bot) + + ctx := context.WithValue(context.Background(), auth.ContextKey, bot.OwnerPubKey) rctx := chi.NewRouteContext() - rctx.URLParams.Add("uuid", mockUUID) - req, err := http.NewRequestWithContext(context.WithValue(ctx, chi.RouteCtxKey, rctx), http.MethodDelete, "/"+mockUUID, nil) + rctx.URLParams.Add("uuid", bot.UUID) + req, err := http.NewRequestWithContext(ctx, http.MethodDelete, "/"+bot.UUID, nil) assert.NoError(t, err) - expectedUUID := "123-456-789" - - mockDb.On("UpdateBot", mockUUID, map[string]interface{}{"deleted": true}).Return(true) - - rr := httptest.NewRecorder() + req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx)) - btHandler.DeleteBot(rr, req) + handler.ServeHTTP(rr, req) assert.Equal(t, http.StatusOK, rr.Code) - - mockDb.AssertCalled(t, "UpdateBot", expectedUUID, map[string]interface{}{"deleted": true}) + deletedBot := db.TestDB.GetBot(bot.UUID) + assert.Empty(t, deletedBot) }) } + func TestCreateOrEditBot(t *testing.T) { mockDb := dbMocks.NewDatabase(t) bHandler := NewBotHandler(mockDb) From 0ed7dccef70b995601860c8c496454092fb4df57 Mon Sep 17 00:00:00 2001 From: Mirza Hanan Date: Tue, 25 Jun 2024 01:58:55 +0500 Subject: [PATCH 5/5] Refactor TestGetBotsByOwner UT --- db/structs.go | 2 +- db/test_config.go | 1 + handlers/bots_test.go | 102 +++++++++++++++++++++++++++--------------- 3 files changed, 67 insertions(+), 38 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..e67559597 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" @@ -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, @@ -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}) }) }