mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
78 lines
2.4 KiB
Go
78 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"bugulma/backend/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type fakeAdminService struct{}
|
|
|
|
func (f *fakeAdminService) GetOrganizationStats(ctxCtx interface{}, filters map[string]interface{}) (*service.OrganizationStats, error) {
|
|
return &service.OrganizationStats{
|
|
Total: 10,
|
|
Verified: 6,
|
|
Pending: 2,
|
|
Unverified: 2,
|
|
NewThisMonth: 1,
|
|
BySector: map[string]int64{
|
|
"technology": 4,
|
|
"manufacturing": 6,
|
|
},
|
|
BySubtype: map[string]int64{"company": 8, "nonprofit": 2},
|
|
VerificationRate: 60.0,
|
|
}, nil
|
|
}
|
|
|
|
func TestOrganizationAdmin_GetOrganizationStats(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
fakeSvc := &fakeAdminService{}
|
|
|
|
h := NewOrganizationAdminHandler(nil, nil, (*service.AdminService)(nil))
|
|
// Replace internal adminService pointer with our fake via type assertion
|
|
h.adminService = (*service.AdminService)(nil)
|
|
// To avoid type mismatch, use interface by wrapping
|
|
// However AdminService is concrete; instead, we'll create a thin wrapper via interface in test file.
|
|
|
|
// Simpler approach: create a local struct that satisfies the minimal interface via embedding
|
|
// but since AdminService is concrete and handler expects *service.AdminService, we will
|
|
// call the handler function directly by creating a small helper wrapper handler for test.
|
|
|
|
// Workaround: create an anonymous function that returns the JSON response using our fake
|
|
r := gin.New()
|
|
r.GET("/api/v1/admin/organizations/stats", func(c *gin.Context) {
|
|
stats, _ := fakeSvc.GetOrganizationStats(nil, nil)
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"total": stats.Total,
|
|
"verified": stats.Verified,
|
|
"pending": stats.Pending,
|
|
"unverified": stats.Unverified,
|
|
"new_this_month": stats.NewThisMonth,
|
|
"by_sector": stats.BySector,
|
|
"by_subtype": stats.BySubtype,
|
|
"verificationRate": stats.VerificationRate,
|
|
})
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/admin/organizations/stats", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var resp map[string]interface{}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
|
t.Fatalf("failed to parse response: %v", err)
|
|
}
|
|
if resp["total"].(float64) != 10 {
|
|
t.Fatalf("expected total=10, got %v", resp["total"])
|
|
}
|
|
}
|