From 9d4a2fb0c82c95327d6d544d9524bf3943bce9f7 Mon Sep 17 00:00:00 2001 From: Abdul Wahab Date: Sat, 17 Feb 2024 12:29:59 +0500 Subject: [PATCH 1/2] Added UTs for bounty.go handlers --- handlers/bounty.go | 32 +++++------ handlers/bounty_test.go | 114 ++++++++++++++++++++++++++++++++++++++++ routes/bounty.go | 10 ++-- 3 files changed, 136 insertions(+), 20 deletions(-) diff --git a/handlers/bounty.go b/handlers/bounty.go index 3019313fb..81243f04d 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -19,14 +19,16 @@ import ( ) type bountyHandler struct { - httpClient HttpClient - db db.Database + httpClient HttpClient + db db.Database + generateBountyHandler func(bounties []db.Bounty) []db.BountyResponse } func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler { return &bountyHandler{ - httpClient: httpClient, - db: db, + httpClient: httpClient, + db: db, + generateBountyHandler: GenerateBountyResponse, } } @@ -54,8 +56,8 @@ func GetBountyById(w http.ResponseWriter, r *http.Request) { } } -func GetNextBountyByCreated(w http.ResponseWriter, r *http.Request) { - bounties, err := db.DB.GetNextBountyByCreated(r) +func (h *bountyHandler) GetNextBountyByCreated(w http.ResponseWriter, r *http.Request) { + bounties, err := h.db.GetNextBountyByCreated(r) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) @@ -65,8 +67,8 @@ func GetNextBountyByCreated(w http.ResponseWriter, r *http.Request) { } } -func GetPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) { - bounties, err := db.DB.GetPreviousBountyByCreated(r) +func (h *bountyHandler) GetPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) { + bounties, err := h.db.GetPreviousBountyByCreated(r) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) @@ -76,8 +78,8 @@ func GetPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) { } } -func GetOrganizationNextBountyByCreated(w http.ResponseWriter, r *http.Request) { - bounties, err := db.DB.GetNextOrganizationBountyByCreated(r) +func (h *bountyHandler) GetOrganizationNextBountyByCreated(w http.ResponseWriter, r *http.Request) { + bounties, err := h.db.GetNextOrganizationBountyByCreated(r) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) @@ -87,8 +89,8 @@ func GetOrganizationNextBountyByCreated(w http.ResponseWriter, r *http.Request) } } -func GetOrganizationPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) { - bounties, err := db.DB.GetPreviousOrganizationBountyByCreated(r) +func (h *bountyHandler) GetOrganizationPreviousBountyByCreated(w http.ResponseWriter, r *http.Request) { + bounties, err := h.db.GetPreviousOrganizationBountyByCreated(r) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) @@ -108,17 +110,17 @@ func GetBountyIndexById(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(bountyIndex) } -func GetBountyByCreated(w http.ResponseWriter, r *http.Request) { +func (h *bountyHandler) GetBountyByCreated(w http.ResponseWriter, r *http.Request) { created := chi.URLParam(r, "created") if created == "" { w.WriteHeader(http.StatusNotFound) } - bounties, err := db.DB.GetBountyDataByCreated(created) + bounties, err := h.db.GetBountyDataByCreated(created) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) } else { - var bountyResponse []db.BountyResponse = GenerateBountyResponse(bounties) + var bountyResponse []db.BountyResponse = h.generateBountyHandler(bounties) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyResponse) } diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index d1f87a632..5bb7c6f00 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -449,3 +449,117 @@ func TestDeleteBounty(t *testing.T) { mockDb.AssertExpectations(t) }) } + +func TestGetBountyByCreated(t *testing.T) { + ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") + mockDb := dbMocks.NewDatabase(t) + mockGenerateBountyHandler := func(bounties []db.Bounty) []db.BountyResponse { + return []db.BountyResponse{} // Mocked response + } + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("Should return bounty by its created value", func(t *testing.T) { + bHandler.generateBountyHandler = mockGenerateBountyHandler + + expectedBounty := []db.Bounty{{ + ID: 1, + Type: "type1", + Title: "Test Bounty", + Description: "Description", + Created: 123456789, + }} + mockDb.On("GetBountyDataByCreated", "123456789").Return(expectedBounty, nil).Once() + + rr := httptest.NewRecorder() + handler := http.HandlerFunc(bHandler.GetBountyByCreated) + + req, err := http.NewRequestWithContext(ctx, "GET", "/bounty/123456789", nil) + if err != nil { + t.Fatal(err) + } + chiCtx := chi.NewRouteContext() + chiCtx.URLParams.Add("created", "123456789") + req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chiCtx)) + + handler.ServeHTTP(rr, req) + + assert.Equal(t, http.StatusOK, rr.Code) + }) +} + +func TestGetNextBountyByCreated(t *testing.T) { + ctx := context.Background() + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("Should test that the next bounty on the bounties homepage can be gotten by its created value and the selected filters", func(t *testing.T) { + mockDb.On("GetNextBountyByCreated", mock.Anything).Return(uint(1), nil).Once() + + rr := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/next/123456789", nil) + + bHandler.GetNextBountyByCreated(rr, req.WithContext(ctx)) + + assert.Equal(t, http.StatusOK, rr.Code) + mockDb.AssertExpectations(t) + }) +} + +func TestGetPreviousBountyByCreated(t *testing.T) { + ctx := context.Background() + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("Should test that the previous bounty on the bounties homepage can be gotten by its created value and the selected filters", func(t *testing.T) { + mockDb.On("GetPreviousBountyByCreated", mock.Anything).Return(uint(1), nil).Once() + + rr := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/previous/123456789", nil) + + bHandler.GetPreviousBountyByCreated(rr, req.WithContext(ctx)) + + assert.Equal(t, http.StatusOK, rr.Code) + mockDb.AssertExpectations(t) + }) +} + +func TestGetOrganizationNextBountyByCreated(t *testing.T) { + ctx := context.Background() + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("Should test that the next bounty on the organization bounties homepage can be gotten by its created value and the selected filters", func(t *testing.T) { + mockDb.On("GetNextOrganizationBountyByCreated", mock.AnythingOfType("*http.Request")).Return(uint(1), nil).Once() + + rr := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/org/next/org-uuid/123456789", nil) + + bHandler.GetOrganizationNextBountyByCreated(rr, req.WithContext(ctx)) + + assert.Equal(t, http.StatusOK, rr.Code) + mockDb.AssertExpectations(t) + }) +} + +func TestGetOrganizationPreviousBountyByCreated(t *testing.T) { + ctx := context.Background() + mockDb := dbMocks.NewDatabase(t) + mockHttpClient := mocks.NewHttpClient(t) + bHandler := NewBountyHandler(mockHttpClient, mockDb) + + t.Run("Should test that the previous bounty on the organization bounties homepage can be gotten by its created value and the selected filters", func(t *testing.T) { + mockDb.On("GetPreviousOrganizationBountyByCreated", mock.AnythingOfType("*http.Request")).Return(uint(1), nil).Once() + + rr := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/org/previous/org-uuid/123456789", nil) + + bHandler.GetOrganizationPreviousBountyByCreated(rr, req.WithContext(ctx)) + + assert.Equal(t, http.StatusOK, rr.Code) + mockDb.AssertExpectations(t) + }) +} diff --git a/routes/bounty.go b/routes/bounty.go index a169c04ee..7eb1497c1 100644 --- a/routes/bounty.go +++ b/routes/bounty.go @@ -16,11 +16,11 @@ func BountyRoutes() chi.Router { r.Get("/all", bountyHandler.GetAllBounties) r.Get("/id/{bountyId}", handlers.GetBountyById) r.Get("/index/{bountyId}", handlers.GetBountyIndexById) - r.Get("/next/{created}", handlers.GetNextBountyByCreated) - r.Get("/previous/{created}", handlers.GetPreviousBountyByCreated) - r.Get("/org/next/{uuid}/{created}", handlers.GetOrganizationNextBountyByCreated) - r.Get("/org/previous/{uuid}/{created}", handlers.GetOrganizationPreviousBountyByCreated) - r.Get("/created/{created}", handlers.GetBountyByCreated) + r.Get("/next/{created}", bountyHandler.GetNextBountyByCreated) + r.Get("/previous/{created}", bountyHandler.GetPreviousBountyByCreated) + r.Get("/org/next/{uuid}/{created}", bountyHandler.GetOrganizationNextBountyByCreated) + r.Get("/org/previous/{uuid}/{created}", bountyHandler.GetOrganizationPreviousBountyByCreated) + r.Get("/created/{created}", bountyHandler.GetBountyByCreated) r.Get("/count/{personKey}/{tabType}", handlers.GetUserBountyCount) r.Get("/count", handlers.GetBountyCount) r.Get("/invoice/{paymentRequest}", handlers.GetInvoiceData) From b691d63dfd1d2b572d53c675ca979be7c6ccb3d8 Mon Sep 17 00:00:00 2001 From: Abdul Wahab Date: Mon, 19 Feb 2024 16:09:55 +0500 Subject: [PATCH 2/2] Addressed changes --- handlers/bounty.go | 6 +++--- handlers/bounty_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/handlers/bounty.go b/handlers/bounty.go index 81243f04d..3c3f186ae 100644 --- a/handlers/bounty.go +++ b/handlers/bounty.go @@ -21,14 +21,14 @@ import ( type bountyHandler struct { httpClient HttpClient db db.Database - generateBountyHandler func(bounties []db.Bounty) []db.BountyResponse + generateBountyResponse func(bounties []db.Bounty) []db.BountyResponse } func NewBountyHandler(httpClient HttpClient, db db.Database) *bountyHandler { return &bountyHandler{ httpClient: httpClient, db: db, - generateBountyHandler: GenerateBountyResponse, + generateBountyResponse: GenerateBountyResponse, } } @@ -120,7 +120,7 @@ func (h *bountyHandler) GetBountyByCreated(w http.ResponseWriter, r *http.Reques w.WriteHeader(http.StatusBadRequest) fmt.Println("Error", err) } else { - var bountyResponse []db.BountyResponse = h.generateBountyHandler(bounties) + var bountyResponse []db.BountyResponse = h.generateBountyResponse(bounties) w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(bountyResponse) } diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 5bb7c6f00..83f6bb7db 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -453,14 +453,14 @@ func TestDeleteBounty(t *testing.T) { func TestGetBountyByCreated(t *testing.T) { ctx := context.WithValue(context.Background(), auth.ContextKey, "test-key") mockDb := dbMocks.NewDatabase(t) - mockGenerateBountyHandler := func(bounties []db.Bounty) []db.BountyResponse { + mockGenerateBountyResponse := func(bounties []db.Bounty) []db.BountyResponse { return []db.BountyResponse{} // Mocked response } mockHttpClient := mocks.NewHttpClient(t) bHandler := NewBountyHandler(mockHttpClient, mockDb) t.Run("Should return bounty by its created value", func(t *testing.T) { - bHandler.generateBountyHandler = mockGenerateBountyHandler + bHandler.generateBountyResponse = mockGenerateBountyResponse expectedBounty := []db.Bounty{{ ID: 1,