Files
query-orchestration/api/queryAPI/custommetadata_errors_test.go
Jay Brown 17fc813823 Merged in feature/mutable-metadata1 (pull request #221)
M1, M2 and M3 complete

* M1, M2 and M3 complete

* review changes

* docs

* docs
2026-04-16 23:11:26 +00:00

151 lines
4.0 KiB
Go

// Package queryapi — white-box unit tests for mapCustomMetadataError.
// These tests live in package queryapi (not package queryapi_test) to access
// the unexported function directly without requiring a real DB or echo context.
package queryapi
import (
"errors"
"fmt"
"net/http"
"testing"
"queryorchestration/internal/customschema"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func httpCode(t *testing.T, err error) int {
t.Helper()
var httpErr *echo.HTTPError
require.True(t, errors.As(err, &httpErr), "expected echo.HTTPError, got %T: %v", err, err)
return httpErr.Code
}
func TestMapCustomMetadataError(t *testing.T) {
cases := []struct {
name string
input error
wantCode int
}{
{
name: "document_not_found",
input: customschema.ErrDocumentNotFound,
wantCode: http.StatusNotFound,
},
{
name: "metadata_version_not_found",
input: customschema.ErrMetadataVersionNotFound,
wantCode: http.StatusNotFound,
},
{
name: "not_bound_to_schema",
input: customschema.ErrDocumentNotBoundToSchema,
wantCode: http.StatusBadRequest,
},
{
name: "payload_too_large",
input: customschema.ErrMetadataPayloadTooLarge,
wantCode: http.StatusBadRequest,
},
{
name: "schema_not_found",
input: customschema.ErrSchemaNotFound,
wantCode: http.StatusNotFound,
},
{
name: "schema_not_active",
input: customschema.ErrSchemaNotActive,
wantCode: http.StatusConflict,
},
{
name: "schema_client_mismatch",
input: customschema.ErrSchemaClientMismatch,
wantCode: http.StatusConflict,
},
{
name: "has_custom_metadata",
input: customschema.ErrDocumentHasCustomMetadata,
wantCode: http.StatusConflict,
},
{
name: "has_legacy_extractions",
input: customschema.ErrDocumentHasLegacyExtractions,
wantCode: http.StatusConflict,
},
{
name: "validate_metadata_string",
input: fmt.Errorf("customschema: validate metadata: payload error"),
wantCode: http.StatusBadRequest,
},
{
name: "limit_must_be_string",
input: fmt.Errorf("customschema: limit must be between 1 and 200"),
wantCode: http.StatusBadRequest,
},
{
name: "offset_must_be_string",
input: fmt.Errorf("customschema: offset must be >= 0"),
wantCode: http.StatusBadRequest,
},
{
name: "document_id_required_string",
input: fmt.Errorf("customschema: document id is required"),
wantCode: http.StatusBadRequest,
},
{
name: "version_must_be_string",
input: fmt.Errorf("customschema: version must be >= 1"),
wantCode: http.StatusBadRequest,
},
{
name: "unknown_error_returns_500",
input: fmt.Errorf("unexpected internal error"),
wantCode: http.StatusInternalServerError,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
got := mapCustomMetadataError(tc.input)
assert.Equal(t, tc.wantCode, httpCode(t, got), "wrong status for %v", tc.input)
})
}
}
// TestMapResetError covers all three branches of mapResetError declared in
// customschemas_reset.go. The function is unexported so the test lives in the
// same package (package queryapi, not package queryapi_test).
func TestMapResetError(t *testing.T) {
cases := []struct {
name string
input error
wantCode int
}{
{
name: "document_not_found",
input: customschema.ErrDocumentNotFound,
wantCode: http.StatusNotFound,
},
{
name: "mutual_exclusivity_violation",
input: customschema.ErrMutualExclusivityViolation,
wantCode: http.StatusConflict,
},
{
name: "unknown_error_returns_500",
input: fmt.Errorf("unexpected reset failure"),
wantCode: http.StatusInternalServerError,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := mapResetError(tc.input)
assert.Equal(t, tc.wantCode, httpCode(t, got), "wrong status for %v", tc.input)
})
}
}