test: add comprehensive tests for all Epic 1 stories
Some checks failed
CI / Test (pull_request) Failing after 17s
CI / Lint (pull_request) Failing after 18s
CI / Build (pull_request) Successful in 12s
CI / Format Check (pull_request) Successful in 2s

Story 1.2: Database Layer
- Test database client creation, connection, ping, and close
- Test connection pooling configuration
- Tests skip if database is not available (short mode)

Story 1.3: Health Monitoring and Metrics
- Test health registry registration and checking
- Test database health checker
- Test liveness and readiness checks
- Test metrics creation, middleware, and handler
- Test Prometheus metrics endpoint

Story 1.4: Error Handling and Error Bus
- Test channel-based error bus creation
- Test error publishing with context
- Test nil error handling
- Test channel full scenario
- Test graceful shutdown
- Fix Close() method to handle multiple calls safely

Story 1.5: HTTP Server and Middleware
- Test server creation with all middleware
- Test request ID middleware
- Test logging middleware
- Test panic recovery middleware
- Test CORS middleware
- Test timeout middleware
- Test health and metrics endpoints
- Test server shutdown

Story 1.6: OpenTelemetry Tracing
- Test tracer initialization (enabled/disabled)
- Test development and production modes
- Test OTLP exporter configuration
- Test graceful shutdown
- Test no-op tracer provider

All tests follow Go testing best practices:
- Table-driven tests where appropriate
- Parallel execution
- Proper mocking of interfaces
- Skip tests requiring external dependencies in short mode
This commit is contained in:
2025-11-05 21:03:27 +01:00
parent 278a727b8c
commit 5fdbb729bd
11 changed files with 1537 additions and 22 deletions

View File

@@ -0,0 +1,125 @@
package metrics
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestNewMetrics(t *testing.T) {
t.Parallel()
metrics := NewMetrics()
if metrics == nil {
t.Fatal("Expected metrics, got nil")
}
if metrics.registry == nil {
t.Error("Expected registry, got nil")
}
if metrics.httpRequestDuration == nil {
t.Error("Expected httpRequestDuration, got nil")
}
if metrics.httpRequestTotal == nil {
t.Error("Expected httpRequestTotal, got nil")
}
if metrics.httpErrorsTotal == nil {
t.Error("Expected httpErrorsTotal, got nil")
}
}
func TestMetrics_HTTPMiddleware(t *testing.T) {
t.Parallel()
metrics := NewMetrics()
// Set Gin to test mode
gin.SetMode(gin.TestMode)
router := gin.New()
router.Use(metrics.HTTPMiddleware())
router.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "test"})
})
req := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", w.Code)
}
// Verify metrics were recorded
// We can't easily verify the internal metrics without exposing them,
// but we can verify the middleware doesn't panic
}
func TestMetrics_HTTPMiddleware_Error(t *testing.T) {
t.Parallel()
metrics := NewMetrics()
gin.SetMode(gin.TestMode)
router := gin.New()
router.Use(metrics.HTTPMiddleware())
router.GET("/error", func(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": "test error"})
})
req := httptest.NewRequest(http.MethodGet, "/error", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
if w.Code != http.StatusInternalServerError {
t.Errorf("Expected status 500, got %d", w.Code)
}
}
func TestMetrics_Handler(t *testing.T) {
t.Parallel()
metrics := NewMetrics()
handler := metrics.Handler()
if handler == nil {
t.Error("Expected handler, got nil")
}
// Test that the handler can be called
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Expected status 200, got %d", w.Code)
}
// Verify Prometheus format
body := w.Body.String()
// Prometheus handler may return empty body if no metrics are registered yet
// This is acceptable - we just verify the handler works
_ = body
}
func TestMetrics_Registry(t *testing.T) {
t.Parallel()
metrics := NewMetrics()
registry := metrics.Registry()
if registry == nil {
t.Error("Expected registry, got nil")
}
}