refactor tests to be consistent with the rest, use require
This commit is contained in:
@@ -4,133 +4,70 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNewDatasourceNotFoundError(t *testing.T) {
|
||||
err := NewDatasourceNotFoundError("test-uid", "org-1")
|
||||
|
||||
if err.Code != ErrCodeDatasourceNotFound {
|
||||
t.Errorf("expected error code %s, got %s", ErrCodeDatasourceNotFound, err.Code)
|
||||
}
|
||||
|
||||
if err.StatusCode != http.StatusNotFound {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusNotFound, err.StatusCode)
|
||||
}
|
||||
|
||||
if err.Details["datasourceUID"] != "test-uid" {
|
||||
t.Errorf("expected datasourceUID detail to be 'test-uid', got %v", err.Details["datasourceUID"])
|
||||
}
|
||||
|
||||
if err.Details["namespace"] != "org-1" {
|
||||
t.Errorf("expected namespace detail to be 'org-1', got %v", err.Details["namespace"])
|
||||
}
|
||||
require.Equal(t, ErrCodeDatasourceNotFound, err.Code)
|
||||
require.Equal(t, http.StatusNotFound, err.StatusCode)
|
||||
require.Equal(t, "test-uid", err.Details["datasourceUID"])
|
||||
require.Equal(t, "org-1", err.Details["namespace"])
|
||||
}
|
||||
|
||||
func TestNewDatasourceWrongTypeError(t *testing.T) {
|
||||
err := NewDatasourceWrongTypeError("test-uid", "prometheus", "influxdb")
|
||||
|
||||
if err.Code != ErrCodeDatasourceWrongType {
|
||||
t.Errorf("expected error code %s, got %s", ErrCodeDatasourceWrongType, err.Code)
|
||||
}
|
||||
|
||||
if err.StatusCode != http.StatusBadRequest {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusBadRequest, err.StatusCode)
|
||||
}
|
||||
|
||||
if err.Details["expectedType"] != "prometheus" {
|
||||
t.Errorf("expected expectedType detail to be 'prometheus', got %v", err.Details["expectedType"])
|
||||
}
|
||||
|
||||
if err.Details["actualType"] != "influxdb" {
|
||||
t.Errorf("expected actualType detail to be 'influxdb', got %v", err.Details["actualType"])
|
||||
}
|
||||
require.Equal(t, ErrCodeDatasourceWrongType, err.Code)
|
||||
require.Equal(t, http.StatusBadRequest, err.StatusCode)
|
||||
require.Equal(t, "prometheus", err.Details["expectedType"])
|
||||
require.Equal(t, "influxdb", err.Details["actualType"])
|
||||
}
|
||||
|
||||
func TestNewDatasourceUnreachableError(t *testing.T) {
|
||||
cause := errors.New("connection refused")
|
||||
err := NewDatasourceUnreachableError("test-uid", "http://localhost:9090", cause)
|
||||
|
||||
if err.Code != ErrCodeDatasourceUnreachable {
|
||||
t.Errorf("expected error code %s, got %s", ErrCodeDatasourceUnreachable, err.Code)
|
||||
}
|
||||
|
||||
if err.StatusCode != http.StatusServiceUnavailable {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusServiceUnavailable, err.StatusCode)
|
||||
}
|
||||
|
||||
if err.Cause != cause {
|
||||
t.Errorf("expected cause to be set")
|
||||
}
|
||||
|
||||
if err.Details["url"] != "http://localhost:9090" {
|
||||
t.Errorf("expected url detail to be 'http://localhost:9090', got %v", err.Details["url"])
|
||||
}
|
||||
require.Equal(t, ErrCodeDatasourceUnreachable, err.Code)
|
||||
require.Equal(t, http.StatusServiceUnavailable, err.StatusCode)
|
||||
require.Equal(t, cause, err.Cause)
|
||||
require.Equal(t, "http://localhost:9090", err.Details["url"])
|
||||
}
|
||||
|
||||
func TestNewAPIUnavailableError(t *testing.T) {
|
||||
err := NewAPIUnavailableError(503, "service unavailable", nil)
|
||||
|
||||
if err.Code != ErrCodeAPIUnavailable {
|
||||
t.Errorf("expected error code %s, got %s", ErrCodeAPIUnavailable, err.Code)
|
||||
}
|
||||
|
||||
if err.StatusCode != http.StatusBadGateway {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusBadGateway, err.StatusCode)
|
||||
}
|
||||
|
||||
if err.Details["upstreamStatus"] != 503 {
|
||||
t.Errorf("expected upstreamStatus detail to be 503, got %v", err.Details["upstreamStatus"])
|
||||
}
|
||||
require.Equal(t, ErrCodeAPIUnavailable, err.Code)
|
||||
require.Equal(t, http.StatusBadGateway, err.StatusCode)
|
||||
require.Equal(t, 503, err.Details["upstreamStatus"])
|
||||
}
|
||||
|
||||
func TestNewAPIInvalidResponseError(t *testing.T) {
|
||||
cause := errors.New("invalid JSON")
|
||||
err := NewAPIInvalidResponseError("missing data field", cause)
|
||||
|
||||
if err.Code != ErrCodeAPIInvalidResponse {
|
||||
t.Errorf("expected error code %s, got %s", ErrCodeAPIInvalidResponse, err.Code)
|
||||
}
|
||||
|
||||
if err.StatusCode != http.StatusBadGateway {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusBadGateway, err.StatusCode)
|
||||
}
|
||||
|
||||
if err.Cause != cause {
|
||||
t.Errorf("expected cause to be set")
|
||||
}
|
||||
require.Equal(t, ErrCodeAPIInvalidResponse, err.Code)
|
||||
require.Equal(t, http.StatusBadGateway, err.StatusCode)
|
||||
require.Equal(t, cause, err.Cause)
|
||||
}
|
||||
|
||||
func TestNewAPITimeoutError(t *testing.T) {
|
||||
cause := errors.New("context deadline exceeded")
|
||||
err := NewAPITimeoutError("http://localhost:9090/api/v1/query", cause)
|
||||
|
||||
if err.Code != ErrCodeAPITimeout {
|
||||
t.Errorf("expected error code %s, got %s", ErrCodeAPITimeout, err.Code)
|
||||
}
|
||||
|
||||
if err.StatusCode != http.StatusGatewayTimeout {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusGatewayTimeout, err.StatusCode)
|
||||
}
|
||||
|
||||
if err.Cause != cause {
|
||||
t.Errorf("expected cause to be set")
|
||||
}
|
||||
require.Equal(t, ErrCodeAPITimeout, err.Code)
|
||||
require.Equal(t, http.StatusGatewayTimeout, err.StatusCode)
|
||||
require.Equal(t, cause, err.Cause)
|
||||
}
|
||||
|
||||
func TestNewDatasourceAuthError(t *testing.T) {
|
||||
err := NewDatasourceAuthError("test-uid", 401)
|
||||
|
||||
if err.Code != ErrCodeDatasourceAuth {
|
||||
t.Errorf("expected error code %s, got %s", ErrCodeDatasourceAuth, err.Code)
|
||||
}
|
||||
|
||||
if err.StatusCode != http.StatusUnauthorized {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusUnauthorized, err.StatusCode)
|
||||
}
|
||||
|
||||
if err.Details["upstreamStatus"] != 401 {
|
||||
t.Errorf("expected upstreamStatus detail to be 401, got %v", err.Details["upstreamStatus"])
|
||||
}
|
||||
require.Equal(t, ErrCodeDatasourceAuth, err.Code)
|
||||
require.Equal(t, http.StatusUnauthorized, err.StatusCode)
|
||||
require.Equal(t, 401, err.Details["upstreamStatus"])
|
||||
}
|
||||
|
||||
func TestValidationErrorChaining(t *testing.T) {
|
||||
@@ -140,30 +77,17 @@ func TestValidationErrorChaining(t *testing.T) {
|
||||
WithDetail("key1", "value1").
|
||||
WithDetail("key2", 123)
|
||||
|
||||
if err.Cause != cause {
|
||||
t.Errorf("expected cause to be set")
|
||||
}
|
||||
|
||||
if err.Details["key1"] != "value1" {
|
||||
t.Errorf("expected detail key1 to be 'value1', got %v", err.Details["key1"])
|
||||
}
|
||||
|
||||
if err.Details["key2"] != 123 {
|
||||
t.Errorf("expected detail key2 to be 123, got %v", err.Details["key2"])
|
||||
}
|
||||
require.Equal(t, cause, err.Cause)
|
||||
require.Equal(t, "value1", err.Details["key1"])
|
||||
require.Equal(t, 123, err.Details["key2"])
|
||||
}
|
||||
|
||||
func TestIsValidationError(t *testing.T) {
|
||||
validationErr := NewDatasourceNotFoundError("test-uid", "org-1")
|
||||
regularErr := errors.New("regular error")
|
||||
|
||||
if !IsValidationError(validationErr) {
|
||||
t.Errorf("expected IsValidationError to return true for ValidationError")
|
||||
}
|
||||
|
||||
if IsValidationError(regularErr) {
|
||||
t.Errorf("expected IsValidationError to return false for regular error")
|
||||
}
|
||||
require.True(t, IsValidationError(validationErr), "expected IsValidationError to return true for ValidationError")
|
||||
require.False(t, IsValidationError(regularErr), "expected IsValidationError to return false for regular error")
|
||||
}
|
||||
|
||||
func TestGetValidationError(t *testing.T) {
|
||||
@@ -171,75 +95,37 @@ func TestGetValidationError(t *testing.T) {
|
||||
regularErr := errors.New("regular error")
|
||||
|
||||
retrieved := GetValidationError(validationErr)
|
||||
if retrieved == nil {
|
||||
t.Errorf("expected GetValidationError to return the ValidationError")
|
||||
}
|
||||
if retrieved.Code != ErrCodeDatasourceNotFound {
|
||||
t.Errorf("expected retrieved error to have correct code")
|
||||
}
|
||||
require.NotNil(t, retrieved, "expected GetValidationError to return the ValidationError")
|
||||
require.Equal(t, ErrCodeDatasourceNotFound, retrieved.Code)
|
||||
|
||||
retrieved = GetValidationError(regularErr)
|
||||
if retrieved != nil {
|
||||
t.Errorf("expected GetValidationError to return nil for regular error")
|
||||
}
|
||||
require.Nil(t, retrieved, "expected GetValidationError to return nil for regular error")
|
||||
}
|
||||
|
||||
func TestGetHTTPStatusCode(t *testing.T) {
|
||||
validationErr := NewDatasourceNotFoundError("test-uid", "org-1")
|
||||
regularErr := errors.New("regular error")
|
||||
|
||||
statusCode := GetHTTPStatusCode(validationErr)
|
||||
if statusCode != http.StatusNotFound {
|
||||
t.Errorf("expected status code %d, got %d", http.StatusNotFound, statusCode)
|
||||
}
|
||||
|
||||
statusCode = GetHTTPStatusCode(regularErr)
|
||||
if statusCode != http.StatusInternalServerError {
|
||||
t.Errorf("expected default status code %d for regular error, got %d", http.StatusInternalServerError, statusCode)
|
||||
}
|
||||
require.Equal(t, http.StatusNotFound, GetHTTPStatusCode(validationErr))
|
||||
require.Equal(t, http.StatusInternalServerError, GetHTTPStatusCode(regularErr), "expected default status code for regular error")
|
||||
}
|
||||
|
||||
func TestErrorUnwrap(t *testing.T) {
|
||||
cause := errors.New("underlying error")
|
||||
err := NewDatasourceUnreachableError("test-uid", "http://localhost:9090", cause)
|
||||
|
||||
unwrapped := errors.Unwrap(err)
|
||||
if unwrapped != cause {
|
||||
t.Errorf("expected Unwrap to return the cause")
|
||||
}
|
||||
require.Equal(t, cause, errors.Unwrap(err), "expected Unwrap to return the cause")
|
||||
}
|
||||
|
||||
func TestErrorErrorMethod(t *testing.T) {
|
||||
// Test without cause
|
||||
err1 := NewDatasourceNotFoundError("test-uid", "org-1")
|
||||
errMsg1 := err1.Error()
|
||||
if errMsg1 == "" {
|
||||
t.Errorf("expected non-empty error message")
|
||||
}
|
||||
require.NotEmpty(t, err1.Error(), "expected non-empty error message")
|
||||
|
||||
// Test with cause
|
||||
cause := errors.New("underlying error")
|
||||
err2 := NewDatasourceUnreachableError("test-uid", "http://localhost:9090", cause)
|
||||
errMsg2 := err2.Error()
|
||||
if errMsg2 == "" {
|
||||
t.Errorf("expected non-empty error message")
|
||||
}
|
||||
// Error message should include the cause
|
||||
if !contains(errMsg2, "underlying error") {
|
||||
t.Errorf("expected error message to include cause, got: %s", errMsg2)
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to check if a string contains a substring
|
||||
func contains(s, substr string) bool {
|
||||
return len(s) >= len(substr) && (s == substr || len(s) > len(substr) && containsHelper(s, substr))
|
||||
}
|
||||
|
||||
func containsHelper(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
require.NotEmpty(t, errMsg2, "expected non-empty error message")
|
||||
require.Contains(t, errMsg2, "underlying error", "error message should include cause")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package validator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIsVariableReference(t *testing.T) {
|
||||
@@ -33,9 +35,7 @@ func TestIsVariableReference(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := isVariableReference(tt.input)
|
||||
if result != tt.expected {
|
||||
t.Errorf("isVariableReference(%q) = %v, want %v", tt.input, result, tt.expected)
|
||||
}
|
||||
require.Equal(t, tt.expected, result, "isVariableReference(%q) returned unexpected result", tt.input)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -63,9 +63,7 @@ func TestExtractVariableName(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := extractVariableName(tt.input)
|
||||
if result != tt.expected {
|
||||
t.Errorf("extractVariableName(%q) = %q, want %q", tt.input, result, tt.expected)
|
||||
}
|
||||
require.Equal(t, tt.expected, result, "extractVariableName(%q) returned unexpected result", tt.input)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -115,9 +113,7 @@ func TestIsPrometheusVariable(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := isPrometheusVariable(tt.varRef, tt.dashboard)
|
||||
if result != tt.expected {
|
||||
t.Errorf("isPrometheusVariable(%q, dashboard) = %v, want %v", tt.varRef, result, tt.expected)
|
||||
}
|
||||
require.Equal(t, tt.expected, result, "isPrometheusVariable(%q, dashboard) returned unexpected result", tt.varRef)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -162,10 +158,7 @@ func TestResolveDatasourceUID(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := resolveDatasourceUID(tt.uid, singleUID, tt.dashboard)
|
||||
if result != tt.expectedUID {
|
||||
t.Errorf("resolveDatasourceUID(%q, %q, dashboard) = %q, want %q (%s)",
|
||||
tt.uid, singleUID, result, tt.expectedUID, tt.description)
|
||||
}
|
||||
require.Equal(t, tt.expectedUID, result, "resolveDatasourceUID(%q, %q, dashboard): %s", tt.uid, singleUID, tt.description)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user