From a668b38dc6c75701f3a2050a6729ad983d7e876b Mon Sep 17 00:00:00 2001 From: AhsanFarooqDev Date: Wed, 26 Jun 2024 20:57:40 +0500 Subject: [PATCH] Refactor TestGetTribe UT --- handlers/tribes_test.go | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/handlers/tribes_test.go b/handlers/tribes_test.go index c6d984623..6f90af6d1 100644 --- a/handlers/tribes_test.go +++ b/handlers/tribes_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "github.com/google/uuid" "net/http" "net/http/httptest" "strings" @@ -86,21 +87,23 @@ func TestGetTribesByOwner(t *testing.T) { } func TestGetTribe(t *testing.T) { - mockDb := mocks.NewDatabase(t) - tHandler := NewTribeHandler(mockDb) + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + tHandler := NewTribeHandler(db.TestDB) + + tribe := db.Tribe{ + UUID: uuid.New().String(), + OwnerPubKey: uuid.New().String(), + Name: "tribe", + Description: "description", + Tags: []string{"tag1", "tag2"}, + Badges: pq.StringArray{}, + } + db.TestDB.CreateOrEditTribe(tribe) t.Run("Should test that a tribe can be returned when the right UUID is passed to the request parameter", func(t *testing.T) { // Mock data - mockUUID := "valid_uuid" - mockTribe := db.Tribe{ - UUID: mockUUID, - } - mockChannels := []db.Channel{ - {ID: 1, TribeUUID: mockUUID}, - {ID: 2, TribeUUID: mockUUID}, - } - mockDb.On("GetTribe", mock.Anything).Return(mockTribe).Once() - mockDb.On("GetChannelsByTribe", mock.Anything).Return(mockChannels).Once() + mockUUID := tribe.UUID // Serve request rr := httptest.NewRecorder() @@ -111,6 +114,8 @@ func TestGetTribe(t *testing.T) { t.Fatal(err) } + fetchedTribe := db.TestDB.GetTribe(mockUUID) + handler := http.HandlerFunc(tHandler.GetTribe) handler.ServeHTTP(rr, req) @@ -121,15 +126,13 @@ func TestGetTribe(t *testing.T) { if err != nil { t.Fatalf("Error decoding JSON response: %s", err) } - assert.Equal(t, mockTribe.UUID, responseData["uuid"]) + assert.Equal(t, tribe.UUID, responseData["uuid"]) + assert.Equal(t, tribe, fetchedTribe) }) t.Run("Should test that no tribe is returned when a nonexistent UUID is passed", func(t *testing.T) { - // Mock data - mockDb.ExpectedCalls = nil + nonexistentUUID := "nonexistent_uuid" - mockDb.On("GetTribe", nonexistentUUID).Return(db.Tribe{}).Once() - mockDb.On("GetChannelsByTribe", mock.Anything).Return([]db.Channel{}).Once() // Serve request rr := httptest.NewRecorder()